Fix digging

This commit is contained in:
Bram Verhulst
2024-04-20 20:20:54 +02:00
parent ebda39f690
commit 5477b8a7f2
11 changed files with 258 additions and 126 deletions

View File

@@ -11,17 +11,17 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="26a0623a-44d5-441c-8048-32ff1dab3479" name="Changes" comment=""> <list default="true" id="26a0623a-44d5-441c-8048-32ff1dab3479" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/Game/Levels/World/Building.cpp" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Game/Levels/World/Building.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/FuelStation.aseprite" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/FuelStation.aseprite" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Engine/utils.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Engine/utils.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Game.vcxproj" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Game.vcxproj" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Engine/utils.h" beforeDir="false" afterPath="$PROJECT_DIR$/Engine/utils.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/GridSystem/WorldGridManager.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/GridSystem/WorldGridManager.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/GridSystem/WorldTile.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/GridSystem/WorldTile.cpp" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Game/GridSystem/WorldTile.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/GridSystem/WorldTile.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/GridSystem/WorldTile.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/GridSystem/WorldTile.h" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Game/GridSystem/WorldTile.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/GridSystem/WorldTile.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Levels/World/WorldLevel.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Levels/World/WorldLevel.cpp" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Game/Levels/World/WorldLevel.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Levels/World/WorldLevel.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Levels/World/WorldLevel.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Levels/World/WorldLevel.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Player.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Player.cpp" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Game/Player.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Player.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Player.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Player.h" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Game/Player.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Player.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/TextureManager.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/TextureManager.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/main.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/main.cpp" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -31,19 +31,15 @@
<component name="Git.Settings"> <component name="Git.Settings">
<option name="RECENT_BRANCH_BY_REPOSITORY"> <option name="RECENT_BRANCH_BY_REPOSITORY">
<map> <map>
<entry key="$PROJECT_DIR$" value="71d364d9d8b80fad541dc883c727d8984c247f4b" /> <entry key="$PROJECT_DIR$" value="ebda39f690147cf0dfe51c6e4e121c4f1e3606e8" />
</map> </map>
</option> </option>
</component> </component>
<component name="HighlightingSettingsPerFile"> <component name="HighlightingSettingsPerFile">
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/../../../../../../Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.37.32822/include/vector" root0="SKIP_HIGHLIGHTING" /> <setting file="file://$PROJECT_DIR$/../../../../../../Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.37.32822/include/vector" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Game/Animations/Animation.h" root0="FORCE_HIGHLIGHTING" /> <setting file="file://$PROJECT_DIR$/Game/Animations/Animation.h" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Game/Camera.cpp" root0="FORCE_HIGHLIGHTING" /> <setting file="file://$PROJECT_DIR$/Game/Camera.cpp" root0="FORCE_HIGHLIGHTING" />
<setting file="mock://C:/Users/Bram/Desktop/Programming 2/Exam/dae16-VerhulstBram/Game/GridSystem/WorldTile.cpp" root0="SKIP_HIGHLIGHTING" /> <setting file="mock://C:/Users/Bram/Desktop/Programming 2/Exam/dae16-VerhulstBram/Game/main.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Game/pch.cpp" root0="FORCE_HIGHLIGHTING" /> <setting file="file://$PROJECT_DIR$/Game/pch.cpp" root0="FORCE_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/Game/pch.h" root0="FORCE_HIGHLIGHTING" /> <setting file="file://$PROJECT_DIR$/Game/pch.h" root0="FORCE_HIGHLIGHTING" />
</component> </component>
@@ -63,28 +59,28 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent">{
"keyToString": { &quot;keyToString&quot;: {
"C++ Project.Game.executor": "Run", &quot;C++ Project.Game.executor&quot;: &quot;Run&quot;,
"RunOnceActivity.OpenProjectViewOnStart": "true", &quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
"RunOnceActivity.ShowReadmeOnStart": "true", &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
"ignore.virus.scanning.warn.message": "true", &quot;ignore.virus.scanning.warn.message&quot;: &quot;true&quot;,
"node.js.detected.package.eslint": "true", &quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
"node.js.detected.package.tslint": "true", &quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
"node.js.selected.package.eslint": "(autodetect)", &quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
"node.js.selected.package.tslint": "(autodetect)", &quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
"nodejs_package_manager_path": "npm", &quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
"settings.editor.selected.configurable": "preferences.sourceCode.C++", &quot;settings.editor.selected.configurable&quot;: &quot;com.github.MitI_7.IDEOMApplicationPlugin&quot;,
"vue.rearranger.settings.migration": "true" &quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
}, },
"keyToStringList": { &quot;keyToStringList&quot;: {
"rider.external.source.directories": [ &quot;rider.external.source.directories&quot;: [
"C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\DecompilerCache", &quot;C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\DecompilerCache&quot;,
"C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\SourcesCache", &quot;C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\SourcesCache&quot;,
"C:\\Users\\Bram\\AppData\\Local\\Symbols\\src" &quot;C:\\Users\\Bram\\AppData\\Local\\Symbols\\src&quot;
] ]
} }
}]]></component> }</component>
<component name="RunManager"> <component name="RunManager">
<configuration name="Game" type="CppProject" factoryName="C++ Project"> <configuration name="Game" type="CppProject" factoryName="C++ Project">
<configuration_1> <configuration_1>
@@ -189,6 +185,13 @@
<workItem from="1713354379040" duration="2279000" /> <workItem from="1713354379040" duration="2279000" />
<workItem from="1713377152822" duration="1364000" /> <workItem from="1713377152822" duration="1364000" />
<workItem from="1713440849218" duration="1218000" /> <workItem from="1713440849218" duration="1218000" />
<workItem from="1713450313096" duration="4942000" />
<workItem from="1713462845365" duration="4464000" />
<workItem from="1713525299050" duration="2330000" />
<workItem from="1713544818472" duration="1549000" />
<workItem from="1713546379562" duration="319000" />
<workItem from="1713609308065" duration="368000" />
<workItem from="1713624356487" duration="6843000" />
</task> </task>
<task id="LOCAL-00001" summary="Rework Tile detection system"> <task id="LOCAL-00001" summary="Rework Tile detection system">
<option name="closed" value="true" /> <option name="closed" value="true" />

