1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#include "script_system.h"
#include <iostream>
#include "game.h"
#include "message_system.h"
#include "animation_system.h"
#include "character_system.h"
ScriptSystem::ScriptSystem(Game& game) : game_(game) {
engine_.open_libraries(
sol::lib::base,
sol::lib::coroutine,
sol::lib::math);
engine_.new_usertype<Sprite>(
"sprite",
"dir", &Sprite::dir,
"followers", &Sprite::followers,
"characterState", &Sprite::characterState,
"controllable", &Sprite::controllable);
engine_.new_usertype<MessageSystem>(
"message",
"displayMessage", &MessageSystem::displayMessage,
"showChoice", &MessageSystem::showChoice,
"hideCutsceneBars", &MessageSystem::hideCutsceneBars,
"isMessageActive", sol::property(&MessageSystem::isMessageActive),
"getChoiceSelection", &MessageSystem::getChoiceSelection);
engine_.new_usertype<AnimationSystem>(
"animation",
"setSpriteAnimation", &AnimationSystem::setSpriteAnimation,
"setSpriteDirection", &AnimationSystem::setSpriteDirection);
engine_.new_usertype<CharacterSystem>(
"character",
"startRunning", &CharacterSystem::startRunning,
"halt", &CharacterSystem::halt);
engine_.new_usertype<Mixer>(
"mixer",
"playSound", &Mixer::playSound,
"loopSound", &Mixer::loopSound,
"stopChannel", &Mixer::stopChannel);
engine_.set_function(
"message",
[&] () -> MessageSystem& {
return game_.getSystem<MessageSystem>();
});
engine_.set_function(
"animation",
[&] () -> AnimationSystem& {
return game_.getSystem<AnimationSystem>();
});
engine_.set_function(
"character",
[&] () -> CharacterSystem& {
return game_.getSystem<CharacterSystem>();
});
engine_.set_function(
"mixer",
[&] () -> Mixer& {
return game_.getMixer();
});
engine_.set_function(
"getSpriteByAlias",
[&] (std::string alias) -> int {
return game_.getSpriteByAlias(alias);
});
engine_.set_function(
"getPlayerSprite",
[&] () -> int {
for (int id : game_.getSprites()) {
Sprite& sprite = game_.getSprite(id);
if (sprite.player) {
return id;
}
}
return -1;
});
engine_.set_function(
"getSprite",
[&] (int id) -> Sprite& {
return game_.getSprite(id);
});
engine_.set_function(
"loadMap",
[&] (std::string filename, std::string warpPoint, Direction dir) {
game_.loadMap(filename, warpPoint, dir);
});
engine_.set_function(
"setFadeoutProgress",
[&] (double val) {
game_.setFadeoutProgress(val);
});
engine_.script_file("../res/scripts/common.lua");
}
void ScriptSystem::tick(double dt) {
if (callable_ && *callable_) {
auto result = (*callable_)(dt);
if (!result.valid()) {
sol::error e = result;
throw e;
}
if (!*callable_) {
callable_.reset();
runner_.reset();
}
}
}
void ScriptSystem::runScript(std::string mapName, std::string scriptName) {
if (!loadedScripts_.count(mapName)) {
engine_.script_file("../res/scripts/" + mapName + ".lua");
loadedScripts_.insert(mapName);
}
runner_.reset(new sol::thread(sol::thread::create(engine_.lua_state())));
callable_.reset(new sol::coroutine(runner_->state().traverse_get<sol::function>(mapName, scriptName)));
if (!*callable_) {
throw std::runtime_error("Error running script: " + mapName + "." + scriptName);
}
auto result = (*callable_)();
if (!result.valid()) {
sol::error e = result;
throw e;
}
if (!*callable_) {
callable_.reset();
runner_.reset();
}
}
|