Inverted control flow, engine as library

This commit is contained in:
2021-10-04 17:45:18 -04:00
parent e0fdb2d42f
commit bc22dc89af
15 changed files with 387 additions and 136 deletions

View File

@@ -144,6 +144,7 @@ void TextGame::do_quit_command(const StringVec &cmd) {
return;
}
actor_id_ = 0;
stop_driver();
}
void TextGame::do_command(const StringVec &words) {
@@ -169,18 +170,28 @@ void TextGame::check_redirects() {
}
}
void TextGame::run()
void TextGame::init()
{
world_.reset(new World(util::WORLD_TYPE_STANDALONE));
world_->update_source();
std::unique_ptr<util::LuaSource> lsource = get_lua_source();
world_->update_source(*lsource);
world_->run_unittests();
actor_id_ = world_->create_login_actor();
std::cerr << "Login actor ID: " << actor_id_ << std::endl;
console_.clear();
}
void TextGame::update() {
check_redirects();
if (actor_id_ == 0) {
stop_driver();
return;
}
// Process lines from stdin.
while (true) {
check_redirects();
if (actor_id_ == 0) break;
console_.add_stdin();
std::string line = get_stdio_channel()->in()->readline();
if (line == "") break;
console_.add(line);
int action = console_.action();
if (action == LuaConsole::DO_LUA) {
do_lua(console_.lua_expression());
@@ -190,5 +201,8 @@ void TextGame::run()
std::cerr << console_.syntax() << std::endl;
}
}
// Process lua source if available.
std::unique_ptr<util::LuaSource> source = get_lua_source();
if (source != nullptr) world_->update_source(*source);
}