summary refs log tree commit diff stats
path: root/src/systems/automating.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-05-10 19:27:59 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-05-17 15:39:39 -0400
commit4bbfeae42a1245b1b84e8847787d7643e6a6f2cf (patch)
tree8dd65d9ab0cfffd0e79f670c94b035c5eebfa934 /src/systems/automating.cpp
parent67b24a8ddd89371cfb944c5b441c852f0edc23b1 (diff)
downloadtherapy-4bbfeae42a1245b1b84e8847787d7643e6a6f2cf.tar.gz
therapy-4bbfeae42a1245b1b84e8847787d7643e6a6f2cf.tar.bz2
therapy-4bbfeae42a1245b1b84e8847787d7643e6a6f2cf.zip
Started integrating Lua as a scripting engine
Currently moving platforms are able to have their movement controlled by a script rather than by XML, which is probably a better implementation and scales better to other things. The scripts, instead of using the components as state, use the stack as state. In this way, they pretend to be multithreaded. For instance, the moving platform calls moveRight and then moveLeft. Both of those functions internally make calls that say to wait until the next tick. When the AutomatingSystem ticks, it continues execution of all scripts (sequentially, of course) until they ask for the next tick again. This is implemented using coroutines.
Diffstat (limited to 'src/systems/automating.cpp')
-rw-r--r--src/systems/automating.cpp99
1 files changed, 57 insertions, 42 deletions
diff --git a/src/systems/automating.cpp b/src/systems/automating.cpp index 61b97d9..6cec3bf 100644 --- a/src/systems/automating.cpp +++ b/src/systems/automating.cpp
@@ -2,13 +2,24 @@
2#include "game.h" 2#include "game.h"
3#include "components/automatable.h" 3#include "components/automatable.h"
4#include "components/ponderable.h" 4#include "components/ponderable.h"
5#include "systems/pondering.h" 5#include "components/realizable.h"
6#include "systems/realizing.h"
7#include "vector.h"
8
9struct script_entity {
10 using id_type = EntityManager::id_type;
11
12 id_type id;
13
14 script_entity(id_type id) : id(id)
15 {
16 }
17};
6 18
7void AutomatingSystem::tick(double dt) 19void AutomatingSystem::tick(double dt)
8{ 20{
9 auto entities = game_.getEntityManager().getEntitiesWithComponents< 21 auto entities = game_.getEntityManager().getEntitiesWithComponents<
10 AutomatableComponent, 22 AutomatableComponent>();
11 PonderableComponent>();
12 23
13 for (id_type entity : entities) 24 for (id_type entity : entities)
14 { 25 {
@@ -20,43 +31,7 @@ void AutomatingSystem::tick(double dt)
20 continue; 31 continue;
21 } 32 }
22 33
23 if (automatable.behaviorRunning && 34 (*automatable.behavior)(dt);
24 (automatable.remaining <= 0.0))
25 {
26 automatable.currentAction++;
27 automatable.actionRunning = false;
28
29 if (automatable.currentAction ==
30 automatable.behaviors[automatable.currentBehavior].size())
31 {
32 automatable.behaviorRunning = false;
33 }
34 }
35
36 if (!automatable.behaviorRunning)
37 {
38 automatable.currentBehavior = automatable.behaviorDist(game_.getRng());
39 automatable.currentAction = 0;
40 automatable.behaviorRunning = true;
41 }
42
43 AutomatableComponent::Action& curAction =
44 automatable.behaviors
45 [automatable.currentBehavior]
46 [automatable.currentAction];
47
48 if (!automatable.actionRunning)
49 {
50 automatable.remaining = curAction.dur;
51 automatable.actionRunning = true;
52 }
53
54 auto& ponderable = game_.getEntityManager().
55 getComponent<PonderableComponent>(entity);
56
57 ponderable.vel = curAction.speed;
58
59 automatable.remaining -= dt;
60 } 35 }
61} 36}
62 37
@@ -65,6 +40,46 @@ void AutomatingSystem::initPrototype(id_type prototype)
65 auto& automatable = game_.getEntityManager(). 40 auto& automatable = game_.getEntityManager().
66 getComponent<AutomatableComponent>(prototype); 41 getComponent<AutomatableComponent>(prototype);
67 42
68 automatable.behaviorRunning = false; 43 auto& realizable = game_.getEntityManager().
69 automatable.actionRunning = false; 44 getComponent<RealizableComponent>(
45 game_.getSystemManager().getSystem<RealizingSystem>().getSingleton());
46 automatable.behavior.reset();
47 automatable.runner = std::unique_ptr<sol::thread>(new sol::thread(sol::thread::create(realizable.scriptEngine.lua_state())));
48 automatable.behavior = std::unique_ptr<sol::coroutine>(new sol::coroutine(automatable.runner->state()["run"]));
49 (*automatable.behavior)(script_entity(prototype));
50}
51
52void AutomatingSystem::initScriptEngine(sol::state& scriptEngine)
53{
54 scriptEngine.open_libraries(sol::lib::base, sol::lib::coroutine);
55 scriptEngine.new_usertype<vec2d>(
56 "vec2d",
57 sol::constructors<vec2d(), vec2d(double, double)>(),
58 "x", sol::property(
59 [] (vec2d& v) -> double { return v.x(); },
60 [] (vec2d& v, double x) { v.x() = x; }),
61 "y", sol::property(
62 [] (vec2d& v) -> double { return v.y(); },
63 [] (vec2d& v, double y) { v.y() = y; }));
64
65 scriptEngine.new_usertype<vec2i>(
66 "vec2i",
67 sol::constructors<vec2i(), vec2i(int, int)>(),
68 "x", [] (vec2i& v) -> int& { return v.x(); },
69 "y", [] (vec2i& v) -> int& { return v.y(); });
70
71 scriptEngine.new_usertype<script_entity>(
72 "entity",
73 sol::constructors<script_entity(id_type)>(),
74 "id", &script_entity::id,
75 "ponderable",
76 [&] (script_entity& entity) -> PonderableComponent& {
77 return game_.getEntityManager().
78 getComponent<PonderableComponent>(entity.id);
79 });
80
81 scriptEngine.new_usertype<PonderableComponent>(
82 "ponderable",
83 "vel", &PonderableComponent::vel,
84 "accel", &PonderableComponent::accel);
70} 85}