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