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 | 21 | ||||
-rw-r--r-- | src/systems/rendering.h | 19 |
6 files changed, 271 insertions, 0 deletions
diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp new file mode 100644 index 0000000..456da3b --- /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..30210b3 --- /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..50a8bc8 --- /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..3fe5473 --- /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 new file mode 100644 index 0000000..251c2bc --- /dev/null +++ b/src/systems/rendering.cpp | |||
@@ -0,0 +1,21 @@ | |||
1 | #include "rendering.h" | ||
2 | #include "game.h" | ||
3 | #include "components/animatable.h" | ||
4 | #include "components/transformable.h" | ||
5 | |||
6 | void RenderingSystem::tick(double dt) | ||
7 | { | ||
8 | texture.fill(texture.entirety(), 0, 0, 0); | ||
9 | |||
10 | std::set<int> spriteEntities = game.getEntityManager().getEntitiesWithComponents<AnimatableComponent, TransformableComponent>(); | ||
11 | for (int entity : spriteEntities) | ||
12 | { | ||
13 | auto& sprite = game.getEntityManager().getComponent<AnimatableComponent>(entity); | ||
14 | auto& transform = game.getEntityManager().getComponent<TransformableComponent>(entity); | ||
15 | Rectangle dstrect {(int) transform.getX(), (int) transform.getY(), transform.getW(), transform.getH()}; | ||
16 | |||
17 | texture.blit(sprite.getTexture(), sprite.getFrameRect(), dstrect); | ||
18 | } | ||
19 | |||
20 | texture.renderScreen(); | ||
21 | } | ||
diff --git a/src/systems/rendering.h b/src/systems/rendering.h new file mode 100644 index 0000000..cec72e2 --- /dev/null +++ b/src/systems/rendering.h | |||
@@ -0,0 +1,19 @@ | |||
1 | #ifndef RENDERING_H_76ABC02A | ||
2 | #define RENDERING_H_76ABC02A | ||
3 | |||
4 | #include "system.h" | ||
5 | #include "renderer.h" | ||
6 | #include "consts.h" | ||
7 | |||
8 | class RenderingSystem : public System { | ||
9 | public: | ||
10 | RenderingSystem(Game& game) | ||
11 | : System(game) {} | ||
12 | |||
13 | void tick(double dt); | ||
14 | |||
15 | private: | ||
16 | Texture texture {GAME_WIDTH, GAME_HEIGHT}; | ||
17 | }; | ||
18 | |||
19 | #endif /* end of include guard: RENDERING_H_76ABC02A */ | ||