objects.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #pragma once
  2. #include <vector>
  3. #include "vec2.h"
  4. #include <gui/canvas.h> // for Canvas*
  5. #include "signals.h"
  6. #define DEF_BALL_RADIUS 20
  7. #define DEF_BUMPER_RADIUS 40
  8. #define DEF_BUMPER_BOUNCE 1.0f
  9. #define DEF_FLIPPER_SIZE 120
  10. #define DEF_RAIL_BOUNCE 0.9f
  11. #define DEF_TURBO_RADIUS 20
  12. #define DEF_TURBO_BOOST 5
  13. #define ARC_TANGENT_RESTITUTION 1.0f
  14. #define ARC_NORMAL_RESTITUTION 0.8f
  15. // A dynamic, moveable object with acceleration
  16. class Object {
  17. public:
  18. Object(const Vec2& p_, float r_);
  19. virtual ~Object() = default;
  20. // Verlet data
  21. Vec2 p; // position
  22. Vec2 prev_p; // previous position
  23. Vec2 a;
  24. float r;
  25. bool physical; // is this a real object that can be hit?
  26. float bounce; // < 1 dampens, > 1 adds power
  27. bool fixed; // should this move?
  28. int score; // incremental score for hitting this
  29. void update(float dt); // updates position
  30. inline void accelerate(const Vec2& da) {
  31. a += da;
  32. }
  33. inline void add_velocity(const Vec2& v, float dt) {
  34. prev_p -= v * dt;
  35. }
  36. virtual void draw(Canvas* canvas) = 0;
  37. };
  38. class Ball : public Object {
  39. public:
  40. Ball(const Vec2& p_ = Vec2(), float r_ = DEF_BALL_RADIUS)
  41. : Object(p_, r_) {
  42. }
  43. void draw(Canvas* canvas);
  44. };
  45. class Flipper {
  46. public:
  47. enum Side {
  48. LEFT,
  49. RIGHT
  50. };
  51. Flipper(const Vec2& p_, Side side, size_t size_ = DEF_FLIPPER_SIZE);
  52. void draw(Canvas* canvas);
  53. void update(float dt); // updates position to new position
  54. bool collide(Ball& ball);
  55. Vec2 get_tip() const;
  56. Vec2 p;
  57. Side side;
  58. size_t size;
  59. float r;
  60. float rest_angle;
  61. float max_rotation;
  62. float sign;
  63. float omega; // angular velocity
  64. float rotation;
  65. float current_omega;
  66. bool powered; // is this flipper being activated? i.e. is keypad pressed?
  67. int score;
  68. void (*notification)(void* app);
  69. };
  70. // A static object that never moves and can be any shape
  71. class FixedObject {
  72. public:
  73. FixedObject()
  74. : bounce(1.0f)
  75. , physical(true)
  76. , hidden(false)
  77. , score(0)
  78. , tx_id(INVALID_ID)
  79. , rx_id(INVALID_ID)
  80. , tx_type(SignalType::ALL)
  81. , notification(nullptr) {
  82. }
  83. virtual ~FixedObject() = default;
  84. float bounce;
  85. bool physical; // interacts with ball vs table decoration
  86. bool hidden; // do not draw
  87. int score;
  88. int tx_id;
  89. int rx_id;
  90. SignalType tx_type;
  91. void (*notification)(void* app);
  92. virtual void draw(Canvas* canvas) = 0;
  93. virtual bool collide(Ball& ball) = 0;
  94. virtual void reset_animation() {};
  95. virtual void step_animation() {};
  96. virtual void signal_receive();
  97. virtual void signal_send();
  98. };
  99. class Polygon : public FixedObject {
  100. public:
  101. Polygon()
  102. : FixedObject() {};
  103. std::vector<Vec2> points;
  104. std::vector<Vec2> normals;
  105. void draw(Canvas* canvas);
  106. bool collide(Ball& ball);
  107. void add_point(const Vec2& np) {
  108. points.push_back(np);
  109. }
  110. void finalize();
  111. };
  112. class Portal : public FixedObject {
  113. public:
  114. Portal(const Vec2& a1_, const Vec2& a2_, const Vec2& b1_, const Vec2& b2_)
  115. : FixedObject()
  116. , a1(a1_)
  117. , a2(a2_)
  118. , b1(b1_)
  119. , b2(b2_) {
  120. score = 200;
  121. }
  122. Vec2 a1, a2; // portal 'a'
  123. Vec2 b1, b2; // portal 'b'
  124. Vec2 na, nb; // normals
  125. Vec2 au, bu; // unit vectors
  126. float amag, bmag; // length of portals
  127. bool bidirectional{true}; // TODO: ehhh?
  128. Vec2 enter_p; // where we entered portal
  129. size_t decay{0}; // used for animation
  130. void draw(Canvas* canvas);
  131. bool collide(Ball& ball);
  132. void reset_animation();
  133. void step_animation();
  134. void finalize();
  135. };
  136. class Arc : public FixedObject {
  137. public:
  138. enum Surface {
  139. OUTSIDE,
  140. INSIDE,
  141. BOTH
  142. };
  143. Arc(const Vec2& p_,
  144. float r_,
  145. float s_ = 0,
  146. float e_ = (float)M_PI * 2,
  147. Surface surf_ = OUTSIDE);
  148. Vec2 p;
  149. float r;
  150. float start;
  151. float end;
  152. Surface surface;
  153. void draw(Canvas* canvas);
  154. bool collide(Ball& ball);
  155. };
  156. class Bumper : public Arc {
  157. public:
  158. Bumper(const Vec2& p_, float r_);
  159. size_t decay;
  160. void draw(Canvas* canvas);
  161. void reset_animation();
  162. void step_animation();
  163. };
  164. class Plunger : public Object {
  165. public:
  166. Plunger(const Vec2& p_);
  167. void draw(Canvas* canvas);
  168. int size; // how tall is it
  169. int compression; // how much is it pulled back?
  170. };
  171. // Simply displays a letter after a rollover
  172. class Rollover : public FixedObject {
  173. public:
  174. Rollover(const Vec2& p_, char c_)
  175. : FixedObject()
  176. , p(p_) {
  177. c[0] = c_;
  178. c[1] = '\0';
  179. score = 400;
  180. }
  181. Vec2 p;
  182. char c[2];
  183. bool activated{false};
  184. void draw(Canvas* canvas);
  185. bool collide(Ball& ball);
  186. void signal_receive();
  187. void signal_send();
  188. };
  189. class Turbo : public FixedObject {
  190. public:
  191. Turbo(const Vec2& p_, float angle_, float boost_, float radius_)
  192. : FixedObject()
  193. , p(p_)
  194. , angle(angle_)
  195. , boost(boost_)
  196. , r(radius_) {
  197. // Our boost direction
  198. dir = Vec2(cosf(angle), -sinf(angle));
  199. // define the points of the chevrons at the 0 angle
  200. chevron_1[0] = Vec2(p.x, p.y - r);
  201. chevron_1[1] = Vec2(p.x + r, p.y);
  202. chevron_1[2] = Vec2(p.x, p.y + r);
  203. chevron_2[0] = Vec2(p.x - r, p.y - r);
  204. chevron_2[1] = Vec2(p.x, p.y);
  205. chevron_2[2] = Vec2(p.x - r, p.y + r);
  206. // rotate the chevrons to the correct angle
  207. for(size_t i = 0; i < 3; i++) {
  208. Vec2& v = chevron_1[i];
  209. Vec2 d = v - p;
  210. v.x = p.x + d.x * cosf(angle) - d.y * sinf(angle);
  211. v.y = p.y + d.x * -sinf(angle) + d.y * -cosf(angle);
  212. }
  213. for(size_t i = 0; i < 3; i++) {
  214. Vec2& v = chevron_2[i];
  215. Vec2 d = v - p;
  216. v.x = p.x + d.x * cosf(angle) - d.y * sinf(angle);
  217. v.y = p.y + d.x * -sinf(angle) + d.y * -cosf(angle);
  218. }
  219. }
  220. Vec2 p;
  221. float angle;
  222. float boost;
  223. float r;
  224. Vec2 dir; // unit normal of turbo direction
  225. Vec2 chevron_1[3];
  226. Vec2 chevron_2[3];
  227. void draw(Canvas* canvas);
  228. bool collide(Ball& ball);
  229. };
  230. // Visual item only - chase of dots in one direction
  231. // AXIS-ALIGNED!
  232. class Chaser : public Polygon {
  233. public:
  234. enum Style {
  235. SIMPLE,
  236. SLASH
  237. };
  238. Chaser(const Vec2& p1, const Vec2& p2, size_t gap_ = 8, size_t speed_ = 3, Style style_ = SIMPLE)
  239. : Polygon()
  240. , tick(0)
  241. , offset(0)
  242. , gap(gap_)
  243. , speed(speed_)
  244. , style(style_) {
  245. physical = false;
  246. points.push_back(p1);
  247. points.push_back(p2);
  248. }
  249. size_t tick;
  250. size_t offset;
  251. size_t gap;
  252. size_t speed;
  253. Style style;
  254. void draw(Canvas* canvas);
  255. void step_animation();
  256. };