summary refs log tree commit diff stats
path: root/src/script_system.cpp
blob: e388c6e58a03be5408634201519921d28bc4bd42 (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#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);

  engine_.new_usertype<MessageSystem>(
    "message",
    "displayMessage", &MessageSystem::displayMessage,
    "hideCutsceneBars", &MessageSystem::hideCutsceneBars,
    "isMessageActive", sol::property(&MessageSystem::isMessageActive));

  engine_.new_usertype<AnimationSystem>(
    "animation",
    "setSpriteAnimation", &AnimationSystem::setSpriteAnimation,
    "setSpriteDirection", &AnimationSystem::setSpriteDirection);

  engine_.new_usertype<CharacterSystem>(
    "character",
    "startRunning", &CharacterSystem::startRunning);

  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(
    "getControllableSprite",
    [&] () -> int {
      for (int id : game_.getSprites()) {
        Sprite& sprite = game_.getSprite(id);
        if (sprite.controllable) {
          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 name) {
  if (!loadedScripts_.count(name)) {
    engine_.script_file("../res/scripts/" + name + ".lua");
    loadedScripts_.insert(name);
  }

  runner_.reset(new sol::thread(sol::thread::create(engine_.lua_state())));
  callable_.reset(new sol::coroutine(runner_->state().get<sol::function>(name)));

  if (!*callable_) {
    throw std::runtime_error("Error running script: " + name);
  }

  auto result = (*callable_)();
  if (!result.valid()) {
    sol::error e = result;
    throw e;
  }

  if (!*callable_) {
    callable_.reset();
    runner_.reset();
  }
}