diff options
Diffstat (limited to 'source/game')
-rw-r--r-- | source/game/StarUniverseClient.cpp | 32 | ||||
-rw-r--r-- | source/game/StarUniverseClient.hpp | 11 |
2 files changed, 40 insertions, 3 deletions
diff --git a/source/game/StarUniverseClient.cpp b/source/game/StarUniverseClient.cpp index 1622518..bc1b0b9 100644 --- a/source/game/StarUniverseClient.cpp +++ b/source/game/StarUniverseClient.cpp @@ -30,6 +30,7 @@ UniverseClient::UniverseClient(PlayerStoragePtr playerStorage, StatisticsPtr sta m_playerStorage = move(playerStorage); m_statistics = move(statistics); m_pause = false; + m_luaRoot = make_shared<LuaRoot>(); reset(); } @@ -86,7 +87,7 @@ Maybe<String> UniverseClient::connect(UniverseConnection connection, bool allowA return String(strf("Join failed! Server does not support connections with protocol version {}", StarProtocolVersion)); connection.pushSingle(make_shared<ClientConnectPacket>(Root::singleton().assets()->digest(), allowAssetsMismatch, m_mainPlayer->uuid(), m_mainPlayer->name(), - m_mainPlayer->species(), m_playerStorage->loadShipData(m_mainPlayer->uuid()), ShipUpgrades(m_mainPlayer->shipUpgrades()), + m_mainPlayer->species(), m_playerStorage->loadShipData(m_mainPlayer->uuid()), m_mainPlayer->shipUpgrades(), m_mainPlayer->log()->introComplete(), account)); connection.sendAll(timeout); @@ -219,8 +220,11 @@ void UniverseClient::update() { m_statistics->update(); - if (!m_pause) + if (!m_pause) { m_worldClient->update(); + for (auto& p : m_scriptContexts) + p.second->update(); + } m_connection->push(m_worldClient->getOutgoingPackets()); if (!m_pause) @@ -444,6 +448,28 @@ void UniverseClient::setLuaCallbacks(String const& groupName, LuaCallbacks const m_worldClient->setLuaCallbacks(groupName, callbacks); } +void UniverseClient::startLua() { + auto assets = Root::singleton().assets(); + for (auto& p : assets->json("/client.config:universeScriptContexts").toObject()) { + auto scriptComponent = make_shared<ScriptComponent>(); + scriptComponent->setLuaRoot(m_luaRoot); + scriptComponent->setScripts(jsonToStringList(p.second.toArray())); + + for (auto& pair : m_luaCallbacks) + scriptComponent->addCallbacks(pair.first, pair.second); + + m_scriptContexts.set(p.first, scriptComponent); + scriptComponent->init(); + } +} + +void UniverseClient::stopLua() { + for (auto& p : m_scriptContexts) + p.second->uninit(); + + m_scriptContexts.clear(); +} + ClockConstPtr UniverseClient::universeClock() const { return m_universeClock; } @@ -539,6 +565,8 @@ void UniverseClient::handlePackets(List<PacketPtr> const& packets) { } void UniverseClient::reset() { + stopLua(); + m_universeClock.reset(); m_worldClient.reset(); m_celestialDatabase.reset(); diff --git a/source/game/StarUniverseClient.hpp b/source/game/StarUniverseClient.hpp index b26f2df..daad481 100644 --- a/source/game/StarUniverseClient.hpp +++ b/source/game/StarUniverseClient.hpp @@ -10,6 +10,7 @@ #include "StarAiTypes.hpp" #include "StarSky.hpp" #include "StarUniverseConnection.hpp" +#include "StarLuaComponents.hpp" namespace Star { @@ -29,8 +30,8 @@ STAR_CLASS(CelestialDatabase); STAR_CLASS(JsonRpcInterface); STAR_CLASS(TeamClient); STAR_CLASS(QuestManager); - STAR_CLASS(UniverseClient); +STAR_CLASS(LuaRoot); class UniverseClient { public: @@ -86,6 +87,8 @@ public: uint16_t maxPlayers(); void setLuaCallbacks(String const& groupName, LuaCallbacks const& callbacks); + void startLua(); + void stopLua(); ClockConstPtr universeClock() const; CelestialLogConstPtr celestialLog() const; @@ -141,6 +144,12 @@ private: List<ChatReceivedMessage> m_pendingMessages; Maybe<String> m_disconnectReason; + + LuaRootPtr m_luaRoot; + + typedef LuaUpdatableComponent<LuaBaseComponent> ScriptComponent; + typedef shared_ptr<ScriptComponent> ScriptComponentPtr; + StringMap<ScriptComponentPtr> m_scriptContexts; }; } |