From ca576ac3cf5e3b6dd06d7f9960d3459efbbf60db Mon Sep 17 00:00:00 2001 From: husho Date: Sat, 26 May 2018 19:13:32 +0300 Subject: [PATCH] Refactor: moved getPlayer to World class --- rwengine/src/engine/GameWorld.cpp | 9 +++++++++ rwengine/src/engine/GameWorld.hpp | 2 ++ rwengine/src/script/ScriptMachine.cpp | 6 ++---- rwengine/src/script/modules/GTA3ModuleImpl.inl | 4 ++-- rwgame/RWGame.cpp | 13 ++----------- rwgame/RWGame.hpp | 3 --- rwgame/states/DebugState.cpp | 16 ++++++++-------- rwgame/states/IngameState.cpp | 14 +++++++------- 8 files changed, 32 insertions(+), 35 deletions(-) diff --git a/rwengine/src/engine/GameWorld.cpp b/rwengine/src/engine/GameWorld.cpp index 66c2e373..01e862cb 100644 --- a/rwengine/src/engine/GameWorld.cpp +++ b/rwengine/src/engine/GameWorld.cpp @@ -1022,4 +1022,13 @@ void GameWorld::clearObjectsWithinArea(const glm::vec3 center, // @todo Remove all temp objects, extinguish all fires, remove all // explosions, remove all projectiles +} + +PlayerController* GameWorld::getPlayer() { + auto object = pedestrianPool.find(state->playerObject); + if (object) { + auto controller = static_cast(object)->controller; + return static_cast(controller); + } + return nullptr; } \ No newline at end of file diff --git a/rwengine/src/engine/GameWorld.hpp b/rwengine/src/engine/GameWorld.hpp index 19d05de7..beb6a5e7 100644 --- a/rwengine/src/engine/GameWorld.hpp +++ b/rwengine/src/engine/GameWorld.hpp @@ -363,6 +363,8 @@ public: void clearObjectsWithinArea(const glm::vec3 center, const float radius, const bool clearParticles); + PlayerController* getPlayer(); + private: /** * @brief Used by objects to delete themselves during updates. diff --git a/rwengine/src/script/ScriptMachine.cpp b/rwengine/src/script/ScriptMachine.cpp index 4c81f801..517548f0 100644 --- a/rwengine/src/script/ScriptMachine.cpp +++ b/rwengine/src/script/ScriptMachine.cpp @@ -12,11 +12,9 @@ #include "script/ScriptModule.hpp" void ScriptMachine::executeThread(SCMThread& t, int msPassed) { - auto& players = getState()->world->players; + auto player = state->world->getPlayer(); - if (!players.empty()) { - // @todo Add support for multiple players - PlayerController* player = players.at(0); + if (player) { if (t.isMission && t.deathOrArrestCheck && (player->isWasted() || player->isBusted())) { t.wastedOrBusted = true; diff --git a/rwengine/src/script/modules/GTA3ModuleImpl.inl b/rwengine/src/script/modules/GTA3ModuleImpl.inl index dd356b0a..800dd898 100644 --- a/rwengine/src/script/modules/GTA3ModuleImpl.inl +++ b/rwengine/src/script/modules/GTA3ModuleImpl.inl @@ -6743,8 +6743,7 @@ void opcode_0254(const ScriptArguments& args) { void opcode_0255(const ScriptArguments& args, ScriptVec3 coord, const ScriptFloat heading) { coord = script::getGround(args, coord); args.getState()->overrideRestart(glm::vec4(coord, heading)); - // @todo Add support for multiple players - PlayerController* player = args.getState()->world->players.at(0); + auto player = args.getWorld()->getPlayer(); player->requestMissionRestart(); } @@ -8180,6 +8179,7 @@ void opcode_02fa(const ScriptArguments& args, const ScriptGarage garage0, const */ void opcode_02fb(const ScriptArguments& args, const ScriptFloat arg1, const ScriptFloat arg2, const ScriptFloat arg3, const ScriptFloat arg4, const ScriptFloat arg5, const ScriptFloat arg6, const ScriptFloat arg7, const ScriptFloat arg8, const ScriptFloat arg9, const ScriptFloat arg10) { RW_UNIMPLEMENTED_OPCODE(0x02fb); + RW_UNUSED(args); RW_UNUSED(arg1); RW_UNUSED(arg2); RW_UNUSED(arg3); diff --git a/rwgame/RWGame.cpp b/rwgame/RWGame.cpp index c3d3c9b3..c390fd94 100644 --- a/rwgame/RWGame.cpp +++ b/rwgame/RWGame.cpp @@ -160,15 +160,6 @@ void RWGame::startScript(const std::string& name) { } } -PlayerController* RWGame::getPlayer() { - auto object = world->pedestrianPool.find(state.playerObject); - if (object) { - auto controller = static_cast(object)->controller; - return static_cast(controller); - } - return nullptr; -} - // Modifiers for GTA3 we try to recreate #define RW_GAME_VERSION 1100 #define RW_GAME_GTA3_GERMAN 0 @@ -192,7 +183,7 @@ void RWGame::handleCheatInput(char symbol) { // Player related cheats { - auto player = getPlayer()->getCharacter(); + auto player = getWorld()->getPlayer()->getCharacter(); #ifdef RW_GAME_GTA3_GERMAN // Germans got their own cheat std::string health_cheat = "GESUNDHEIT"; @@ -893,7 +884,7 @@ void RWGame::globalKeyEvent(const SDL_Event& event) { } std::string keyName = SDL_GetKeyName(event.key.keysym.sym); - if (getPlayer() && keyName.length() == 1) { + if (getWorld()->getPlayer() && keyName.length() == 1) { char symbol = keyName[0]; handleCheatInput(symbol); } diff --git a/rwgame/RWGame.hpp b/rwgame/RWGame.hpp index 4e055c0b..3ae92f5a 100644 --- a/rwgame/RWGame.hpp +++ b/rwgame/RWGame.hpp @@ -120,9 +120,6 @@ public: void saveGame(const std::string& savename); void loadGame(const std::string& savename); - /** shortcut for getWorld()->state.player->getCharacter() */ - PlayerController* getPlayer(); - private: void tick(float dt); void render(float alpha, float dt); diff --git a/rwgame/states/DebugState.cpp b/rwgame/states/DebugState.cpp index 0b3708e1..a5d708e7 100644 --- a/rwgame/states/DebugState.cpp +++ b/rwgame/states/DebugState.cpp @@ -35,8 +35,8 @@ static void jumpCharacter(RWGame* game, CharacterObject* player, std::shared_ptr DebugState::createDebugMenu() { CharacterObject* player = nullptr; - if (game->getPlayer()) { - player = game->getPlayer()->getCharacter(); + if (game->getWorld()->getPlayer()) { + player = game->getWorld()->getPlayer()->getCharacter(); } auto menu = Menu::create( @@ -78,8 +78,8 @@ std::shared_ptr DebugState::createDebugMenu() { std::shared_ptr DebugState::createMapMenu() { CharacterObject* player = nullptr; - if (game->getPlayer()) { - player = game->getPlayer()->getCharacter(); + if (game->getWorld()->getPlayer()) { + player = game->getWorld()->getPlayer()->getCharacter(); } auto menu = Menu::create( @@ -468,7 +468,7 @@ void DebugState::printCameraDetails() { } void DebugState::spawnVehicle(unsigned int id) { - auto ch = game->getPlayer()->getCharacter(); + auto ch = game->getWorld()->getPlayer()->getCharacter(); if (!ch) return; auto playerRot = ch->getRotation(); @@ -480,7 +480,7 @@ void DebugState::spawnVehicle(unsigned int id) { } void DebugState::spawnFollower(unsigned int id) { - auto ch = game->getPlayer()->getCharacter(); + auto ch = game->getWorld()->getPlayer()->getCharacter(); if (!ch) return; glm::vec3 fwd = ch->rotation * glm::vec3(0.f, 1.f, 0.f); @@ -498,8 +498,8 @@ void DebugState::spawnFollower(unsigned int id) { void DebugState::giveItem(int slot) { CharacterObject* player = nullptr; - if (game->getPlayer()) { - player = game->getPlayer()->getCharacter(); + if (game->getWorld()->getPlayer()) { + player = game->getWorld()->getPlayer()->getCharacter(); } if (player) { diff --git a/rwgame/states/IngameState.cpp b/rwgame/states/IngameState.cpp index 7f39e450..d1606877 100644 --- a/rwgame/states/IngameState.cpp +++ b/rwgame/states/IngameState.cpp @@ -166,7 +166,7 @@ void IngameState::tick(float dt) { } } - auto player = game->getPlayer(); + auto player = game->getWorld()->getPlayer(); if (player) { // Force all input to 0 if player input is disabled @@ -280,14 +280,14 @@ void IngameState::tick(float dt) { void IngameState::draw(GameRenderer* r) { if (!getWorld()->state->isCinematic && getWorld()->isCutsceneDone()) { - drawHUD(_look, game->getPlayer(), getWorld(), r); + drawHUD(_look, game->getWorld()->getPlayer(), getWorld(), r); } State::draw(r); } void IngameState::handleEvent(const SDL_Event& event) { - auto player = game->getPlayer(); + auto player = game->getWorld()->getPlayer(); switch (event.type) { case SDL_KEYDOWN: @@ -323,7 +323,7 @@ void IngameState::handleEvent(const SDL_Event& event) { } void IngameState::handlePlayerInput(const SDL_Event& event) { - auto player = game->getPlayer(); + auto player = game->getWorld()->getPlayer(); switch (event.type) { case SDL_MOUSEBUTTONDOWN: switch (event.button.button) { @@ -373,7 +373,7 @@ bool IngameState::shouldWorldUpdate() { const ViewCamera& IngameState::getCamera(float alpha) { auto state = game->getState(); - auto player = game->getPlayer(); + auto player = game->getWorld()->getPlayer(); auto world = getWorld(); if (state->currentCutscene && state->cutsceneStartTime >= 0.f) { @@ -508,8 +508,8 @@ GameObject* IngameState::getCameraTarget() const { auto target = getWorld()->pedestrianPool.find(game->getState()->cameraTarget); - if (target == nullptr && game->getPlayer()) { - target = game->getPlayer()->getCharacter(); + if (target == nullptr && game->getWorld()->getPlayer()) { + target = game->getWorld()->getPlayer()->getCharacter(); } // If the target is a character in a vehicle, make the vehicle the target