Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
add script arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilKleistGao committed Jul 8, 2021
1 parent 69c328a commit 569a843
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 25 deletions.
47 changes: 41 additions & 6 deletions kernel/components/state_machine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ void StateMachine::init(const typename resources::ConfigResource::JsonObject& da
}
}

if (data.HasMember("properties")) {
auto properties = data["properties"].GetArray();
for (const auto& p : properties) {
initProperty(p);
if (data.HasMember("args")) {
auto args = data["args"].GetArray();
for (const auto& arg : args) {
initArgument(arg);
}
}
}
Expand Down Expand Up @@ -141,8 +141,43 @@ void StateMachine::notifyAll(const luabridge::LuaRef& sender, const std::string&
ob->notifyAll(sender, name, data);
}

void StateMachine::initProperty(const typename resources::ConfigResource::JsonObject& data) {
// TODO:
void StateMachine::initArgument(const typename resources::ConfigResource::JsonObject &data) {
std::string name = data["name"].GetString();
auto ref = _instance.rawget(name);

initArgument(ref, data);
_instance[name] = ref;
}

void StateMachine::initArgument(luabridge::LuaRef& ref, const typename resources::ConfigResource::JsonObject& data) {
if (ref.isNil()) {
return;
}

if (data["value"].IsInt()) {
ref = data["value"].GetInt();
}
else if (data["value"].IsFloat()) {
ref = data["value"].GetFloat();
}
else if (data["value"].IsBool()) {
ref = data["value"].GetBool();
}
else if (data["value"].IsString()) {
ref = data["value"].GetString();
}
else if (data["value"].IsArray()) {
auto table = data["value"].GetArray();
for (const auto& item : table) {
std::string name = item["name"].GetString();
auto next = ref.rawget(name);
initArgument(next, item);
ref[name] = next;
}
}
else {
ref = luabridge::LuaRef(script::LuaState::getInstance()->getState());
}
}

} // namespace ngind::components
4 changes: 3 additions & 1 deletion kernel/components/state_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ class StateMachine : public Component {
*/
std::map<std::string, std::unordered_set<std::string>> _subscribe;

void initProperty(const typename resources::ConfigResource::JsonObject& data);
void initArgument(const typename resources::ConfigResource::JsonObject& data);

void initArgument(luabridge::LuaRef& ref, const typename resources::ConfigResource::JsonObject& data);
};

NGIND_LUA_BRIDGE_REGISTRATION(StateMachine) {
Expand Down
4 changes: 4 additions & 0 deletions kernel/objects/entity_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "script/lua_registration.h"

namespace ngind::objects {
class ObjectFactory;

/**
* This class is a special kind of object that you can specify its position, rotation and scale.
*/
Expand Down Expand Up @@ -288,6 +290,8 @@ class EntityObject : public Object {
}

static EntityObject* create(const typename resources::ConfigResource::JsonObject& data);

friend class ObjectFactory;
private:
/**
* The position of this object
Expand Down
36 changes: 22 additions & 14 deletions kernel/objects/object_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,36 @@
#include "object_factory.h"

#include "components/component_factory.h"
#include "prefab_factory.h"

namespace ngind::objects {

EntityObject* ObjectFactory::createEntityObject(const typename resources::ConfigResource::JsonObject& data) {
auto* entity = EntityObject::create(data);
if (data.HasMember("prefab")) {
auto* entity = PrefabFactory::getInstance()->loadPrefab(data["prefab"].GetString());
entity->init(data);
return entity;
}
else {
auto* entity = EntityObject::create(data);

if (data.HasMember("components")) {
auto components = data["components"].GetArray();
for (const auto& com : components) {
components::Component* next = createComponent(com);
entity->addComponent(com["name"].GetString(), next);
if (data.HasMember("components")) {
auto components = data["components"].GetArray();
for (const auto& com : components) {
components::Component* next = createComponent(com);
entity->addComponent(com["name"].GetString(), next);
}
}
}
if (data.HasMember("children")) {
auto children = data["children"].GetArray();
for (const auto& child : children) {
EntityObject* next = createEntityObject(child);
entity->addChild(child["name"].GetString(), next);
if (data.HasMember("children")) {
auto children = data["children"].GetArray();
for (const auto& child : children) {
EntityObject* next = createEntityObject(child);
entity->addChild(child["name"].GetString(), next);
}
}
}

return entity;
return entity;
}
}

components::Component* ObjectFactory::createComponent(const typename resources::ConfigResource::JsonObject& data) {
Expand Down
6 changes: 6 additions & 0 deletions resources/config/prefabs/spin.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
"driver-script": "spin.lua",
"classname": "Spin",
"subscription": [
],
"args": [
{
"name": "direction",
"value": -1
}
]
}
]
Expand Down
15 changes: 15 additions & 0 deletions resources/config/worlds/world-prefab.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@
"alignment": 2
}
]
},
{
"id": 2,
"name": "spin",
"prefab": "spin",
"position": {
"x": 824,
"y": 384
},
"scale": {
"x": 2,
"y": 2
},
"rotate": 0,
"z-order": 0
}
],
"components": [
Expand Down
6 changes: 3 additions & 3 deletions resources/script/prefab_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ end

function PrefabLoader:updateCreate(delta)
local spin1 = PrefabFactory.loadPrefab("spin")
local spin2 = PrefabFactory.loadPrefab("spin")
--local spin2 = PrefabFactory.loadPrefab("spin")
spin1:setPosition(engine.vec2(200, 384))
spin2:setPosition(engine.vec2(824, 384))
--spin2:setPosition(engine.vec2(824, 384))
self.game_object:addChild("spin1", spin1)
self.game_object:addChild("spin2", spin2)
--self.game_object:addChild("spin2", spin2)
self.move("Idle")
end

Expand Down
2 changes: 1 addition & 1 deletion resources/script/spin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
Spin = class("Spin")

function Spin:ctor()
self.direction = 1
end

function Spin:enter()
self.r = 0
self.direction = 1
self.move("Idle")
end

Expand Down

0 comments on commit 569a843

Please sign in to comment.