View File

@@ -5,6 +5,8 @@
#include <iostream> #include <iostream>
#include "utils.h" #include "utils.h"
#include "colors.h"
#pragma region OpenGLDrawFunctionality #pragma region OpenGLDrawFunctionality
void utils::SetColor(const Color4f& color) { void utils::SetColor(const Color4f& color) {
glColor4f(color.r, color.g, color.b, color.a); 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<Vector2f>& vertices) { void utils::FillPolygon(const std::vector<Vector2f>& vertices) {
FillPolygon(vertices.data(), vertices.size()); 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) { void utils::FillPolygon(const Vector2f* pVertices, size_t nrVertices) {
glBegin(GL_POLYGON); glBegin(GL_POLYGON);

View File

@@ -60,6 +60,8 @@ namespace utils
void DrawPolygon( const Vector2f* pVertices, size_t nrVertices, bool closed = true, float lineWidth = 1.0f ); void DrawPolygon( const Vector2f* pVertices, size_t nrVertices, bool closed = true, float lineWidth = 1.0f );
void FillPolygon( const std::vector<Vector2f>& vertices); void FillPolygon( const std::vector<Vector2f>& vertices);
void FillPolygon( const Vector2f* pVertices, size_t nrVertices); 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 endregion OpenGLDrawFunctionality
#pragma region CollisionFunctionality #pragma region CollisionFunctionality

View File

@@ -5,6 +5,7 @@
#include "structs.h" #include "structs.h"
class GroundTileType;
static const int WORLD_WIDTH = 34; static const int WORLD_WIDTH = 34;
static const int WORLD_HEIGHT = 34; static const int WORLD_HEIGHT = 34;
@@ -40,6 +41,18 @@ struct surroundingTiles
WorldTile* GetTile(TileDirection direction) { WorldTile* GetTile(TileDirection direction) {
return m_tiles[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 class WorldGridManager

View File

@@ -32,6 +32,51 @@ WorldTile::~WorldTile() {
delete m_pTexture; delete m_pTexture;
} }
void WorldTile::Draw() { 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<TileDirection>(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) { if (*m_GroundTileType != Tiles::AIR) {
m_pTexture->Draw(m_Position); m_pTexture->Draw(m_Position);
if (m_Hightlight) { 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<TileDirection>(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) { void WorldTile::Update(const Camera* camera) {

View File

@@ -18,7 +18,7 @@ enum class GroundTileTypes
class GroundTileType class GroundTileType
{ {
public: 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; virtual ~GroundTileType() = default;
bool operator==(const GroundTileType& rhs) const { bool operator==(const GroundTileType& rhs) const {
@@ -53,7 +53,7 @@ private:
class RandomGroundTile : public GroundTileType class RandomGroundTile : public GroundTileType
{ {
public: 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; ~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_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_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* 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) { void SetTileType(GroundTileType* type) {
m_GroundTileType = type; m_GroundTileType = type;
m_pTexture = TextureManager::GetInstance()->GetTexture(type->getPath()); //Chage the texture when setting a new type
} }
Collision::TileCollisionRect GetCollisionRect(); Collision::TileCollisionRect GetCollisionRect();
@@ -134,7 +137,6 @@ public:
private: private:
void DrawSide(const TileDirection& direction); void DrawSide(const TileDirection& direction);
Vector2f m_Position; Vector2f m_Position;
GroundTileType* m_GroundTileType; GroundTileType* m_GroundTileType;

View File

@@ -19,7 +19,7 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
m_player(Player { Vector2f { 0, 100 }, TextureManager::GetInstance() }), m_player(Player { Vector2f { 0, 100 }, TextureManager::GetInstance() }),
m_mousePos { 0, 0 }, m_mousePos { 0, 0 },
m_viewport(viewport), 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 // 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 x { 0 }; x < WORLD_WIDTH; ++x) {
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) { for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
@@ -40,21 +40,20 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
std::cout << "??" << '\n'; 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, 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_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 { -700, -50 })->SetTileType(Tiles::Special::HARD_LEFT);
m_gridManager.GetTileAtWorldPos(Vector2f{-650, -50})->SetTileType(Tiles::Special::HARD_MIDDLE); 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 { -600, -50 })->SetTileType(Tiles::Special::HARD_RIGHT);
} }
WorldLevel::~WorldLevel() { WorldLevel::~WorldLevel() {
//delete m_pTextTexture; //delete m_pTextTexture;
} }
void WorldLevel::Update(float elapsedSec) { void WorldLevel::Update(float elapsedSec) {
int mouseX, mouseY; int mouseX, mouseY;
@@ -84,15 +83,16 @@ void WorldLevel::Update(float elapsedSec) {
} }
//Get the diogonal tiles of the selected tile //Get the diogonal tiles of the selected tile
if(m_pSelectedTile != nullptr) { if (m_pSelectedTile != nullptr) {
surroundingTiles surroundingTiles = m_gridManager.GetSurroundingTiles(m_pSelectedTile); surroundingTiles surroundingTiles = m_gridManager.GetSurroundingTiles(m_pSelectedTile);
TileDirection direction = TileDirection::TopMiddle; TileDirection direction = TileDirection::TopMiddle;
const std::array<TileDirection, 8> directions = {TileDirection::TopLeft, TileDirection::TopMiddle, TileDirection::TopRight, TileDirection::MiddleLeft, TileDirection::MiddleRight, TileDirection::BottomLeft, TileDirection::BottomMiddle, TileDirection::BottomRight}; const std::array<TileDirection, 8> 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]; direction = directions[i];
if(surroundingTiles.GetTile(direction) != nullptr) { if (surroundingTiles.GetTile(direction) != nullptr) {
if(surroundingTiles.GetTile(direction)->GetTileType() != Tiles::AIR) { if (surroundingTiles.GetTile(direction)->GetTileType() != Tiles::AIR) {
//surroundingTiles.GetTile(direction)->m_Hightlight = true; //surroundingTiles.GetTile(direction)->m_Hightlight = true;
} }
} }
@@ -112,7 +112,6 @@ void WorldLevel::Update(float elapsedSec) {
//place the player in the center of the camera //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}); //m_pCamera->SetPosition(Vector2f{playerPos.x - m_viewport.width / 2, playerPos.y - m_viewport.height / 2});
} }
void WorldLevel::Draw() const { void WorldLevel::Draw() const {
m_pCamera->BeginRendering(); m_pCamera->BeginRendering();
@@ -131,14 +130,17 @@ void WorldLevel::Draw() const {
} }
utils::SetColor(Colors::MAGENTA); 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_pSelectedTile->Draw();
} }
m_refeulBuilding->Draw(); m_refeulBuilding->Draw();
m_player.Draw();
utils::SetColor(Colors::GREEN);
utils::DrawArrow(Vector2f{0, 0}, m_mousePos);
m_pCamera->EndRendering(); m_pCamera->EndRendering();
@@ -153,6 +155,32 @@ void WorldLevel::MouseMove(const Vector2f& mousePos) {
} }
void WorldLevel::ProcessImGui() { 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::BeginMainMenuBar()) {
if (ImGui::BeginMenu("Properties")) { if (ImGui::BeginMenu("Properties")) {
if (ImGui::MenuItem("TextureManager Info")) { if (ImGui::MenuItem("TextureManager Info")) {
@@ -166,17 +194,17 @@ void WorldLevel::ProcessImGui() {
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }
if(ImGui::BeginMenu("Screens")) { if (ImGui::BeginMenu("Screens")) {
if(ImGui::MenuItem("Open Fuel screen")) { if (ImGui::MenuItem("Open Fuel screen")) {
ScreenManager::GetInstance()->OpenScreen(ScreenManager::Fuel); ScreenManager::GetInstance()->OpenScreen(ScreenManager::Fuel);
} }
if(ImGui::MenuItem("Open Sell screen")) { if (ImGui::MenuItem("Open Sell screen")) {
ScreenManager::GetInstance()->OpenScreen(ScreenManager::SellScreen); ScreenManager::GetInstance()->OpenScreen(ScreenManager::SellScreen);
} }
if(ImGui::MenuItem("Close Screen")) { if (ImGui::MenuItem("Close Screen")) {
ScreenManager::GetInstance()->CloseScreen(); ScreenManager::GetInstance()->CloseScreen();
} }
@@ -184,8 +212,7 @@ void WorldLevel::ProcessImGui() {
} }
const Vector2f screenPos = utils::GetMousePos(); const Vector2f screenPos = utils::GetMousePos();
const std::string mousePos = "Mouse Pos: (" + std::to_string(screenPos.x) + ", " + std::to_string(screenPos.y) + ")"; const std::string mousePos = "Mouse Pos: (" + std::to_string(screenPos.x) + ", " + std::to_string(screenPos.y) + ")";
if(ImGui::BeginMenu(mousePos.c_str())) { if (ImGui::BeginMenu(mousePos.c_str())) {
ImGui::EndMenu(); ImGui::EndMenu();
} }

View File

@@ -21,6 +21,7 @@ Player::Player(const Vector2f& Position, TextureManager* manager) : m_Position(P
8, 0.1f, Rectf { 0, 0, 70, 70 }); 8, 0.1f, Rectf { 0, 0, 70, 70 });
m_currentAnimation = m_walkAnimation; m_currentAnimation = m_walkAnimation;
} }
Collision::CollisionRect Player::GetCollisionRect() const { Collision::CollisionRect Player::GetCollisionRect() const {
Collision::CollisionRect rect = { m_Position, m_Size, m_Vel }; Collision::CollisionRect rect = { m_Position, m_Size, m_Vel };
return rect; return rect;
@@ -36,15 +37,17 @@ void Player::Draw() const {
const int frameWidth = 70; //TODO: fix this const int frameWidth = 70; //TODO: fix this
int halfFrameWidth = frameWidth / 2; int halfFrameWidth = frameWidth / 2;
float bobOffset = m_BobUp ? 1 : 0; 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() { void Player::ProcessImGui() {
ImGui::Begin("Collision Info", nullptr, ImGuiWindowFlags_AlwaysAutoResize); ImGui::Begin("Collision Info", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Text("is Grounded: %s", m_Grounded ? "true" : "false"); ImGui::Text("is Grounded: %s", m_Grounded ? "true" : "false");
ImGui::Checkbox("Draw Collision Rect", &m_DrawCollisionRect); ImGui::Checkbox("Draw Collision Rect", &m_DrawCollisionRect);
std::string currentState{}; std::string currentState {};
switch (m_State) { switch (m_State) {
case PlayerState::Idle: case PlayerState::Idle:
currentState = "Idle"; currentState = "Idle";
@@ -69,53 +72,88 @@ void Player::ProcessImGui() {
ImGui::Text("Right: %s", m_ContactMap[Collision::CollisionDirection::Right] != nullptr ? "true" : "false"); ImGui::Text("Right: %s", m_ContactMap[Collision::CollisionDirection::Right] != nullptr ? "true" : "false");
ImGui::End(); 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) { void Player::Update(float elapsedTime, WorldLevel& level) {
m_BobTimer += elapsedTime; m_BobTimer += elapsedTime;
if(m_BobTimer >= m_BobTime) { if (m_BobTimer >= m_BobTime) {
m_BobUp = !m_BobUp; m_BobUp = !m_BobUp;
m_BobTimer = 0.0f; m_BobTimer = 0.0f;
} }
//check for keys //check for keys
if(m_State != PlayerState::Digging) { if (m_State != PlayerState::Digging) {
m_Vel = Vector2f { 0, -100 }; m_Vel = Vector2f { 0, -100 };
if (utils::isKeyDown(SDL_SCANCODE_W)) { if (utils::isKeyDown(SDL_SCANCODE_W)) {
m_Vel.y = 100; m_Vel.y = 100;
m_Grounded = false; m_Grounded = false;
} }
if (utils::isKeyPressed(SDL_SCANCODE_S)) { if (utils::isKeyPressed(SDL_SCANCODE_S)) {
m_Vel.y = -100;
if (m_Grounded) { if (m_Grounded) {
if (m_ContactMap[Collision::CollisionDirection::Bottom] != nullptr) { if (this->CanDig(Collision::Bottom, level)) {
//Do the digging this->Dig(Collision::CollisionDirection::Bottom, level);
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;
} }
} }
else {
m_Vel.y = -100;
}
} }
if (utils::isKeyDown(SDL_SCANCODE_A)) { if (utils::isKeyDown(SDL_SCANCODE_A)) {
m_walkAnimation->SetFlipped(false); m_walkAnimation->SetFlipped(false);
m_Vel.x = -100; 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)) { if (utils::isKeyDown(SDL_SCANCODE_D)) {
m_Vel.x = 100; m_Vel.x = 100;
m_walkAnimation->SetFlipped(true); m_walkAnimation->SetFlipped(true);
if (m_Grounded && !m_DidJustDigRight) { if (m_Grounded && !m_DidJustDigRight) {
//Check if the player doesnt come from digging a tile //Check if the player doesnt come from digging a tile
if (m_ContactMap[Collision::CollisionDirection::Right] != nullptr) { if (this->CanDig(Collision::CollisionDirection::Right, level)) {
m_ContactMap[Collision::CollisionDirection::Right]->SetTileType(Tiles::AIR); this->Dig(Collision::CollisionDirection::Right, level);
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;
m_DidJustDigRight = true; m_DidJustDigRight = true;
} }
} }
@@ -126,6 +164,7 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
} }
} }
m_walkAnimation->Update(elapsedTime); m_walkAnimation->Update(elapsedTime);
@@ -155,8 +194,7 @@ 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; 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::CollisionRect rect = world_tile->GetCollisionRect().getCollisionRect(); //TODO: fix this mess
Collision::ResolvePlayerVsRect(*this, elapsedTime, &rect); 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) { if (m_Vel.x != 0.0f) {
m_State = PlayerState::Walking; m_State = PlayerState::Walking;
} }
@@ -207,7 +245,6 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
} }
} }
} }
switch (m_State) { switch (m_State) {
case PlayerState::Idle: case PlayerState::Idle:
m_walkAnimation->SetPlaying(false); m_walkAnimation->SetPlaying(false);
@@ -218,11 +255,17 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
case PlayerState::Digging: { case PlayerState::Digging: {
m_walkAnimation->SetPlaying(false); m_walkAnimation->SetPlaying(false);
//Diganimation //Diganimation
if (!m_Digging) { //TODO: fix for setting the start position
m_Digging = true;
m_DigStart = m_Position;
}
m_DigProgress += elapsedTime; m_DigProgress += elapsedTime;
//lerp to the destination //lerp to the destination
float progress = utils::map(m_DigProgress, 0.0f, m_DigTime, 0.0f, 1.0f); float progress = utils::map(m_DigProgress, 0.0f, m_DigTime, 0.0f, 1.0f);
std::cout << progress << '\n'; 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) { if (progress >= 0.5f && !m_HasDeletedTile) {
m_DigTile->SetTileType(Tiles::AIR); m_DigTile->SetTileType(Tiles::AIR);
m_DigTile = nullptr; m_DigTile = nullptr;
@@ -231,15 +274,15 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
if (progress >= 1.0f) { if (progress >= 1.0f) {
m_State = PlayerState::Idle; m_State = PlayerState::Idle;
m_HasDeletedTile = false; m_HasDeletedTile = false;
m_Digging = false;
} }
break; break;
} }
default: default:
break; break;
} }
if(m_State != PlayerState::Digging) { if (m_State != PlayerState::Digging) {
m_Position = m_Position + m_Vel * elapsedTime; m_Position = m_Position + m_Vel * elapsedTime;
} }
} }

View File

@@ -59,6 +59,9 @@ public:
void ProcessImGui(); void ProcessImGui();
private: private:
void Dig(Collision::CollisionDirection dir, WorldLevel& level);
bool CanDig(Collision::CollisionDirection dir, WorldLevel& level);
Vector2f m_Position; Vector2f m_Position;
Vector2f m_Size; Vector2f m_Size;
@@ -73,15 +76,18 @@ private:
bool m_BobUp{ true }; bool m_BobUp{ true };
Vector2f m_DigDestination{}; Vector2f m_DigDestination{};
bool m_Digging{ false };
Vector2f m_DigStart{};
float m_DigProgress{}; float m_DigProgress{};
bool m_HasDeletedTile{ false }; bool m_HasDeletedTile{ false };
WorldTile* m_DigTile{ nullptr }; WorldTile* m_DigTile{ nullptr };
const float m_DigTime{ 1.0f }; const float m_DigTime{ 0.5f };
bool m_Grounded { false }; bool m_Grounded { false };
bool m_DidJustDigRight { false }; bool m_DidJustDigRight { false };
bool m_DidJustDigLeft { false };
Animation* m_currentAnimation{ nullptr }; Animation* m_currentAnimation{ nullptr };
Animation* m_walkAnimation; Animation* m_walkAnimation;
Animation* m_turnAnimation; Animation* m_turnAnimation;

View File

@@ -18,4 +18,7 @@ Texture * TextureManager::GetTexture(const std::string& name) {
} }
TextureManager::~TextureManager() { TextureManager::~TextureManager() {
//TODO: Loop over the m_Textures to delete them //TODO: Loop over the m_Textures to delete them
for ( const auto &p : m_Textures ) {
delete p.second;
}
} }

View File

@@ -2,7 +2,7 @@
#include <ctime> #include <ctime>
#include "Game.h" #include "Game.h"
#define DEBUG
void StartHeapControl(); void StartHeapControl();
void DumpMemoryLeaks(); void DumpMemoryLeaks();