Skip to content

Commit

Permalink
feat: animation
Browse files Browse the repository at this point in the history
  • Loading branch information
phuwit committed Oct 17, 2023
1 parent 31aae79 commit 1564430
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 31 deletions.
21 changes: 17 additions & 4 deletions source/Player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,21 @@ void Player::setSpeedReset() {
void Player::update(Vector2f mousePosition, Time frameTime) {
// movement
// detect diagonals
int buttonPressed = 0;
m_MovementButtonPressed = 0;
for (unsigned int i = 0; i < (unsigned int)(sizeof(MovementKey)); i++) {
if (m_MovementKeyPressed[i] == true) {
buttonPressed++;
m_MovementButtonPressed++;
}
}

// actually move
if (buttonPressed == 1) {
if (m_MovementButtonPressed == 1) {
if (m_MovementKeyPressed[MovementKey::MOVEMENT_LEFT]) m_Position.x -= m_Speed * frameTime.asSeconds();
if (m_MovementKeyPressed[MovementKey::MOVEMENT_RIGHT]) m_Position.x += m_Speed * frameTime.asSeconds();
if (m_MovementKeyPressed[MovementKey::MOVEMENT_UP]) m_Position.y -= m_Speed * frameTime.asSeconds();
if (m_MovementKeyPressed[MovementKey::MOVEMENT_DOWN]) m_Position.y += m_Speed * frameTime.asSeconds();
}
else if (buttonPressed > 1) {
else if (m_MovementButtonPressed > 1) {
if (m_MovementKeyPressed[MovementKey::MOVEMENT_LEFT]) m_Position.x -= m_SpeedDiagonal * frameTime.asSeconds();
if (m_MovementKeyPressed[MovementKey::MOVEMENT_RIGHT]) m_Position.x += m_SpeedDiagonal * frameTime.asSeconds();
if (m_MovementKeyPressed[MovementKey::MOVEMENT_UP]) m_Position.y -= m_SpeedDiagonal * frameTime.asSeconds();
Expand Down Expand Up @@ -132,6 +132,19 @@ void Player::update(Vector2f mousePosition, Time frameTime) {
m_SetSpritesPosition();
}

void Player::animate() {
if (m_MovementButtonPressed > 0) {
m_Base.setTexture(TextureHolder::GetTexture("assets/sprites/player/base/Run2.png"));
} else {
m_Base.setTexture(TextureHolder::GetTexture("assets/sprites/player/base/Idle2.png"));
}
IntRect newCrop = IntRect(m_Base.getTextureRect().left + SPRITE_SIZE, m_TEXTURE_SHEET_OFFSET.top, m_TEXTURE_SHEET_OFFSET.width, m_TEXTURE_SHEET_OFFSET.height);
if ((unsigned int)newCrop.left > m_Base.getTexture()->getSize().x) {
newCrop.left = m_TEXTURE_SHEET_OFFSET.left;
}
m_Base.setTextureRect(newCrop);
}

void Player::m_SetSpritesPosition() {
m_Base.setPosition(m_Position);
m_Arm.setPosition(Vector2f(m_Position.x - m_ARM_BASE_OFFSET.x, m_Position.y - m_ARM_BASE_OFFSET.y));
Expand Down
3 changes: 3 additions & 0 deletions source/Player/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using namespace sf;
class Player {
private:
const IntRect m_TEXTURE_SHEET_OFFSET = IntRect(3, 13, 20, 35);
const int SPRITE_SIZE = 48;
// Texture textureBase, textureArm, textureGun;
float m_SpriteScaling = 1;
// const Vector2f m_ARM_BASE_OFFSET = Vector2f(10.5 * m_SpriteScaling, 29.5 * m_SpriteScaling);
Expand All @@ -20,6 +21,7 @@ class Player {
float m_SpeedDiagonal = m_Speed * std::sqrt(0.5);

bool m_MovementKeyPressed[4] = {false};
int m_MovementButtonPressed = 0;
Vector2f m_Position;
void m_SetSpritesPosition();
float m_ArmAngle{};
Expand Down Expand Up @@ -50,4 +52,5 @@ class Player {
void setMovementKeyPressed(int movementKey, bool isPressed);

void update(Vector2f mousePosition, Time deltaTime);
void animate();
};
13 changes: 11 additions & 2 deletions source/Scenes/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ SceneChange Game::run(RenderWindow &window) {

// UPDATE FRAME
Time frameTime = m_FrameTimeClock.restart();
m_AnimationTimer += frameTime;

if (frameTime > seconds(2)) {
continue;
Expand Down Expand Up @@ -219,7 +220,7 @@ SceneChange Game::run(RenderWindow &window) {
Vector2f(m_HealthBarSegmentSize * m_PlayerHealth, m_HealthBar.getSize().y));
if (m_PlayerHealth <= 0) {
// game over idk
return SceneChange(ScenesList::SCENE_GAMEOVER);
return {ScenesList::SCENE_GAMEOVER};
}
}
}
Expand Down Expand Up @@ -333,6 +334,14 @@ SceneChange Game::run(RenderWindow &window) {
m_Player.setMovementKeyPressed(i, m_MovementKeyPressed[i]);
}

if (m_AnimationTimer > M_TIME_BETWEEN_ANIMATION) {
m_Player.animate();
for (int i = 0; i < m_NumZombies; i++) {
m_Zombies[i].animate();
}
m_AnimationTimer = seconds(0);
}

// DRAW SCENE
window.clear(COLOR_BACKGROUND);

Expand All @@ -343,7 +352,7 @@ SceneChange Game::run(RenderWindow &window) {

for(int i = 0; i < m_NumZombies; i++) {
window.draw(m_Zombies[i].getSprite());
// window.draw(m_Zombies[i].getDrawableHitbox());
// window.draw(m_Zombies[i].getDrawableHitbox());
}

for (unsigned int i = 0; i < (unsigned int)sizeof(PickupsType); i++) {
Expand Down
3 changes: 3 additions & 0 deletions source/Scenes/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class Game : public Scenes {
Zombie* m_Zombies = nullptr;
const Time M_SCORE_BONUS_MAX_TIME = seconds(5);

const Time M_TIME_BETWEEN_ANIMATION = milliseconds(100);
Time m_AnimationTimer;

void handlePickUps_(PickupsType pickUpsType, int pickupValue, Time buffDuration);
void removeBuff_(PickupsType pickUpsType);
public:
Expand Down
6 changes: 3 additions & 3 deletions source/Scenes/GameOver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class GameOver : public Scenes {
SceneChange GameOver::run(RenderWindow &window) {
Font fontBebas = FontHolder::GetFont("assets/fonts/BebasNeue-Regular.otf");

Text gameOverText("Game Over", fontBebas);
Text gameOverText("Game Over!", fontBebas);
gameOverText.setFillColor(Color::White);
gameOverText.setCharacterSize(72);
gameOverText.setCharacterSize(80);
gameOverText.setStyle(Text::Bold);
gameOverText.setPosition(Vector2f(window.getSize().x / 2, 150));
textSetOriginCenter(gameOverText);
Expand All @@ -40,7 +40,7 @@ SceneChange GameOver::run(RenderWindow &window) {
enterYourNameText.setCharacterSize(56);
enterYourNameText.setStyle(Text::Bold);
textSetOriginCenter(enterYourNameText);
enterYourNameText.setPosition(Vector2f(window.getSize().x / 2, 300));
enterYourNameText.setPosition(Vector2f(window.getSize().x / 2, 500));

string nameString = "";

Expand Down
62 changes: 41 additions & 21 deletions source/Zombie/Zombie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Zombie.hpp"
#include "../CommonEnum.hpp"
#include "../Holders/TextureHolder.hpp"
#include "../Tools/SetOriginCenter.cpp"

using namespace std;

Expand All @@ -19,7 +20,7 @@ void Zombie::spawn(Vector2f spawnLoaction, float spriteScaling, ZombieType type,
// Vector2f textureSize = (Vector2f)texture.getSize();
m_Sprite = Sprite(texture, M_SPRITE_RUN_CROP[m_Type]);
m_Sprite.setScale(Vector2f(m_SpriteScaling, m_SpriteScaling));
m_CenterOffset = Vector2f((m_Sprite.getLocalBounds().width * m_SpriteScaling) / 2, ((m_Sprite.getLocalBounds().height * m_SpriteScaling) / 2));
// m_CenterOffset = Vector2f((m_Sprite.getLocalBounds().width * m_SpriteScaling) / 2, ((m_Sprite.getLocalBounds().height * m_SpriteScaling) / 2));
m_Health = M_HEALTH_BASE[m_Type];
m_Sprite.setColor(M_COLOR_BASE[m_Type]);

Expand All @@ -38,7 +39,8 @@ void Zombie::spawn(Vector2f spawnLoaction, float spriteScaling, ZombieType type,
m_PlayArea = playArea;
m_MoveStyle = moveStyle;
// set origin to center
m_Sprite.setOrigin(m_CenterOffset);
// m_Sprite.setOrigin(m_CenterOffset);
spriteSetOriginCenter(m_Sprite);
// set position
m_Sprite.setPosition(m_Position);
// set bounds size
Expand Down Expand Up @@ -67,12 +69,13 @@ bool Zombie::isAlive() {
}

FloatRect Zombie::getHitBox() {
FloatRect bounds(
m_Position.x - (m_CenterOffset.x * m_SpriteScaling),
m_Position.y - (m_CenterOffset.y * m_SpriteScaling),
m_Bounds.width * m_SpriteScaling,
m_Bounds.height * m_SpriteScaling);
return bounds;
// FloatRect bounds(
// m_Position.x - (m_CenterOffset.x * m_SpriteScaling),
// m_Position.y - (m_CenterOffset.y * m_SpriteScaling),
// m_Bounds.width * m_SpriteScaling,
// m_Bounds.height * m_SpriteScaling);
// return bounds;
return m_Sprite.getGlobalBounds();
}

RectangleShape Zombie::getDrawableHitbox() {
Expand Down Expand Up @@ -137,12 +140,12 @@ void Zombie::update(Time frameTime, Vector2f playerLocation) {
moveY = true;
}

// if (distanceX < 0) {
// m_Sprite.setScale(Vector2f(-m_SpriteScaling, m_SpriteScaling));
// }
// else {
// m_Sprite.setScale(Vector2f(m_SpriteScaling, m_SpriteScaling));
// }
if (distanceX < 0) {
m_Sprite.setScale(Vector2f(-m_SpriteScaling, m_SpriteScaling));
}
else {
m_Sprite.setScale(Vector2f(m_SpriteScaling, m_SpriteScaling));
}


if (m_MoveStyle == ZombieMoveStyle::ZOMBIE_MOVESTYLE_XFIRST) {
Expand Down Expand Up @@ -178,13 +181,6 @@ void Zombie::update(Time frameTime, Vector2f playerLocation) {
}

// flip zombie if facing in -x direction
// if ((playerLocation.x - m_Position.x ) < 0) {
// m_Sprite.setScale(Vector2f(-m_SpriteScaling, m_SpriteScaling));
// }
// else {
// m_Sprite.setScale(Vector2f(-m_SpriteScaling, m_SpriteScaling));
// }

// set new position
m_Sprite.setPosition(m_Position);

Expand All @@ -195,3 +191,27 @@ void Zombie::update(Time frameTime, Vector2f playerLocation) {
// m_Sprite.setRotation(angle);
}
}

//void Player::animate() {
// if (m_MovementButtonPressed > 0) {
// m_Base.setTexture(TextureHolder::GetTexture("assets/sprites/player/base/Run2.png"));
// } else {
// m_Base.setTexture(TextureHolder::GetTexture("assets/sprites/player/base/Idle2.png"));
// }
// IntRect newCrop = IntRect(m_Base.getTextureRect().left + SPRITE_SIZE, m_TEXTURE_SHEET_OFFSET.top, m_TEXTURE_SHEET_OFFSET.width, m_TEXTURE_SHEET_OFFSET.height);
// if ((unsigned int)newCrop.left > m_Base.getTexture()->getSize().x) {
// newCrop.left = m_TEXTURE_SHEET_OFFSET.left;
// }
// m_Base.setTextureRect(newCrop);
//}

void Zombie::animate() {
if (m_Alive) {
IntRect newCrop = IntRect(m_Sprite.getTextureRect().left + 96, m_Sprite.getTextureRect().top,
m_Sprite.getTextureRect().width, m_Sprite.getTextureRect().height);
if ((unsigned int) newCrop.left >= m_Sprite.getTexture()->getSize().x) {
newCrop.left = 0;
}
m_Sprite.setTextureRect(newCrop);
}
}
4 changes: 3 additions & 1 deletion source/Zombie/Zombie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class Zombie {
const Color M_COLOR_BASE[sizeof(ZombieType)] = {Color(255, 255, 0, 255), Color(0, 255, 255, 255), Color(255, 0, 255, 255)};
const String M_SPRITE_RUN_FILENAME[sizeof(ZombieType)] = {"assets/sprites/zombies/wild/Run.png", "assets/sprites/zombies/male/Run.png", "assets/sprites/zombies/female/Run.png"};
const String M_SPRITE_DEATH_FILENAME[sizeof(ZombieType)] = {"assets/sprites/zombies/wild/Dead.png", "assets/sprites/zombies/male/Dead.png","assets/sprites/zombies/female/Dead.png"};
const IntRect M_SPRITE_RUN_CROP[sizeof(ZombieType)] = {IntRect(26 + 96, 40, 50, 56), IntRect(18, 32, 68, 64) , IntRect(31, 33, 57, 63)};
// const IntRect M_SPRITE_RUN_CROP[sizeof(ZombieType)] = {IntRect(26, 40, 50, 56), IntRect(18, 32, 68, 64) , IntRect(31, 33, 57, 63)};
const IntRect M_SPRITE_RUN_CROP[sizeof(ZombieType)] = {IntRect(0, 0, 96, 96), IntRect(0, 0, 96, 96) , IntRect(0, 0, 96, 96)};
const IntRect M_SPRITE_DEATH_CROP[sizeof(ZombieType)] = {IntRect(406, 86, 74, 10), IntRect(416, 86, 64, 10) , IntRect(389, 86, 64, 10)};
// make each zombie speed varies slightly to prevent bunching up
const int M_MAX_VARIANCE = 30;
Expand Down Expand Up @@ -49,4 +50,5 @@ class Zombie {
Time getTimeSinceSpawned();

void update(Time frameTime, Vector2f playerLocation);
void animate();
};

0 comments on commit 1564430

Please sign in to comment.