Fix digging

This commit is contained in:
Bram Verhulst
2024-05-07 10:35:18 +02:00
parent 77784a167e
commit d3b932df22
20 changed files with 406 additions and 83 deletions

View File

@@ -25,7 +25,11 @@ Player::Player(const Vector2f& Position, TextureManager* manager) : m_Position(P
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);
7, 0.07f, Rectf { 0, 0, 70, 70 }, false);
m_digAnimation = new Animation(
manager->GetTexture("animations/player/player_dig.png"),
7, 0.05f, Rectf { 0, 0, 70, 70 }, true);
m_currentAnimation = m_walkAnimation;
}
Player::Player(Player&& other) {
@@ -90,6 +94,7 @@ void Player::ProcessImGui() {
break;
}
ImGui::Text("Player State %s", currentState.c_str());
ImGui::Text("Is digging Primed: %s", m_IsDiggingPrimed ? "true" : "false");
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");
@@ -165,14 +170,18 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
if (m_State != PlayerState::Digging) {
if (utils::isKeyDown(SDL_SCANCODE_W)) {
// if (m_Grounded) {
m_State = PlayerState::Flying;
m_Vel.y = m_Speed;
m_Grounded = false;
m_State = PlayerState::Flying;
m_Vel.y = m_Speed;
m_Grounded = false;
// }
}
if (utils::isKeyPressed(SDL_SCANCODE_S)) {
if (m_Grounded) {
if (this->CanDig(Collision::Bottom, level)) {
m_DigDirection = DigDirection::Down;
m_currentAnimation = m_digStartAnimation;
m_currentAnimation->Reset();
m_IsDiggingPrimed = false;
this->Dig(Collision::CollisionDirection::Bottom, level);
}
}
@@ -192,6 +201,7 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
if (m_Grounded && !m_DidJustDigLeft) {
//Check if the player doesnt come from digging a tile
if (this->CanDig(Collision::CollisionDirection::Left, level)) {
m_DigDirection = DigDirection::Left;
this->Dig(Collision::CollisionDirection::Left, level);
m_DidJustDigLeft = true;
}
@@ -220,6 +230,7 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
if (m_Grounded && !m_DidJustDigRight) {
//Check if the player doesnt come from digging a tile
if (this->CanDig(Collision::CollisionDirection::Right, level)) {
m_DigDirection = DigDirection::Right;
this->Dig(Collision::CollisionDirection::Right, level);
m_DidJustDigRight = true;
}
@@ -245,10 +256,17 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
m_currentAnimation->Update(elapsedTime);
if (m_currentAnimation->IsDone() && m_IsTurning) {
m_currentAnimation = m_walkAnimation;
m_IsTurning = false;
}
if(m_currentAnimation == m_digStartAnimation) {
}
#pragma region Collision
m_ContactMap[Collision::CollisionDirection::Top] = nullptr;
@@ -338,30 +356,39 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
m_walkAnimation->SetPlaying(true);
break;
case PlayerState::Digging: {
m_walkAnimation->SetPlaying(false);
// m_walkAnimation->SetPlaying(false);
//Diganimation
if (!m_Digging) { //TODO: fix for setting the start position
m_Digging = true;
m_DigStart = m_Position;
m_Vel = Vector2f { 0, 0 };
m_currentAnimation->Update(elapsedTime);
if(m_currentAnimation->IsDone() && m_State == PlayerState::Digging && !m_IsDiggingPrimed) {
m_IsDiggingPrimed = true;
m_currentAnimation = m_digAnimation;
}
m_DigProgress += elapsedTime;
//lerp to the destination
float progress = utils::map(m_DigProgress, 0.0f, m_DigTime, 0.0f, 1.0f);
std::cout << progress << '\n';
m_Position = utils::lerp(m_DigStart, m_DigDestination, progress);
if (progress >= 0.5f && !m_HasDeletedTile) {
m_DigTile->SetTileType(GroundTileTypeManager::GetInstance()->AIR);
m_DigTile = nullptr;
m_HasDeletedTile = true;
}
if (progress >= 1.0f) {
m_State = PlayerState::Idle;
m_HasDeletedTile = false;
m_Digging = false;
if(m_IsDiggingPrimed) {
if (!m_Digging) { //TODO: fix for setting the start position
m_Digging = true;
m_DigStart = m_Position;
m_Vel = Vector2f { 0, 0 };
}
m_DigProgress += elapsedTime;
//lerp to the destination
float progress = utils::map(m_DigProgress, 0.0f, m_DigTime, 0.0f, 1.0f);
std::cout << progress << '\n';
m_Position = utils::lerp(m_DigStart, m_DigDestination, progress);
if (progress >= 0.5f && !m_HasDeletedTile) {
m_DigTile->SetTileType(GroundTileTypeManager::GetInstance()->AIR);
m_DigTile = nullptr;
m_HasDeletedTile = true;
}
if (progress >= 1.0f) {
m_State = PlayerState::Idle;
m_currentAnimation = m_walkAnimation;
m_HasDeletedTile = false;
m_Digging = false;
}
}
break;
}
default: