diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-06-26 19:59:28 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-06-26 19:59:28 -0400 |
commit | 428c401f9c1053f7e13ffe641758dfb72791d8dc (patch) | |
tree | 3b7c74e0346db3d08319e309c37e975e19395d74 /src | |
parent | 55c8a14a7e2b2dadf0def3e09f970818164366f5 (diff) | |
download | therapy-428c401f9c1053f7e13ffe641758dfb72791d8dc.tar.gz therapy-428c401f9c1053f7e13ffe641758dfb72791d8dc.tar.bz2 therapy-428c401f9c1053f7e13ffe641758dfb72791d8dc.zip |
Player now moves
Diffstat (limited to 'src')
-rw-r--r-- | src/components/animatable.cpp | 27 | ||||
-rw-r--r-- | src/components/animatable.h (renamed from src/components/sprite_renderable.h) | 10 | ||||
-rw-r--r-- | src/components/controllable.cpp | 71 | ||||
-rw-r--r-- | src/components/controllable.h | 36 | ||||
-rw-r--r-- | src/components/droppable.cpp | 11 | ||||
-rw-r--r-- | src/components/droppable.h | 15 | ||||
-rw-r--r-- | src/components/ponderable.cpp | 41 | ||||
-rw-r--r-- | src/components/ponderable.h | 24 | ||||
-rw-r--r-- | src/components/sprite_renderable.cpp | 27 | ||||
-rw-r--r-- | src/consts.h | 3 | ||||
-rw-r--r-- | src/direction.h | 11 | ||||
-rw-r--r-- | src/game.cpp | 75 | ||||
-rw-r--r-- | src/game.h | 24 | ||||
-rw-r--r-- | src/main.cpp | 25 | ||||
-rw-r--r-- | src/system.h | 10 | ||||
-rw-r--r-- | src/system_manager.h | 37 | ||||
-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 |
22 files changed, 635 insertions, 60 deletions
diff --git a/src/components/animatable.cpp b/src/components/animatable.cpp new file mode 100644 index 0000000..fcd277c --- /dev/null +++ b/src/components/animatable.cpp | |||
@@ -0,0 +1,27 @@ | |||
1 | #include "animatable.h" | ||
2 | |||
3 | AnimatableComponent::AnimatableComponent(const char* filename, int frame_width, int frame_height, int frames_across) | ||
4 | : texture(filename), frame_width(frame_width), frame_height(frame_height), frames_across(frames_across) | ||
5 | { | ||
6 | |||
7 | } | ||
8 | |||
9 | int AnimatableComponent::getFrame() const | ||
10 | { | ||
11 | return frame; | ||
12 | } | ||
13 | |||
14 | void AnimatableComponent::setFrame(int frame) | ||
15 | { | ||
16 | this->frame = frame; | ||
17 | } | ||
18 | |||
19 | const Texture& AnimatableComponent::getTexture() const | ||
20 | { | ||
21 | return texture; | ||
22 | } | ||
23 | |||
24 | Rectangle AnimatableComponent::getFrameRect() const | ||
25 | { | ||
26 | return {frame_width * (frame % frames_across), frame_height * (frame / frames_across), frame_width, frame_height}; | ||
27 | } | ||
diff --git a/src/components/sprite_renderable.h b/src/components/animatable.h index b4465c3..cf6ee54 100644 --- a/src/components/sprite_renderable.h +++ b/src/components/animatable.h | |||
@@ -3,10 +3,11 @@ | |||
3 | 3 | ||
4 | #include "component.h" | 4 | #include "component.h" |
5 | #include "renderer.h" | 5 | #include "renderer.h" |
6 | #include "direction.h" | ||
6 | 7 | ||
7 | class SpriteRenderableComponent : public Component { | 8 | class AnimatableComponent : public Component { |
8 | public: | 9 | public: |
9 | SpriteRenderableComponent(const char* filename, int frame_width, int frame_height, int frames_across); | 10 | AnimatableComponent(const char* filename, int frame_width, int frame_height, int frames_across); |
10 | 11 | ||
11 | int getFrame() const; | 12 | int getFrame() const; |
12 | void setFrame(int frame); | 13 | void setFrame(int frame); |
@@ -14,6 +15,11 @@ class SpriteRenderableComponent : public Component { | |||
14 | const Texture& getTexture() const; | 15 | const Texture& getTexture() const; |
15 | Rectangle getFrameRect() const; | 16 | Rectangle getFrameRect() const; |
16 | 17 | ||
18 | void setDirection(Direction dir) {}; | ||
19 | void setWalking(bool w) {}; | ||
20 | void setJumping(bool w) {}; | ||
21 | void setCrouching(bool w) {}; | ||
22 | |||
17 | private: | 23 | private: |
18 | Texture texture; | 24 | Texture texture; |
19 | int frame_width; | 25 | int frame_width; |
diff --git a/src/components/controllable.cpp b/src/components/controllable.cpp new file mode 100644 index 0000000..a4d45f2 --- /dev/null +++ b/src/components/controllable.cpp | |||
@@ -0,0 +1,71 @@ | |||
1 | #include "controllable.h" | ||
2 | |||
3 | int ControllableComponent::getLeftKey() const | ||
4 | { | ||
5 | return leftKey; | ||
6 | } | ||
7 | |||
8 | void ControllableComponent::setLeftKey(int k) | ||
9 | { | ||
10 | leftKey = k; | ||
11 | } | ||
12 | |||
13 | int ControllableComponent::getRightKey() const | ||
14 | { | ||
15 | return rightKey; | ||
16 | } | ||
17 | |||
18 | void ControllableComponent::setRightKey(int k) | ||
19 | { | ||
20 | rightKey = k; | ||
21 | } | ||
22 | |||
23 | int ControllableComponent::getJumpKey() const | ||
24 | { | ||
25 | return jumpKey; | ||
26 | } | ||
27 | |||
28 | void ControllableComponent::setJumpKey(int k) | ||
29 | { | ||
30 | jumpKey = k; | ||
31 | } | ||
32 | |||
33 | int ControllableComponent::getDropKey() const | ||
34 | { | ||
35 | return dropKey; | ||
36 | } | ||
37 | |||
38 | void ControllableComponent::setDropKey(int k) | ||
39 | { | ||
40 | dropKey = k; | ||
41 | } | ||
42 | |||
43 | bool ControllableComponent::isFrozen() const | ||
44 | { | ||
45 | return frozen; | ||
46 | } | ||
47 | |||
48 | void ControllableComponent::setFrozen(bool f) | ||
49 | { | ||
50 | frozen = f; | ||
51 | } | ||
52 | |||
53 | bool ControllableComponent::isHoldingLeft() const | ||
54 | { | ||
55 | return holdingLeft; | ||
56 | } | ||
57 | |||
58 | void ControllableComponent::setHoldingLeft(bool f) | ||
59 | { | ||
60 | holdingLeft = f; | ||
61 | } | ||
62 | |||
63 | bool ControllableComponent::isHoldingRight() const | ||
64 | { | ||
65 | return holdingRight; | ||
66 | } | ||
67 | |||
68 | void ControllableComponent::setHoldingRight(bool f) | ||
69 | { | ||
70 | holdingRight = f; | ||
71 | } | ||
diff --git a/src/components/controllable.h b/src/components/controllable.h new file mode 100644 index 0000000..317d68d --- /dev/null +++ b/src/components/controllable.h | |||
@@ -0,0 +1,36 @@ | |||
1 | #ifndef CONTROLLABLE_H_4E0B85B4 | ||
2 | #define CONTROLLABLE_H_4E0B85B4 | ||
3 | |||
4 | #include "component.h" | ||
5 | #include "renderer.h" | ||
6 | |||
7 | class ControllableComponent : public Component { | ||
8 | public: | ||
9 | int getLeftKey() const; | ||
10 | void setLeftKey(int k); | ||
11 | int getRightKey() const; | ||
12 | void setRightKey(int k); | ||
13 | int getJumpKey() const; | ||
14 | void setJumpKey(int k); | ||
15 | int getDropKey() const; | ||
16 | void setDropKey(int k); | ||
17 | |||
18 | bool isFrozen() const; | ||
19 | void setFrozen(bool f); | ||
20 | bool isHoldingLeft() const; | ||
21 | void setHoldingLeft(bool f); | ||
22 | bool isHoldingRight() const; | ||
23 | void setHoldingRight(bool f); | ||
24 | |||
25 | private: | ||
26 | int leftKey = GLFW_KEY_LEFT; | ||
27 | int rightKey = GLFW_KEY_RIGHT; | ||
28 | int jumpKey = GLFW_KEY_UP; | ||
29 | int dropKey = GLFW_KEY_DOWN; | ||
30 | |||
31 | bool frozen = false; | ||
32 | bool holdingLeft = false; | ||
33 | bool holdingRight = false; | ||
34 | }; | ||
35 | |||
36 | #endif /* end of include guard: CONTROLLABLE_H_4E0B85B4 */ | ||
diff --git a/src/components/droppable.cpp b/src/components/droppable.cpp new file mode 100644 index 0000000..534fd9a --- /dev/null +++ b/src/components/droppable.cpp | |||
@@ -0,0 +1,11 @@ | |||
1 | #include "droppable.h" | ||
2 | |||
3 | void DroppableComponent::setDroppable(bool can) | ||
4 | { | ||
5 | droppable = can; | ||
6 | } | ||
7 | |||
8 | bool DroppableComponent::isDroppable() const | ||
9 | { | ||
10 | return droppable; | ||
11 | } | ||
diff --git a/src/components/droppable.h b/src/components/droppable.h new file mode 100644 index 0000000..1f5608b --- /dev/null +++ b/src/components/droppable.h | |||
@@ -0,0 +1,15 @@ | |||
1 | #ifndef DROPPABLE_H_5DB254EF | ||
2 | #define DROPPABLE_H_5DB254EF | ||
3 | |||
4 | #include "component.h" | ||
5 | |||
6 | class DroppableComponent : public Component { | ||
7 | public: | ||
8 | void setDroppable(bool can); | ||
9 | bool isDroppable() const; | ||
10 | |||
11 | private: | ||
12 | bool droppable = false; | ||
13 | }; | ||
14 | |||
15 | #endif /* end of include guard: DROPPABLE_H_5DB254EF */ | ||
diff --git a/src/components/ponderable.cpp b/src/components/ponderable.cpp new file mode 100644 index 0000000..2cfa6a6 --- /dev/null +++ b/src/components/ponderable.cpp | |||
@@ -0,0 +1,41 @@ | |||
1 | #include "ponderable.h" | ||
2 | |||
3 | double PonderableComponent::getVelocityX() const | ||
4 | { | ||
5 | return velocityX; | ||
6 | } | ||
7 | |||
8 | void PonderableComponent::setVelocityX(double v) | ||
9 | { | ||
10 | velocityX = v; | ||
11 | } | ||
12 | |||
13 | double PonderableComponent::getVelocityY() const | ||
14 | { | ||
15 | return velocityY; | ||
16 | } | ||
17 | |||
18 | void PonderableComponent::setVelocityY(double v) | ||
19 | { | ||
20 | velocityY = v; | ||
21 | } | ||
22 | |||
23 | double PonderableComponent::getAccelX() const | ||
24 | { | ||
25 | return accelX; | ||
26 | } | ||
27 | |||
28 | void PonderableComponent::setAccelX(double v) | ||
29 | { | ||
30 | accelX = v; | ||
31 | } | ||
32 | |||
33 | double PonderableComponent::getAccelY() const | ||
34 | { | ||
35 | return accelY; | ||
36 | } | ||
37 | |||
38 | void PonderableComponent::setAccelY(double v) | ||
39 | { | ||
40 | accelY = v; | ||
41 | } | ||
diff --git a/src/components/ponderable.h b/src/components/ponderable.h new file mode 100644 index 0000000..5aab4b3 --- /dev/null +++ b/src/components/ponderable.h | |||
@@ -0,0 +1,24 @@ | |||
1 | #ifndef TANGIBLE_H_746DB3EE | ||
2 | #define TANGIBLE_H_746DB3EE | ||
3 | |||
4 | #include "component.h" | ||
5 | |||
6 | class PonderableComponent : public Component { | ||
7 | public: | ||
8 | double getVelocityX() const; | ||
9 | void setVelocityX(double v); | ||
10 | double getVelocityY() const; | ||
11 | void setVelocityY(double v); | ||
12 | double getAccelX() const; | ||
13 | void setAccelX(double v); | ||
14 | double getAccelY() const; | ||
15 | void setAccelY(double v); | ||
16 | |||
17 | private: | ||
18 | double velocityX = 0.0; | ||
19 | double velocityY = 0.0; | ||
20 | double accelX = 0.0; | ||
21 | double accelY = 0.0; | ||
22 | }; | ||
23 | |||
24 | #endif /* end of include guard: TANGIBLE_H_746DB3EE */ | ||
diff --git a/src/components/sprite_renderable.cpp b/src/components/sprite_renderable.cpp deleted file mode 100644 index 4c61111..0000000 --- a/src/components/sprite_renderable.cpp +++ /dev/null | |||
@@ -1,27 +0,0 @@ | |||
1 | #include "sprite_renderable.h" | ||
2 | |||
3 | SpriteRenderableComponent::SpriteRenderableComponent(const char* filename, int frame_width, int frame_height, int frames_across) | ||
4 | : texture(filename), frame_width(frame_width), frame_height(frame_height), frames_across(frames_across) | ||
5 | { | ||
6 | |||
7 | } | ||
8 | |||
9 | int SpriteRenderableComponent::getFrame() const | ||
10 | { | ||
11 | return frame; | ||
12 | } | ||
13 | |||
14 | void SpriteRenderableComponent::setFrame(int frame) | ||
15 | { | ||
16 | this->frame = frame; | ||
17 | } | ||
18 | |||
19 | const Texture& SpriteRenderableComponent::getTexture() const | ||
20 | { | ||
21 | return texture; | ||
22 | } | ||
23 | |||
24 | Rectangle SpriteRenderableComponent::getFrameRect() const | ||
25 | { | ||
26 | return {frame_width * (frame % frames_across), frame_height * (frame / frames_across), frame_width, frame_height}; | ||
27 | } | ||
diff --git a/src/consts.h b/src/consts.h index 804c761..4595719 100644 --- a/src/consts.h +++ b/src/consts.h | |||
@@ -11,4 +11,7 @@ const int MAP_HEIGHT = GAME_HEIGHT/TILE_HEIGHT - 1; | |||
11 | const int FRAMES_PER_SECOND = 60; | 11 | const int FRAMES_PER_SECOND = 60; |
12 | const double SECONDS_PER_FRAME = 1.0 / FRAMES_PER_SECOND; | 12 | const double SECONDS_PER_FRAME = 1.0 / FRAMES_PER_SECOND; |
13 | 13 | ||
14 | #define JUMP_VELOCITY(h, l) (-2 * (h) / (l)) | ||
15 | #define JUMP_GRAVITY(h, l) (2 * ((h) / (l)) / (l)) | ||
16 | |||
14 | #endif | 17 | #endif |
diff --git a/src/direction.h b/src/direction.h new file mode 100644 index 0000000..32d6b41 --- /dev/null +++ b/src/direction.h | |||
@@ -0,0 +1,11 @@ | |||
1 | #ifndef DIRECTION_H_9C49EAFD | ||
2 | #define DIRECTION_H_9C49EAFD | ||
3 | |||
4 | enum class Direction { | ||
5 | Left, | ||
6 | Right, | ||
7 | Up, | ||
8 | Down | ||
9 | }; | ||
10 | |||
11 | #endif /* end of include guard: DIRECTION_H_9C49EAFD */ | ||
diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..b3fa9a8 --- /dev/null +++ b/src/game.cpp | |||
@@ -0,0 +1,75 @@ | |||
1 | #include "game.h" | ||
2 | #include "components/animatable.h" | ||
3 | #include "components/transformable.h" | ||
4 | #include "components/controllable.h" | ||
5 | #include "components/droppable.h" | ||
6 | #include "components/ponderable.h" | ||
7 | #include "systems/rendering.h" | ||
8 | #include "systems/controlling.h" | ||
9 | #include "systems/pondering.h" | ||
10 | |||
11 | void key_callback(GLFWwindow* window, int key, int, int action, int) | ||
12 | { | ||
13 | Game& game = *((Game*) glfwGetWindowUserPointer(window)); | ||
14 | |||
15 | if ((action == GLFW_PRESS) && (key == GLFW_KEY_ESCAPE)) | ||
16 | { | ||
17 | game.shouldQuit = true; | ||
18 | |||
19 | return; | ||
20 | } | ||
21 | |||
22 | game.systemManager.getSystem<ControllingSystem>().input(key, action); | ||
23 | } | ||
24 | |||
25 | Game::Game(GLFWwindow* window) : window(window) | ||
26 | { | ||
27 | systemManager.emplaceSystem<ControllingSystem>(*this); | ||
28 | systemManager.emplaceSystem<RenderingSystem>(*this); | ||
29 | systemManager.emplaceSystem<PonderingSystem>(*this); | ||
30 | |||
31 | int player = entityManager.emplaceEntity(); | ||
32 | entityManager.emplaceComponent<AnimatableComponent>(player, "res/Starla.png", 10, 12, 6); | ||
33 | entityManager.emplaceComponent<TransformableComponent>(player, 203, 44, 10, 12); | ||
34 | entityManager.emplaceComponent<DroppableComponent>(player); | ||
35 | entityManager.emplaceComponent<PonderableComponent>(player); | ||
36 | entityManager.emplaceComponent<ControllableComponent>(player); | ||
37 | |||
38 | glfwSwapInterval(1); | ||
39 | glfwSetWindowUserPointer(window, this); | ||
40 | glfwSetKeyCallback(window, key_callback); | ||
41 | } | ||
42 | |||
43 | void Game::execute() | ||
44 | { | ||
45 | double lastTime = glfwGetTime(); | ||
46 | const double dt = 0.01; | ||
47 | double accumulator = 0.0; | ||
48 | |||
49 | while (!(shouldQuit || glfwWindowShouldClose(window))) | ||
50 | { | ||
51 | double currentTime = glfwGetTime(); | ||
52 | double frameTime = currentTime - lastTime; | ||
53 | lastTime = currentTime; | ||
54 | |||
55 | glfwPollEvents(); | ||
56 | |||
57 | accumulator += frameTime; | ||
58 | while (accumulator >= dt) | ||
59 | { | ||
60 | systemManager.getSystem<ControllingSystem>().tick(dt); | ||
61 | systemManager.getSystem<PonderingSystem>().tick(dt); | ||
62 | |||
63 | accumulator -= dt; | ||
64 | } | ||
65 | |||
66 | systemManager.getSystem<RenderingSystem>().tick(frameTime); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | EntityManager& Game::getEntityManager() | ||
71 | { | ||
72 | return entityManager; | ||
73 | } | ||
74 | |||
75 | |||
diff --git a/src/game.h b/src/game.h new file mode 100644 index 0000000..3822700 --- /dev/null +++ b/src/game.h | |||
@@ -0,0 +1,24 @@ | |||
1 | #ifndef GAME_H_1014DDC9 | ||
2 | #define GAME_H_1014DDC9 | ||
3 | |||
4 | #include "renderer.h" | ||
5 | #include "entity_manager.h" | ||
6 | #include "system_manager.h" | ||
7 | |||
8 | class Game { | ||
9 | public: | ||
10 | Game(GLFWwindow* window); | ||
11 | |||
12 | void execute(); | ||
13 | EntityManager& getEntityManager(); | ||
14 | |||
15 | friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); | ||
16 | |||
17 | private: | ||
18 | EntityManager entityManager; | ||
19 | SystemManager systemManager; | ||
20 | GLFWwindow* const window; | ||
21 | bool shouldQuit = false; | ||
22 | }; | ||
23 | |||
24 | #endif /* end of include guard: GAME_H_1014DDC9 */ | ||
diff --git a/src/main.cpp b/src/main.cpp index dcf8d87..35749f5 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -3,10 +3,7 @@ | |||
3 | #include <cstdlib> | 3 | #include <cstdlib> |
4 | #include "renderer.h" | 4 | #include "renderer.h" |
5 | #include "muxer.h" | 5 | #include "muxer.h" |
6 | #include "entity_manager.h" | 6 | #include "game.h" |
7 | #include "components/sprite_renderable.h" | ||
8 | #include "components/transformable.h" | ||
9 | #include "systems/rendering.h" | ||
10 | 7 | ||
11 | int main() | 8 | int main() |
12 | { | 9 | { |
@@ -19,24 +16,8 @@ int main() | |||
19 | 16 | ||
20 | // Put this in a block so game goes out of scope before we destroy the renderer | 17 | // Put this in a block so game goes out of scope before we destroy the renderer |
21 | { | 18 | { |
22 | EntityManager manager; | 19 | Game game {window}; |
23 | 20 | game.execute(); | |
24 | int player = manager.emplaceEntity(); | ||
25 | manager.emplaceComponent<SpriteRenderableComponent>(player, "res/Starla.png", 10, 12, 6); | ||
26 | manager.emplaceComponent<TransformableComponent>(player, 203, 44, 10, 12); | ||
27 | |||
28 | std::list<std::unique_ptr<System>> loop; | ||
29 | loop.push_back(std::unique_ptr<System>(new RenderingSystem())); | ||
30 | |||
31 | while (!glfwWindowShouldClose(window)) | ||
32 | { | ||
33 | for (auto& sys : loop) | ||
34 | { | ||
35 | sys->tick(manager, 1.0); | ||
36 | } | ||
37 | |||
38 | glfwPollEvents(); | ||
39 | } | ||
40 | } | 21 | } |
41 | 22 | ||
42 | destroyMuxer(); | 23 | destroyMuxer(); |
diff --git a/src/system.h b/src/system.h index 85415f0..e08db0a 100644 --- a/src/system.h +++ b/src/system.h | |||
@@ -1,11 +1,17 @@ | |||
1 | #ifndef SYSTEM_H_B61A8CEA | 1 | #ifndef SYSTEM_H_B61A8CEA |
2 | #define SYSTEM_H_B61A8CEA | 2 | #define SYSTEM_H_B61A8CEA |
3 | 3 | ||
4 | class EntityManager; | 4 | class Game; |
5 | 5 | ||
6 | class System { | 6 | class System { |
7 | public: | 7 | public: |
8 | virtual void tick(EntityManager& manager, float dt) = 0; | 8 | System(Game& game) |
9 | : game(game) {} | ||
10 | |||
11 | virtual void tick(double dt) = 0; | ||
12 | |||
13 | protected: | ||
14 | Game& game; | ||
9 | }; | 15 | }; |
10 | 16 | ||
11 | #endif /* end of include guard: SYSTEM_H_B61A8CEA */ | 17 | #endif /* end of include guard: SYSTEM_H_B61A8CEA */ |
diff --git a/src/system_manager.h b/src/system_manager.h new file mode 100644 index 0000000..8f76db2 --- /dev/null +++ b/src/system_manager.h | |||
@@ -0,0 +1,37 @@ | |||
1 | #ifndef SYSTEM_MANAGER_H_544E6056 | ||
2 | #define SYSTEM_MANAGER_H_544E6056 | ||
3 | |||
4 | #include <list> | ||
5 | #include <memory> | ||
6 | #include <map> | ||
7 | #include <typeindex> | ||
8 | #include "system.h" | ||
9 | |||
10 | class SystemManager { | ||
11 | private: | ||
12 | std::list<std::unique_ptr<System>> loop; | ||
13 | std::map<std::type_index, System*> systems; | ||
14 | |||
15 | public: | ||
16 | template <class T, class... Args> | ||
17 | void emplaceSystem(Game& game, Args&&... args) | ||
18 | { | ||
19 | std::unique_ptr<T> ptr = std::unique_ptr<T>(new T(game, std::forward<Args>(args)...)); | ||
20 | std::type_index systemType = typeid(T); | ||
21 | |||
22 | systems[systemType] = ptr.get(); | ||
23 | loop.push_back(std::move(ptr)); | ||
24 | } | ||
25 | |||
26 | template <class T> | ||
27 | T& getSystem() | ||
28 | { | ||
29 | std::type_index systemType = typeid(T); | ||
30 | |||
31 | assert(systems.count(systemType) == 1); | ||
32 | |||
33 | return *((T*)systems[systemType]); | ||
34 | } | ||
35 | }; | ||
36 | |||
37 | #endif /* end of include guard: SYSTEM_MANAGER_H_544E6056 */ | ||
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}; |