Peoplemon  0.1.0
Peoplemon 3 game source documentation
StorageCursor.cpp
Go to the documentation of this file.
2 
3 #include <BLIB/Components/Velocity2D.hpp>
4 #include <BLIB/Util/FileUtil.hpp>
7 #include <Core/Properties.hpp>
8 #include <Core/Resources.hpp>
10 
11 namespace game
12 {
13 namespace menu
14 {
15 float StorageCursor::size = 40.f;
16 namespace
17 {
18 constexpr float MoveTime = 0.3f;
19 constexpr float RotateRate = 360.f;
20 constexpr float FlashPeriod = 0.4f;
21 
22 glm::vec2 moveVector(core::input::EntityControl dir) {
23  switch (dir) {
25  return {1.f, 0.f};
27  return {0.f, 1.f};
29  return {-1.f, 0.f};
31  return {0.f, -1.f};
32  default:
33  return {0.f, 0.f};
34  }
35 }
36 } // namespace
37 
38 StorageCursor::StorageCursor(bl::engine::Engine& engine)
39 : engine(engine)
40 , position(0, 0)
41 , moveDir(core::input::Control::EntityControl::None)
42 , pplInScene(false) {
43  cursorTxtr = engine.renderer().texturePool().getOrLoadTexture(bl::util::FileUtil::joinPath(
44  core::Properties::MenuImagePath(), "StorageSystem/storageCursor.png"));
45  cursor.create(engine, cursorTxtr);
46  cursor.getTransform().setDepth(bl::cam::OverlayCamera::MinDepth + 1.f);
47 
48  size = cursorTxtr->size().x;
49  moveVel = size / MoveTime;
50  offset = 0.f;
51 }
52 
53 void StorageCursor::activate(bl::ecs::Entity parent) {
54  cursor.setParent(parent);
55  bl::rc::Scene* overlay = engine.renderer().getObserver().getCurrentOverlay();
56  cursor.addToScene(overlay, bl::rc::UpdateSpeed::Dynamic);
57  if (peoplemon.entity() != bl::ecs::InvalidEntity) {
58  peoplemon.addToScene(overlay, bl::rc::UpdateSpeed::Dynamic);
59  pplInScene = true;
60  }
61  else {
62  pplInScene = false;
63  cursor.flash(FlashPeriod, FlashPeriod);
64  }
65  syncPos();
66 }
67 
69  cursor.removeFromScene();
70  if (peoplemon.entity() != bl::ecs::InvalidEntity) { peoplemon.removeFromScene(); }
71  pplInScene = false;
72 }
73 
74 void StorageCursor::setHidden(bool hide) {
75  if (hide) {
76  cursor.stopFlashing();
77  cursor.setHidden(true);
78  }
79  else if (!pplInScene && !engine.ecs().hasComponent<bl::com::Toggler>(cursor.entity())) {
80  cursor.flash(FlashPeriod, FlashPeriod);
81  }
82 }
83 
84 void StorageCursor::update(float dt) {
85  if (pplInScene) { peoplemon.getTransform().rotate(RotateRate * dt); }
86 
87  switch (moveDir) {
92  offset += moveVel * dt;
93  cursor.getTransform().move(moveVector(moveDir) * moveVel * dt);
94  if (offset >= size) {
96  syncPos();
97  }
98  break;
99 
100  default:
101  break;
102  }
103 }
104 
106  if (moving()) {
107  if (!skip) return false;
108 
109  // finish in-progress move
110  syncPos();
111  }
112 
113  moveDir = cmd;
114  offset = 0.f;
115  switch (cmd) {
117  if (position.y > 0) {
118  --position.y;
119  return true;
120  }
121  return false;
122 
124  if (position.x < core::player::StorageSystem::BoxWidth - 1) {
125  ++position.x;
126  return true;
127  }
128  return false;
129 
131  if (position.y < core::player::StorageSystem::BoxHeight - 1) {
132  ++position.y;
133  return true;
134  }
135  return false;
136 
138  if (position.x > 0) {
139  --position.x;
140  return true;
141  }
142  return false;
143 
144  default:
146  return false;
147  }
148 }
149 
151  if (ppl != core::pplmn::Id::Unknown) {
152  pplTxtr = engine.renderer().texturePool().getOrLoadTexture(
154 
155  if (peoplemon.entity() == bl::ecs::InvalidEntity) {
156  peoplemon.create(engine, pplTxtr);
157  peoplemon.getTransform().setOrigin(pplTxtr->size() * 0.5f);
158  peoplemon.getTransform().setPosition(size * 0.5f, size * 0.5f);
159  peoplemon.setParent(cursor);
160  }
161  else { peoplemon.setTexture(pplTxtr); }
162 
163  if (!pplInScene) {
164  pplInScene = true;
165  peoplemon.addToScene(engine.renderer().getObserver().getCurrentOverlay(),
166  bl::rc::UpdateSpeed::Dynamic);
167  }
168 
169  peoplemon.getTransform().setRotation(0.f);
170  peoplemon.scaleToSize({size, size});
171  cursor.stopFlashing();
172  }
173  else if (peoplemon.entity() != bl::ecs::InvalidEntity) {
174  peoplemon.removeFromScene();
175  pplInScene = false;
176  cursor.flash(FlashPeriod, FlashPeriod);
177  }
178 }
179 
181 
182 const sf::Vector2i& StorageCursor::getPosition() const { return position; }
183 
184 void StorageCursor::setX(int x) {
185  position.x = x;
186  syncPos();
187 }
188 
190  position.x = core::player::StorageSystem::BoxWidth - 1;
191  syncPos();
192 }
193 
195  position.x = 0;
196  syncPos();
197 }
198 
199 void StorageCursor::syncPos() { cursor.getTransform().setPosition(makePos()); }
200 
201 glm::vec2 StorageCursor::makePos() const {
202  return glm::vec2(position.x, position.y) * size + menu::StorageGrid::BoxPosition;
203 }
204 
205 float StorageCursor::TileSize() { return size; }
206 
207 } // namespace menu
208 } // namespace game
EntityControl
Contains the various inputs a player or AI can use to control the game or an NPC.
Definition: Control.hpp:28
Id
The id of a peoplemon.
Definition: Id.hpp:16
Core classes and functionality for both the editor and game.
std::underlying_type_t< Control::EntityControl > EntityControl
Helper typedef to avoid too much casting boilerplate.
Definition: Control.hpp:44
Parent namespace for all functionality unique to the game.
static std::string thumbnailImage(Id id)
Returns the path of the image to render as the peoplemon thumbnail.
Definition: Peoplemon.cpp:211
static constexpr int BoxHeight
static constexpr int BoxWidth
static const std::string & MenuImagePath()
Definition: Properties.cpp:303
void deactivate()
Removes from the current scene.
void update(float dt)
Updates the cursor motion.
void setHidden(bool hide)
Show or hide the cursor.
void pageRight()
Updates the cursor position if the box to the right is switched to.
bool process(core::input::EntityControl cmd, bool skipAnim)
Processes user input to move the cursor.
void setHolding(core::pplmn::Id peoplemon)
Sets a Peoplemon to render with the cursor. Pass Unknown to clear.
const sf::Vector2i & getPosition() const
Returns the position of the cursor in the box.
static float TileSize()
Returns the size of a square on the grid.
void activate(bl::ecs::Entity parent)
Adds to the current overlay.
bool moving() const
Returns whether or not the cursor position is being interpolated.
void setX(int x)
Sets the x position of the cursor.
StorageCursor(bl::engine::Engine &engine)
Construct a new Storage Cursor.
void pageLeft()
Updates the cursor position if the box to the left is switched to.
static constexpr glm::vec2 BoxPosition
Definition: StorageGrid.hpp:22