diff --git a/.idea/.idea.Motherload/.idea/workspace.xml b/.idea/.idea.Motherload/.idea/workspace.xml
index ad6b7d1..a1f6f60 100644
--- a/.idea/.idea.Motherload/.idea/workspace.xml
+++ b/.idea/.idea.Motherload/.idea/workspace.xml
@@ -11,17 +11,17 @@
-
-
-
-
+
+
+
-
+
+
@@ -31,19 +31,15 @@
-
-
-
-
-
+
@@ -63,28 +59,28 @@
- {
+ "keyToString": {
+ "C++ Project.Game.executor": "Run",
+ "RunOnceActivity.OpenProjectViewOnStart": "true",
+ "RunOnceActivity.ShowReadmeOnStart": "true",
+ "ignore.virus.scanning.warn.message": "true",
+ "node.js.detected.package.eslint": "true",
+ "node.js.detected.package.tslint": "true",
+ "node.js.selected.package.eslint": "(autodetect)",
+ "node.js.selected.package.tslint": "(autodetect)",
+ "nodejs_package_manager_path": "npm",
+ "settings.editor.selected.configurable": "com.github.MitI_7.IDEOMApplicationPlugin",
+ "vue.rearranger.settings.migration": "true"
},
- "keyToStringList": {
- "rider.external.source.directories": [
- "C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\DecompilerCache",
- "C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\SourcesCache",
- "C:\\Users\\Bram\\AppData\\Local\\Symbols\\src"
+ "keyToStringList": {
+ "rider.external.source.directories": [
+ "C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\DecompilerCache",
+ "C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\SourcesCache",
+ "C:\\Users\\Bram\\AppData\\Local\\Symbols\\src"
]
}
-}]]>
+}
@@ -189,6 +185,13 @@
+
+
+
+
+
+
+
diff --git a/Engine/utils.cpp b/Engine/utils.cpp
index 4985a5d..ac3c593 100644
--- a/Engine/utils.cpp
+++ b/Engine/utils.cpp
@@ -5,6 +5,8 @@
#include
#include "utils.h"
+#include "colors.h"
+
#pragma region OpenGLDrawFunctionality
void utils::SetColor(const Color4f& color) {
glColor4f(color.r, color.g, color.b, color.a);
@@ -228,6 +230,18 @@ void utils::DrawPolygon(const Vector2f* pVertices, size_t nrVertices, bool close
void utils::FillPolygon(const std::vector& vertices) {
FillPolygon(vertices.data(), vertices.size());
}
+void utils::DrawArrow(const Vector2f& start, const Vector2f& end, float lineWidth, float arrowSize) {
+ // Origin is bottom left
+ utils::DrawLine(start, end);
+ Vector2f arrowEnd = end - start;
+ Vector2f dir = end - start;
+
+ utils::SetColor(Colors::RED);
+ utils::DrawLine(end, end + Vector2f{ dir.y, -dir.x }.Normalized() * arrowSize);
+ utils::DrawLine(end, end + Vector2f{ -dir.y, dir.x }.Normalized() * arrowSize);
+
+
+}
void utils::FillPolygon(const Vector2f* pVertices, size_t nrVertices) {
glBegin(GL_POLYGON);
diff --git a/Engine/utils.h b/Engine/utils.h
index 02683c1..50670ad 100644
--- a/Engine/utils.h
+++ b/Engine/utils.h
@@ -60,6 +60,8 @@ namespace utils
void DrawPolygon( const Vector2f* pVertices, size_t nrVertices, bool closed = true, float lineWidth = 1.0f );
void FillPolygon( const std::vector& vertices);
void FillPolygon( const Vector2f* pVertices, size_t nrVertices);
+
+ void DrawArrow(const Vector2f& start, const Vector2f& end, float lineWidth = 1.0f, float arrowSize = 10.0f);
#pragma endregion OpenGLDrawFunctionality
#pragma region CollisionFunctionality
diff --git a/Game/GridSystem/WorldGridManager.h b/Game/GridSystem/WorldGridManager.h
index c8861bc..898d448 100644
--- a/Game/GridSystem/WorldGridManager.h
+++ b/Game/GridSystem/WorldGridManager.h
@@ -5,6 +5,7 @@
#include "structs.h"
+class GroundTileType;
static const int WORLD_WIDTH = 34;
static const int WORLD_HEIGHT = 34;
@@ -40,6 +41,18 @@ struct surroundingTiles
WorldTile* GetTile(TileDirection direction) {
return m_tiles[direction];
}
+
+ // bool IsAllType(const GroundTileType& type) {
+ // for (const auto& tile : m_tiles) {
+ // if (tile.second == nullptr) {
+ // return false;
+ // }
+ // if (tile.second->GetTileType() != &type) {
+ // return false;
+ // }
+ // }
+ // return true;
+ // }
};
class WorldGridManager
diff --git a/Game/GridSystem/WorldTile.cpp b/Game/GridSystem/WorldTile.cpp
index c191140..85c3513 100644
--- a/Game/GridSystem/WorldTile.cpp
+++ b/Game/GridSystem/WorldTile.cpp
@@ -32,6 +32,51 @@ WorldTile::~WorldTile() {
delete m_pTexture;
}
void WorldTile::Draw() {
+
+
+ switch (m_GroundTileType->getType()) {
+ case GroundTileTypes::Air: {
+ //check if it's all around dirt
+ bool allDirt = true;
+ for (int i = 0; i < 8; i++) {
+ const WorldTile* tile = m_SurroundingTiles.GetTile(static_cast(i));
+ if(tile != nullptr) { //Tile exists
+ const GroundTileTypes type = tile->GetTileType()->getType();
+ if(type != Tiles::DIRT->getType()) {
+ allDirt = false;
+ break;
+ }
+ }
+ }
+ if(allDirt) {
+ m_pAllTexture->Draw(m_Position);
+ return;
+ }
+ if(*m_GroundTileType == Tiles::AIR) {
+ this->DrawSide(TileDirection::TopLeft);
+ this->DrawSide(TileDirection::TopRight);
+ this->DrawSide(TileDirection::BottomLeft);
+ this->DrawSide(TileDirection::BottomRight);
+
+ this->DrawSide(TileDirection::TopMiddle);
+ this->DrawSide(TileDirection::BottomMiddle);
+ this->DrawSide(TileDirection::MiddleLeft);
+ this->DrawSide(TileDirection::MiddleRight);
+ break;
+ }
+
+ }
+ case GroundTileTypes::Dirt:
+ case GroundTileTypes::Hard:
+ case GroundTileTypes::Stone:
+ case GroundTileTypes::Iron:
+ m_pTexture->Draw(m_Position);
+ break;
+ default:
+ break;
+ }
+
+
if (*m_GroundTileType != Tiles::AIR) {
m_pTexture->Draw(m_Position);
if (m_Hightlight) {
@@ -40,35 +85,9 @@ void WorldTile::Draw() {
}
}
- //check if it's all around dirt
- bool allDirt = true;
- for (int i = 0; i < 8; i++) {
- const WorldTile* tile = m_SurroundingTiles.GetTile(static_cast(i));
- if(tile != nullptr) { //Tile exists
- const GroundTileTypes type = tile->GetTileType()->getType();
- if(type != Tiles::DIRT->getType()) {
- allDirt = false;
- break;
- }
- }
- }
- if(allDirt) {
- m_pAllTexture->Draw(m_Position);
- return;
- }
- if(*m_GroundTileType == Tiles::AIR) {
- this->DrawSide(TileDirection::TopLeft);
- this->DrawSide(TileDirection::TopRight);
- this->DrawSide(TileDirection::BottomLeft);
- this->DrawSide(TileDirection::BottomRight);
- this->DrawSide(TileDirection::TopMiddle);
- this->DrawSide(TileDirection::BottomMiddle);
- this->DrawSide(TileDirection::MiddleLeft);
- this->DrawSide(TileDirection::MiddleRight);
-
- }
+
}
void WorldTile::Update(const Camera* camera) {
diff --git a/Game/GridSystem/WorldTile.h b/Game/GridSystem/WorldTile.h
index 217474e..56effde 100644
--- a/Game/GridSystem/WorldTile.h
+++ b/Game/GridSystem/WorldTile.h
@@ -18,7 +18,7 @@ enum class GroundTileTypes
class GroundTileType
{
public:
- GroundTileType(const std::string& filePath, GroundTileTypes type): m_filePath(filePath), m_type(type) {
+ GroundTileType(const std::string&& filePath, GroundTileTypes type): m_filePath(filePath), m_type(type) {
}
virtual ~GroundTileType() = default;
bool operator==(const GroundTileType& rhs) const {
@@ -53,7 +53,7 @@ private:
class RandomGroundTile : public GroundTileType
{
public:
- RandomGroundTile(const std::string& filePath, GroundTileTypes type, int maxRandom): GroundTileType(filePath, type), m_maxRandom(maxRandom) {
+ RandomGroundTile(const std::string& filePath, GroundTileTypes type, int maxRandom): GroundTileType(std::move(filePath), type), m_maxRandom(maxRandom) {
}
~RandomGroundTile() override = default;
@@ -96,6 +96,8 @@ namespace Tiles
static GroundTileType* HARD_LEFT = new GroundTileType("tiles/dirt/special/hardLeft.png", GroundTileTypes::Hard);
static GroundTileType* HARD_RIGHT = new GroundTileType("tiles/dirt/special/hardRight.png", GroundTileTypes::Hard);
static GroundTileType* HARD_MIDDLE = new GroundTileType("tiles/dirt/special/hardMiddle.png", GroundTileTypes::Hard);
+
+ static GroundTileType* GRASS = new RandomGroundTile("tiles/dirt/special/grass[0].png", GroundTileTypes::Dirt, 2);
}
}
@@ -125,6 +127,7 @@ public:
}
void SetTileType(GroundTileType* type) {
m_GroundTileType = type;
+ m_pTexture = TextureManager::GetInstance()->GetTexture(type->getPath()); //Chage the texture when setting a new type
}
Collision::TileCollisionRect GetCollisionRect();
@@ -133,7 +136,6 @@ public:
private:
void DrawSide(const TileDirection& direction);
-
Vector2f m_Position;
GroundTileType* m_GroundTileType;
diff --git a/Game/Levels/World/WorldLevel.cpp b/Game/Levels/World/WorldLevel.cpp
index edc1409..7706d04 100644
--- a/Game/Levels/World/WorldLevel.cpp
+++ b/Game/Levels/World/WorldLevel.cpp
@@ -19,7 +19,7 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
m_player(Player { Vector2f { 0, 100 }, TextureManager::GetInstance() }),
m_mousePos { 0, 0 },
m_viewport(viewport),
- m_screenManager(ScreenManager::GetInstance()) {
+ m_screenManager(ScreenManager::GetInstance()) {
// The grid is 34 x 50 big, the top center is 0,0 in world coords
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
@@ -39,22 +39,21 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
default:
std::cout << "??" << '\n';
}
-
- m_gridManager.SetTileAtIndex(x, y, new WorldTile { pos, type, TextureManager::GetInstance(), &m_gridManager});
+
+ m_gridManager.SetTileAtIndex(x, y, new WorldTile { pos, Tiles::DIRT, TextureManager::GetInstance(), &m_gridManager });
}
}
- for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
+ for (int x { 0 }; x < WORLD_WIDTH; ++x) {
m_gridManager.GetTileAtIndex(x, 0)->SetTileType(Tiles::AIR);
+ m_gridManager.GetTileAtIndex(x, 1)->SetTileType(Tiles::IRON);
}
m_refeulBuilding = new Building { "buildings/fuelStation.png", Vector2f { -700, -50 }, TextureManager::GetInstance() };
- m_gridManager.GetTileAtWorldPos(Vector2f{-700, -50})->SetTileType(Tiles::Special::HARD_LEFT);
- m_gridManager.GetTileAtWorldPos(Vector2f{-650, -50})->SetTileType(Tiles::Special::HARD_MIDDLE);
- m_gridManager.GetTileAtWorldPos(Vector2f{-600, -50})->SetTileType(Tiles::Special::HARD_RIGHT);
-
+ m_gridManager.GetTileAtWorldPos(Vector2f { -700, -50 })->SetTileType(Tiles::Special::HARD_LEFT);
+ m_gridManager.GetTileAtWorldPos(Vector2f { -650, -50 })->SetTileType(Tiles::Special::HARD_MIDDLE);
+ m_gridManager.GetTileAtWorldPos(Vector2f { -600, -50 })->SetTileType(Tiles::Special::HARD_RIGHT);
}
WorldLevel::~WorldLevel() {
//delete m_pTextTexture;
-
}
void WorldLevel::Update(float elapsedSec) {
int mouseX, mouseY;
@@ -62,7 +61,7 @@ void WorldLevel::Update(float elapsedSec) {
m_mousePos = Vector2f { float(mouseX), float(mouseY) };
m_mousePos = m_pCamera->TransformMouse(m_mousePos);
-
+
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
m_gridManager.GetTileAtIndex(x, y)->m_Hightlight = false;
@@ -84,15 +83,16 @@ void WorldLevel::Update(float elapsedSec) {
}
//Get the diogonal tiles of the selected tile
- if(m_pSelectedTile != nullptr) {
+ if (m_pSelectedTile != nullptr) {
surroundingTiles surroundingTiles = m_gridManager.GetSurroundingTiles(m_pSelectedTile);
TileDirection direction = TileDirection::TopMiddle;
- const std::array directions = {TileDirection::TopLeft, TileDirection::TopMiddle, TileDirection::TopRight, TileDirection::MiddleLeft, TileDirection::MiddleRight, TileDirection::BottomLeft, TileDirection::BottomMiddle, TileDirection::BottomRight};
+ const std::array directions = { TileDirection::TopLeft, TileDirection::TopMiddle, TileDirection::TopRight, TileDirection::MiddleLeft,
+ TileDirection::MiddleRight, TileDirection::BottomLeft, TileDirection::BottomMiddle, TileDirection::BottomRight };
- for(int i = 0; i < 8; i++) {
+ for (int i = 0; i < 8; i++) {
direction = directions[i];
- if(surroundingTiles.GetTile(direction) != nullptr) {
- if(surroundingTiles.GetTile(direction)->GetTileType() != Tiles::AIR) {
+ if (surroundingTiles.GetTile(direction) != nullptr) {
+ if (surroundingTiles.GetTile(direction)->GetTileType() != Tiles::AIR) {
//surroundingTiles.GetTile(direction)->m_Hightlight = true;
}
}
@@ -105,14 +105,13 @@ void WorldLevel::Update(float elapsedSec) {
if (screen != nullptr) {
screen->Update(elapsedSec);
}
-
+
//Vector2f playerPos = m_player.GetPosition();
//Vector2f newCameraPos = playerPos;
//m_pCamera->SetPosition(newCameraPos);
//place the player in the center of the camera
//m_pCamera->SetPosition(Vector2f{playerPos.x - m_viewport.width / 2, playerPos.y - m_viewport.height / 2});
-
}
void WorldLevel::Draw() const {
m_pCamera->BeginRendering();
@@ -131,15 +130,18 @@ void WorldLevel::Draw() const {
}
utils::SetColor(Colors::MAGENTA);
- utils::FillEllipse(0, 0, 5, 5);
+ utils::FillEllipse(-5, -5, 5, 5);
- m_player.Draw();
- if(m_pSelectedTile != nullptr) {
+ if (m_pSelectedTile != nullptr) {
m_pSelectedTile->Draw();
}
m_refeulBuilding->Draw();
-
+ m_player.Draw();
+
+ utils::SetColor(Colors::GREEN);
+ utils::DrawArrow(Vector2f{0, 0}, m_mousePos);
+
m_pCamera->EndRendering();
utils::FillRect(utils::GetMousePos(), 10, 10);
@@ -153,6 +155,32 @@ void WorldLevel::MouseMove(const Vector2f& mousePos) {
}
void WorldLevel::ProcessImGui() {
+ ImGui::Begin("Selected Tile");
+ std::string tileType = "None";
+ if (m_pSelectedTile != nullptr) {
+ switch (m_pSelectedTile->GetTileType()->getType()) {
+ case GroundTileTypes::Air:
+ tileType = "Air";
+ break;
+ case GroundTileTypes::Dirt:
+ tileType = "Dirt";
+ break;
+ case GroundTileTypes::Iron:
+ tileType = "Iron";
+ break;
+ case GroundTileTypes::Hard:
+ tileType = "Hard";
+ break;
+ case GroundTileTypes::Stone:
+ tileType = "Stone";
+ break;
+ default:
+ tileType = "Unknown";
+ break;
+ }
+ }
+ ImGui::Text("Selected Tile: %s", tileType.c_str());
+ ImGui::End();
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("Properties")) {
if (ImGui::MenuItem("TextureManager Info")) {
@@ -166,29 +194,28 @@ void WorldLevel::ProcessImGui() {
}
ImGui::EndMenu();
}
- if(ImGui::BeginMenu("Screens")) {
- if(ImGui::MenuItem("Open Fuel screen")) {
+ if (ImGui::BeginMenu("Screens")) {
+ if (ImGui::MenuItem("Open Fuel screen")) {
ScreenManager::GetInstance()->OpenScreen(ScreenManager::Fuel);
}
-
- if(ImGui::MenuItem("Open Sell screen")) {
+
+ if (ImGui::MenuItem("Open Sell screen")) {
ScreenManager::GetInstance()->OpenScreen(ScreenManager::SellScreen);
}
- if(ImGui::MenuItem("Close Screen")) {
- ScreenManager::GetInstance()->CloseScreen();
+ if (ImGui::MenuItem("Close Screen")) {
+ ScreenManager::GetInstance()->CloseScreen();
}
-
+
ImGui::EndMenu();
}
const Vector2f screenPos = utils::GetMousePos();
const std::string mousePos = "Mouse Pos: (" + std::to_string(screenPos.x) + ", " + std::to_string(screenPos.y) + ")";
- if(ImGui::BeginMenu(mousePos.c_str())) {
-
- ImGui::EndMenu();
+ if (ImGui::BeginMenu(mousePos.c_str())) {
+ ImGui::EndMenu();
}
-
+
ImGui::EndMainMenuBar();
}
diff --git a/Game/Player.cpp b/Game/Player.cpp
index e7c13b4..51dda10 100644
--- a/Game/Player.cpp
+++ b/Game/Player.cpp
@@ -21,6 +21,7 @@ Player::Player(const Vector2f& Position, TextureManager* manager) : m_Position(P
8, 0.1f, Rectf { 0, 0, 70, 70 });
m_currentAnimation = m_walkAnimation;
}
+
Collision::CollisionRect Player::GetCollisionRect() const {
Collision::CollisionRect rect = { m_Position, m_Size, m_Vel };
return rect;
@@ -36,15 +37,17 @@ 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} ;
+ Vector2f drawPos = Vector2f { center.x - halfFrameWidth, center.y - halfFrameWidth + 9 + bobOffset };
- m_walkAnimation->Draw(drawPos, Rectf { drawPos.x, drawPos.y , frameWidth, frameWidth });
+ m_walkAnimation->Draw(drawPos, Rectf { drawPos.x, drawPos.y, frameWidth, frameWidth });
+ utils::DrawEllipse(m_DigDestination, 5, 5);
+ utils::DrawEllipse(m_DigStart, 5, 5);
}
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 {};
switch (m_State) {
case PlayerState::Idle:
currentState = "Idle";
@@ -59,8 +62,8 @@ 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");
-
-
+
+
//ContactMap
ImGui::Text("ContactMap:");
ImGui::Text("Top: %s", m_ContactMap[Collision::CollisionDirection::Top] != nullptr ? "true" : "false");
@@ -69,53 +72,88 @@ void Player::ProcessImGui() {
ImGui::Text("Right: %s", m_ContactMap[Collision::CollisionDirection::Right] != nullptr ? "true" : "false");
ImGui::End();
}
+void Player::Dig(Collision::CollisionDirection dir, WorldLevel& level) {
+ m_State = PlayerState::Digging;
+ m_DigProgress = 0;
+ m_DigTile = m_ContactMap[dir];
+ //Set the digging location in the center of the destination tile;
+ const WorldTile* tile = m_ContactMap[dir];
+ //Add case for bottom because otherwise i clip through the floor
+ m_DigDestination = tile->GetPosition();
+ if (dir == Collision::Bottom) {
+ m_DigDestination += Vector2f { 0, 2 };
+ //Center
+ m_DigDestination.x += tile->GetSize().x / 2 - m_Size.x / 2;
+ }
+ if(dir == Collision::Left) {
+ m_DigDestination += Vector2f{ 2, 0};
+ }
+ m_ContactMap[dir] = nullptr;
+}
+
+bool Player::CanDig(Collision::CollisionDirection dir, WorldLevel& level) {
+ WorldTile* tile = m_ContactMap[dir];
+ if (tile == nullptr) {
+ return false;
+ }
+ GroundTileType type = *tile->GetTileType();
+ //TODO: Add a list of non diggable tiles
+
+ if (type == Tiles::Special::HARD_LEFT || type == Tiles::Special::HARD_MIDDLE || type == Tiles::Special::HARD_RIGHT) {
+ return false;
+ }
+
+ return true;
+}
void Player::Update(float elapsedTime, WorldLevel& level) {
m_BobTimer += elapsedTime;
- if(m_BobTimer >= m_BobTime) {
+ if (m_BobTimer >= m_BobTime) {
m_BobUp = !m_BobUp;
m_BobTimer = 0.0f;
}
//check for keys
- if(m_State != PlayerState::Digging) {
+ if (m_State != PlayerState::Digging) {
m_Vel = Vector2f { 0, -100 };
if (utils::isKeyDown(SDL_SCANCODE_W)) {
m_Vel.y = 100;
m_Grounded = false;
}
if (utils::isKeyPressed(SDL_SCANCODE_S)) {
- m_Vel.y = -100;
if (m_Grounded) {
- if (m_ContactMap[Collision::CollisionDirection::Bottom] != nullptr) {
- //Do the digging
- m_State = PlayerState::Digging;
- m_DigProgress = 0;
- m_DigTile = m_ContactMap[Collision::CollisionDirection::Bottom];
- //Set the digging location in the center of the destination tile;
- const WorldTile* tile = m_ContactMap[Collision::CollisionDirection::Bottom];
- m_DigDestination = tile->GetPosition() + Vector2f{0, 0};
- m_ContactMap[Collision::CollisionDirection::Bottom] = nullptr;
+ if (this->CanDig(Collision::Bottom, level)) {
+ this->Dig(Collision::CollisionDirection::Bottom, level);
}
}
+ else {
+ m_Vel.y = -100;
+ }
}
if (utils::isKeyDown(SDL_SCANCODE_A)) {
m_walkAnimation->SetFlipped(false);
m_Vel.x = -100;
+ if (m_Grounded && !m_DidJustDigLeft) {
+ //Check if the player doesnt come from digging a tile
+ if (this->CanDig(Collision::CollisionDirection::Left, level)) {
+ this->Dig(Collision::CollisionDirection::Left, level);
+ m_DidJustDigLeft = true;
+ }
+ }
}
+ if (m_DidJustDigLeft) {
+ if (!utils::isKeyDown(SDL_SCANCODE_A)) {
+ m_DidJustDigLeft = false;
+ }
+ }
+
if (utils::isKeyDown(SDL_SCANCODE_D)) {
m_Vel.x = 100;
m_walkAnimation->SetFlipped(true);
if (m_Grounded && !m_DidJustDigRight) {
//Check if the player doesnt come from digging a tile
- if (m_ContactMap[Collision::CollisionDirection::Right] != nullptr) {
- m_ContactMap[Collision::CollisionDirection::Right]->SetTileType(Tiles::AIR);
- WorldTile* tile = m_ContactMap[Collision::CollisionDirection::Right];
- //center of tile
- const Vector2f tileCenter = tile->GetCollisionRect().getCollisionRect().pos + tile->GetCollisionRect().getCollisionRect().size / 2;
- m_Position = Vector2f { tileCenter.x - m_Size.x / 2, tileCenter.y - m_Size.y / 2 + 5 };
-
- m_ContactMap[Collision::CollisionDirection::Right] = nullptr;
+ if (this->CanDig(Collision::CollisionDirection::Right, level)) {
+ this->Dig(Collision::CollisionDirection::Right, level);
m_DidJustDigRight = true;
}
}
@@ -126,6 +164,7 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
}
}
+
m_walkAnimation->Update(elapsedTime);
@@ -155,8 +194,7 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
}
}
- std::sort(contactTimes.begin(), contactTimes.end(), [](const std::pair& a, const std::pair& b)
- {
+ std::sort(contactTimes.begin(), contactTimes.end(), [](const std::pair& a, const std::pair& b) {
return a.second < b.second;
});
@@ -198,7 +236,7 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
Collision::CollisionRect rect = world_tile->GetCollisionRect().getCollisionRect(); //TODO: fix this mess
Collision::ResolvePlayerVsRect(*this, elapsedTime, &rect);
}
- if(m_State != PlayerState::Digging) { //Fix for when the state is JUST set to digging
+ 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;
}
@@ -207,7 +245,6 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
}
}
}
-
switch (m_State) {
case PlayerState::Idle:
m_walkAnimation->SetPlaying(false);
@@ -218,11 +255,17 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
case PlayerState::Digging: {
m_walkAnimation->SetPlaying(false);
//Diganimation
+
+ if (!m_Digging) { //TODO: fix for setting the start position
+ m_Digging = true;
+ m_DigStart = m_Position;
+ }
+
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_Position, m_DigDestination, progress);
+ m_Position = utils::lerp(m_DigStart, m_DigDestination, progress);
if (progress >= 0.5f && !m_HasDeletedTile) {
m_DigTile->SetTileType(Tiles::AIR);
m_DigTile = nullptr;
@@ -231,15 +274,15 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
if (progress >= 1.0f) {
m_State = PlayerState::Idle;
m_HasDeletedTile = false;
+ m_Digging = false;
}
break;
}
default:
break;
-
}
- if(m_State != PlayerState::Digging) {
+ if (m_State != PlayerState::Digging) {
m_Position = m_Position + m_Vel * elapsedTime;
}
}
diff --git a/Game/Player.h b/Game/Player.h
index c7dd994..10a3346 100644
--- a/Game/Player.h
+++ b/Game/Player.h
@@ -59,6 +59,9 @@ public:
void ProcessImGui();
private:
+ void Dig(Collision::CollisionDirection dir, WorldLevel& level);
+ bool CanDig(Collision::CollisionDirection dir, WorldLevel& level);
+
Vector2f m_Position;
Vector2f m_Size;
@@ -73,15 +76,18 @@ private:
bool m_BobUp{ true };
Vector2f m_DigDestination{};
+ bool m_Digging{ false };
+ Vector2f m_DigStart{};
float m_DigProgress{};
bool m_HasDeletedTile{ false };
WorldTile* m_DigTile{ nullptr };
- const float m_DigTime{ 1.0f };
+ const float m_DigTime{ 0.5f };
bool m_Grounded { false };
bool m_DidJustDigRight { false };
+ bool m_DidJustDigLeft { false };
Animation* m_currentAnimation{ nullptr };
Animation* m_walkAnimation;
Animation* m_turnAnimation;
diff --git a/Game/TextureManager.cpp b/Game/TextureManager.cpp
index 52fb8c0..59e4ca3 100644
--- a/Game/TextureManager.cpp
+++ b/Game/TextureManager.cpp
@@ -18,4 +18,7 @@ Texture * TextureManager::GetTexture(const std::string& name) {
}
TextureManager::~TextureManager() {
//TODO: Loop over the m_Textures to delete them
+ for ( const auto &p : m_Textures ) {
+ delete p.second;
+ }
}
diff --git a/Game/main.cpp b/Game/main.cpp
index 6831e7f..0046b01 100644
--- a/Game/main.cpp
+++ b/Game/main.cpp
@@ -2,7 +2,7 @@
#include
#include "Game.h"
-
+#define DEBUG
void StartHeapControl();
void DumpMemoryLeaks();