From 5269e7c09a0b17c8c972c8ad996b04d42dbcd9cb Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 13 May 2018 00:50:11 -0400 Subject: Started event handlers The AutomatingSystem has been renamed to the ScriptingSystem, since the automatic behavior script is just a special case of the scripts that an entity can exhibit. The AutomatableComponent has largely been moved to the new RunnableComponent (might not be the final name for it). The Lua state object, previously living on the singleton RealizableComponent, is now a member of the ScriptingSystem itself, because it A) doesn't really belong on the realizable entity, and B) a singleton entity seems weird and like a cumbersome attempt to apply the ECS rules to places they don't apply. In a similar vein, the RealizableComponent itself will probably soon be integrated into the RealizingSystem too. The attempt at using Lua environments in order to encapsulate the different behaviors that objects exhibit was scrapped in preference of just creating differently named Lua tables for each prototype. The new PrototypableComponent contains some information about entities which were prototyped. It is partially used by the ScriptingSystem to figure out what event handlers are appropriate, which may not be the best approach. It also has some data about automatic behavior, which also maybe does not belong in this component. The OnTouch event is raised by a player colliding with a physics body with the collider type "event", which may not be the best way to implement this. The result of all of this is that checkpoints now work, although no sound is played, and the result is not persistent across exiting the game. --- CMakeLists.txt | 2 +- res/platform.lua | 24 ----- scripts/checkpoint.lua | 17 ++++ scripts/common.lua | 21 ++++ scripts/movplat.lua | 8 ++ src/components/automatable.h | 25 ----- src/components/playable.h | 3 + src/components/ponderable.h | 3 +- src/components/prototypable.h | 22 +++++ src/components/realizable.h | 3 - src/components/runnable.h | 15 +++ src/game.cpp | 4 +- src/systems/automating.cpp | 106 -------------------- src/systems/automating.h | 22 ----- src/systems/pondering.cpp | 13 +++ src/systems/realizing.cpp | 66 ++++++++----- src/systems/scripting.cpp | 219 ++++++++++++++++++++++++++++++++++++++++++ src/systems/scripting.h | 28 ++++++ 18 files changed, 393 insertions(+), 208 deletions(-) delete mode 100644 res/platform.lua create mode 100644 scripts/checkpoint.lua create mode 100644 scripts/common.lua create mode 100644 scripts/movplat.lua delete mode 100644 src/components/automatable.h create mode 100644 src/components/prototypable.h create mode 100644 src/components/runnable.h delete mode 100644 src/systems/automating.cpp delete mode 100644 src/systems/automating.h create mode 100644 src/systems/scripting.cpp create mode 100644 src/systems/scripting.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fbc843c..837df0d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,7 +70,7 @@ add_executable(Aromatherapy src/systems/playing.cpp src/systems/scheduling.cpp src/systems/realizing.cpp - src/systems/automating.cpp + src/systems/scripting.cpp vendor/stb_image.cpp ) diff --git a/res/platform.lua b/res/platform.lua deleted file mode 100644 index 5200e22..0000000 --- a/res/platform.lua +++ /dev/null @@ -1,24 +0,0 @@ -function moveLeft(entity, len, speed) - local remaining = len / speed - - while (remaining > 0) do - entity:ponderable().vel.x = -speed - remaining = remaining - coroutine.yield() - end -end - -function moveRight(entity, len, speed) - local remaining = len / speed - - while (remaining > 0) do - entity:ponderable().vel.x = speed - remaining = remaining - coroutine.yield() - end -end - -function run(entity) - while true do - moveRight(entity, 90, 30) - moveLeft(entity, 90, 30) - end -end \ No newline at end of file diff --git a/scripts/checkpoint.lua b/scripts/checkpoint.lua new file mode 100644 index 0000000..452f81d --- /dev/null +++ b/scripts/checkpoint.lua @@ -0,0 +1,17 @@ +checkpoint = {} + +function checkpoint.OnTouch(id, player) + curMap = entity.new(realizing():singleton():realizable().activeMap) + + if not player:playable().checkpointMapObject or + not curMap:mappable().mapId == player:playable().checkpointMapId or + not id:prototypable().mapObjectIndex == + player:playable().checkpointMapObjectIndex then + + player:playable().checkpointMapObject = true + player:playable().checkpointMapId = curMap:mappable().mapId + player:playable().checkpointMapObjectIndex = + id:prototypable().mapObjectIndex + player:playable().checkpointPos = player:transformable().pos + end +end diff --git a/scripts/common.lua b/scripts/common.lua new file mode 100644 index 0000000..d84d97d --- /dev/null +++ b/scripts/common.lua @@ -0,0 +1,21 @@ +function waitForTick() + return coroutine.yield() +end + +function moveLeft(id, len, speed) + local remaining = len / speed + + while (remaining > 0) do + id:ponderable().vel.x = -speed + remaining = remaining - waitForTick() + end +end + +function moveRight(id, len, speed) + local remaining = len / speed + + while (remaining > 0) do + id:ponderable().vel.x = speed + remaining = remaining - waitForTick() + end +end diff --git a/scripts/movplat.lua b/scripts/movplat.lua new file mode 100644 index 0000000..21cc61a --- /dev/null +++ b/scripts/movplat.lua @@ -0,0 +1,8 @@ +movplat = {} + +function movplat.Behavior(id) + while true do + moveRight(id, 90, 30) + moveLeft(id, 90, 30) + end +end diff --git a/src/components/automatable.h b/src/components/automatable.h deleted file mode 100644 index d30340a..0000000 --- a/src/components/automatable.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef AUTOMATABLE_H_3D519131 -#define AUTOMATABLE_H_3D519131 - -#include "component.h" -#include -#include - -class AutomatableComponent : public Component { -public: - - std::unique_ptr runner; - std::unique_ptr behavior; - - sol::environment origBehavior; - - /** - * If this flag is disabled, the entity will be ignored by the automating - * system. - * - * @managed_by RealizingSystem - */ - bool active = false; -}; - -#endif /* end of include guard: AUTOMATABLE_H_3D519131 */ diff --git a/src/components/playable.h b/src/components/playable.h index b8af0f2..7404f1f 100644 --- a/src/components/playable.h +++ b/src/components/playable.h @@ -27,6 +27,9 @@ public: size_t checkpointMapId; vec2d checkpointPos; + bool checkpointMapObject = false; + size_t checkpointMapObjectIndex; + }; #endif /* end of include guard: PLAYABLE_H_DDC566C3 */ diff --git a/src/components/ponderable.h b/src/components/ponderable.h index e6aa976..221d267 100644 --- a/src/components/ponderable.h +++ b/src/components/ponderable.h @@ -34,7 +34,8 @@ public: platform, adjacency, warp, - danger + danger, + event }; /** diff --git a/src/components/prototypable.h b/src/components/prototypable.h new file mode 100644 index 0000000..4659e7c --- /dev/null +++ b/src/components/prototypable.h @@ -0,0 +1,22 @@ +#ifndef PROTOTYPABLE_H_817F2205 +#define PROTOTYPABLE_H_817F2205 + +#include "component.h" +#include "entity_manager.h" + +class PrototypableComponent : public Component { +public: + + using id_type = EntityManager::id_type; + + size_t mapObjectIndex; + + std::string prototypeId; + + bool hasBehavior = false; + bool runningBehavior = false; + + id_type behaviorScript; +}; + +#endif /* end of include guard: PROTOTYPABLE_H_817F2205 */ diff --git a/src/components/realizable.h b/src/components/realizable.h index bc834a2..b749aeb 100644 --- a/src/components/realizable.h +++ b/src/components/realizable.h @@ -4,7 +4,6 @@ #include "component.h" #include #include -#include #include "entity_manager.h" #include "vector.h" @@ -70,8 +69,6 @@ public: * The entity ID of the currently active player. */ id_type activePlayer; - - sol::state scriptEngine; }; #endif /* end of include guard: REALIZABLE_H_36D8D71E */ diff --git a/src/components/runnable.h b/src/components/runnable.h new file mode 100644 index 0000000..1b994fb --- /dev/null +++ b/src/components/runnable.h @@ -0,0 +1,15 @@ +#ifndef AUTOMATABLE_H_3D519131 +#define AUTOMATABLE_H_3D519131 + +#include "component.h" +#include +#include + +class RunnableComponent : public Component { +public: + + std::unique_ptr runner; + std::unique_ptr callable; +}; + +#endif /* end of include guard: AUTOMATABLE_H_3D519131 */ diff --git a/src/game.cpp b/src/game.cpp index bf2b10b..f2992e1 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -12,7 +12,7 @@ #include "systems/playing.h" #include "systems/scheduling.h" #include "systems/realizing.h" -#include "systems/automating.h" +#include "systems/scripting.h" #include "animation.h" #include "consts.h" @@ -36,7 +36,7 @@ Game::Game(std::mt19937& rng) : rng_(rng) systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); - systemManager_.emplaceSystem(*this); + systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); diff --git a/src/systems/automating.cpp b/src/systems/automating.cpp deleted file mode 100644 index 4a5357d..0000000 --- a/src/systems/automating.cpp +++ /dev/null @@ -1,106 +0,0 @@ -#include "automating.h" -#include "game.h" -#include "components/automatable.h" -#include "components/ponderable.h" -#include "components/realizable.h" -#include "components/transformable.h" -#include "systems/realizing.h" -#include "vector.h" - -struct script_entity { - using id_type = EntityManager::id_type; - - id_type id; - - script_entity(id_type id) : id(id) - { - } -}; - -void AutomatingSystem::tick(double dt) -{ - auto entities = game_.getEntityManager().getEntitiesWithComponents< - AutomatableComponent>(); - - for (id_type entity : entities) - { - auto& automatable = game_.getEntityManager(). - getComponent(entity); - - if (!automatable.active) - { - continue; - } - - auto result = (*automatable.behavior)(dt); - if (!result.valid()) - { - sol::error e = result; - throw std::runtime_error(e.what()); - } - } -} - -void AutomatingSystem::initPrototype(id_type prototype) -{ - auto& automatable = game_.getEntityManager(). - getComponent(prototype); - - auto& realizable = game_.getEntityManager(). - getComponent( - game_.getSystemManager().getSystem().getSingleton()); - automatable.behavior.reset(); - automatable.runner = std::unique_ptr(new sol::thread(sol::thread::create(realizable.scriptEngine.lua_state()))); - automatable.behavior = std::unique_ptr(new sol::coroutine(automatable.runner->state()["run"])); - - auto result = (*automatable.behavior)(script_entity(prototype)); - if (!result.valid()) - { - sol::error e = result; - throw std::runtime_error(e.what()); - } -} - -void AutomatingSystem::initScriptEngine(sol::state& scriptEngine) -{ - scriptEngine.open_libraries(sol::lib::base, sol::lib::coroutine); - scriptEngine.new_usertype( - "vec2d", - sol::constructors(), - "x", sol::property( - [] (vec2d& v) -> double { return v.x(); }, - [] (vec2d& v, double x) { v.x() = x; }), - "y", sol::property( - [] (vec2d& v) -> double { return v.y(); }, - [] (vec2d& v, double y) { v.y() = y; })); - - scriptEngine.new_usertype( - "vec2i", - sol::constructors(), - "x", [] (vec2i& v) -> int& { return v.x(); }, - "y", [] (vec2i& v) -> int& { return v.y(); }); - - scriptEngine.new_usertype( - "entity", - sol::constructors(), - "id", &script_entity::id, - "transformable", - [&] (script_entity& entity) -> TransformableComponent& { - return game_.getEntityManager(). - getComponent(entity.id); - }, - "ponderable", - [&] (script_entity& entity) -> PonderableComponent& { - return game_.getEntityManager(). - getComponent(entity.id); - }); - - scriptEngine.new_usertype( - "transformable", - "pos", &TransformableComponent::pos); - - scriptEngine.new_usertype( - "ponderable", - "vel", &PonderableComponent::vel, - "accel", &PonderableComponent::accel); -} diff --git a/src/systems/automating.h b/src/systems/automating.h deleted file mode 100644 index 117b622..0000000 --- a/src/systems/automating.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef AUTOMATING_H_E6E5D76E -#define AUTOMATING_H_E6E5D76E - -#include "system.h" -#include - -class AutomatingSystem : public System { -public: - - AutomatingSystem(Game& game) : System(game) - { - } - - void tick(double dt); - - void initPrototype(id_type prototype); - - void initScriptEngine(sol::state& scriptEngine); - -}; - -#endif /* end of include guard: AUTOMATING_H_E6E5D76E */ diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index c806cc8..a3eb36d 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp @@ -11,6 +11,7 @@ #include "systems/orienting.h" #include "systems/playing.h" #include "systems/realizing.h" +#include "systems/scripting.h" #include "consts.h" void PonderingSystem::tick(double dt) @@ -857,6 +858,18 @@ void PonderingSystem::processCollision( break; } + case PonderableComponent::Collision::event: + { + if (game_.getEntityManager(). + hasComponent(entity)) + { + game_.getSystemManager().getSystem(). + onTouch(collider, entity); + } + + break; + } + default: { // Not yet implemented. diff --git a/src/systems/realizing.cpp b/src/systems/realizing.cpp index 28e2279..2ee5897 100644 --- a/src/systems/realizing.cpp +++ b/src/systems/realizing.cpp @@ -12,11 +12,11 @@ #include "components/playable.h" #include "components/ponderable.h" #include "components/transformable.h" -#include "components/automatable.h" +#include "components/prototypable.h" #include "systems/mapping.h" #include "systems/animating.h" #include "systems/pondering.h" -#include "systems/automating.h" +#include "systems/scripting.h" inline xmlChar* getProp(xmlNodePtr node, const char* attr) { @@ -40,9 +40,6 @@ EntityManager::id_type RealizingSystem::initSingleton( auto& realizable = game_.getEntityManager(). emplaceComponent(world); - game_.getSystemManager().getSystem(). - initScriptEngine(realizable.scriptEngine); - realizable.worldFile = worldFile; realizable.prototypeFile = prototypeFile; @@ -216,14 +213,28 @@ EntityManager::id_type RealizingSystem::initSingleton( game_.getSystemManager().getSystem(). initializeBody(mapObject, PonderableComponent::Type::vacuumed); + + + + + auto& prototypable = game_.getEntityManager(). + emplaceComponent(mapObject); + + prototypable.prototypeId = prototypeId; + + key = getProp(mapNode, "index"); + prototypable.mapObjectIndex = atoi(reinterpret_cast(key)); + xmlFree(key); + if (prototypeId == "movplat") { - auto& automatable = game_.getEntityManager(). - emplaceComponent(mapObject); - + prototypable.hasBehavior = true; + } else if (prototypeId == "checkpoint") + { + auto& ponderable = game_.getEntityManager(). + getComponent(mapObject); - realizable.scriptEngine.script_file( - "res/platform.lua");//, + ponderable.colliderType = PonderableComponent::Collision::event; } mappable.objects.push_back(mapObject); @@ -319,7 +330,6 @@ void RealizingSystem::loadMap(id_type mapEntity) auto& animating = game_.getSystemManager().getSystem(); auto& pondering = game_.getSystemManager().getSystem(); - auto& automating = game_.getSystemManager().getSystem(); std::set players = game_.getEntityManager().getEntitiesWithComponents< @@ -380,11 +390,6 @@ void RealizingSystem::loadMap(id_type mapEntity) pondering.initPrototype(prototype); } - if (game_.getEntityManager().hasComponent(prototype)) - { - automating.initPrototype(prototype); - } - enterActiveMap(prototype); } @@ -419,12 +424,19 @@ void RealizingSystem::enterActiveMap(id_type entity) ponderable.active = true; } - if (game_.getEntityManager().hasComponent(entity)) + if (game_.getEntityManager().hasComponent(entity)) { - auto& automatable = game_.getEntityManager(). - getComponent(entity); + auto& prototypable = game_.getEntityManager(). + getComponent(entity); + + if (prototypable.hasBehavior) + { + auto& scripting = game_.getSystemManager().getSystem(); - automatable.active = true; + prototypable.hasBehavior = true; + prototypable.runningBehavior = true; + prototypable.behaviorScript = scripting.runBehaviorScript(entity); + } } } @@ -446,11 +458,17 @@ void RealizingSystem::leaveActiveMap(id_type entity) ponderable.active = false; } - if (game_.getEntityManager().hasComponent(entity)) + if (game_.getEntityManager().hasComponent(entity)) { - auto& automatable = game_.getEntityManager(). - getComponent(entity); + auto& prototypable = game_.getEntityManager(). + getComponent(entity); - automatable.active = false; + if (prototypable.runningBehavior) + { + auto& scripting = game_.getSystemManager().getSystem(); + scripting.killScript(prototypable.behaviorScript); + + prototypable.runningBehavior = false; + } } } diff --git a/src/systems/scripting.cpp b/src/systems/scripting.cpp new file mode 100644 index 0000000..dc1fff5 --- /dev/null +++ b/src/systems/scripting.cpp @@ -0,0 +1,219 @@ +#include "scripting.h" +#include "game.h" +#include "components/runnable.h" +#include "components/ponderable.h" +#include "components/realizable.h" +#include "components/transformable.h" +#include "components/playable.h" +#include "components/mappable.h" +#include "components/prototypable.h" +#include "systems/realizing.h" +#include "vector.h" + +struct script_entity { + using id_type = EntityManager::id_type; + + id_type id; + + script_entity(id_type id) : id(id) + { + } +}; + +ScriptingSystem::ScriptingSystem(Game& game) : System(game) +{ + id_type entity = game_.getEntityManager().emplaceEntity(); + + engine.open_libraries(sol::lib::base, sol::lib::coroutine); + + engine.new_usertype( + "vec2d", + sol::constructors(), + "x", sol::property( + [] (vec2d& v) -> double { return v.x(); }, + [] (vec2d& v, double x) { v.x() = x; }), + "y", sol::property( + [] (vec2d& v) -> double { return v.y(); }, + [] (vec2d& v, double y) { v.y() = y; })); + + engine.new_usertype( + "vec2i", + sol::constructors(), + "x", [] (vec2i& v) -> int& { return v.x(); }, + "y", [] (vec2i& v) -> int& { return v.y(); }); + + engine.new_usertype( + "entity", + sol::constructors(), + "id", &script_entity::id, + "transformable", + [&] (script_entity& entity) -> TransformableComponent& { + return game_.getEntityManager(). + getComponent(entity.id); + }, + "ponderable", + [&] (script_entity& entity) -> PonderableComponent& { + return game_.getEntityManager(). + getComponent(entity.id); + }, + "mappable", + [&] (script_entity& entity) -> MappableComponent& { + return game_.getEntityManager(). + getComponent(entity.id); + }, + "playable", + [&] (script_entity& entity) -> PlayableComponent& { + return game_.getEntityManager(). + getComponent(entity.id); + }, + "realizable", + [&] (script_entity& entity) -> RealizableComponent& { + return game_.getEntityManager(). + getComponent(entity.id); + }, + "prototypable", + [&] (script_entity& entity) -> PrototypableComponent& { + return game_.getEntityManager(). + getComponent(entity.id); + }); + + engine.new_usertype( + "transformable", + "pos", &TransformableComponent::pos); + + engine.new_usertype( + "ponderable", + "vel", &PonderableComponent::vel, + "accel", &PonderableComponent::accel); + + engine.new_usertype( + "mappable", + "mapId", &MappableComponent::mapId); + + engine.new_usertype( + "playable", + "checkpointPos", &PlayableComponent::checkpointPos, + "checkpointMapId", &PlayableComponent::checkpointMapId, + "checkpointMapObject", &PlayableComponent::checkpointMapObject, + "checkpointMapObjectIndex", &PlayableComponent::checkpointMapObjectIndex); + + engine.new_usertype( + "realizable", + "activeMap", &RealizableComponent::activeMap); + + engine.new_usertype( + "prototypable", + "mapObjectIndex", &PrototypableComponent::mapObjectIndex, + "prototypeId", &PrototypableComponent::prototypeId); + + engine.new_usertype( + "realizing", + "singleton", + [&] (RealizingSystem& realizing) -> script_entity { + return realizing.getSingleton(); + }); + + engine.set_function( + "realizing", + [&] () { + return game_.getSystemManager().getSystem(); + }); + + engine.script_file("scripts/common.lua"); + engine.script_file("scripts/movplat.lua"); + engine.script_file("scripts/checkpoint.lua"); +} + +void ScriptingSystem::tick(double dt) +{ + auto entities = game_.getEntityManager().getEntitiesWithComponents< + RunnableComponent>(); + + for (id_type entity : entities) + { + auto& runnable = game_.getEntityManager(). + getComponent(entity); + + if (*runnable.callable) + { + auto result = (*runnable.callable)(dt); + if (!result.valid()) + { + sol::error e = result; + throw std::runtime_error(e.what()); + } + } + + if (!*runnable.callable) + { + game_.getEntityManager().deleteEntity(entity); + } + } +} + +void ScriptingSystem::killScript(id_type entity) +{ + if (game_.getEntityManager().hasComponent(entity)) + { + game_.getEntityManager().deleteEntity(entity); + } +} + +template +EntityManager::id_type ScriptingSystem::runScript( + std::string event, + id_type entity, + Args&&... args) +{ + auto& prototypable = game_.getEntityManager(). + getComponent(entity); + + id_type script = game_.getEntityManager().emplaceEntity(); + + auto& runnable = game_.getEntityManager(). + emplaceComponent(script); + + runnable.runner = + std::unique_ptr( + new sol::thread( + sol::thread::create( + engine.lua_state()))); + + runnable.callable = + std::unique_ptr( + new sol::coroutine( + runnable.runner->state(). + traverse_get( + prototypable.prototypeId, + event))); + + if (!*runnable.callable) + { + throw std::runtime_error("Error running script"); + } + + auto result = (*runnable.callable)( + script_entity(entity), + std::forward(args)...); + + if (!result.valid()) + { + sol::error e = result; + throw std::runtime_error(e.what()); + } + + return script; +} + +EntityManager::id_type ScriptingSystem::runBehaviorScript(id_type entity) +{ + return runScript("Behavior", entity); +} + +void ScriptingSystem::onTouch(id_type entity, id_type player) +{ + runScript( + "OnTouch", + entity, + script_entity(player)); +} diff --git a/src/systems/scripting.h b/src/systems/scripting.h new file mode 100644 index 0000000..d5380f1 --- /dev/null +++ b/src/systems/scripting.h @@ -0,0 +1,28 @@ +#ifndef AUTOMATING_H_E6E5D76E +#define AUTOMATING_H_E6E5D76E + +#include "system.h" +#include + +class ScriptingSystem : public System { +public: + + ScriptingSystem(Game& game); + + void tick(double dt); + + void killScript(id_type entity); + + id_type runBehaviorScript(id_type entity); + + void onTouch(id_type entity, id_type player); + +private: + + template + id_type runScript(std::string event, id_type entity, Args&&... args); + + sol::state engine; +}; + +#endif /* end of include guard: AUTOMATING_H_E6E5D76E */ -- cgit 1.4.1