diff options
Diffstat (limited to 'src/systems/automating.cpp')
| -rw-r--r-- | src/systems/automating.cpp | 106 |
1 files changed, 0 insertions, 106 deletions
| 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 @@ | |||
| 1 | #include "automating.h" | ||
| 2 | #include "game.h" | ||
| 3 | #include "components/automatable.h" | ||
| 4 | #include "components/ponderable.h" | ||
| 5 | #include "components/realizable.h" | ||
| 6 | #include "components/transformable.h" | ||
| 7 | #include "systems/realizing.h" | ||
| 8 | #include "vector.h" | ||
| 9 | |||
| 10 | struct script_entity { | ||
| 11 | using id_type = EntityManager::id_type; | ||
| 12 | |||
| 13 | id_type id; | ||
| 14 | |||
| 15 | script_entity(id_type id) : id(id) | ||
| 16 | { | ||
| 17 | } | ||
| 18 | }; | ||
| 19 | |||
| 20 | void AutomatingSystem::tick(double dt) | ||
| 21 | { | ||
| 22 | auto entities = game_.getEntityManager().getEntitiesWithComponents< | ||
| 23 | AutomatableComponent>(); | ||
| 24 | |||
| 25 | for (id_type entity : entities) | ||
| 26 | { | ||
| 27 | auto& automatable = game_.getEntityManager(). | ||
| 28 | getComponent<AutomatableComponent>(entity); | ||
| 29 | |||
| 30 | if (!automatable.active) | ||
| 31 | { | ||
| 32 | continue; | ||
| 33 | } | ||
| 34 | |||
| 35 | auto result = (*automatable.behavior)(dt); | ||
| 36 | if (!result.valid()) | ||
| 37 | { | ||
| 38 | sol::error e = result; | ||
| 39 | throw std::runtime_error(e.what()); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | void AutomatingSystem::initPrototype(id_type prototype) | ||
| 45 | { | ||
| 46 | auto& automatable = game_.getEntityManager(). | ||
| 47 | getComponent<AutomatableComponent>(prototype); | ||
| 48 | |||
| 49 | auto& realizable = game_.getEntityManager(). | ||
| 50 | getComponent<RealizableComponent>( | ||
| 51 | game_.getSystemManager().getSystem<RealizingSystem>().getSingleton()); | ||
| 52 | automatable.behavior.reset(); | ||
| 53 | automatable.runner = std::unique_ptr<sol::thread>(new sol::thread(sol::thread::create(realizable.scriptEngine.lua_state()))); | ||
| 54 | automatable.behavior = std::unique_ptr<sol::coroutine>(new sol::coroutine(automatable.runner->state()["run"])); | ||
| 55 | |||
| 56 | auto result = (*automatable.behavior)(script_entity(prototype)); | ||
| 57 | if (!result.valid()) | ||
| 58 | { | ||
| 59 | sol::error e = result; | ||
| 60 | throw std::runtime_error(e.what()); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | void AutomatingSystem::initScriptEngine(sol::state& scriptEngine) | ||
| 65 | { | ||
| 66 | scriptEngine.open_libraries(sol::lib::base, sol::lib::coroutine); | ||
| 67 | scriptEngine.new_usertype<vec2d>( | ||
| 68 | "vec2d", | ||
| 69 | sol::constructors<vec2d(), vec2d(double, double)>(), | ||
| 70 | "x", sol::property( | ||
| 71 | [] (vec2d& v) -> double { return v.x(); }, | ||
| 72 | [] (vec2d& v, double x) { v.x() = x; }), | ||
| 73 | "y", sol::property( | ||
| 74 | [] (vec2d& v) -> double { return v.y(); }, | ||
| 75 | [] (vec2d& v, double y) { v.y() = y; })); | ||
| 76 | |||
| 77 | scriptEngine.new_usertype<vec2i>( | ||
| 78 | "vec2i", | ||
| 79 | sol::constructors<vec2i(), vec2i(int, int)>(), | ||
| 80 | "x", [] (vec2i& v) -> int& { return v.x(); }, | ||
| 81 | "y", [] (vec2i& v) -> int& { return v.y(); }); | ||
| 82 | |||
| 83 | scriptEngine.new_usertype<script_entity>( | ||
| 84 | "entity", | ||
| 85 | sol::constructors<script_entity(id_type)>(), | ||
| 86 | "id", &script_entity::id, | ||
| 87 | "transformable", | ||
| 88 | [&] (script_entity& entity) -> TransformableComponent& { | ||
| 89 | return game_.getEntityManager(). | ||
| 90 | getComponent<TransformableComponent>(entity.id); | ||
| 91 | }, | ||
| 92 | "ponderable", | ||
| 93 | [&] (script_entity& entity) -> PonderableComponent& { | ||
| 94 | return game_.getEntityManager(). | ||
| 95 | getComponent<PonderableComponent>(entity.id); | ||
| 96 | }); | ||
| 97 | |||
| 98 | scriptEngine.new_usertype<TransformableComponent>( | ||
| 99 | "transformable", | ||
| 100 | "pos", &TransformableComponent::pos); | ||
| 101 | |||
| 102 | scriptEngine.new_usertype<PonderableComponent>( | ||
| 103 | "ponderable", | ||
| 104 | "vel", &PonderableComponent::vel, | ||
| 105 | "accel", &PonderableComponent::accel); | ||
| 106 | } | ||
