mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2026-02-04 08:09:19 +01:00
Add player turning (Finally)
This commit is contained in:
@@ -2,17 +2,21 @@
|
||||
#include "Animation.h"
|
||||
|
||||
#include "utils.h"
|
||||
Animation::Animation(Texture* pTexture, int frames, float frameDuration, Rectf srcRect): m_pTexture(pTexture), m_SrcRect(srcRect), m_Frames(frames) {
|
||||
Animation::Animation(Texture* pTexture, int frames, float frameDuration, Rectf srcRect, bool isLooping): m_pTexture(pTexture), m_SrcRect(srcRect), m_Frames(frames), m_isLooping(isLooping), m_FrameDuration(frameDuration) {
|
||||
|
||||
}
|
||||
void Animation::Update(float elapsedSec) {
|
||||
if (m_isPlaying) {
|
||||
m_FrameTimer -= elapsedSec;
|
||||
if (m_FrameTimer <= 0.0f) {
|
||||
m_FrameTimer = 0.1f;
|
||||
m_FrameTimer = m_FrameDuration;
|
||||
++m_CurrentFrame;
|
||||
if (m_CurrentFrame >= m_Frames) {
|
||||
m_CurrentFrame = 0;
|
||||
if(!m_isLooping) {
|
||||
m_hasPlayedOnce = true;
|
||||
m_isPlaying = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,13 @@
|
||||
class Animation
|
||||
{
|
||||
public:
|
||||
Animation(Texture* pTexture, int frames, float frameDuration, Rectf srcRect);
|
||||
Animation(Texture* pTexture, int frames, float frameDuration, Rectf srcRect, bool isLooping = true);
|
||||
~Animation() = default;
|
||||
|
||||
void Update(float elapsedSec);
|
||||
void Draw(const Vector2f& pos) const;
|
||||
void Draw(const Vector2f& pos, const Rectf& dst) const;
|
||||
|
||||
|
||||
void SetPlaying(bool isPlaying) {
|
||||
m_isPlaying = isPlaying;
|
||||
@@ -18,6 +19,15 @@ public:
|
||||
m_isFlipped = isFlipped;
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
m_CurrentFrame = 0;
|
||||
m_hasPlayedOnce = false;
|
||||
m_isPlaying = true;
|
||||
}
|
||||
bool IsDone() const {
|
||||
return m_hasPlayedOnce && !m_isLooping;
|
||||
}
|
||||
|
||||
private:
|
||||
Texture* m_pTexture;
|
||||
Rectf m_SrcRect;
|
||||
@@ -26,6 +36,11 @@ private:
|
||||
int m_CurrentFrame { 0 };
|
||||
float m_FrameTimer { 0.0f };
|
||||
|
||||
float m_FrameDuration { 0.1f };
|
||||
|
||||
bool m_isPlaying { true };
|
||||
bool m_isFlipped { false };
|
||||
|
||||
bool m_isLooping { true };
|
||||
bool m_hasPlayedOnce { false };
|
||||
};
|
||||
|
||||
@@ -20,8 +20,14 @@ Player::Player(const Vector2f& Position, TextureManager* manager) : m_Position(P
|
||||
m_walkAnimation = new Animation(
|
||||
manager->GetTexture("animations/player/player_walk.png"),
|
||||
8, 0.1f, Rectf { 0, 0, 70, 70 });
|
||||
m_turnAnimation = new Animation(
|
||||
manager->GetTexture("animations/player/player_turn.png"),
|
||||
5, 0.07f, Rectf{ 0, 0, 70, 70 }, false);
|
||||
m_currentAnimation = m_walkAnimation;
|
||||
}
|
||||
Player::Player(Player&& other) {
|
||||
|
||||
}
|
||||
|
||||
Player::~Player() {
|
||||
delete m_walkAnimation;
|
||||
@@ -46,7 +52,7 @@ void Player::Draw() const {
|
||||
float bobOffset = m_BobUp ? 1 : 0;
|
||||
Vector2f drawPos = Vector2f { center.x - halfFrameWidth, center.y - halfFrameWidth + 9 + bobOffset };
|
||||
|
||||
m_walkAnimation->Draw(drawPos, Rectf { drawPos.x, drawPos.y, frameWidth, frameWidth });
|
||||
m_currentAnimation->Draw(drawPos, Rectf { drawPos.x, drawPos.y, frameWidth, frameWidth });
|
||||
utils::DrawEllipse(m_DigDestination, 5, 5);
|
||||
utils::DrawEllipse(m_DigStart, 5, 5);
|
||||
}
|
||||
@@ -69,6 +75,22 @@ void Player::ProcessImGui() {
|
||||
ImGui::Text("Player State %s", currentState.c_str());
|
||||
ImGui::Text("Bob counter: %f", m_BobTimer);
|
||||
ImGui::Text("Bob up: %s", m_BobUp ? "true" : "false");
|
||||
std::string direction {};
|
||||
switch (m_Direction) {
|
||||
case PlayerDirection::Down:
|
||||
direction = "Down";
|
||||
break;
|
||||
case PlayerDirection::Left:
|
||||
direction = "Left";
|
||||
break;
|
||||
case PlayerDirection::Right:
|
||||
direction = "Right";
|
||||
break;
|
||||
case PlayerDirection::Up:
|
||||
direction = "Up";
|
||||
break;
|
||||
}
|
||||
ImGui::Text("Direction: %s", direction.c_str());
|
||||
|
||||
|
||||
//ContactMap
|
||||
@@ -138,8 +160,17 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
||||
}
|
||||
}
|
||||
if (utils::isKeyDown(SDL_SCANCODE_A)) {
|
||||
m_walkAnimation->SetFlipped(false);
|
||||
m_Acc.x = -m_Speed;
|
||||
if(!m_IsTurning) {
|
||||
m_walkAnimation->SetFlipped(false);
|
||||
m_Acc.x = -m_Speed;
|
||||
}
|
||||
if(m_Direction == PlayerDirection::Right) {
|
||||
m_IsTurning = true;
|
||||
m_currentAnimation = m_turnAnimation;
|
||||
m_currentAnimation->SetFlipped(true);
|
||||
m_currentAnimation->Reset();
|
||||
}
|
||||
m_Direction = PlayerDirection::Left;
|
||||
if (m_Grounded && !m_DidJustDigLeft) {
|
||||
//Check if the player doesnt come from digging a tile
|
||||
if (this->CanDig(Collision::CollisionDirection::Left, level)) {
|
||||
@@ -155,8 +186,20 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
||||
}
|
||||
|
||||
if (utils::isKeyDown(SDL_SCANCODE_D)) {
|
||||
m_Acc.x = m_Speed;
|
||||
m_walkAnimation->SetFlipped(true);
|
||||
|
||||
if(!m_IsTurning) {
|
||||
m_Acc.x = m_Speed;
|
||||
m_walkAnimation->SetFlipped(true);
|
||||
}
|
||||
|
||||
if(m_Direction == PlayerDirection::Left) {
|
||||
m_IsTurning = true;
|
||||
m_currentAnimation = m_turnAnimation;
|
||||
m_currentAnimation->SetFlipped(false);
|
||||
m_currentAnimation->Reset();
|
||||
}
|
||||
|
||||
m_Direction = PlayerDirection::Right;
|
||||
if (m_Grounded && !m_DidJustDigRight) {
|
||||
//Check if the player doesnt come from digging a tile
|
||||
if (this->CanDig(Collision::CollisionDirection::Right, level)) {
|
||||
@@ -183,7 +226,12 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
||||
m_Acc = Vector2f { 0, 0 };
|
||||
|
||||
|
||||
m_walkAnimation->Update(elapsedTime);
|
||||
m_currentAnimation->Update(elapsedTime);
|
||||
if(m_currentAnimation->IsDone()) {
|
||||
m_currentAnimation = m_walkAnimation; //TODO: fix this hack
|
||||
m_IsTurning = false;
|
||||
}
|
||||
#pragma region Collision
|
||||
|
||||
|
||||
m_ContactMap[Collision::CollisionDirection::Top] = nullptr;
|
||||
@@ -192,7 +240,6 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
||||
m_ContactMap[Collision::CollisionDirection::Right] = nullptr;
|
||||
m_Grounded = false;
|
||||
|
||||
|
||||
float t = 0, min_t = INFINITY;
|
||||
Vector2f intersectionPoint, normal;
|
||||
|
||||
@@ -254,6 +301,8 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
||||
Collision::CollisionRect rect = world_tile->GetCollisionRect().getCollisionRect(); //TODO: fix this mess
|
||||
Collision::ResolvePlayerVsRect(*this, elapsedTime, &rect);
|
||||
}
|
||||
#pragma endregion
|
||||
|
||||
if (m_State != PlayerState::Digging) { //Fix for when the state is JUST set to digging
|
||||
if (m_Vel.x != 0.0f) {
|
||||
m_State = PlayerState::Walking;
|
||||
|
||||
@@ -10,6 +10,7 @@ enum class PlayerState
|
||||
Idle,
|
||||
Walking,
|
||||
Digging,
|
||||
Turning
|
||||
};
|
||||
|
||||
enum class PlayerDirection
|
||||
@@ -32,7 +33,9 @@ class Player
|
||||
public:
|
||||
explicit Player(const Vector2f& Position, TextureManager* pTextureManager);
|
||||
Player( const Player& other ) = default;
|
||||
Player(Player&& other) = default;
|
||||
Player(Player&& other);
|
||||
Player& operator=(const Player& other) = delete;
|
||||
Player& operator=(Player&& other) = delete;
|
||||
|
||||
~Player();
|
||||
|
||||
@@ -92,6 +95,7 @@ private:
|
||||
const float m_DigTime{ 0.5f };
|
||||
|
||||
bool m_Grounded { false };
|
||||
bool m_IsTurning{ false };
|
||||
|
||||
bool m_DidJustDigRight { false };
|
||||
bool m_DidJustDigLeft { false };
|
||||
@@ -101,7 +105,7 @@ private:
|
||||
Animation* m_digAnimation{ nullptr };
|
||||
|
||||
PlayerState m_State { PlayerState::Idle };
|
||||
PlayerDirection m_Direction { PlayerDirection::Right };
|
||||
PlayerDirection m_Direction { PlayerDirection::Left };
|
||||
DigDirection m_DigDirection { DigDirection::Right };
|
||||
|
||||
//Testing
|
||||
|
||||
Reference in New Issue
Block a user