diff options
Diffstat (limited to 'src/systems')
-rw-r--r-- | src/systems/controlling.cpp | 168 | ||||
-rw-r--r-- | src/systems/controlling.h | 26 | ||||
-rw-r--r-- | src/systems/pondering.cpp | 23 | ||||
-rw-r--r-- | src/systems/pondering.h | 14 | ||||
-rw-r--r-- | src/systems/rendering.cpp | 12 | ||||
-rw-r--r-- | src/systems/rendering.h | 5 |
6 files changed, 241 insertions, 7 deletions
diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp new file mode 100644 index 0000000..b1e73ad --- /dev/null +++ b/src/systems/controlling.cpp | |||
@@ -0,0 +1,168 @@ | |||
1 | #include "controlling.h" | ||
2 | #include "game.h" | ||
3 | #include "components/controllable.h" | ||
4 | #include "components/ponderable.h" | ||
5 | #include "components/animatable.h" | ||
6 | #include "components/droppable.h" | ||
7 | #include "direction.h" | ||
8 | #include "muxer.h" | ||
9 | #include "consts.h" | ||
10 | |||
11 | void ControllingSystem::tick(double dt) | ||
12 | { | ||
13 | while (!actions.empty()) | ||
14 | { | ||
15 | int key = actions.front().first; | ||
16 | int action = actions.front().second; | ||
17 | |||
18 | auto entities = game.getEntityManager().getEntitiesWithComponents<ControllableComponent, PonderableComponent, AnimatableComponent, DroppableComponent>(); | ||
19 | for (auto entity : entities) | ||
20 | { | ||
21 | auto& controllable = game.getEntityManager().getComponent<ControllableComponent>(entity); | ||
22 | |||
23 | if (action == GLFW_PRESS) | ||
24 | { | ||
25 | if (key == controllable.getLeftKey()) | ||
26 | { | ||
27 | controllable.setHoldingLeft(true); | ||
28 | |||
29 | if (!controllable.isFrozen()) | ||
30 | { | ||
31 | walkLeft(entity); | ||
32 | } | ||
33 | } else if (key == controllable.getRightKey()) | ||
34 | { | ||
35 | controllable.setHoldingRight(true); | ||
36 | |||
37 | if (!controllable.isFrozen()) | ||
38 | { | ||
39 | walkRight(entity); | ||
40 | } | ||
41 | } else if (key == controllable.getJumpKey()) | ||
42 | { | ||
43 | if (!controllable.isFrozen()) | ||
44 | { | ||
45 | jump(entity); | ||
46 | } | ||
47 | } else if (key == controllable.getDropKey()) | ||
48 | { | ||
49 | if (!controllable.isFrozen()) | ||
50 | { | ||
51 | drop(entity, true); | ||
52 | } | ||
53 | } | ||
54 | } else if (action == GLFW_RELEASE) | ||
55 | { | ||
56 | if (key == controllable.getLeftKey()) | ||
57 | { | ||
58 | controllable.setHoldingLeft(false); | ||
59 | |||
60 | if (!controllable.isFrozen()) | ||
61 | { | ||
62 | if (controllable.isHoldingRight()) | ||
63 | { | ||
64 | walkRight(entity); | ||
65 | } else { | ||
66 | stopWalking(entity); | ||
67 | } | ||
68 | } | ||
69 | } else if (key == controllable.getRightKey()) | ||
70 | { | ||
71 | controllable.setHoldingRight(false); | ||
72 | |||
73 | if (!controllable.isFrozen()) | ||
74 | { | ||
75 | if (controllable.isHoldingRight()) | ||
76 | { | ||
77 | walkLeft(entity); | ||
78 | } else { | ||
79 | stopWalking(entity); | ||
80 | } | ||
81 | } | ||
82 | } else if (key == controllable.getDropKey()) | ||
83 | { | ||
84 | if (!controllable.isFrozen()) | ||
85 | { | ||
86 | drop(entity, false); | ||
87 | } | ||
88 | } else if (key == controllable.getJumpKey()) | ||
89 | { | ||
90 | if (!controllable.isFrozen()) | ||
91 | { | ||
92 | stopJumping(entity); | ||
93 | } | ||
94 | } | ||
95 | } | ||
96 | } | ||
97 | |||
98 | actions.pop(); | ||
99 | } | ||
100 | } | ||
101 | |||
102 | void ControllingSystem::input(int key, int action) | ||
103 | { | ||
104 | actions.push(std::make_pair(key, action)); | ||
105 | } | ||
106 | |||
107 | void ControllingSystem::walkLeft(int entity) | ||
108 | { | ||
109 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | ||
110 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | ||
111 | |||
112 | ponderable.setVelocityX(-90); | ||
113 | |||
114 | animatable.setDirection(Direction::Left); | ||
115 | animatable.setWalking(true); | ||
116 | } | ||
117 | |||
118 | void ControllingSystem::walkRight(int entity) | ||
119 | { | ||
120 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | ||
121 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | ||
122 | |||
123 | ponderable.setVelocityX(90); | ||
124 | |||
125 | animatable.setDirection(Direction::Right); | ||
126 | animatable.setWalking(true); | ||
127 | } | ||
128 | |||
129 | void ControllingSystem::stopWalking(int entity) | ||
130 | { | ||
131 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | ||
132 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | ||
133 | |||
134 | ponderable.setVelocityX(0); | ||
135 | |||
136 | animatable.setWalking(false); | ||
137 | } | ||
138 | |||
139 | void ControllingSystem::jump(int entity) | ||
140 | { | ||
141 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | ||
142 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | ||
143 | |||
144 | playSound("res/Randomize87.wav", 0.25); | ||
145 | |||
146 | ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3)); | ||
147 | ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3)); | ||
148 | |||
149 | animatable.setJumping(true); | ||
150 | } | ||
151 | |||
152 | void ControllingSystem::stopJumping(int entity) | ||
153 | { | ||
154 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | ||
155 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | ||
156 | |||
157 | ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); | ||
158 | animatable.setJumping(false); | ||
159 | } | ||
160 | |||
161 | void ControllingSystem::drop(int entity, bool start) | ||
162 | { | ||
163 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | ||
164 | auto& droppable = game.getEntityManager().getComponent<DroppableComponent>(entity); | ||
165 | |||
166 | droppable.setDroppable(start); | ||
167 | animatable.setCrouching(start); | ||
168 | } | ||
diff --git a/src/systems/controlling.h b/src/systems/controlling.h new file mode 100644 index 0000000..61f86eb --- /dev/null +++ b/src/systems/controlling.h | |||
@@ -0,0 +1,26 @@ | |||
1 | #ifndef CONTROLLING_H_80B1BB8D | ||
2 | #define CONTROLLING_H_80B1BB8D | ||
3 | |||
4 | #include "system.h" | ||
5 | #include <queue> | ||
6 | |||
7 | class ControllingSystem : public System { | ||
8 | public: | ||
9 | ControllingSystem(Game& game) | ||
10 | : System(game) {} | ||
11 | |||
12 | void tick(double dt); | ||
13 | void input(int key, int action); | ||
14 | |||
15 | private: | ||
16 | void walkLeft(int entity); | ||
17 | void walkRight(int entity); | ||
18 | void stopWalking(int entity); | ||
19 | void jump(int entity); | ||
20 | void stopJumping(int entity); | ||
21 | void drop(int entity, bool start); | ||
22 | |||
23 | std::queue<std::pair<int,int>> actions; | ||
24 | }; | ||
25 | |||
26 | #endif /* end of include guard: CONTROLLING_H_80B1BB8D */ | ||
diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp new file mode 100644 index 0000000..96775d0 --- /dev/null +++ b/src/systems/pondering.cpp | |||
@@ -0,0 +1,23 @@ | |||
1 | #include "pondering.h" | ||
2 | #include "game.h" | ||
3 | #include "components/ponderable.h" | ||
4 | #include "components/transformable.h" | ||
5 | |||
6 | void PonderingSystem::tick(double dt) | ||
7 | { | ||
8 | auto entities = game.getEntityManager().getEntitiesWithComponents<PonderableComponent, TransformableComponent>(); | ||
9 | |||
10 | for (auto entity : entities) | ||
11 | { | ||
12 | auto& transformable = game.getEntityManager().getComponent<TransformableComponent>(entity); | ||
13 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | ||
14 | |||
15 | // Accelerate | ||
16 | ponderable.setVelocityX(ponderable.getVelocityX() + ponderable.getAccelX() * dt); | ||
17 | ponderable.setVelocityY(ponderable.getVelocityY() + ponderable.getAccelY() * dt); | ||
18 | |||
19 | // Move | ||
20 | transformable.setX(transformable.getX() + ponderable.getVelocityX() * dt); | ||
21 | transformable.setY(transformable.getY() + ponderable.getVelocityY() * dt); | ||
22 | } | ||
23 | } | ||
diff --git a/src/systems/pondering.h b/src/systems/pondering.h new file mode 100644 index 0000000..ad01a22 --- /dev/null +++ b/src/systems/pondering.h | |||
@@ -0,0 +1,14 @@ | |||
1 | #ifndef PONDERING_H_F2530E0E | ||
2 | #define PONDERING_H_F2530E0E | ||
3 | |||
4 | #include "system.h" | ||
5 | |||
6 | class PonderingSystem : public System { | ||
7 | public: | ||
8 | PonderingSystem(Game& game) | ||
9 | : System(game) {} | ||
10 | |||
11 | void tick(double dt); | ||
12 | }; | ||
13 | |||
14 | #endif /* end of include guard: PONDERING_H_F2530E0E */ | ||
diff --git a/src/systems/rendering.cpp b/src/systems/rendering.cpp index 0034dc3..251c2bc 100644 --- a/src/systems/rendering.cpp +++ b/src/systems/rendering.cpp | |||
@@ -1,17 +1,17 @@ | |||
1 | #include "rendering.h" | 1 | #include "rendering.h" |
2 | #include "entity_manager.h" | 2 | #include "game.h" |
3 | #include "components/sprite_renderable.h" | 3 | #include "components/animatable.h" |
4 | #include "components/transformable.h" | 4 | #include "components/transformable.h" |
5 | 5 | ||
6 | void RenderingSystem::tick(EntityManager& manager, float dt) | 6 | void RenderingSystem::tick(double dt) |
7 | { | 7 | { |
8 | texture.fill(texture.entirety(), 0, 0, 0); | 8 | texture.fill(texture.entirety(), 0, 0, 0); |
9 | 9 | ||
10 | std::set<int> spriteEntities = manager.getEntitiesWithComponents<SpriteRenderableComponent, TransformableComponent>(); | 10 | std::set<int> spriteEntities = game.getEntityManager().getEntitiesWithComponents<AnimatableComponent, TransformableComponent>(); |
11 | for (int entity : spriteEntities) | 11 | for (int entity : spriteEntities) |
12 | { | 12 | { |
13 | auto& sprite = manager.getComponent<SpriteRenderableComponent>(entity); | 13 | auto& sprite = game.getEntityManager().getComponent<AnimatableComponent>(entity); |
14 | auto& transform = manager.getComponent<TransformableComponent>(entity); | 14 | auto& transform = game.getEntityManager().getComponent<TransformableComponent>(entity); |
15 | Rectangle dstrect {(int) transform.getX(), (int) transform.getY(), transform.getW(), transform.getH()}; | 15 | Rectangle dstrect {(int) transform.getX(), (int) transform.getY(), transform.getW(), transform.getH()}; |
16 | 16 | ||
17 | texture.blit(sprite.getTexture(), sprite.getFrameRect(), dstrect); | 17 | texture.blit(sprite.getTexture(), sprite.getFrameRect(), dstrect); |
diff --git a/src/systems/rendering.h b/src/systems/rendering.h index 80ea79e..9b6e27e 100644 --- a/src/systems/rendering.h +++ b/src/systems/rendering.h | |||
@@ -7,7 +7,10 @@ | |||
7 | 7 | ||
8 | class RenderingSystem : public System { | 8 | class RenderingSystem : public System { |
9 | public: | 9 | public: |
10 | void tick(EntityManager& manager, float dt); | 10 | RenderingSystem(Game& game) |
11 | : System(game) {} | ||
12 | |||
13 | void tick(double dt); | ||
11 | 14 | ||
12 | private: | 15 | private: |
13 | Texture texture {GAME_WIDTH, GAME_HEIGHT}; | 16 | Texture texture {GAME_WIDTH, GAME_HEIGHT}; |