summary refs log tree commit diff stats
path: root/src/components.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/components.h')
-rw-r--r--src/components.h93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/components.h b/src/components.h new file mode 100644 index 0000000..687eab1 --- /dev/null +++ b/src/components.h
@@ -0,0 +1,93 @@
1#ifndef COMPONENTS_H
2#define COMPONENTS_H
3
4#include "entity.h"
5#include <utility>
6#include <list>
7#include "map.h"
8
9class UserMovementComponent : public Component {
10 public:
11 UserMovementComponent(Entity& parent) : Component(parent) {};
12 void input(int key, int action);
13
14 private:
15 bool holdingLeft = false;
16 bool holdingRight = false;
17};
18
19class PhysicsBodyComponent : public Component, public Collidable, public Locatable {
20 public:
21 PhysicsBodyComponent(Entity& parent) : Component(parent) {};
22 void receive(message_t msg);
23 void tick();
24 void detectCollision(Entity& player, Locatable& physics, std::pair<double, double> old_position);
25};
26
27class PlayerSpriteComponent : public Component {
28 public:
29 PlayerSpriteComponent(Entity& parent, Locatable& physics);
30 ~PlayerSpriteComponent();
31 void render(Texture* buffer);
32 void receive(message_t msg);
33 void tick();
34
35 private:
36 Locatable& physics;
37 Texture* sprite;
38 int animFrame = 0;
39 bool facingLeft = false;
40 bool isMoving = false;
41};
42
43class PlayerPhysicsComponent : public Component, public Locatable {
44 public:
45 PlayerPhysicsComponent(Entity& parent);
46 void tick();
47 void receive(message_t msg);
48
49 private:
50 double jump_velocity;
51 double jump_gravity;
52 double jump_gravity_short;
53 int direction = 0;
54 bool canDrop = false;
55};
56
57class MapRenderComponent : public Component {
58 public:
59 MapRenderComponent(Entity& parent, Map* map);
60 ~MapRenderComponent();
61 void render(Texture* buffer);
62
63 private:
64 Texture* screen;
65};
66
67enum direction_t {
68 up, left, down, right
69};
70
71typedef struct {
72 int axis;
73 int lower;
74 int upper;
75 int type;
76} collision_t;
77
78class MapCollisionComponent : public Component, public Collidable {
79 public:
80 MapCollisionComponent(Entity& parent, Map* map);
81 void detectCollision(Entity& player, Locatable& physics, std::pair<double, double> old_position);
82
83 private:
84 void add_collision(int axis, int lower, int upper, direction_t dir, int type);
85
86 std::list<collision_t> left_collisions;
87 std::list<collision_t> right_collisions;
88 std::list<collision_t> up_collisions;
89 std::list<collision_t> down_collisions;
90 Map* map;
91};
92
93#endif