Peoplemon  0.1.0
Peoplemon 3 game source documentation
BattleState.cpp
Go to the documentation of this file.
2 
4 #include <Core/Events/Battle.hpp>
5 #include <Core/Properties.hpp>
8 
9 namespace game
10 {
11 namespace state
12 {
13 namespace
14 {
15 class DummyController : public core::battle::BattleController {
16 public:
17  DummyController() = default;
18  virtual ~DummyController() = default;
19 
20 private:
21  sf::Clock timer;
22 
23  virtual void onCommandQueued(const core::battle::Command&) override {}
24  virtual void onCommandProcessed(const core::battle::Command&) override {}
25  virtual void onUpdate(bool, bool) override {
26  if (timer.getElapsedTime().asSeconds() >= 3.f) {
28  }
29  }
30 };
31 
32 } // namespace
33 
34 bl::engine::State::Ptr BattleState::create(core::system::Systems& systems,
35  std::unique_ptr<core::battle::Battle>&& battle) {
36  return bl::engine::State::Ptr(
37  new BattleState(systems, std::forward<std::unique_ptr<core::battle::Battle>>(battle)));
38 }
39 
40 BattleState::BattleState(core::system::Systems& systems,
41  std::unique_ptr<core::battle::Battle>&& battle)
42 : State(systems, bl::engine::StateMask::Menu)
43 , battle(std::forward<std::unique_ptr<core::battle::Battle>>(battle)) {
44  if (!this->battle->controller) {
45  BL_LOG_WARN << "Invalid battle controller, using dummy";
46  this->battle->setController(std::make_unique<DummyController>());
47  }
48 }
49 
50 const char* BattleState::name() const { return "BattleState"; }
51 
52 void BattleState::activate(bl::engine::Engine& engine) {
53  if (!scene) {
54  scene = engine.renderer().getObserver().pushScene<bl::rc::scene::CodeScene>(
55  std::bind(&core::battle::BattleView::render, &battle->view, std::placeholders::_1));
56  battle->view.init(static_cast<bl::rc::scene::CodeScene*>(scene.get()));
57  }
58  else { engine.renderer().getObserver().pushScene(scene); }
59  systems.engine().inputSystem().getActor().addListener(battle->view);
60  bl::event::Dispatcher::subscribe(this);
61 }
62 
63 void BattleState::deactivate(bl::engine::Engine& engine) {
64  engine.renderer().getObserver().popScene();
65  systems.engine().inputSystem().getActor().removeListener(battle->view);
66  bl::event::Dispatcher::unsubscribe(this);
67  bl::audio::AudioSystem::popPlaylist();
68 }
69 
70 void BattleState::update(bl::engine::Engine& engine, float dt, float) {
71  battle->controller->update();
72  battle->view.update(dt);
73  battle->state.localPlayer().refresh();
74  battle->state.enemy().refresh();
75  if (battle->state.currentStage() == core::battle::BattleState::Stage::Completed) {
76  engine.popState();
77  if (battle->type == core::battle::Battle::Type::Online) {
79  }
80  else {
82  }
83 
84  bl::event::Dispatcher::dispatch<core::event::BattleCompleted>(
85  {battle->type, std::move(battle->result)});
86  }
87 }
88 
89 void BattleState::observe(const core::event::OpenPeoplemonMenu& event) {
90  systems.engine().pushState(
91  PeoplemonMenu::create(systems, event.context, event.outNow, event.chosen));
92 }
93 
94 void BattleState::observe(const core::event::OpenBagMenu& event) {
95  systems.engine().pushState(
96  BagMenu::create(systems, event.context, event.result, event.outNow, event.chosenPeoplemon));
97 }
98 
99 } // namespace state
100 } // namespace game
Core classes and functionality for both the editor and game.
Parent namespace for all functionality unique to the game.
Interface for battle controllers. Battle controllers manage the flow of state and interaction for bat...
void render(bl::rc::scene::CodeScene::RenderContext &ctx)
Renders the battle view.
Definition: BattleView.cpp:126
Emitted by the BattleFSM. All UI is handled via a queue of commands. Commands are resolved in the ord...
Definition: Command.hpp:22
Event that is fired when the bag menu should be opened.
Definition: BagMenu.hpp:16
item::Id *const result
Definition: BagMenu.hpp:36
const Context context
Definition: BagMenu.hpp:35
int *const chosenPeoplemon
Definition: BagMenu.hpp:38
Special event to trigger the opening of the peoplemon menu.
bool hasLivingPeoplemon() const
Returns whether or not the player has any living peoplemon.
Definition: State.cpp:27
void healPeoplemon()
Restores HP and removes all ailments.
Definition: State.cpp:16
player::State & state()
Returns the state of the player.
Definition: Player.cpp:154
void whiteout()
Heals all Peoplemon and respawns at the last PC center.
Definition: Player.cpp:97
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
static bl::engine::State::Ptr create(core::system::Systems &systems, Context ctx, core::item::Id *result=nullptr, int outNow=-1, int *chosenPeoplemon=nullptr, bool *unpause=nullptr)
Creates a new BagMenu.
Definition: BagMenu.cpp:73
This game state does all battling. When the battle is complete it will pop itself from the engine sta...
Definition: BattleState.hpp:22
virtual const char * name() const override
Returns "BattleState".
Definition: BattleState.cpp:50
virtual void deactivate(bl::engine::Engine &) override
Does nothing.
Definition: BattleState.cpp:63
static bl::engine::State::Ptr create(core::system::Systems &systems, std::unique_ptr< core::battle::Battle > &&battle)
Creates a new BattleState.
Definition: BattleState.cpp:34
virtual void activate(bl::engine::Engine &) override
Does nothing.
Definition: BattleState.cpp:52
virtual void update(bl::engine::Engine &, float dt, float) override
Calls into the battle's update method.
Definition: BattleState.cpp:70
static bl::engine::State::Ptr create(core::system::Systems &systems, Context ctx, int outNow=-1, int *chosen=nullptr, core::item::Id item=core::item::Id::None)
Creates a new PeoplemonMenu state.
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