about summary refs log tree commit diff stats
path: root/data/maps/the_bearer/rooms/Green Town.txtpb
blob: 184ded3ebb2913e0802d2bbe138969cfbd723a41 (plain) (blame)
1
2
name: "Green Town"
display_name: "Main Area"
background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#include "script_system.h"
#include <iostream>
#include "game.h"
#include "message_system.h"
#include "animation_system.h"

ScriptSystem::ScriptSystem(Game& game) : game_(game) {
  engine_.open_libraries(
    sol::lib::base,
    sol::lib::coroutine,
    sol::lib::math);

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

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

  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(
    "mixer",
    [&] () -> Mixer& {
      return game_.getMixer();
    });

  engine_.set_function(
    "getSpriteByAlias",
    [&] (std::string alias) -> int {
      return game_.getSpriteByAlias(alias);
    });

  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 std::runtime_error(e.what());
    }

    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 std::runtime_error(e.what());
  }

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