diff options
Diffstat (limited to 'src/systems/scripting.cpp')
-rw-r--r-- | src/systems/scripting.cpp | 70 |
1 files changed, 59 insertions, 11 deletions
diff --git a/src/systems/scripting.cpp b/src/systems/scripting.cpp index 57c3fd5..c423558 100644 --- a/src/systems/scripting.cpp +++ b/src/systems/scripting.cpp | |||
@@ -6,6 +6,7 @@ | |||
6 | #include "components/playable.h" | 6 | #include "components/playable.h" |
7 | #include "components/mappable.h" | 7 | #include "components/mappable.h" |
8 | #include "components/prototypable.h" | 8 | #include "components/prototypable.h" |
9 | #include "components/automatable.h" | ||
9 | #include "systems/realizing.h" | 10 | #include "systems/realizing.h" |
10 | #include "vector.h" | 11 | #include "vector.h" |
11 | 12 | ||
@@ -133,28 +134,34 @@ void ScriptingSystem::tick(double dt) | |||
133 | 134 | ||
134 | if (!*runnable.callable) | 135 | if (!*runnable.callable) |
135 | { | 136 | { |
136 | game_.getEntityManager().deleteEntity(entity); | 137 | killScript(entity); |
137 | } | 138 | } |
138 | } | 139 | } |
139 | } | 140 | } |
140 | 141 | ||
141 | void ScriptingSystem::killScript(id_type entity) | 142 | void ScriptingSystem::killScript(id_type entity) |
142 | { | 143 | { |
143 | if (game_.getEntityManager().hasComponent<RunnableComponent>(entity)) | 144 | auto& runnable = game_.getEntityManager(). |
145 | getComponent<RunnableComponent>(entity); | ||
146 | |||
147 | if (runnable.behavior) | ||
144 | { | 148 | { |
145 | game_.getEntityManager().deleteEntity(entity); | 149 | auto& automatable = game_.getEntityManager(). |
150 | getComponent<AutomatableComponent>(runnable.actor); | ||
151 | |||
152 | automatable.running = false; | ||
146 | } | 153 | } |
154 | |||
155 | game_.getEntityManager().deleteEntity(entity); | ||
147 | } | 156 | } |
148 | 157 | ||
149 | template <typename... Args> | 158 | template <typename... Args> |
150 | EntityManager::id_type ScriptingSystem::runScript( | 159 | sol::optional<EntityManager::id_type> ScriptingSystem::runScript( |
160 | std::string table, | ||
151 | std::string event, | 161 | std::string event, |
152 | id_type entity, | 162 | id_type entity, |
153 | Args&&... args) | 163 | Args&&... args) |
154 | { | 164 | { |
155 | auto& prototypable = game_.getEntityManager(). | ||
156 | getComponent<PrototypableComponent>(entity); | ||
157 | |||
158 | id_type script = game_.getEntityManager().emplaceEntity(); | 165 | id_type script = game_.getEntityManager().emplaceEntity(); |
159 | 166 | ||
160 | auto& runnable = game_.getEntityManager(). | 167 | auto& runnable = game_.getEntityManager(). |
@@ -171,7 +178,7 @@ EntityManager::id_type ScriptingSystem::runScript( | |||
171 | new sol::coroutine( | 178 | new sol::coroutine( |
172 | runnable.runner->state(). | 179 | runnable.runner->state(). |
173 | traverse_get<sol::function>( | 180 | traverse_get<sol::function>( |
174 | prototypable.prototypeId, | 181 | table, |
175 | event))); | 182 | event))); |
176 | 183 | ||
177 | if (!*runnable.callable) | 184 | if (!*runnable.callable) |
@@ -189,17 +196,58 @@ EntityManager::id_type ScriptingSystem::runScript( | |||
189 | throw std::runtime_error(e.what()); | 196 | throw std::runtime_error(e.what()); |
190 | } | 197 | } |
191 | 198 | ||
192 | return script; | 199 | if (*runnable.callable) |
200 | { | ||
201 | return { script }; | ||
202 | } else { | ||
203 | killScript(script); | ||
204 | |||
205 | return {}; | ||
206 | } | ||
207 | } | ||
208 | |||
209 | void ScriptingSystem::startBehavior(id_type entity) | ||
210 | { | ||
211 | auto& automatable = game_.getEntityManager(). | ||
212 | getComponent<AutomatableComponent>(entity); | ||
213 | |||
214 | sol::optional<id_type> script = | ||
215 | runScript( | ||
216 | automatable.table, | ||
217 | "Behavior", | ||
218 | entity); | ||
219 | |||
220 | if (script) | ||
221 | { | ||
222 | automatable.script = *script; | ||
223 | automatable.running = true; | ||
224 | |||
225 | auto& runnable = game_.getEntityManager(). | ||
226 | getComponent<RunnableComponent>(automatable.script); | ||
227 | |||
228 | runnable.behavior = true; | ||
229 | runnable.actor = entity; | ||
230 | } | ||
193 | } | 231 | } |
194 | 232 | ||
195 | EntityManager::id_type ScriptingSystem::runBehaviorScript(id_type entity) | 233 | void ScriptingSystem::stopBehavior(id_type entity) |
196 | { | 234 | { |
197 | return runScript("Behavior", entity); | 235 | auto& automatable = game_.getEntityManager(). |
236 | getComponent<AutomatableComponent>(entity); | ||
237 | |||
238 | if (automatable.running) | ||
239 | { | ||
240 | killScript(automatable.script); | ||
241 | } | ||
198 | } | 242 | } |
199 | 243 | ||
200 | void ScriptingSystem::onTouch(id_type entity, id_type player) | 244 | void ScriptingSystem::onTouch(id_type entity, id_type player) |
201 | { | 245 | { |
246 | auto& prototypable = game_.getEntityManager(). | ||
247 | getComponent<PrototypableComponent>(entity); | ||
248 | |||
202 | runScript( | 249 | runScript( |
250 | prototypable.prototypeId, | ||
203 | "OnTouch", | 251 | "OnTouch", |
204 | entity, | 252 | entity, |
205 | script_entity(player)); | 253 | script_entity(player)); |