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

@@ -1,6 +1,8 @@
#include "pch.h"
#include "Animation.h"
#include <iostream>
#include "utils.h"
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) {

View File

@@ -23,6 +23,7 @@ public:
m_CurrentFrame = 0;
m_hasPlayedOnce = false;
m_isPlaying = true;
m_FrameTimer = m_FrameDuration;
}
bool IsDone() const {
return m_hasPlayedOnce && !m_isLooping;

View File

@@ -308,7 +308,9 @@
<BrowseInformation Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</BrowseInformation>
</ClCompile>
<ClCompile Include="GroundTileTypeManager.cpp" />
<ClCompile Include="Gui\Button.cpp" />
<ClCompile Include="Gui\GuiButton.cpp" />
<ClCompile Include="Gui\GuiElement.cpp" />
<ClCompile Include="Gui\GuiMeter.cpp" />
<ClCompile Include="Gui\Screen.cpp" />
<ClCompile Include="Gui\Screens\FuelScreen.cpp" />
<ClCompile Include="Gui\Screens\ScreenManager.cpp" />
@@ -477,7 +479,9 @@
<ClInclude Include="GridSystem\WorldGridManager.h" />
<ClInclude Include="GridSystem\WorldTile.h" />
<ClInclude Include="GroundTileTypeManager.h" />
<ClInclude Include="Gui\Button.h" />
<ClInclude Include="Gui\GuiButton.h" />
<ClInclude Include="Gui\GuiElement.h" />
<ClInclude Include="Gui\GuiMeter.h" />
<ClInclude Include="Gui\Screen.h" />
<ClInclude Include="Gui\Screens\FuelScreen.h" />
<ClInclude Include="Gui\Screens\ScreenManager.h" />

View File

@@ -1,28 +1,28 @@
#include "pch.h"
#include "Button.h"
#include "GuiButton.h"
#include <iostream>
#include "colors.h"
#include "utils.h"
Button::Button(const std::string& filePath, Vector2f pos, Vector2f size, TextureManager* manager): m_Position(pos), m_Size(size) {
GuiButton::GuiButton(const std::string& filePath, Vector2f pos, Vector2f size, TextureManager* manager): m_Position(pos), m_Size(size) {
m_Texture = manager->GetTexture(filePath);
if(size.x == 0 && size.y == 0) {
m_Size = Vector2f{float(m_Texture->GetWidth()), float(m_Texture->GetHeight())};
}
std::cout << "Button created" << '\n';
}
Button::~Button() {
GuiButton::~GuiButton() {
std::cout << "Button destroyed" << '\n';
}
void Button::Draw() const {
void GuiButton::Draw() const {
Rectf dest = Rectf(m_Position, m_Size);
Rectf src = Rectf(0, 0, m_Texture->GetWidth(), m_Texture->GetHeight());
if(m_IsHovered) {
m_Texture->Draw(dest, src, false);
}
}
void Button::Update(float elapsedSec) {
void GuiButton::Update(float elapsedSec) {
Vector2f mousePos = utils::GetMousePos();
Rectf buttonRect = Rectf(m_Position, m_Size);

View File

@@ -2,17 +2,18 @@
#include <functional>
#include <iostream>
#include "GuiElement.h"
#include "Texture.h"
#include "../TextureManager.h"
class Button
class GuiButton : public GuiElement
{
public:
Button() = default;
Button(const std::string& filePath, Vector2f pos, Vector2f size, TextureManager* manager);
~Button();
void Draw() const;
void Update(float elapsedSec);
GuiButton() = default;
GuiButton(const std::string& filePath, Vector2f pos, Vector2f size, TextureManager* manager);
~GuiButton() override;
virtual void Draw() const override;
virtual void Update(float elapsedSec) override;
void SetOnClick(std::function<void(void)> onClick) {
m_OnClick = onClick;

2
Game/Gui/GuiElement.cpp Normal file
View File

@@ -0,0 +1,2 @@
#include "pch.h"
#include "GuiElement.h"

14
Game/Gui/GuiElement.h Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
class GuiElement
{
public:
GuiElement() = default;
virtual ~GuiElement() = default;
virtual void Draw() const = 0;
virtual void Update(float elapsedSec) = 0;
private:
};

18
Game/Gui/GuiMeter.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include "pch.h"
#include "GuiMeter.h"
#include "TextureManager.h"
GuiMeter::GuiMeter(const std::string& filePath, Vector2f pos, Vector2f frameSize, int frameCount, TextureManager* manager): m_Position(pos), m_FrameCount(frameCount) {
m_Animation = new Animation(manager->GetTexture(filePath),frameCount, 0.0f, Rectf{0, 0, frameSize.x, frameSize.y}, false);
}
GuiMeter::~GuiMeter() {
delete m_Animation;
}
void GuiMeter::Draw() const {
m_Animation->Draw(m_Position);
}
void GuiMeter::Update(float elapsedSec) {
}

25
Game/Gui/GuiMeter.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#include "GuiElement.h"
#include "Texture.h"
#include "Animations/Animation.h"
class TextureManager;
class GuiMeter : public GuiElement
{
public:
GuiMeter() = default;
GuiMeter(const std::string& filePath, Vector2f pos, Vector2f frameSize, int frameCount, TextureManager* manager);
virtual ~GuiMeter();
virtual void Draw() const override;
virtual void Update(float elapsedSec) override;
private:
Animation* m_Animation{ nullptr };
Vector2f m_Position;
float m_Value{ 0.0f };
int m_FrameCount;
};

View File

@@ -5,12 +5,12 @@ Screen::Screen(const std::string& filePath, Vector2f pos, Vector2f size, Texture
m_Background = manager->GetTexture(filePath);
}
Screen::~Screen() {
for (Button* b : m_Buttons) {
for (GuiElement* b : m_Elements) {
delete b;
}
}
void Screen::Update(float elapsedSecs) {
for (Button* b : m_Buttons) {
for (GuiElement* b : m_Elements) {
b->Update(elapsedSecs);
}
}
@@ -19,7 +19,7 @@ void Screen::Draw() const {
Rectf src = Rectf(0, 0, m_Background->GetWidth(), m_Background->GetHeight());
m_Background->Draw(dest, src, false);
for (Button* b : m_Buttons) {
for (GuiElement* b : m_Elements) {
b->Draw();
}
}

View File

@@ -1,7 +1,7 @@
#pragma once
#include <vector>
#include "Button.h"
#include "GuiButton.h"
#include "structs.h"
#include "Texture.h"
#include "../TextureManager.h"
@@ -14,7 +14,7 @@ public:
virtual ~Screen();
void AddButton(Button* button) { m_Buttons.push_back(button); }
void AddElement(GuiElement* element) { m_Elements.push_back(element); }
virtual void Update(float elapsedSecs);
virtual void Draw() const;
@@ -24,5 +24,5 @@ private:
Texture* m_Background{ nullptr };
std::vector<Button*> m_Buttons;
std::vector<GuiElement*> m_Elements;
};

View File

@@ -15,29 +15,29 @@ FuelScreen::FuelScreen(const std::string& filePath, Vector2f pos, Vector2f size,
const Vector2f closeButtonOffset = Vector2f { 460, 396 - 14 };
Vector2f closeButtonPos = fuelScreenCenter + closeButtonOffset;
closeButtonPos.y -= 18;
Button* closeFuelButton = new Button { "gui/close.png", closeButtonPos, Vector2f{0,0}, TextureManager::GetInstance() };
GuiButton* closeFuelButton = new GuiButton { "gui/close.png", closeButtonPos, Vector2f{0,0}, TextureManager::GetInstance() };
closeFuelButton->SetOnClick([this]() { ScreenManager::GetInstance()->CloseScreen(); });
this->AddButton(closeFuelButton);
this->AddElement(closeFuelButton);
const Vector2f oneDollarButtonPos = Vector2f { 451, 287 };
Button* fiveDollarButton = new Button { "gui/fuel/5dollars.png", oneDollarButtonPos , Vector2f{0,0}, TextureManager::GetInstance() };
this->AddButton(fiveDollarButton);
GuiButton* fiveDollarButton = new GuiButton { "gui/fuel/5dollars.png", oneDollarButtonPos , Vector2f{0,0}, TextureManager::GetInstance() };
this->AddElement(fiveDollarButton);
const Vector2f tenDollarButtonPos = oneDollarButtonPos + Vector2f { 113, -1 };
Button* tenDollarButton = new Button { "gui/fuel/10dollars.png", tenDollarButtonPos, Vector2f{0,0}, TextureManager::GetInstance() };
this->AddButton(tenDollarButton);
GuiButton* tenDollarButton = new GuiButton { "gui/fuel/10dollars.png", tenDollarButtonPos, Vector2f{0,0}, TextureManager::GetInstance() };
this->AddElement(tenDollarButton);
const Vector2f twentyFiveDollarButtonPos = oneDollarButtonPos + Vector2f { 0, -89 };
Button* twentyFiveDollarButton = new Button { "gui/fuel/25dollars.png", twentyFiveDollarButtonPos, Vector2f{0,0}, TextureManager::GetInstance() };
this->AddButton(twentyFiveDollarButton);
GuiButton* twentyFiveDollarButton = new GuiButton { "gui/fuel/25dollars.png", twentyFiveDollarButtonPos, Vector2f{0,0}, TextureManager::GetInstance() };
this->AddElement(twentyFiveDollarButton);
const Vector2f fiftyDollarButtonPos = twentyFiveDollarButtonPos + Vector2f { 114, 0 };
Button* fiftyDollarButton = new Button { "gui/fuel/50dollars.png", fiftyDollarButtonPos, Vector2f{0,0}, TextureManager::GetInstance() };
this->AddButton(fiftyDollarButton);
GuiButton* fiftyDollarButton = new GuiButton { "gui/fuel/50dollars.png", fiftyDollarButtonPos, Vector2f{0,0}, TextureManager::GetInstance() };
this->AddElement(fiftyDollarButton);
const Vector2f fillTankButtonPos = Vector2f { 450, 108 };
Button* fillTankButton = new Button { "gui/fuel/fillTank.png", fillTankButtonPos, Vector2f{0,0}, TextureManager::GetInstance() };
this->AddButton(fillTankButton);
GuiButton* fillTankButton = new GuiButton { "gui/fuel/fillTank.png", fillTankButtonPos, Vector2f{0,0}, TextureManager::GetInstance() };
this->AddElement(fillTankButton);
}

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:

View File

@@ -93,6 +93,8 @@ private:
bool m_HasDeletedTile{ false };
WorldTile* m_DigTile{ nullptr };
bool m_IsDiggingPrimed{ false };
const float m_DigTime{ 0.5f };
bool m_Grounded { false };