Add window icon,

Add digging animation (Non functional)
This commit is contained in:
Bram Verhulst
2024-05-02 12:28:03 +02:00
parent 64915567dc
commit b6be73019f
16 changed files with 140 additions and 101 deletions

View File

@@ -19,14 +19,17 @@ 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 });
8, 0.1f, Rectf { 0, 0, 70, 70 }, true);
m_turnAnimation = new Animation(
manager->GetTexture("animations/player/player_turn.png"),
5, 0.07f, Rectf{ 0, 0, 70, 70 }, false);
5, 0.07f, Rectf { 0, 0, 70, 70 }, false);
m_digStartAnimation = new Animation(
manager->GetTexture("animations/player/player_dig_start.png"),
8, 0.1f, Rectf { 0, 0, 70, 70 }, false);
m_currentAnimation = m_walkAnimation;
}
Player::Player(Player&& other) {
}
Player::~Player() {
@@ -50,9 +53,19 @@ void Player::Draw() const {
const int frameWidth = 70; //TODO: fix this
int halfFrameWidth = frameWidth / 2;
float bobOffset = m_BobUp ? 1 : 0;
Vector2f drawPos = Vector2f { center.x - halfFrameWidth, center.y - halfFrameWidth + 9 + bobOffset };
m_currentAnimation->Draw(drawPos, Rectf { drawPos.x, drawPos.y, frameWidth, frameWidth });
float rotateOffset = std::abs(m_Vel.x) / 40;
Vector2f drawPos = Vector2f { center.x, center.y + 9 + bobOffset };
glPushMatrix();
glTranslatef(drawPos.x - halfFrameWidth, drawPos.y - halfFrameWidth, 0);
if (!m_Grounded && std::abs(m_Vel.y) > 0.1f) {
glRotatef(m_Direction == PlayerDirection::Left ? rotateOffset : -rotateOffset, 0, 0, 1);
}
{
m_currentAnimation->Draw(Vector2f { 0, 0 }, Rectf { 0, 0, frameWidth, frameWidth });
}
glPopMatrix();
utils::DrawEllipse(m_DigDestination, 5, 5);
utils::DrawEllipse(m_DigStart, 5, 5);
}
@@ -60,7 +73,7 @@ void Player::ProcessImGui() {
ImGui::Begin("Collision Info", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Text("is Grounded: %s", m_Grounded ? "true" : "false");
ImGui::Checkbox("Draw Collision Rect", &m_DrawCollisionRect);
std::string currentState {};
std::string currentState { "No idea" };
switch (m_State) {
case PlayerState::Idle:
currentState = "Idle";
@@ -71,10 +84,14 @@ void Player::ProcessImGui() {
case PlayerState::Walking:
currentState = "Walking";
break;
case PlayerState::Flying:
currentState = "Flying";
break;
}
ImGui::Text("Player State %s", currentState.c_str());
ImGui::Text("Bob counter: %f", m_BobTimer);
ImGui::Text("Bob up: %s", m_BobUp ? "true" : "false");
ImGui::Text("Is Grounded: %s", m_Grounded ? "true" : "false");
std::string direction {};
switch (m_Direction) {
case PlayerDirection::Down:
@@ -114,8 +131,8 @@ void Player::Dig(Collision::CollisionDirection dir, WorldLevel& level) {
//Center
m_DigDestination.x += tile->GetSize().x / 2 - m_Size.x / 2;
}
if(dir == Collision::Left) {
m_DigDestination += Vector2f{ 2, 0};
if (dir == Collision::Left) {
m_DigDestination += Vector2f { 2, 0 };
}
m_ContactMap[dir] = nullptr;
}
@@ -128,7 +145,8 @@ bool Player::CanDig(Collision::CollisionDirection dir, WorldLevel& level) {
GroundTileType type = *tile->GetTileType();
//TODO: Add a list of non diggable tiles
if (type == GroundTileTypeManager::GetInstance()->HARD_LEFT || type == GroundTileTypeManager::GetInstance()->HARD_MIDDLE || type == GroundTileTypeManager::GetInstance()->HARD_RIGHT) {
if (type == GroundTileTypeManager::GetInstance()->HARD_LEFT || type == GroundTileTypeManager::GetInstance()->HARD_MIDDLE || type == GroundTileTypeManager::GetInstance()->
HARD_RIGHT) {
return false;
}
@@ -144,10 +162,12 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
//check for keys
if (m_State != PlayerState::Digging) {
// m_Acc = Vector2f { 0, -100 };
if (utils::isKeyDown(SDL_SCANCODE_W)) {
m_Vel.y = m_Speed;
m_Grounded = false;
// if (m_Grounded) {
m_State = PlayerState::Flying;
m_Vel.y = m_Speed;
m_Grounded = false;
// }
}
if (utils::isKeyPressed(SDL_SCANCODE_S)) {
if (m_Grounded) {
@@ -155,16 +175,13 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
this->Dig(Collision::CollisionDirection::Bottom, level);
}
}
else {
//m_Acc.y = -100;
}
}
if (utils::isKeyDown(SDL_SCANCODE_A)) {
if(!m_IsTurning) {
if (!m_IsTurning) {
m_walkAnimation->SetFlipped(false);
m_Acc.x = -m_Speed;
}
if(m_Direction == PlayerDirection::Right) {
m_Acc.x = -m_Speed;
if (m_Direction == PlayerDirection::Right) {
m_IsTurning = true;
m_currentAnimation = m_turnAnimation;
m_currentAnimation->SetFlipped(true);
@@ -186,19 +203,18 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
}
if (utils::isKeyDown(SDL_SCANCODE_D)) {
if(!m_IsTurning) {
m_Acc.x = m_Speed;
if (!m_IsTurning) {
m_walkAnimation->SetFlipped(true);
}
m_Acc.x = m_Speed;
if(m_Direction == PlayerDirection::Left) {
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
@@ -216,24 +232,24 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
m_Vel = m_Vel + m_Gravity * elapsedTime;
m_Vel = m_Vel + m_Acc * elapsedTime;
m_Vel = utils::clamp(m_Vel, Vector2f { -m_Speed, -m_Speed }, Vector2f { m_Speed, m_Speed });
//air resistance
//only if not moving
if(abs(m_Acc.x) < 0.1f) {
m_Vel.x = m_Vel.x * 0.85f;
if (abs(m_Acc.x) < 0.1f) {
m_Vel.x = m_Vel.x * 0.90f;
}
m_Acc = Vector2f { 0, 0 };
m_currentAnimation->Update(elapsedTime);
if(m_currentAnimation->IsDone()) {
m_currentAnimation = m_walkAnimation; //TODO: fix this hack
if (m_currentAnimation->IsDone() && m_IsTurning) {
m_currentAnimation = m_walkAnimation;
m_IsTurning = false;
}
#pragma region Collision
m_ContactMap[Collision::CollisionDirection::Top] = nullptr;
m_ContactMap[Collision::CollisionDirection::Bottom] = nullptr;
m_ContactMap[Collision::CollisionDirection::Left] = nullptr;
@@ -259,7 +275,8 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
}
}
std::sort(contactTimes.begin(), contactTimes.end(), [](const std::pair<int, float>& a, const std::pair<int, float>& b) {
std::sort(contactTimes.begin(), contactTimes.end(), [](const std::pair<int, float>& a, const std::pair<int, float>& b)
{
return a.second < b.second;
});
@@ -302,13 +319,13 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
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;
if (m_State == PlayerState::Walking || m_State == PlayerState::Idle) { //Fix for when the state is JUST set to digging
if (std::abs(m_Vel.x) < 0.1f) {
m_State = PlayerState::Idle;
}
else {
m_State = PlayerState::Idle;
m_State = PlayerState::Walking;
}
}
}