Skip to content

Commit

Permalink
Moved the walking animation to the abstarct MovableState class
Browse files Browse the repository at this point in the history
  • Loading branch information
Notiooo committed May 9, 2021
1 parent 5519d12 commit 4024a33
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 53 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Worms Similar Game
The game was created for the project during the 4th semester of studies in computer science at the Silesian University of Technology. The time I spent on the project oscillated around 4 - 5 weeks.
<div align="center">
<a href="http://www.youtube.com/watch?v=KxvEYr_Knc4"><img src="http://img.youtube.com/vi/KxvEYr_Knc4/0.jpg"/></a>
<a href="https://www.youtube.com/watch?v=iI0FcAPFU6o"><img src="http://img.youtube.com/vi/iI0FcAPFU6o/0.jpg"/></a>
<p>A video showing how the game works</p>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Worm::Worm(b2World& world, TextureManager& textures, const FontManager& fonts, s
// ======= Setup the WormStack States ======= //

// Save states of the worms
wormStack.saveState<WormHideState>(State_ID::WormHideState, *this);
wormStack.saveState<WormHideState>(State_ID::WormHideState, *this, textures);
wormStack.saveState<WormPlayState>(State_ID::WormPlayState, *this, textures);
wormStack.saveState<WormWaitState>(State_ID::WormWaitState, *this);
wormStack.saveState<WormHitState>(State_ID::WormHitState, *this, textures);
Expand All @@ -125,7 +125,8 @@ Worm::Worm(b2World& world, TextureManager& textures, const FontManager& fonts, s
void Worm::drawThis(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(ropeSprite, states);


// These two states independently draw the worm in their own way.
if(currentState != State_ID::WormHitState && currentState != State_ID::WormPlayState)
target.draw(wormSprite, states);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include "WormHideState.h"

WormHideState::WormHideState(StateStack& stack, Worm& worm) :
WormMoveableState(stack, worm)
WormHideState::WormHideState(StateStack& stack, Worm& worm, const TextureManager& textures) :
WormMoveableState(stack, worm, textures)
{
#ifdef SHOW_WORM_STATES
worm.setName("HideState");
#endif
}

void WormHideState::draw(sf::RenderTarget&, sf::RenderStates) const
void WormHideState::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
drawMovement(target, states);
}

bool WormHideState::update(sf::Time dt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class WormHideState : public WormMoveableState
{
public:
WormHideState(StateStack&, Worm&);
WormHideState(StateStack& stack, Worm& worm, const TextureManager& textures);

/**
* \brief Draws only this state (current state of the worm) to the passed target
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ WormHitState::WormHitState(StateStack& stack, Worm& worm, TextureManager& textur
}

void WormHitState::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
{
states.transform *= worm.wormSprite.getTransform();
target.draw(hitWorm, states);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "WormMoveableState.h"

WormMoveableState::WormMoveableState(StateStack& stack, Worm& worm) :
WormMoveableState::WormMoveableState(StateStack& stack, Worm& worm, const TextureManager& textures) :
State(stack),
worm(worm),
direction(worm.Worm::facingRight() ? -1 : 1)
direction(worm.Worm::facingRight() ? -1 : 1),
walkingAnimation(textures.getResourceReference(Textures_ID::WormWalking), sf::Vector2i(45, 49), 8, sf::seconds(1))
{
walkingAnimation.setReversing(true);
}

bool WormMoveableState::facingRight() const noexcept
Expand Down Expand Up @@ -55,6 +57,12 @@ void WormMoveableState::handleMovement(const sf::Event& event)
worm.Body->ApplyForceToCenter({ direction * worm.jumpStrength, -3 * worm.jumpStrength }, true);
}
}

// If the player starts moving then the animation should start from the beginning.
if (event.key.code == worm.leftButton || event.key.code == worm.rightButton)
{
walkingAnimation.restartAnimation();
}
}
break;
}
Expand All @@ -72,4 +80,22 @@ void WormMoveableState::updateMovement(sf::Time deltaTime)
if (sf::Keyboard::isKeyPressed(worm.rightButton))
worm.Body->SetLinearVelocity({ worm.movingSpeed, worm.Body->GetLinearVelocity().y });
}

// Update the walking animation
walkingAnimation.update(deltaTime);
}

void WormMoveableState::drawMovement(sf::RenderTarget& target, sf::RenderStates states) const
{
// If the worm does not move at a certain speed or touch the ground, a normal worm is drawn
if (!(direction * worm.Body->GetLinearVelocity().x > animationSpeedThreshold && worm.footCollisions))
{
target.draw(worm.wormSprite, states);
}
// Otherwise, an animated sprite is drawn containing animation of the worm walking
else
{
states.transform *= worm.wormSprite.getTransform();
target.draw(walkingAnimation, states);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define WORMMOVEABLESTATE_H
#include "../../../../States/State.h"
#include "Worm.h"
#include "../../../../Resources/Animation.h"


/**
Expand All @@ -13,7 +14,7 @@
class WormMoveableState : public State
{
protected:
WormMoveableState(StateStack&, Worm&);
WormMoveableState(StateStack& stack, Worm& worm, const TextureManager& textures);

public:

Expand Down Expand Up @@ -61,6 +62,14 @@ class WormMoveableState : public State
*/
void updateMovement(sf::Time deltaTime);


/**
* \brief Draws a walking worm animation when it moves and a standard sprite when it doesn't move.
* \param target where it should be drawn to
* \param states provides information about rendering process (transform, shader, blend mode)
*/
void drawMovement(sf::RenderTarget& target, sf::RenderStates states) const;

/**
* \brief Determines the direction in which the worm is looking.
*
Expand All @@ -72,5 +81,9 @@ class WormMoveableState : public State

Worm& worm;

// === Animation === //
Animation walkingAnimation; //!< Sprite showing animation of the worm walking
float animationSpeedThreshold = worm.movingSpeed / 2.f; //!< Speed at which the walking animation starts

};
#endif
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
#include "WormPlayState.h"

#include <iostream>
#include <memory>

#include "../../../../utils.h"

WormPlayState::WormPlayState(StateStack& stack, Worm& worm, const TextureManager& textures) :
WormMoveableState(stack, worm),
WormMoveableState(stack, worm, textures),
triangularPointer(10.f, 3),
shootingBar({shootingBarSize, 10.f}),
walkingAnimation(textures.getResourceReference(Textures_ID::WormWalking), sf::Vector2i(45, 49), 8, sf::seconds(1))
shootingBar({shootingBarSize, 10.f})
{
#ifdef SHOW_WORM_STATES
worm.setName("PlayState");
#endif // DEBUG

walkingAnimation.setReversing(true);
const auto wormTeamColor = worm.teamColor;

pointer = {std::sin(pointerAngle), std::cos(pointerAngle)};
Expand All @@ -39,26 +35,13 @@ WormPlayState::WormPlayState(StateStack& stack, Worm& worm, const TextureManager
void WormPlayState::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(triangularPointer, states);
drawMovement(target, states);


// Draw the shooting bar only if player is shooting (and if there is enough ammo)
if (currentShootingForce && worm.selectedWeapon->first)
target.draw(shootingBar, states);


// From the given speed, a worm walking animation is drawn
if (direction * worm.Body->GetLinearVelocity().x > animationSpeedThreshold && worm.footCollisions)
{
sf::RenderStates renderstate = states;
renderstate.transform *= worm.wormSprite.getTransform();
target.draw(walkingAnimation, renderstate);
}
// A standard worm is drawn otherwise
else
{
target.draw(worm.wormSprite, states);
}

states.transform *= worm.wormSprite.getTransform();
worm.selectedWeapon->second->rotateWeapon((pointerAngle * 180 / b2_pi) - 90);
worm.selectedWeapon->second->drawThis(target, states);
Expand All @@ -68,8 +51,7 @@ bool WormPlayState::update(sf::Time deltaTime)
{
updateMovement(deltaTime);
updateShooting(deltaTime);
walkingAnimation.update(deltaTime);


return false;
}

Expand Down Expand Up @@ -98,11 +80,7 @@ bool WormPlayState::handleEvent(const sf::Event& event)
// If ANY key is pressed I should
case (sf::Event::KeyPressed):
{
// If the player starts moving then the animation should start from the beginning.
if (event.key.code == worm.leftButton || event.key.code == worm.rightButton)
{
walkingAnimation.restartAnimation();
}

}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "SFML/Graphics/CircleShape.hpp"
#include "SFML/Graphics/RectangleShape.hpp"
#include "WormMoveableState.h"
#include "../../../../Resources/Animation.h"

#include "Weapons/Weapon.h"

Expand All @@ -20,7 +19,7 @@
class WormPlayState : public WormMoveableState
{
public:
WormPlayState(StateStack& stack, Worm& worm, const TextureManager& textures);
WormPlayState(StateStack& stack, Worm& worm, const TextureManager& texturesd);

/**
* \brief Draws only this state (current state of the worm) to the passed target
Expand Down Expand Up @@ -62,11 +61,6 @@ class WormPlayState : public WormMoveableState
*/
void updateShooting(sf::Time deltaTime);


// === Animation === //
Animation walkingAnimation; //!< Sprite showing animation of the worm walking
float animationSpeedThreshold = worm.movingSpeed / 2.f; //!< Speed at which the walking animation starts

//// ====== SHOOTING ====== ////

// === Controls === /
Expand Down
13 changes: 6 additions & 7 deletions Worms-Clone/Worms-Clone/Resources/Maps/main_world.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
0 1438.24 452.763
0 1037.98 643.411
0 398.78 513.98
0 765.269 517.457
1 1030.8 362.649 237 12 3.05176e-05
1 -296.503 369.072 300 21 0
1 1249.76 435.125 259 9 35.8319
Expand All @@ -54,9 +53,9 @@
2 809.897 629.027 21 129 0
3 2870.37 830.042 1953.41 145.995 0
1 2869.39 999.802 1950.91 220.992 0
1 467 215 130.996 58.4985 0
2 548 105 113.496 21 0
4 648 377 245.991 16.0002 0
4 132 195 305.988 198.493 0
4 335 272 53.4987 45.999 0
2 334 201 45.999 53.4987 0
4 940 556 21 218.492 0
0 768 496
4 737 347 210.992 23.4999 0
1 657 245 48.4989 38.4993 0
2 683 131 45.999 40.9992 0
1 375 191 85.9974 50.9988 51.6646
2 changes: 1 addition & 1 deletion Worms-Clone/Worms-Clone/Worms-Clone.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>sfml-main.lib;sfml-graphics-s-d.lib;sfml-window-s-d.lib;sfml-audio-s-d.lib;sfml-system-s-d.lib;opengl32.lib;gdi32.lib;winmm.lib;freetype.lib;box2d.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(SolutionDir)External Libraries\box2d\lib;$(SolutionDir)External Libraries\SFML\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
Expand Down

0 comments on commit 4024a33

Please sign in to comment.