Skip to content

Commit

Permalink
Merge pull request #39 from runvs/CleanUpTransitionFunctions
Browse files Browse the repository at this point in the history
Clean up transition functions
  • Loading branch information
Laguna1989 authored Aug 4, 2023
2 parents 80899de + bc98f9b commit 5e0a910
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 23 deletions.
2 changes: 2 additions & 0 deletions impl/game_client_lib/internal_state/end_lose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <system_helper.hpp>
#include <imgui.h>

void EndLose::create(StateGame& /*state*/) { }

void EndLose::update(StateGame& /*state*/, float /*elapsed*/) { }

void EndLose::draw(StateGame& state)
Expand Down
1 change: 1 addition & 0 deletions impl/game_client_lib/internal_state/end_lose.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class EndLose : public InternalStateInterface {
public:
void create(StateGame& state) override;
void update(StateGame& state, float elapsed) override;
void draw(StateGame& state) override;

Expand Down
2 changes: 2 additions & 0 deletions impl/game_client_lib/internal_state/end_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <system_helper.hpp>
#include <imgui.h>

void EndWin::create(StateGame& /*state*/) { }

void EndWin::update(StateGame& /*state*/, float /*elapsed*/) { }

void EndWin::draw(StateGame& state)
Expand Down
1 change: 1 addition & 0 deletions impl/game_client_lib/internal_state/end_win.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class EndWin : public InternalStateInterface {
public:
void create(StateGame& state) override;
void update(StateGame& state, float elapsed) override;
void draw(StateGame& state) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class StateGame;
class InternalStateInterface {
public:
virtual ~InternalStateInterface() = default;
virtual void create(StateGame& state) = 0;
virtual void update(StateGame& state, float elapsed) = 0;
virtual void draw(StateGame& state) = 0;
};
Expand Down
21 changes: 18 additions & 3 deletions impl/game_client_lib/internal_state/internal_state_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "internal_state_manager.hpp"
#include <game_interface.hpp>
#include <internal_state/end_lose.hpp>
#include <internal_state/end_win.hpp>
#include <internal_state/place_units.hpp>
Expand All @@ -24,7 +25,20 @@ InternalStateManager::InternalStateManager()
m_transitions[std::make_pair(
InternalState::WaitForAllPlayers, InternalState::SelectStartingUnits)]
= [](StateGame& state) {
state.transitionWaitForPlayersToSelectStartingUnits();
auto const playerId = state.getServerConnection()->getPlayerId();

auto const playerIdDispatcher = std::make_shared<PlayerIdDispatcher>(playerId);

auto const unitInfo = std::make_shared<UnitInfoCollection>(
state.getGame()->logger(), state.getServerConnection()->getUnitInfo());

state.setPlayerIdDispatcher(playerIdDispatcher);
state.setUnitInfo(unitInfo);
state.setPlacementManager(std::make_shared<PlacementManager>(
state.getTerrain(), playerId, playerIdDispatcher, unitInfo));

state.setStartingUnits(state.getServerConnection()->getStartingUnits());

state.getPlacementManager()->setActive(false);
};

Expand Down Expand Up @@ -61,11 +75,12 @@ InternalStateManager::InternalStateManager()
};

m_transitions[std::make_pair(InternalState::WaitForSimulationResults, InternalState::Playback)]
= [](StateGame& state) { state.transitionWaitForSimulationResultsToPlayback(); };
= [](StateGame& state) { state.startPlayback(); };

m_transitions[std::make_pair(InternalState::Playback, InternalState::PlaceUnits)]
= [](StateGame& state) {
state.transitionPlaybackToPlaceUnits();
state.resetSimulation();

state.getPlacementManager()->setActive(true);
state.getPlacementManager()->addFunds(40 + 50 * state.getRound());

Expand Down
2 changes: 2 additions & 0 deletions impl/game_client_lib/internal_state/place_units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ auto getWindowPos(auto const mousePosition)

} // namespace

void PlaceUnits::create(StateGame& /*state*/) { }

void PlaceUnits::drawUnitUpgradeWindow(
std::shared_ptr<UnitInterface> selectedUnit, StateGame& state)
{
Expand Down
1 change: 1 addition & 0 deletions impl/game_client_lib/internal_state/place_units.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class UnitInterface;

class PlaceUnits : public InternalStateInterface {
public:
void create(StateGame& state) override;
void update(StateGame& state, float elapsed) override;
void draw(StateGame& state) override;

Expand Down
2 changes: 2 additions & 0 deletions impl/game_client_lib/internal_state/playback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <internal_state/common_functions.hpp>
#include <state_game.hpp>

void Playback::create(StateGame& /*state*/) { }

void Playback::update(StateGame& state, float elapsed)
{
state.playbackSimulation(elapsed);
Expand Down
1 change: 1 addition & 0 deletions impl/game_client_lib/internal_state/playback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class Playback : public InternalStateInterface {
public:
void create(StateGame& state) override;
void update(StateGame& state, float elapsed) override;
void draw(StateGame& state) override;
};
Expand Down
3 changes: 3 additions & 0 deletions impl/game_client_lib/internal_state/select_starting_units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
#include <imgui.h>
#include <string>

void SelectStartingUnits::create(StateGame& /*state*/) { }

void SelectStartingUnits::update(StateGame& /*state*/, float /*elapsed*/) { }

void SelectStartingUnits::draw(StateGame& state)
{
ImGui::Begin("Select Starting Units");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class SelectStartingUnits : public InternalStateInterface {
public:
void create(StateGame& state) override;
void update(StateGame& state, float elapsed) override;
void draw(StateGame& state) override;
};
Expand Down
3 changes: 3 additions & 0 deletions impl/game_client_lib/internal_state/wait_for_all_players.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
#include <internal_state/internal_state_manager.hpp>
#include <state_game.hpp>

void WaitForAllPlayers::create(StateGame& state) { }

void WaitForAllPlayers::update(StateGame& state, float /*elapsed*/)
{
if (state.getServerConnection()->areAllPlayersConnected()) {

state.getStateManager()->switchToState(InternalState::SelectStartingUnits, state);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class WaitForAllPlayers : public InternalStateInterface {
public:
void create(StateGame& state) override;
void update(StateGame& state, float elapsed) override;
void draw(StateGame& state) override;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <internal_state/internal_state_manager.hpp>
#include <state_game.hpp>

void WaitForSimulationResults::create(StateGame& /*state*/) { }

void WaitForSimulationResults::update(StateGame& state, float /*elapsed*/)
{
if (state.getServerConnection()->isRoundDataReady()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class WaitForSimulationResults : public InternalStateInterface {
public:
void create(StateGame& state) override;
void update(StateGame& state, float elapsed) override;
void draw(StateGame& state) override;
};
Expand Down
39 changes: 24 additions & 15 deletions impl/game_client_lib/state_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,31 +305,19 @@ std::shared_ptr<Unit> StateGame::findOrCreateUnit(int pid, int uid, const std::s
return newUnit;
}

void StateGame::transitionWaitForPlayersToSelectStartingUnits()
{
m_playerIdDispatcher = std::make_shared<PlayerIdDispatcher>(m_serverConnection->getPlayerId());
m_unitInfo = std::make_shared<UnitInfoCollection>(
getGame()->logger(), m_serverConnection->getUnitInfo());

m_placementManager = std::make_shared<PlacementManager>(
m_terrain, m_serverConnection->getPlayerId(), m_playerIdDispatcher, m_unitInfo);
add(m_placementManager);

m_startingUnits = m_serverConnection->getStartingUnits();
}

void StateGame::transitionWaitForSimulationResultsToPlayback()
void StateGame::startPlayback()
{
m_simulationResultsForAllFrames = m_serverConnection->getRoundData();
m_tickId = 0;
}

void StateGame::transitionPlaybackToPlaceUnits()
void StateGame::resetSimulation()
{
m_tickId = 0;
resetAllUnits();
m_round++;
m_textRound->setText("Round " + std::to_string(m_round));

getGame()->logger().info("finished playing round simulation", { "StateGame" });
}

Expand Down Expand Up @@ -537,3 +525,24 @@ void StateGame::flashUnitsForUpgrade(const std::string& unitType)
}

std::shared_ptr<SelectUnitInfoCollection> StateGame::getStartingUnits() { return m_startingUnits; }

void StateGame::setPlayerIdDispatcher(std::shared_ptr<PlayerIdDispatcher> dispatcher)
{
m_playerIdDispatcher = dispatcher;
}

void StateGame::setUnitInfo(std::shared_ptr<UnitInfoCollection> unitInfo) { m_unitInfo = unitInfo; }

void StateGame::setPlacementManager(std::shared_ptr<PlacementManager> manager)
{
if (m_placementManager) {
throw std::logic_error { "Placement Manager set a second time. Aborting" };
}
m_placementManager = manager;
add(m_placementManager);
}

void StateGame::setStartingUnits(std::shared_ptr<SelectUnitInfoCollection> startingUnits)
{
m_startingUnits = startingUnits;
}
13 changes: 8 additions & 5 deletions impl/game_client_lib/state_game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ class StateGame : public jt::GameState {
std::shared_ptr<jt::ObjectGroup<Unit>> getUnits();
std::shared_ptr<jt::ObjectGroup<Critter>> getCritters();

void transitionWaitForPlayersToSelectStartingUnits();
void transitionWaitForSimulationResultsToPlayback();
void transitionPlaybackToPlaceUnits();
void startPlayback();
void resetSimulation();

void playbackSimulation(float elapsed);
void drawArrows() const;
Expand All @@ -74,6 +73,11 @@ class StateGame : public jt::GameState {

std::shared_ptr<SelectUnitInfoCollection> getStartingUnits();

void setPlayerIdDispatcher(std::shared_ptr<PlayerIdDispatcher> dispatcher);
void setUnitInfo(std::shared_ptr<UnitInfoCollection> unitInfo);
void setPlacementManager(std::shared_ptr<PlacementManager> manager);
void setStartingUnits(std::shared_ptr<SelectUnitInfoCollection> startingUnits);

private:
std::shared_ptr<ClientNetworkConnection> m_connection { nullptr };
std::shared_ptr<ServerConnection> m_serverConnection { nullptr };
Expand All @@ -90,6 +94,7 @@ class StateGame : public jt::GameState {
std::shared_ptr<jt::ObjectGroup<Critter>> m_critters { nullptr };

std::shared_ptr<UnitInfoCollection> m_unitInfo { nullptr };
std::shared_ptr<SelectUnitInfoCollection> m_startingUnits { nullptr };

std::shared_ptr<InternalStateManager> m_internalStateManager { nullptr };

Expand Down Expand Up @@ -127,8 +132,6 @@ class StateGame : public jt::GameState {
// at end of round
float m_playbackOverflowTime = 2.5f;

std::shared_ptr<SelectUnitInfoCollection> m_startingUnits;

void onCreate() override;
void onEnter() override;
void onUpdate(float const elapsed) override;
Expand Down

0 comments on commit 5e0a910

Please sign in to comment.