Peoplemon  0.1.0
Peoplemon 3 game source documentation
MainGame.cpp
Go to the documentation of this file.
2 
4 #include <Core/Properties.hpp>
12 #include <iostream>
13 
14 namespace game
15 {
16 namespace state
17 {
18 namespace
19 {
20 void runDebugScript(core::system::Systems& systems, std::atomic_bool& rflag) {
21  std::cout << "Enter script: ";
22  std::string cmd;
23  std::getline(std::cin, cmd);
24 
25  bl::script::Script script(cmd);
26  if (script.valid()) {
27  script.resetContext(core::script::DebugScriptContext(systems));
28  script.run(&systems.engine().scriptManager());
29  }
30 
31  rflag = false;
32 }
33 } // namespace
34 
35 MainGame::Ptr MainGame::create(core::system::Systems& systems) {
36  return Ptr(new MainGame(systems));
37 }
38 
39 MainGame::MainGame(core::system::Systems& systems)
40 : State(systems, bl::engine::StateMask::Running)
41 , state(MapFadein)
42 , fadeout(nullptr)
43 , spawnId(0) {}
44 
45 MainGame::~MainGame() { bl::event::Dispatcher::unsubscribe(this); }
46 
47 const char* MainGame::name() const { return "MainGame"; }
48 
49 void MainGame::activate(bl::engine::Engine&) {
50  bl::event::Dispatcher::subscribe(this);
51  if (state == MapFadein) {
53  fadeout = systems.engine()
54  .renderer()
55  .getObserver()
56  .getRenderGraph()
57  .putTask<bl::rc::rgi::FadeEffectTask>(
59  }
60 }
61 
62 void MainGame::deactivate(bl::engine::Engine& engine) {
63  state = Running;
64  engine.renderer().getObserver().getRenderGraph().removeTasks<bl::rc::rgi::FadeEffectTask>();
65 }
66 
67 void MainGame::update(bl::engine::Engine&, float dt, float) {
68  switch (state) {
69  case SwitchMapFadeout:
70  if (fadeout->complete()) {
71  BL_LOG_INFO << "Switching to map: " << replacementMap << " spawn " << spawnId;
72  if (!systems.world().switchMaps(replacementMap, spawnId)) {
73  BL_LOG_ERROR << "Failed to switch maps";
74  systems.engine().flags().set(bl::engine::Flags::Terminate);
75  return;
76  }
77 
78  state = MapFadein;
79  fadeout = systems.engine()
80  .renderer()
81  .getObserver()
82  .getRenderGraph()
83  .putTask<bl::rc::rgi::FadeEffectTask>(
85  }
86  break;
87 
88  case MapFadein:
89  if (fadeout->complete()) {
90  state = Running;
92  fadeout = nullptr;
93  systems.engine()
94  .renderer()
95  .getObserver()
96  .getRenderGraph()
97  .removeTask<bl::rc::rgi::FadeEffectTask>();
98  }
99  break;
100 
101  default:
102  break;
103  }
104 }
105 
106 void MainGame::observe(const core::event::StateChange& event) {
107  switch (event.type) {
109  systems.engine().pushState(PauseMenu::create(systems));
110  break;
111 
112  default:
113  break;
114  }
115 }
116 
117 void MainGame::observe(const sf::Event& event) {
118 #ifdef PEOPLEMON_DEBUG
119  if (event.type == sf::Event::KeyPressed) {
120  static std::atomic_bool running = false;
121 
122  if (event.key.code == sf::Keyboard::F1) {
124  }
125  else if (event.key.code == sf::Keyboard::Tilde) {
126  if (!running) {
127  running = true;
128  std::thread t(&runDebugScript, std::ref(systems), std::ref(running));
129  t.detach();
130  }
131  }
132  else if (event.key.code == sf::Keyboard::F5) {
133  auto& team = systems.player().state().peoplemon;
134  if (!team.empty()) {
135  auto& ppl = team.front();
136  if (ppl.evolvesInto() != core::pplmn::Id::Unknown) {
137  const unsigned int lvl = ppl.evolveLevel();
138  if (lvl <= 100) {
139  while (ppl.currentLevel() < lvl) {
140  const auto move = ppl.levelUp();
141  if (move != core::pplmn::MoveId::Unknown) { ppl.gainMove(move); }
142  }
143  systems.engine().pushState(Evolution::create(systems, ppl));
144  }
145  }
146  }
147  }
148  else if (event.key.code == sf::Keyboard::F6) {
149  auto& visited = systems.player().state().visitedTowns;
150  const auto& towns = core::map::Map::FlyMapTowns();
151  for (const auto& town : towns) { visited.emplace(town.name); }
152  core::debug::DebugBanner::display("All towns visited");
153  }
154  else if (event.key.code == sf::Keyboard::F7) { systems.wildPeoplemon().startDebugBattle(); }
155  }
156 #else
157  (void)event;
158 #endif
159 }
160 
161 void MainGame::observe(const core::event::SwitchMapTriggered& event) {
162  replacementMap = event.newMap;
163  spawnId = event.spawn;
164  state = SwitchMapFadeout;
165  fadeout = systems.engine()
166  .renderer()
167  .getObserver()
168  .getRenderGraph()
169  .putTask<bl::rc::rgi::FadeEffectTask>(core::Properties::ScreenFadePeriod());
170  systems.controllable().setAllLocks(true, false);
171 }
172 
173 void MainGame::observe(const core::event::BattleStarted& event) {
174  std::unique_ptr<core::battle::Battle>& battle =
175  const_cast<std::unique_ptr<core::battle::Battle>&>(event.battle);
176  systems.engine().pushState(BattleWrapperState::create(systems, std::move(battle)));
177 }
178 
179 void MainGame::observe(const core::event::StoreOpened& store) {
180  systems.engine().pushState(StoreMenu::create(systems, store));
181 }
182 
183 void MainGame::observe(const core::event::StorageSystemOpened&) {
185 }
186 
187 } // namespace state
188 } // namespace game
Parent namespace for all functionality unique to the game.
Fired when a battle should start. This does not actually start a battle, but communicates to the curr...
Definition: Battle.hpp:17
Fired when the map should be switched to something else.
Definition: Maps.hpp:79
Fired when the game should switch states. This event type is primarily for decoupling input code in C...
Definition: StateChange.hpp:15
@ GamePaused
The game should pause.
Definition: StateChange.hpp:19
const Type type
The type of state change that should occur.
Definition: StateChange.hpp:23
Fired when the storage system should be opened.
Event that is fired by the Core module to signal to the game to open a store.
Definition: Store.hpp:17
static const std::vector< Town > & FlyMapTowns()
Returns the set of towns that can be flown to.
Definition: Map.cpp:587
std::vector< pplmn::OwnedPeoplemon > peoplemon
Definition: State.hpp:46
std::unordered_set< std::string > visitedTowns
Definition: State.hpp:48
static float ScreenFadePeriod()
Definition: Properties.cpp:624
Script context for debug scripts ran from the terminal.
void resetAllLocks()
Resets all controllable entity locks to the last stored state.
void setAllLocks(bool locked, bool preserve=true)
Sets the lock state for all controllable entities.
player::State & state()
Returns the state of the player.
Definition: Player.cpp:154
Owns all primary systems and a reference to the engine.
Definition: Systems.hpp:47
const bl::engine::Engine & engine() const
Const accessor for the Engine.
Definition: Systems.cpp:35
Player & player()
Returns the player system.
Definition: Systems.cpp:59
Controllable & controllable()
Returns the controllable entity system.
Definition: Systems.cpp:63
WildPeoplemon & wildPeoplemon()
Returns the wild peoplemon system.
Definition: Systems.cpp:79
World & world()
Modifiable accessor for the world system.
Definition: Systems.cpp:43
void startDebugBattle()
Starts a wild Peoplemon battle for debugging.
bool switchMaps(const std::string &newMap, int spawnId)
Switches the current map to the map in the given file.
Definition: World.cpp:26
static bl::engine::State::Ptr create(core::system::Systems &systems, std::unique_ptr< core::battle::Battle > &&battle)
Creates a new BattleState.
static bl::engine::State::Ptr create(core::system::Systems &systems, core::pplmn::OwnedPeoplemon &ppl)
Definition: Evolution.cpp:68
The big honcho. This is the primary state that runs a majority of the time.
Definition: MainGame.hpp:26
virtual const char * name() const override
Returns "MainGame".
Definition: MainGame.cpp:47
virtual void deactivate(bl::engine::Engine &engine) override
Deactivates the game state.
Definition: MainGame.cpp:62
virtual void activate(bl::engine::Engine &engine) override
Activates the state.
Definition: MainGame.cpp:49
virtual ~MainGame()
Destroy the Main Game state.
Definition: MainGame.cpp:45
virtual void update(bl::engine::Engine &engine, float dt, float) override
Updates all of the primary systems.
Definition: MainGame.cpp:67
static bl::engine::State::Ptr create(core::system::Systems &systems)
Creates the main game state. The game state must be initialized before invoking the main game state.
Definition: MainGame.cpp:35
static bl::engine::State::Ptr create(core::system::Systems &systems)
Creates a new MapExplorer state.
Definition: MapExplorer.cpp:54
static bl::engine::State::Ptr create(core::system::Systems &systems)
Creates a new pause menu state.
Definition: PauseMenu.cpp:15
Parent to all game states. Provides some commonly required data like core game systems.
Definition: State.hpp:29
core::system::Systems & systems
Definition: State.hpp:66
static bl::engine::State::Ptr create(core::system::Systems &systems)
Create a new storage system menu.
static bl::engine::State::Ptr create(core::system::Systems &systems, const core::event::StoreOpened &data)
Create a new StoreMenu.
Definition: StoreMenu.cpp:27