diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/animation.cpp | 35 | ||||
-rw-r--r-- | src/animation.h | 100 | ||||
-rw-r--r-- | src/components/animatable.cpp | 27 | ||||
-rw-r--r-- | src/components/animatable.h | 75 | ||||
-rw-r--r-- | src/components/orientable.h | 24 | ||||
-rw-r--r-- | src/components/ponderable.h | 13 | ||||
-rw-r--r-- | src/game.cpp | 73 | ||||
-rw-r--r-- | src/game.h | 36 | ||||
-rw-r--r-- | src/system.h | 12 | ||||
-rw-r--r-- | src/systems/animating.cpp | 38 | ||||
-rw-r--r-- | src/systems/animating.h | 20 | ||||
-rw-r--r-- | src/systems/controlling.cpp | 138 | ||||
-rw-r--r-- | src/systems/controlling.h | 36 | ||||
-rw-r--r-- | src/systems/pondering.cpp | 10 | ||||
-rw-r--r-- | src/systems/pondering.h | 10 | ||||
-rw-r--r-- | src/systems/rendering.cpp | 40 | ||||
-rw-r--r-- | src/systems/rendering.h | 15 |
17 files changed, 520 insertions, 182 deletions
diff --git a/src/animation.cpp b/src/animation.cpp new file mode 100644 index 0000000..31ba21f --- /dev/null +++ b/src/animation.cpp | |||
@@ -0,0 +1,35 @@ | |||
1 | #include "animation.h" | ||
2 | |||
3 | AnimationSet::AnimationSet( | ||
4 | Texture texture, | ||
5 | int frameWidth, | ||
6 | int frameHeight, | ||
7 | int framesAcross) : | ||
8 | texture_(std::move(texture)), | ||
9 | frameWidth_(frameWidth), | ||
10 | frameHeight_(frameHeight), | ||
11 | framesAcross_(framesAcross) | ||
12 | { | ||
13 | } | ||
14 | |||
15 | void AnimationSet::emplaceAnimation( | ||
16 | std::string animation, | ||
17 | size_t firstFrame, | ||
18 | size_t numFrames, | ||
19 | size_t delay) | ||
20 | { | ||
21 | animations_.emplace( | ||
22 | std::piecewise_construct, | ||
23 | std::make_tuple(animation), | ||
24 | std::make_tuple(firstFrame, numFrames, delay)); | ||
25 | } | ||
26 | |||
27 | Rectangle AnimationSet::getFrameRect(int frame) const | ||
28 | { | ||
29 | return { | ||
30 | frameWidth_ * (frame % framesAcross_), | ||
31 | frameHeight_ * (frame / framesAcross_), | ||
32 | frameWidth_, | ||
33 | frameHeight_ | ||
34 | }; | ||
35 | } | ||
diff --git a/src/animation.h b/src/animation.h new file mode 100644 index 0000000..50446d0 --- /dev/null +++ b/src/animation.h | |||
@@ -0,0 +1,100 @@ | |||
1 | #ifndef ANIMATION_H_74EB0901 | ||
2 | #define ANIMATION_H_74EB0901 | ||
3 | |||
4 | #include "renderer.h" | ||
5 | #include <string> | ||
6 | #include <map> | ||
7 | #include <stdexcept> | ||
8 | |||
9 | class Animation { | ||
10 | public: | ||
11 | |||
12 | Animation( | ||
13 | size_t firstFrame, | ||
14 | size_t numFrames, | ||
15 | size_t delay) : | ||
16 | firstFrame_(firstFrame), | ||
17 | numFrames_(numFrames), | ||
18 | delay_(delay) | ||
19 | { | ||
20 | } | ||
21 | |||
22 | inline size_t getFirstFrame() const | ||
23 | { | ||
24 | return firstFrame_; | ||
25 | } | ||
26 | |||
27 | inline size_t getNumFrames() const | ||
28 | { | ||
29 | return numFrames_; | ||
30 | } | ||
31 | |||
32 | inline size_t getDelay() const | ||
33 | { | ||
34 | return delay_; | ||
35 | } | ||
36 | |||
37 | private: | ||
38 | |||
39 | size_t firstFrame_; | ||
40 | size_t numFrames_; | ||
41 | size_t delay_; | ||
42 | }; | ||
43 | |||
44 | class AnimationSet { | ||
45 | public: | ||
46 | |||
47 | AnimationSet( | ||
48 | Texture texture, | ||
49 | int frameWidth, | ||
50 | int frameHeight, | ||
51 | int framesAcross); | ||
52 | |||
53 | void emplaceAnimation( | ||
54 | std::string animation, | ||
55 | size_t firstFrame, | ||
56 | size_t numFrames, | ||
57 | size_t delay); | ||
58 | |||
59 | inline const Animation& getAnimation(std::string animation) const | ||
60 | { | ||
61 | if (!animations_.count(animation)) | ||
62 | { | ||
63 | throw std::invalid_argument("Animation does not exist"); | ||
64 | } | ||
65 | |||
66 | return animations_.at(animation); | ||
67 | } | ||
68 | |||
69 | inline const Texture& getTexture() const | ||
70 | { | ||
71 | return texture_; | ||
72 | } | ||
73 | |||
74 | inline int getFrameWidth() const | ||
75 | { | ||
76 | return frameWidth_; | ||
77 | } | ||
78 | |||
79 | inline int getFrameHeight() const | ||
80 | { | ||
81 | return frameHeight_; | ||
82 | } | ||
83 | |||
84 | inline int getFramesAcross() const | ||
85 | { | ||
86 | return framesAcross_; | ||
87 | } | ||
88 | |||
89 | Rectangle getFrameRect(int frame) const; | ||
90 | |||
91 | private: | ||
92 | |||
93 | std::map<std::string, Animation> animations_; | ||
94 | Texture texture_; | ||
95 | int frameWidth_; | ||
96 | int frameHeight_; | ||
97 | int framesAcross_; | ||
98 | }; | ||
99 | |||
100 | #endif /* end of include guard: ANIMATION_H_74EB0901 */ | ||
diff --git a/src/components/animatable.cpp b/src/components/animatable.cpp deleted file mode 100644 index fcd277c..0000000 --- a/src/components/animatable.cpp +++ /dev/null | |||
@@ -1,27 +0,0 @@ | |||
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/animatable.h b/src/components/animatable.h index cf6ee54..ed0133e 100644 --- a/src/components/animatable.h +++ b/src/components/animatable.h | |||
@@ -2,30 +2,61 @@ | |||
2 | #define SPRITE_RENDERABLE_H_D3AACBBF | 2 | #define SPRITE_RENDERABLE_H_D3AACBBF |
3 | 3 | ||
4 | #include "component.h" | 4 | #include "component.h" |
5 | #include "renderer.h" | 5 | #include "animation.h" |
6 | #include "direction.h" | 6 | #include <string> |
7 | 7 | ||
8 | class AnimatableComponent : public Component { | 8 | class AnimatableComponent : public Component { |
9 | public: | 9 | public: |
10 | AnimatableComponent(const char* filename, int frame_width, int frame_height, int frames_across); | 10 | |
11 | 11 | AnimatableComponent( | |
12 | int getFrame() const; | 12 | AnimationSet animationSet, |
13 | void setFrame(int frame); | 13 | std::string animation) : |
14 | 14 | animationSet_(std::move(animationSet)), | |
15 | const Texture& getTexture() const; | 15 | animation_(std::move(animation)) |
16 | Rectangle getFrameRect() const; | 16 | { |
17 | 17 | } | |
18 | void setDirection(Direction dir) {}; | 18 | |
19 | void setWalking(bool w) {}; | 19 | inline size_t getFrame() const |
20 | void setJumping(bool w) {}; | 20 | { |
21 | void setCrouching(bool w) {}; | 21 | return frame_; |
22 | 22 | } | |
23 | private: | 23 | |
24 | Texture texture; | 24 | inline void setFrame(size_t v) |
25 | int frame_width; | 25 | { |
26 | int frame_height; | 26 | frame_ = v; |
27 | int frames_across; | 27 | } |
28 | int frame = 0; | 28 | |
29 | inline size_t getCountdown() const | ||
30 | { | ||
31 | return countdown_; | ||
32 | } | ||
33 | |||
34 | inline void setCountdown(size_t v) | ||
35 | { | ||
36 | countdown_ = v; | ||
37 | } | ||
38 | |||
39 | inline const AnimationSet& getAnimationSet() const | ||
40 | { | ||
41 | return animationSet_; | ||
42 | } | ||
43 | |||
44 | inline const Animation& getAnimation() const | ||
45 | { | ||
46 | return animationSet_.getAnimation(animation_); | ||
47 | } | ||
48 | |||
49 | inline void setAnimation(std::string animation) | ||
50 | { | ||
51 | animation_ = std::move(animation); | ||
52 | } | ||
53 | |||
54 | private: | ||
55 | |||
56 | AnimationSet animationSet_; | ||
57 | std::string animation_; | ||
58 | size_t frame_ = 0; | ||
59 | size_t countdown_ = 0; | ||
29 | }; | 60 | }; |
30 | 61 | ||
31 | #endif /* end of include guard: SPRITE_RENDERABLE_H_D3AACBBF */ | 62 | #endif /* end of include guard: SPRITE_RENDERABLE_H_D3AACBBF */ |
diff --git a/src/components/orientable.h b/src/components/orientable.h new file mode 100644 index 0000000..8f56912 --- /dev/null +++ b/src/components/orientable.h | |||
@@ -0,0 +1,24 @@ | |||
1 | #ifndef ORIENTABLE_H_EDB6C4A1 | ||
2 | #define ORIENTABLE_H_EDB6C4A1 | ||
3 | |||
4 | #include "component.h" | ||
5 | |||
6 | class OrientableComponent : public Component { | ||
7 | public: | ||
8 | |||
9 | inline bool isFacingRight() const | ||
10 | { | ||
11 | return facingRight_; | ||
12 | } | ||
13 | |||
14 | inline void setFacingRight(bool v) | ||
15 | { | ||
16 | facingRight_ = v; | ||
17 | } | ||
18 | |||
19 | private: | ||
20 | |||
21 | bool facingRight_ = false; | ||
22 | }; | ||
23 | |||
24 | #endif /* end of include guard: ORIENTABLE_H_EDB6C4A1 */ | ||
diff --git a/src/components/ponderable.h b/src/components/ponderable.h index 80100d7..dfbf908 100644 --- a/src/components/ponderable.h +++ b/src/components/ponderable.h | |||
@@ -6,6 +6,13 @@ | |||
6 | class PonderableComponent : public Component { | 6 | class PonderableComponent : public Component { |
7 | public: | 7 | public: |
8 | 8 | ||
9 | enum class state { | ||
10 | grounded, | ||
11 | jumping, | ||
12 | falling, | ||
13 | dropping | ||
14 | }; | ||
15 | |||
9 | inline double getVelocityX() const | 16 | inline double getVelocityX() const |
10 | { | 17 | { |
11 | return velX_; | 18 | return velX_; |
@@ -51,12 +58,18 @@ public: | |||
51 | return state_; | 58 | return state_; |
52 | } | 59 | } |
53 | 60 | ||
61 | inline void setState(state arg) | ||
62 | { | ||
63 | state_ = arg; | ||
64 | } | ||
65 | |||
54 | private: | 66 | private: |
55 | 67 | ||
56 | double velX_ = 0.0; | 68 | double velX_ = 0.0; |
57 | double velY_ = 0.0; | 69 | double velY_ = 0.0; |
58 | double accelX_ = 0.0; | 70 | double accelX_ = 0.0; |
59 | double accelY_ = 0.0; | 71 | double accelY_ = 0.0; |
72 | state state_ = state::grounded; | ||
60 | }; | 73 | }; |
61 | 74 | ||
62 | #endif /* end of include guard: TANGIBLE_H_746DB3EE */ | 75 | #endif /* end of include guard: TANGIBLE_H_746DB3EE */ |
diff --git a/src/game.cpp b/src/game.cpp index 5d1ec18..492e4d0 100644 --- a/src/game.cpp +++ b/src/game.cpp | |||
@@ -4,40 +4,59 @@ | |||
4 | #include "components/controllable.h" | 4 | #include "components/controllable.h" |
5 | #include "components/droppable.h" | 5 | #include "components/droppable.h" |
6 | #include "components/ponderable.h" | 6 | #include "components/ponderable.h" |
7 | #include "components/orientable.h" | ||
7 | #include "systems/rendering.h" | 8 | #include "systems/rendering.h" |
8 | #include "systems/controlling.h" | 9 | #include "systems/controlling.h" |
9 | #include "systems/pondering.h" | 10 | #include "systems/pondering.h" |
11 | #include "systems/animating.h" | ||
12 | #include "animation.h" | ||
10 | 13 | ||
11 | void key_callback(GLFWwindow* window, int key, int, int action, int) | 14 | void key_callback(GLFWwindow* window, int key, int, int action, int) |
12 | { | 15 | { |
13 | Game& game = *((Game*) glfwGetWindowUserPointer(window)); | 16 | Game& game = *((Game*) glfwGetWindowUserPointer(window)); |
14 | 17 | ||
15 | if ((action == GLFW_PRESS) && (key == GLFW_KEY_ESCAPE)) | 18 | if ((action == GLFW_PRESS) && (key == GLFW_KEY_ESCAPE)) |
16 | { | 19 | { |
17 | game.shouldQuit = true; | 20 | game.shouldQuit_ = true; |
18 | 21 | ||
19 | return; | 22 | return; |
20 | } | 23 | } |
21 | 24 | ||
22 | game.systemManager.getSystem<ControllingSystem>().input(key, action); | 25 | game.systemManager_.getSystem<ControllingSystem>().input(key, action); |
23 | } | 26 | } |
24 | 27 | ||
25 | Game::Game(GLFWwindow* window) : window(window) | 28 | Game::Game(GLFWwindow* window) : window_(window) |
26 | { | 29 | { |
27 | systemManager.emplaceSystem<ControllingSystem>(*this); | 30 | systemManager_.emplaceSystem<ControllingSystem>(*this); |
28 | systemManager.emplaceSystem<RenderingSystem>(*this); | 31 | systemManager_.emplaceSystem<PonderingSystem>(*this); |
29 | systemManager.emplaceSystem<PonderingSystem>(*this); | 32 | systemManager_.emplaceSystem<RenderingSystem>(*this); |
30 | 33 | systemManager_.emplaceSystem<AnimatingSystem>(*this); | |
31 | int player = entityManager.emplaceEntity(); | 34 | |
32 | entityManager.emplaceComponent<AnimatableComponent>(player, "res/Starla.png", 10, 12, 6); | 35 | int player = entityManager_.emplaceEntity(); |
33 | entityManager.emplaceComponent<TransformableComponent>(player, 203, 44, 10, 12); | 36 | |
34 | entityManager.emplaceComponent<DroppableComponent>(player); | 37 | AnimationSet playerGraphics {"res/Starla2.bmp", 10, 12, 6}; |
35 | entityManager.emplaceComponent<PonderableComponent>(player); | 38 | playerGraphics.emplaceAnimation("stillLeft", 3, 1, 1); |
36 | entityManager.emplaceComponent<ControllableComponent>(player); | 39 | playerGraphics.emplaceAnimation("stillRight", 0, 1, 1); |
37 | 40 | playerGraphics.emplaceAnimation("walkingLeft", 4, 2, 10); | |
41 | playerGraphics.emplaceAnimation("walkingRight", 1, 2, 10); | ||
42 | |||
43 | entityManager_.emplaceComponent<AnimatableComponent>( | ||
44 | player, | ||
45 | std::move(playerGraphics), | ||
46 | "stillLeft"); | ||
47 | |||
48 | entityManager_.emplaceComponent<TransformableComponent>( | ||
49 | player, | ||
50 | 203, 44, 10, 12); | ||
51 | |||
52 | entityManager_.emplaceComponent<DroppableComponent>(player); | ||
53 | entityManager_.emplaceComponent<PonderableComponent>(player); | ||
54 | entityManager_.emplaceComponent<ControllableComponent>(player); | ||
55 | entityManager_.emplaceComponent<OrientableComponent>(player); | ||
56 | |||
38 | glfwSwapInterval(1); | 57 | glfwSwapInterval(1); |
39 | glfwSetWindowUserPointer(window, this); | 58 | glfwSetWindowUserPointer(window_, this); |
40 | glfwSetKeyCallback(window, key_callback); | 59 | glfwSetKeyCallback(window_, key_callback); |
41 | } | 60 | } |
42 | 61 | ||
43 | void Game::execute() | 62 | void Game::execute() |
@@ -46,7 +65,7 @@ void Game::execute() | |||
46 | const double dt = 0.01; | 65 | const double dt = 0.01; |
47 | double accumulator = 0.0; | 66 | double accumulator = 0.0; |
48 | 67 | ||
49 | while (!(shouldQuit || glfwWindowShouldClose(window))) | 68 | while (!(shouldQuit_ || glfwWindowShouldClose(window_))) |
50 | { | 69 | { |
51 | double currentTime = glfwGetTime(); | 70 | double currentTime = glfwGetTime(); |
52 | double frameTime = currentTime - lastTime; | 71 | double frameTime = currentTime - lastTime; |
@@ -57,19 +76,13 @@ void Game::execute() | |||
57 | accumulator += frameTime; | 76 | accumulator += frameTime; |
58 | while (accumulator >= dt) | 77 | while (accumulator >= dt) |
59 | { | 78 | { |
60 | systemManager.getSystem<ControllingSystem>().tick(dt); | 79 | systemManager_.getSystem<ControllingSystem>().tick(dt); |
61 | systemManager.getSystem<PonderingSystem>().tick(dt); | 80 | systemManager_.getSystem<PonderingSystem>().tick(dt); |
81 | systemManager_.getSystem<AnimatingSystem>().tick(dt); | ||
62 | 82 | ||
63 | accumulator -= dt; | 83 | accumulator -= dt; |
64 | } | 84 | } |
65 | 85 | ||
66 | systemManager.getSystem<RenderingSystem>().tick(frameTime); | 86 | systemManager_.getSystem<RenderingSystem>().tick(frameTime); |
67 | } | 87 | } |
68 | } | 88 | } |
69 | |||
70 | EntityManager& Game::getEntityManager() | ||
71 | { | ||
72 | return entityManager; | ||
73 | } | ||
74 | |||
75 | |||
diff --git a/src/game.h b/src/game.h index ec667c8..7bd038e 100644 --- a/src/game.h +++ b/src/game.h | |||
@@ -6,19 +6,35 @@ | |||
6 | #include "system_manager.h" | 6 | #include "system_manager.h" |
7 | 7 | ||
8 | class Game { | 8 | class Game { |
9 | public: | 9 | public: |
10 | Game(GLFWwindow* window); | ||
11 | 10 | ||
12 | void execute(); | 11 | Game(GLFWwindow* window); |
13 | EntityManager& getEntityManager(); | ||
14 | 12 | ||
15 | friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); | 13 | void execute(); |
16 | 14 | ||
17 | private: | 15 | inline EntityManager& getEntityManager() |
18 | EntityManager entityManager; | 16 | { |
19 | SystemManager systemManager; | 17 | return entityManager_; |
20 | GLFWwindow* const window; | 18 | } |
21 | bool shouldQuit = false; | 19 | |
20 | inline SystemManager& getSystemManager() | ||
21 | { | ||
22 | return systemManager_; | ||
23 | } | ||
24 | |||
25 | friend void key_callback( | ||
26 | GLFWwindow* window, | ||
27 | int key, | ||
28 | int scancode, | ||
29 | int action, | ||
30 | int mods); | ||
31 | |||
32 | private: | ||
33 | |||
34 | EntityManager entityManager_; | ||
35 | SystemManager systemManager_; | ||
36 | GLFWwindow* const window_; | ||
37 | bool shouldQuit_ = false; | ||
22 | }; | 38 | }; |
23 | 39 | ||
24 | #endif /* end of include guard: GAME_H_1014DDC9 */ | 40 | #endif /* end of include guard: GAME_H_1014DDC9 */ |
diff --git a/src/system.h b/src/system.h index 489afd0..6a3cd14 100644 --- a/src/system.h +++ b/src/system.h | |||
@@ -1,12 +1,18 @@ | |||
1 | #ifndef SYSTEM_H_B61A8CEA | 1 | #ifndef SYSTEM_H_B61A8CEA |
2 | #define SYSTEM_H_B61A8CEA | 2 | #define SYSTEM_H_B61A8CEA |
3 | 3 | ||
4 | #include "entity_manager.h" | ||
5 | |||
4 | class Game; | 6 | class Game; |
5 | 7 | ||
6 | class System { | 8 | class System { |
7 | public: | 9 | public: |
8 | System(Game& game) | 10 | |
9 | : game(game) {} | 11 | using id_type = EntityManager::id_type; |
12 | |||
13 | System(Game& game) : game_(game) | ||
14 | { | ||
15 | } | ||
10 | 16 | ||
11 | virtual ~System() = default; | 17 | virtual ~System() = default; |
12 | 18 | ||
@@ -14,7 +20,7 @@ public: | |||
14 | 20 | ||
15 | protected: | 21 | protected: |
16 | 22 | ||
17 | Game& game; | 23 | Game& game_; |
18 | }; | 24 | }; |
19 | 25 | ||
20 | #endif /* end of include guard: SYSTEM_H_B61A8CEA */ | 26 | #endif /* end of include guard: SYSTEM_H_B61A8CEA */ |
diff --git a/src/systems/animating.cpp b/src/systems/animating.cpp new file mode 100644 index 0000000..fcbfca5 --- /dev/null +++ b/src/systems/animating.cpp | |||
@@ -0,0 +1,38 @@ | |||
1 | #include "animating.h" | ||
2 | #include "game.h" | ||
3 | #include "components/animatable.h" | ||
4 | |||
5 | void AnimatingSystem::tick(double) | ||
6 | { | ||
7 | std::set<id_type> spriteEntities = | ||
8 | game_.getEntityManager().getEntitiesWithComponents<AnimatableComponent>(); | ||
9 | |||
10 | for (id_type entity : spriteEntities) | ||
11 | { | ||
12 | auto& sprite = game_.getEntityManager(). | ||
13 | getComponent<AnimatableComponent>(entity); | ||
14 | |||
15 | sprite.setCountdown(sprite.getCountdown() + 1); | ||
16 | |||
17 | const Animation& anim = sprite.getAnimation(); | ||
18 | if (sprite.getCountdown() >= anim.getDelay()) | ||
19 | { | ||
20 | sprite.setFrame(sprite.getFrame() + 1); | ||
21 | sprite.setCountdown(0); | ||
22 | |||
23 | if (sprite.getFrame() >= anim.getFirstFrame() + anim.getNumFrames()) | ||
24 | { | ||
25 | sprite.setFrame(anim.getFirstFrame()); | ||
26 | } | ||
27 | } | ||
28 | } | ||
29 | } | ||
30 | |||
31 | void AnimatingSystem::startAnimation(id_type entity, std::string animation) | ||
32 | { | ||
33 | auto& sprite = game_.getEntityManager(). | ||
34 | getComponent<AnimatableComponent>(entity); | ||
35 | |||
36 | sprite.setAnimation(animation); | ||
37 | sprite.setFrame(sprite.getAnimation().getFirstFrame()); | ||
38 | } | ||
diff --git a/src/systems/animating.h b/src/systems/animating.h new file mode 100644 index 0000000..150a74a --- /dev/null +++ b/src/systems/animating.h | |||
@@ -0,0 +1,20 @@ | |||
1 | #ifndef ANIMATING_H_5BBF0094 | ||
2 | #define ANIMATING_H_5BBF0094 | ||
3 | |||
4 | #include "system.h" | ||
5 | #include <string> | ||
6 | |||
7 | class AnimatingSystem : public System { | ||
8 | public: | ||
9 | |||
10 | AnimatingSystem(Game& game) : System(game) | ||
11 | { | ||
12 | } | ||
13 | |||
14 | void tick(double dt); | ||
15 | |||
16 | void startAnimation(id_type entity, std::string animation); | ||
17 | |||
18 | }; | ||
19 | |||
20 | #endif /* end of include guard: ANIMATING_H_5BBF0094 */ | ||
diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp index ec62e9a..3647ff8 100644 --- a/src/systems/controlling.cpp +++ b/src/systems/controlling.cpp | |||
@@ -4,21 +4,30 @@ | |||
4 | #include "components/ponderable.h" | 4 | #include "components/ponderable.h" |
5 | #include "components/animatable.h" | 5 | #include "components/animatable.h" |
6 | #include "components/droppable.h" | 6 | #include "components/droppable.h" |
7 | #include "components/orientable.h" | ||
8 | #include "systems/animating.h" | ||
7 | #include "direction.h" | 9 | #include "direction.h" |
8 | #include "muxer.h" | 10 | #include "muxer.h" |
9 | #include "consts.h" | 11 | #include "consts.h" |
10 | 12 | ||
11 | void ControllingSystem::tick(double dt) | 13 | void ControllingSystem::tick(double) |
12 | { | 14 | { |
13 | while (!actions.empty()) | 15 | while (!actions_.empty()) |
14 | { | 16 | { |
15 | int key = actions.front().first; | 17 | int key = actions_.front().first; |
16 | int action = actions.front().second; | 18 | int action = actions_.front().second; |
19 | |||
20 | auto entities = game_.getEntityManager().getEntitiesWithComponents< | ||
21 | ControllableComponent, | ||
22 | PonderableComponent, | ||
23 | AnimatableComponent, | ||
24 | DroppableComponent, | ||
25 | OrientableComponent>(); | ||
17 | 26 | ||
18 | auto entities = game.getEntityManager().getEntitiesWithComponents<ControllableComponent, PonderableComponent, AnimatableComponent, DroppableComponent>(); | ||
19 | for (auto entity : entities) | 27 | for (auto entity : entities) |
20 | { | 28 | { |
21 | auto& controllable = game.getEntityManager().getComponent<ControllableComponent>(entity); | 29 | auto& controllable = game_.getEntityManager(). |
30 | getComponent<ControllableComponent>(entity); | ||
22 | 31 | ||
23 | if (action == GLFW_PRESS) | 32 | if (action == GLFW_PRESS) |
24 | { | 33 | { |
@@ -95,74 +104,107 @@ void ControllingSystem::tick(double dt) | |||
95 | } | 104 | } |
96 | } | 105 | } |
97 | 106 | ||
98 | actions.pop(); | 107 | actions_.pop(); |
99 | } | 108 | } |
100 | } | 109 | } |
101 | 110 | ||
102 | void ControllingSystem::input(int key, int action) | 111 | void ControllingSystem::input(int key, int action) |
103 | { | 112 | { |
104 | actions.push(std::make_pair(key, action)); | 113 | actions_.push(std::make_pair(key, action)); |
105 | } | 114 | } |
106 | 115 | ||
107 | void ControllingSystem::walkLeft(int entity) | 116 | void ControllingSystem::walkLeft(id_type entity) |
108 | { | 117 | { |
109 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | 118 | auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); |
110 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | 119 | auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity); |
111 | 120 | ||
121 | orientable.setFacingRight(false); | ||
112 | ponderable.setVelocityX(-90); | 122 | ponderable.setVelocityX(-90); |
113 | 123 | ||
114 | animatable.setDirection(Direction::Left); | 124 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); |
115 | animatable.setWalking(true); | 125 | |
126 | if (ponderable.getState() == PonderableComponent::state::grounded) | ||
127 | { | ||
128 | animating.startAnimation(entity, "walkingLeft"); | ||
129 | } else { | ||
130 | animating.startAnimation(entity, "stillLeft"); | ||
131 | } | ||
116 | } | 132 | } |
117 | 133 | ||
118 | void ControllingSystem::walkRight(int entity) | 134 | void ControllingSystem::walkRight(id_type entity) |
119 | { | 135 | { |
120 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | 136 | auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); |
121 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | 137 | auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity); |
122 | 138 | ||
139 | orientable.setFacingRight(true); | ||
123 | ponderable.setVelocityX(90); | 140 | ponderable.setVelocityX(90); |
124 | 141 | ||
125 | animatable.setDirection(Direction::Right); | 142 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); |
126 | animatable.setWalking(true); | 143 | |
144 | if (ponderable.getState() == PonderableComponent::state::grounded) | ||
145 | { | ||
146 | animating.startAnimation(entity, "walkingRight"); | ||
147 | } else { | ||
148 | animating.startAnimation(entity, "stillRight"); | ||
149 | } | ||
127 | } | 150 | } |
128 | 151 | ||
129 | void ControllingSystem::stopWalking(int entity) | 152 | void ControllingSystem::stopWalking(id_type entity) |
130 | { | 153 | { |
131 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | 154 | auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); |
132 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | 155 | auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity); |
133 | 156 | ||
134 | ponderable.setVelocityX(0); | 157 | ponderable.setVelocityX(0); |
135 | 158 | ||
136 | animatable.setWalking(false); | 159 | if (ponderable.getState() == PonderableComponent::state::grounded) |
160 | { | ||
161 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); | ||
162 | |||
163 | if (orientable.isFacingRight()) | ||
164 | { | ||
165 | animating.startAnimation(entity, "stillRight"); | ||
166 | } else { | ||
167 | animating.startAnimation(entity, "stillLeft"); | ||
168 | } | ||
169 | } | ||
137 | } | 170 | } |
138 | 171 | ||
139 | void ControllingSystem::jump(int entity) | 172 | void ControllingSystem::jump(id_type entity) |
140 | { | 173 | { |
141 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | 174 | auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); |
142 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | 175 | |
143 | 176 | if (ponderable.getState() == PonderableComponent::state::grounded) | |
144 | playSound("res/Randomize87.wav", 0.25); | 177 | { |
145 | 178 | playSound("res/Randomize87.wav", 0.25); | |
146 | ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3)); | 179 | |
147 | ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3)); | 180 | ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3)); |
148 | 181 | ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3)); | |
149 | animatable.setJumping(true); | 182 | ponderable.setState(PonderableComponent::state::jumping); |
183 | } | ||
150 | } | 184 | } |
151 | 185 | ||
152 | void ControllingSystem::stopJumping(int entity) | 186 | void ControllingSystem::stopJumping(id_type entity) |
153 | { | 187 | { |
154 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | 188 | auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); |
155 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | 189 | |
156 | 190 | if (ponderable.getState() == PonderableComponent::state::jumping) | |
157 | ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); | 191 | { |
158 | animatable.setJumping(false); | 192 | ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); |
193 | ponderable.setState(PonderableComponent::state::falling); | ||
194 | } | ||
159 | } | 195 | } |
160 | 196 | ||
161 | void ControllingSystem::drop(int entity, bool start) | 197 | void ControllingSystem::drop(id_type entity, bool start) |
162 | { | 198 | { |
163 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | 199 | auto& droppable = game_.getEntityManager().getComponent<DroppableComponent>(entity); |
164 | auto& droppable = game.getEntityManager().getComponent<DroppableComponent>(entity); | 200 | auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); |
165 | 201 | ||
202 | if (start && (ponderable.getState() == PonderableComponent::state::grounded)) | ||
203 | { | ||
204 | ponderable.setState(PonderableComponent::state::dropping); | ||
205 | } else if ((!start) && (ponderable.getState() == PonderableComponent::state::dropping)) | ||
206 | { | ||
207 | ponderable.setState(PonderableComponent::state::grounded); | ||
208 | } | ||
166 | droppable.setDroppable(start); | 209 | droppable.setDroppable(start); |
167 | animatable.setCrouching(start); | ||
168 | } | 210 | } |
diff --git a/src/systems/controlling.h b/src/systems/controlling.h index 30210b3..1f1e8a0 100644 --- a/src/systems/controlling.h +++ b/src/systems/controlling.h | |||
@@ -3,24 +3,28 @@ | |||
3 | 3 | ||
4 | #include "system.h" | 4 | #include "system.h" |
5 | #include <queue> | 5 | #include <queue> |
6 | #include "entity_manager.h" | ||
6 | 7 | ||
7 | class ControllingSystem : public System { | 8 | class ControllingSystem : public System { |
8 | public: | 9 | public: |
9 | ControllingSystem(Game& game) | 10 | |
10 | : System(game) {} | 11 | ControllingSystem(Game& game) : System(game) |
11 | 12 | { | |
12 | void tick(double dt); | 13 | } |
13 | void input(int key, int action); | 14 | |
14 | 15 | void tick(double dt); | |
15 | private: | 16 | void input(int key, int action); |
16 | void walkLeft(int entity); | 17 | |
17 | void walkRight(int entity); | 18 | private: |
18 | void stopWalking(int entity); | 19 | |
19 | void jump(int entity); | 20 | void walkLeft(id_type entity); |
20 | void stopJumping(int entity); | 21 | void walkRight(id_type entity); |
21 | void drop(int entity, bool start); | 22 | void stopWalking(id_type entity); |
22 | 23 | void jump(id_type entity); | |
23 | std::queue<std::pair<int,int>> actions; | 24 | void stopJumping(id_type entity); |
25 | void drop(id_type entity, bool start); | ||
26 | |||
27 | std::queue<std::pair<int,int>> actions_; | ||
24 | }; | 28 | }; |
25 | 29 | ||
26 | #endif /* end of include guard: CONTROLLING_H_80B1BB8D */ | 30 | #endif /* end of include guard: CONTROLLING_H_80B1BB8D */ |
diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 50a8bc8..e40db1d 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp | |||
@@ -5,12 +5,14 @@ | |||
5 | 5 | ||
6 | void PonderingSystem::tick(double dt) | 6 | void PonderingSystem::tick(double dt) |
7 | { | 7 | { |
8 | auto entities = game.getEntityManager().getEntitiesWithComponents<PonderableComponent, TransformableComponent>(); | 8 | auto entities = game_.getEntityManager().getEntitiesWithComponents< |
9 | PonderableComponent, | ||
10 | TransformableComponent>(); | ||
9 | 11 | ||
10 | for (auto entity : entities) | 12 | for (id_type entity : entities) |
11 | { | 13 | { |
12 | auto& transformable = game.getEntityManager().getComponent<TransformableComponent>(entity); | 14 | auto& transformable = game_.getEntityManager().getComponent<TransformableComponent>(entity); |
13 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | 15 | auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); |
14 | 16 | ||
15 | // Accelerate | 17 | // Accelerate |
16 | ponderable.setVelocityX(ponderable.getVelocityX() + ponderable.getAccelX() * dt); | 18 | ponderable.setVelocityX(ponderable.getVelocityX() + ponderable.getAccelX() * dt); |
diff --git a/src/systems/pondering.h b/src/systems/pondering.h index 3fe5473..44e7600 100644 --- a/src/systems/pondering.h +++ b/src/systems/pondering.h | |||
@@ -4,11 +4,13 @@ | |||
4 | #include "system.h" | 4 | #include "system.h" |
5 | 5 | ||
6 | class PonderingSystem : public System { | 6 | class PonderingSystem : public System { |
7 | public: | 7 | public: |
8 | PonderingSystem(Game& game) | ||
9 | : System(game) {} | ||
10 | 8 | ||
11 | void tick(double dt); | 9 | PonderingSystem(Game& game) : System(game) |
10 | { | ||
11 | } | ||
12 | |||
13 | void tick(double dt); | ||
12 | }; | 14 | }; |
13 | 15 | ||
14 | #endif /* end of include guard: PONDERING_H_F2530E0E */ | 16 | #endif /* end of include guard: PONDERING_H_F2530E0E */ |
diff --git a/src/systems/rendering.cpp b/src/systems/rendering.cpp index 251c2bc..8219732 100644 --- a/src/systems/rendering.cpp +++ b/src/systems/rendering.cpp | |||
@@ -3,19 +3,35 @@ | |||
3 | #include "components/animatable.h" | 3 | #include "components/animatable.h" |
4 | #include "components/transformable.h" | 4 | #include "components/transformable.h" |
5 | 5 | ||
6 | void RenderingSystem::tick(double dt) | 6 | void RenderingSystem::tick(double) |
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 = game.getEntityManager().getEntitiesWithComponents<AnimatableComponent, TransformableComponent>(); | 10 | std::set<id_type> spriteEntities = |
11 | for (int entity : spriteEntities) | 11 | game_.getEntityManager().getEntitiesWithComponents< |
12 | AnimatableComponent, | ||
13 | TransformableComponent>(); | ||
14 | |||
15 | for (id_type entity : spriteEntities) | ||
12 | { | 16 | { |
13 | auto& sprite = game.getEntityManager().getComponent<AnimatableComponent>(entity); | 17 | auto& sprite = game_.getEntityManager(). |
14 | auto& transform = game.getEntityManager().getComponent<TransformableComponent>(entity); | 18 | getComponent<AnimatableComponent>(entity); |
15 | Rectangle dstrect {(int) transform.getX(), (int) transform.getY(), transform.getW(), transform.getH()}; | 19 | |
16 | 20 | auto& transform = game_.getEntityManager(). | |
17 | texture.blit(sprite.getTexture(), sprite.getFrameRect(), dstrect); | 21 | getComponent<TransformableComponent>(entity); |
22 | |||
23 | Rectangle dstrect { | ||
24 | static_cast<int>(transform.getX()), | ||
25 | static_cast<int>(transform.getY()), | ||
26 | transform.getW(), | ||
27 | transform.getH()}; | ||
28 | |||
29 | const AnimationSet& aset = sprite.getAnimationSet(); | ||
30 | texture_.blit( | ||
31 | aset.getTexture(), | ||
32 | aset.getFrameRect(sprite.getFrame()), | ||
33 | dstrect); | ||
18 | } | 34 | } |
19 | 35 | ||
20 | texture.renderScreen(); | 36 | texture_.renderScreen(); |
21 | } | 37 | } |
diff --git a/src/systems/rendering.h b/src/systems/rendering.h index cec72e2..a53ee64 100644 --- a/src/systems/rendering.h +++ b/src/systems/rendering.h | |||
@@ -6,14 +6,17 @@ | |||
6 | #include "consts.h" | 6 | #include "consts.h" |
7 | 7 | ||
8 | class RenderingSystem : public System { | 8 | class RenderingSystem : public System { |
9 | public: | 9 | public: |
10 | RenderingSystem(Game& game) | ||
11 | : System(game) {} | ||
12 | 10 | ||
13 | void tick(double dt); | 11 | RenderingSystem(Game& game) : System(game) |
12 | { | ||
13 | } | ||
14 | 14 | ||
15 | private: | 15 | void tick(double dt); |
16 | Texture texture {GAME_WIDTH, GAME_HEIGHT}; | 16 | |
17 | private: | ||
18 | |||
19 | Texture texture_ {GAME_WIDTH, GAME_HEIGHT}; | ||
17 | }; | 20 | }; |
18 | 21 | ||
19 | #endif /* end of include guard: RENDERING_H_76ABC02A */ | 22 | #endif /* end of include guard: RENDERING_H_76ABC02A */ |