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. --- 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 ++++++ 6 files changed, 302 insertions(+), 152 deletions(-) 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 (limited to 'src/systems') 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