From cefe66cdbb8786dc455657376e36f0ff8785d5bc Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 8 Feb 2018 12:34:42 -0500 Subject: Introduced animated sprites Also restyled a lot of the code. --- src/systems/animating.cpp | 38 ++++++++++++ src/systems/animating.h | 20 +++++++ src/systems/controlling.cpp | 138 +++++++++++++++++++++++++++++--------------- src/systems/controlling.h | 36 +++++++----- src/systems/pondering.cpp | 10 ++-- src/systems/pondering.h | 10 ++-- src/systems/rendering.cpp | 40 +++++++++---- src/systems/rendering.h | 15 +++-- 8 files changed, 217 insertions(+), 90 deletions(-) create mode 100644 src/systems/animating.cpp create mode 100644 src/systems/animating.h (limited to 'src/systems') 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 @@ +#include "animating.h" +#include "game.h" +#include "components/animatable.h" + +void AnimatingSystem::tick(double) +{ + std::set spriteEntities = + game_.getEntityManager().getEntitiesWithComponents(); + + for (id_type entity : spriteEntities) + { + auto& sprite = game_.getEntityManager(). + getComponent(entity); + + sprite.setCountdown(sprite.getCountdown() + 1); + + const Animation& anim = sprite.getAnimation(); + if (sprite.getCountdown() >= anim.getDelay()) + { + sprite.setFrame(sprite.getFrame() + 1); + sprite.setCountdown(0); + + if (sprite.getFrame() >= anim.getFirstFrame() + anim.getNumFrames()) + { + sprite.setFrame(anim.getFirstFrame()); + } + } + } +} + +void AnimatingSystem::startAnimation(id_type entity, std::string animation) +{ + auto& sprite = game_.getEntityManager(). + getComponent(entity); + + sprite.setAnimation(animation); + sprite.setFrame(sprite.getAnimation().getFirstFrame()); +} 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 @@ +#ifndef ANIMATING_H_5BBF0094 +#define ANIMATING_H_5BBF0094 + +#include "system.h" +#include + +class AnimatingSystem : public System { +public: + + AnimatingSystem(Game& game) : System(game) + { + } + + void tick(double dt); + + void startAnimation(id_type entity, std::string animation); + +}; + +#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 @@ #include "components/ponderable.h" #include "components/animatable.h" #include "components/droppable.h" +#include "components/orientable.h" +#include "systems/animating.h" #include "direction.h" #include "muxer.h" #include "consts.h" -void ControllingSystem::tick(double dt) +void ControllingSystem::tick(double) { - while (!actions.empty()) + while (!actions_.empty()) { - int key = actions.front().first; - int action = actions.front().second; + int key = actions_.front().first; + int action = actions_.front().second; + + auto entities = game_.getEntityManager().getEntitiesWithComponents< + ControllableComponent, + PonderableComponent, + AnimatableComponent, + DroppableComponent, + OrientableComponent>(); - auto entities = game.getEntityManager().getEntitiesWithComponents(); for (auto entity : entities) { - auto& controllable = game.getEntityManager().getComponent(entity); + auto& controllable = game_.getEntityManager(). + getComponent(entity); if (action == GLFW_PRESS) { @@ -95,74 +104,107 @@ void ControllingSystem::tick(double dt) } } - actions.pop(); + actions_.pop(); } } void ControllingSystem::input(int key, int action) { - actions.push(std::make_pair(key, action)); + actions_.push(std::make_pair(key, action)); } -void ControllingSystem::walkLeft(int entity) +void ControllingSystem::walkLeft(id_type entity) { - auto& ponderable = game.getEntityManager().getComponent(entity); - auto& animatable = game.getEntityManager().getComponent(entity); - + auto& ponderable = game_.getEntityManager().getComponent(entity); + auto& orientable = game_.getEntityManager().getComponent(entity); + + orientable.setFacingRight(false); ponderable.setVelocityX(-90); - - animatable.setDirection(Direction::Left); - animatable.setWalking(true); + + auto& animating = game_.getSystemManager().getSystem(); + + if (ponderable.getState() == PonderableComponent::state::grounded) + { + animating.startAnimation(entity, "walkingLeft"); + } else { + animating.startAnimation(entity, "stillLeft"); + } } -void ControllingSystem::walkRight(int entity) +void ControllingSystem::walkRight(id_type entity) { - auto& ponderable = game.getEntityManager().getComponent(entity); - auto& animatable = game.getEntityManager().getComponent(entity); - + auto& ponderable = game_.getEntityManager().getComponent(entity); + auto& orientable = game_.getEntityManager().getComponent(entity); + + orientable.setFacingRight(true); ponderable.setVelocityX(90); - animatable.setDirection(Direction::Right); - animatable.setWalking(true); + auto& animating = game_.getSystemManager().getSystem(); + + if (ponderable.getState() == PonderableComponent::state::grounded) + { + animating.startAnimation(entity, "walkingRight"); + } else { + animating.startAnimation(entity, "stillRight"); + } } -void ControllingSystem::stopWalking(int entity) +void ControllingSystem::stopWalking(id_type entity) { - auto& ponderable = game.getEntityManager().getComponent(entity); - auto& animatable = game.getEntityManager().getComponent(entity); - + auto& ponderable = game_.getEntityManager().getComponent(entity); + auto& orientable = game_.getEntityManager().getComponent(entity); + ponderable.setVelocityX(0); - - animatable.setWalking(false); + + if (ponderable.getState() == PonderableComponent::state::grounded) + { + auto& animating = game_.getSystemManager().getSystem(); + + if (orientable.isFacingRight()) + { + animating.startAnimation(entity, "stillRight"); + } else { + animating.startAnimation(entity, "stillLeft"); + } + } } -void ControllingSystem::jump(int entity) +void ControllingSystem::jump(id_type entity) { - auto& ponderable = game.getEntityManager().getComponent(entity); - auto& animatable = game.getEntityManager().getComponent(entity); - - playSound("res/Randomize87.wav", 0.25); - - ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3)); - ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3)); - - animatable.setJumping(true); + auto& ponderable = game_.getEntityManager().getComponent(entity); + + if (ponderable.getState() == PonderableComponent::state::grounded) + { + playSound("res/Randomize87.wav", 0.25); + + ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3)); + ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3)); + ponderable.setState(PonderableComponent::state::jumping); + } } -void ControllingSystem::stopJumping(int entity) +void ControllingSystem::stopJumping(id_type entity) { - auto& ponderable = game.getEntityManager().getComponent(entity); - auto& animatable = game.getEntityManager().getComponent(entity); - - ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); - animatable.setJumping(false); + auto& ponderable = game_.getEntityManager().getComponent(entity); + + if (ponderable.getState() == PonderableComponent::state::jumping) + { + ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); + ponderable.setState(PonderableComponent::state::falling); + } } -void ControllingSystem::drop(int entity, bool start) +void ControllingSystem::drop(id_type entity, bool start) { - auto& animatable = game.getEntityManager().getComponent(entity); - auto& droppable = game.getEntityManager().getComponent(entity); - + auto& droppable = game_.getEntityManager().getComponent(entity); + auto& ponderable = game_.getEntityManager().getComponent(entity); + + if (start && (ponderable.getState() == PonderableComponent::state::grounded)) + { + ponderable.setState(PonderableComponent::state::dropping); + } else if ((!start) && (ponderable.getState() == PonderableComponent::state::dropping)) + { + ponderable.setState(PonderableComponent::state::grounded); + } droppable.setDroppable(start); - animatable.setCrouching(start); } 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 @@ #include "system.h" #include +#include "entity_manager.h" class ControllingSystem : public System { - public: - ControllingSystem(Game& game) - : System(game) {} - - void tick(double dt); - void input(int key, int action); - - private: - void walkLeft(int entity); - void walkRight(int entity); - void stopWalking(int entity); - void jump(int entity); - void stopJumping(int entity); - void drop(int entity, bool start); - - std::queue> actions; +public: + + ControllingSystem(Game& game) : System(game) + { + } + + void tick(double dt); + void input(int key, int action); + +private: + + void walkLeft(id_type entity); + void walkRight(id_type entity); + void stopWalking(id_type entity); + void jump(id_type entity); + void stopJumping(id_type entity); + void drop(id_type entity, bool start); + + std::queue> actions_; }; #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 @@ void PonderingSystem::tick(double dt) { - auto entities = game.getEntityManager().getEntitiesWithComponents(); + auto entities = game_.getEntityManager().getEntitiesWithComponents< + PonderableComponent, + TransformableComponent>(); - for (auto entity : entities) + for (id_type entity : entities) { - auto& transformable = game.getEntityManager().getComponent(entity); - auto& ponderable = game.getEntityManager().getComponent(entity); + auto& transformable = game_.getEntityManager().getComponent(entity); + auto& ponderable = game_.getEntityManager().getComponent(entity); // Accelerate 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 @@ #include "system.h" class PonderingSystem : public System { - public: - PonderingSystem(Game& game) - : System(game) {} +public: - void tick(double dt); + PonderingSystem(Game& game) : System(game) + { + } + + void tick(double dt); }; #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 @@ #include "components/animatable.h" #include "components/transformable.h" -void RenderingSystem::tick(double dt) +void RenderingSystem::tick(double) { - texture.fill(texture.entirety(), 0, 0, 0); - - std::set spriteEntities = game.getEntityManager().getEntitiesWithComponents(); - for (int entity : spriteEntities) + texture_.fill(texture_.entirety(), 0, 0, 0); + + std::set spriteEntities = + game_.getEntityManager().getEntitiesWithComponents< + AnimatableComponent, + TransformableComponent>(); + + for (id_type entity : spriteEntities) { - auto& sprite = game.getEntityManager().getComponent(entity); - auto& transform = game.getEntityManager().getComponent(entity); - Rectangle dstrect {(int) transform.getX(), (int) transform.getY(), transform.getW(), transform.getH()}; - - texture.blit(sprite.getTexture(), sprite.getFrameRect(), dstrect); + auto& sprite = game_.getEntityManager(). + getComponent(entity); + + auto& transform = game_.getEntityManager(). + getComponent(entity); + + Rectangle dstrect { + static_cast(transform.getX()), + static_cast(transform.getY()), + transform.getW(), + transform.getH()}; + + const AnimationSet& aset = sprite.getAnimationSet(); + texture_.blit( + aset.getTexture(), + aset.getFrameRect(sprite.getFrame()), + dstrect); } - - texture.renderScreen(); + + texture_.renderScreen(); } 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 @@ #include "consts.h" class RenderingSystem : public System { - public: - RenderingSystem(Game& game) - : System(game) {} +public: - void tick(double dt); + RenderingSystem(Game& game) : System(game) + { + } - private: - Texture texture {GAME_WIDTH, GAME_HEIGHT}; + void tick(double dt); + +private: + + Texture texture_ {GAME_WIDTH, GAME_HEIGHT}; }; #endif /* end of include guard: RENDERING_H_76ABC02A */ -- cgit 1.4.1