objects.h 6.1 KB

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