Raylib Bubbles
C++11 Raylib bubble shooter game.
action.hpp
1 #ifndef ACTION_HPP_
2 #define ACTION_HPP_
3 
4 #include <vector>
5 #include <unordered_set>
6 #include <algorithm>
7 #include <stdexcept>
8 
9 #include "board.hpp"
10 #include "pboard.hpp"
11 #include "util.hpp"
12 
23 public:
24 
33  struct AnimState {
34 
40  enum class AnimType {
41  BUBBLE,
42  BUBBLE_GLOW,
43  PARTICLE
44  };
45 
46  size_t hue;
47  ssize_t duration;
48  AnimType type;
49  float x;
50  float y;
51  float xVel;
52  float yVel;
53  bool hasGravity;
54 
67  AnimState(const float x, const float y, const float xVel, const float yVel, const AnimType type, const size_t hue, const ssize_t duration, const bool hasGravity)
68  : x(x), y(y), xVel(xVel), yVel(yVel), type(type), hue(hue), duration(duration), hasGravity(hasGravity) {};
69 
72 
73  static AnimState bubble(const float x, const float y, const float xVel, const float yVel, const size_t hue) {
74  return AnimState(x, y, xVel, yVel, AnimType::BUBBLE, hue, -1, false);
75  }
76 
77  static AnimState gravityBubble(const float x, const float y, const float xVel, const float yVel, const size_t hue) {
78  return AnimState(x, y, xVel, yVel, AnimType::BUBBLE, hue, -1, true);
79  }
80 
81  static AnimState particle(const float x, const float y, const float xVel, const float yVel, const ssize_t duration) {
82  return AnimState(x, y, xVel, yVel, AnimType::PARTICLE, 0, duration, false);
83  }
84 
85  static AnimState gravityParticle(const float x, const float y, const float xVel, const float yVel, const ssize_t duration) {
86  return AnimState(x, y, xVel, yVel, AnimType::PARTICLE, 0, duration, true);
87  }
88 
90  };
91 
101  struct ActionType {
102 
107  enum class Effect {
108  STATIC,
109  KINEMATIC,
110  BOUNCE,
111  BUBBLE
112  };
113 
118  enum class PruneType {
119  OOB_BOARD,
120  OOB_WINDOW
121  };
122 
123  GameActionMgr* parent;
124  AnimState obj;
125  Effect effect;
126  PruneType pruneType;
127  bool pruneFlag;
128  size_t id;
129 
137  ActionType(const Effect effect, const PruneType pruneType, const AnimState& animData, GameActionMgr* mgr)
138  : effect(effect), parent(mgr), obj(animData), pruneType(pruneType), pruneFlag(false) {};
139 
156  void step(const float scale = 1.0f);
157 
170  bool shouldPrune() const;
171 
179  bool repeated(const size_t row, const size_t col);
180  };
181 
188  GameActionMgr(PlacedBoard& boardData);
189 
192 
197  std::vector<ActionType>::iterator begin() {
198  return actions.begin();
199  }
200 
205  std::vector<ActionType>::iterator end() {
206  return actions.end();
207  }
208 
210 
213 
218  size_t size() const;
219 
221 
224 
231  size_t enqueue(ActionType action);
232 
238  void step(const float scale = 1.0f);
239 
244  void prune();
245 
252  bool find(const size_t id) const;
253 
255 
256 private:
257  const float GRAVITY;
258  const size_t MAX_IDS;
259 
260  PlacedBoard& boardData;
261  std::vector<ActionType> actions;
262  std::unordered_set<size_t> ids;
263  size_t lastId;
264 
265  void nextId();
266 };
267 
268 #endif
Manages game actions and allows them to interact with the game board if necessary.
Definition: action.hpp:22
bool find(const size_t id) const
Checks if an action is still ongoing (was not pruned).
Definition: action.cpp:88
size_t enqueue(ActionType action)
Adds an action to the manager.
Definition: action.cpp:66
void step(const float scale=1.0f)
Steps all managed actions forward, by calling their step() methods.
Definition: action.cpp:74
size_t size() const
Returns the number of actions currently in the manager.
Definition: action.cpp:62
std::vector< ActionType >::iterator end()
Builds an iterator to the end of the actions container.
Definition: action.hpp:205
void prune()
Prunes all actions that are no longer needed, using their shouldPrune() methods.
Definition: action.cpp:79
GameActionMgr(PlacedBoard &boardData)
Constructs a GameActionMgr object with the given dimensions and a reference to the game board.
Definition: action.cpp:5
std::vector< ActionType >::iterator begin()
Builds an iterator to the beginning of the actions container.
Definition: action.hpp:197
Represents an action to be taken by the game.
Definition: action.hpp:101
Effect
Selects the effect logic of an action.
Definition: action.hpp:107
bool shouldPrune() const
Checks if the action should be pruned.
Definition: action.cpp:53
bool repeated(const size_t row, const size_t col)
Provides a boolean condition against repeat use of same row-column.
void step(const float scale=1.0f)
Steps the action forward, according to the chosen effect logic.
Definition: action.cpp:13
ActionType(const Effect effect, const PruneType pruneType, const AnimState &animData, GameActionMgr *mgr)
internally assigned.
Definition: action.hpp:137
PruneType
Selects the type of pruning to be used by an action.
Definition: action.hpp:118
Represents the state of an object to be animated.
Definition: action.hpp:33
AnimState(const float x, const float y, const float xVel, const float yVel, const AnimType type, const size_t hue, const ssize_t duration, const bool hasGravity)
Constructs an animation state with the given data.
Definition: action.hpp:67
AnimType
Selects the type of object being animated, to later determine what to draw.
Definition: action.hpp:40
Describes a game board placed in a window.
Definition: pboard.hpp:16