mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2026-02-04 09:29:20 +01:00
Add more digging logic
This commit is contained in:
@@ -149,6 +149,7 @@ xcopy "$(SolutionDir)Resources\*.*" "$(TargetDir)" /y /d /s</Command>
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="Camera.cpp" />
|
<ClCompile Include="Camera.cpp" />
|
||||||
<ClCompile Include="Game.cpp" />
|
<ClCompile Include="Game.cpp" />
|
||||||
|
<ClCompile Include="WorldGridManager.cpp" />
|
||||||
<ClCompile Include="Level.cpp">
|
<ClCompile Include="Level.cpp">
|
||||||
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
|
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
|
||||||
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
|
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
|
||||||
@@ -306,6 +307,7 @@ xcopy "$(SolutionDir)Resources\*.*" "$(TargetDir)" /y /d /s</Command>
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Camera.h" />
|
<ClInclude Include="Camera.h" />
|
||||||
<ClInclude Include="Game.h" />
|
<ClInclude Include="Game.h" />
|
||||||
|
<ClInclude Include="WorldGridManager.h" />
|
||||||
<ClInclude Include="Level.h" />
|
<ClInclude Include="Level.h" />
|
||||||
<ClInclude Include="pch.h" />
|
<ClInclude Include="pch.h" />
|
||||||
<ClInclude Include="Player.h" />
|
<ClInclude Include="Player.h" />
|
||||||
|
|||||||
@@ -40,6 +40,11 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
|||||||
if(m_Grounded) {
|
if(m_Grounded) {
|
||||||
if(m_ContactMap[Collision::CollisionDirection::Bottom] != nullptr) {
|
if(m_ContactMap[Collision::CollisionDirection::Bottom] != nullptr) {
|
||||||
m_ContactMap[Collision::CollisionDirection::Bottom]->SetTileType(GroundTileTypes::Air);
|
m_ContactMap[Collision::CollisionDirection::Bottom]->SetTileType(GroundTileTypes::Air);
|
||||||
|
WorldTile* tile = m_ContactMap[Collision::CollisionDirection::Bottom];
|
||||||
|
//center of tile
|
||||||
|
Point2f tileCenter = tile->GetCollisionRect().getCollisionRect().pos + tile->GetCollisionRect().getCollisionRect().size / 2;
|
||||||
|
m_Position = Point2f{tileCenter.x - m_Size.x / 2, tileCenter.y - m_Size.y / 2};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -57,11 +62,14 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
|||||||
|
|
||||||
std::vector<std::pair<int, float>> contactTimes{};
|
std::vector<std::pair<int, float>> contactTimes{};
|
||||||
|
|
||||||
for (size_t x { 0 }; x < level.WORLD_WIDTH; ++x) {
|
WorldGridManager& gridManager = level.GetGridManager();
|
||||||
for(size_t y { 0 }; y < level.WORLD_HEIGHT; ++y) {
|
|
||||||
if(level.GetTileAt(Point2f{(float)x,(float)y})->GetTileType() == GroundTileTypes::Dirt) {
|
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||||
if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, level.GetTileAt(Point2f{(float)x, (float)y})->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
|
for(size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||||
contactTimes.push_back(std::pair<int, float>{x + y * level.WORLD_WIDTH, t});
|
if(gridManager.GetTileAtIndex(x,y)->GetTileType() == GroundTileTypes::Dirt) {
|
||||||
|
gridManager.GetTileAtIndex(x,y)->m_Hightlight = false;
|
||||||
|
if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, gridManager.GetTileAtIndex(x, y)->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
|
||||||
|
contactTimes.push_back(std::pair<int, float>{x + y * WORLD_WIDTH, t});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,18 +80,42 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
for (std::pair<int, float> contact_time : contactTimes) {
|
for (std::pair<int, float> contact_time : contactTimes) {
|
||||||
int x = contact_time.first % WorldLevel::WORLD_WIDTH;
|
int x = contact_time.first % WORLD_WIDTH;
|
||||||
int y = contact_time.first / WorldLevel::WORLD_WIDTH;
|
int y = contact_time.first / WORLD_WIDTH;
|
||||||
WorldTile* world_tile = level.GetTileAt(Point2f{(float) x, (float) y});
|
WorldTile* world_tile = gridManager.GetTileAtIndex(x,y);
|
||||||
//check if tile is next to player
|
world_tile->m_Hightlight = true;
|
||||||
|
|
||||||
//check if the collision happend beneeth the player
|
bool isDiggingCandidate = true;
|
||||||
if(world_tile->GetCollisionRect().pos.y < m_Position.y) {
|
Collision::CollisionRect tileRect = world_tile->GetCollisionRect().getCollisionRect();
|
||||||
m_Grounded = true;
|
//check if the tile is above the player
|
||||||
|
if(tileRect.pos.y > m_Position.y + m_Size.y) {
|
||||||
|
isDiggingCandidate = false;
|
||||||
|
}
|
||||||
|
//check if tile is left of player
|
||||||
|
if(tileRect.pos.x > m_Position.x + m_Size.x) {
|
||||||
|
isDiggingCandidate = false;
|
||||||
|
}
|
||||||
|
//check if tile is right of player
|
||||||
|
if(tileRect.pos.x + tileRect.size.x < m_Position.x) {
|
||||||
|
isDiggingCandidate = false;
|
||||||
|
}
|
||||||
|
//check if the collision happend under the player
|
||||||
|
if(tileRect.pos.y < m_Position.y - m_Size.y) {
|
||||||
|
//Tile is under player
|
||||||
|
|
||||||
|
//check if the center of the player is between the centers of the tiles left and right
|
||||||
|
if(m_Position.x + m_Size.x / 2 > tileRect.pos.x && m_Position.x + m_Size.x / 2 < tileRect.pos.x + tileRect.size.x) {
|
||||||
|
if(isDiggingCandidate) {
|
||||||
|
//Dig the tile
|
||||||
m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
|
m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
|
||||||
|
m_Grounded = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
|
||||||
|
//m_Grounded = true;
|
||||||
|
|
||||||
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);
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ public:
|
|||||||
auto GetContactMap(){ return m_ContactMap; }
|
auto GetContactMap(){ return m_ContactMap; }
|
||||||
void SetContactMap(Collision::CollisionDirection dir, WorldTile* tile) { m_ContactMap[dir] = tile; }
|
void SetContactMap(Collision::CollisionDirection dir, WorldTile* tile) { m_ContactMap[dir] = tile; }
|
||||||
|
|
||||||
|
void ProcessImGui();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Point2f m_Position;
|
Point2f m_Position;
|
||||||
Point2f m_Size;
|
Point2f m_Size;
|
||||||
|
|||||||
29
Game/WorldGridManager.cpp
Normal file
29
Game/WorldGridManager.cpp
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#include "WorldGridManager.h"
|
||||||
|
WorldGridManager::WorldGridManager() {
|
||||||
|
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||||
|
for(size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||||
|
m_worldTiles[x][y] = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
WorldGridManager::~WorldGridManager() {
|
||||||
|
|
||||||
|
}
|
||||||
|
WorldTile* WorldGridManager::GetTileAtIndex(const int x, const int y) const {
|
||||||
|
if (x < 0 || x >= WORLD_WIDTH || y < 0 || y >= WORLD_HEIGHT) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return m_worldTiles[x][y];
|
||||||
|
}
|
||||||
|
WorldTile* WorldGridManager::GetTileAtWorldPos(const Point2f& pos) const {
|
||||||
|
int x = int(pos.x / TILE_WIDTH + WORLD_WIDTH / 2);
|
||||||
|
int y = int(-pos.y / TILE_HEIGHT);
|
||||||
|
if (x < 0 || x >= WORLD_WIDTH || y < 0 || y >= WORLD_HEIGHT) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return m_worldTiles[x][y];
|
||||||
|
}
|
||||||
|
void WorldGridManager::SetTileAtIndex(const int x, const int y, WorldTile* tile) {
|
||||||
|
m_worldTiles[x][y] = tile;
|
||||||
|
}
|
||||||
30
Game/WorldGridManager.h
Normal file
30
Game/WorldGridManager.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
#include "structs.h"
|
||||||
|
|
||||||
|
static const int WORLD_WIDTH = 34;
|
||||||
|
static const int WORLD_HEIGHT = 34;
|
||||||
|
|
||||||
|
static const int TILE_WIDTH = 50;
|
||||||
|
static const int TILE_HEIGHT = 50;
|
||||||
|
|
||||||
|
class WorldTile;
|
||||||
|
|
||||||
|
class WorldGridManager
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
WorldGridManager();
|
||||||
|
~WorldGridManager();
|
||||||
|
|
||||||
|
WorldTile* GetTileAtIndex(const int x, const int y) const;
|
||||||
|
WorldTile* GetTileAtWorldPos(const Point2f& pos) const;
|
||||||
|
|
||||||
|
void SetTileAtIndex(const int x, const int y, WorldTile* tile);
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
std::array<std::array<WorldTile*, WORLD_WIDTH>, WORLD_HEIGHT> m_worldTiles;
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
@@ -15,14 +15,15 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport):
|
|||||||
Level(camera),
|
Level(camera),
|
||||||
m_player(Player { Point2f { 0, 100 } }),
|
m_player(Player { Point2f { 0, 100 } }),
|
||||||
m_mousePos { 0, 0 },
|
m_mousePos { 0, 0 },
|
||||||
m_viewport(viewport)
|
m_viewport(viewport),
|
||||||
|
m_gridManager(WorldGridManager())
|
||||||
{
|
{
|
||||||
// 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) {
|
||||||
int actualX = x - WORLD_WIDTH / 2;
|
int actualX = x - WORLD_WIDTH / 2;
|
||||||
Point2f pos = Point2f{ float(actualX * TILE_WIDTH), -float(y * TILE_HEIGHT) - TILE_HEIGHT};
|
Point2f pos = Point2f{ float(actualX * TILE_WIDTH), -float(y * TILE_HEIGHT) - TILE_HEIGHT};
|
||||||
m_worldTiles[x][y] = new WorldTile{ pos, GroundTileTypes::Dirt, TextureManager::GetInstance() };
|
m_gridManager.SetTileAtIndex(x,y, new WorldTile{ pos, GroundTileTypes::Dirt, TextureManager::GetInstance() });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,8 +44,8 @@ void WorldLevel::Update(float elapsedSec) {
|
|||||||
|
|
||||||
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) {
|
||||||
if(m_worldTiles[x][y]->GetCollisionRect().Contains(m_mousePos)) {
|
if(m_gridManager.GetTileAtIndex(x,y)->GetCollisionRect().Contains(m_mousePos)) {
|
||||||
selectedTile = m_worldTiles[x][y];
|
selectedTile = m_gridManager.GetTileAtIndex(x,y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,6 +57,11 @@ void WorldLevel::Update(float elapsedSec) {
|
|||||||
|
|
||||||
m_player.Update(elapsedSec, *this);
|
m_player.Update(elapsedSec, *this);
|
||||||
|
|
||||||
|
WorldTile* t = m_gridManager.GetTileAtWorldPos(m_mousePos);
|
||||||
|
if(t != nullptr) {
|
||||||
|
t->SetTileType(GroundTileTypes::Air);
|
||||||
|
}
|
||||||
|
|
||||||
//Point2f playerPos = m_player.GetPosition();
|
//Point2f playerPos = m_player.GetPosition();
|
||||||
//Point2f newCameraPos = playerPos;
|
//Point2f newCameraPos = playerPos;
|
||||||
//m_pCamera->SetPosition(newCameraPos);
|
//m_pCamera->SetPosition(newCameraPos);
|
||||||
@@ -81,15 +87,15 @@ void WorldLevel::Draw() const {
|
|||||||
|
|
||||||
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) {
|
||||||
m_worldTiles[x][y]->Draw();
|
m_gridManager.GetTileAtIndex(x,y)->Draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//loop over worldtiles
|
//loop over worldtiles
|
||||||
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
|
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||||
for (int y { 0 }; y < WORLD_HEIGHT; ++y) {
|
for (int y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||||
if(m_worldTiles[x][y]->GetTileType() == GroundTileTypes::Dirt) {
|
if(m_gridManager.GetTileAtIndex(x,y)->GetTileType() == GroundTileTypes::Dirt) {
|
||||||
Collision::CollisionRect rect = m_worldTiles[x][y]->GetCollisionRect().getCollisionRect();
|
Collision::CollisionRect rect = m_gridManager.GetTileAtIndex(x,y)->GetCollisionRect().getCollisionRect();
|
||||||
utils::SetColor(Colors::GREEN);
|
utils::SetColor(Colors::GREEN);
|
||||||
utils::DrawRect(rect.pos, rect.size.x, rect.size.y);
|
utils::DrawRect(rect.pos, rect.size.x, rect.size.y);
|
||||||
}
|
}
|
||||||
@@ -153,16 +159,7 @@ void WorldLevel::ProcessImGui() {
|
|||||||
// ImGui::Text("Size: (%f, %f)", contact.second->size.x, contact.second->size.y);
|
// ImGui::Text("Size: (%f, %f)", contact.second->size.x, contact.second->size.y);
|
||||||
// }
|
// }
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
}
|
|
||||||
}
|
|
||||||
WorldTile* WorldLevel::GetTileAt(const Point2f& pos) const {
|
|
||||||
return m_worldTiles[pos.x][pos.y];
|
|
||||||
}
|
|
||||||
void WorldLevel::SetTileAt(const Point2f& pos, WorldTile* tile) {
|
|
||||||
}
|
|
||||||
std::array<std::array<WorldTile*, WorldLevel::WORLD_WIDTH>, WorldLevel::WORLD_HEIGHT> WorldLevel::GetAllTiles() const {
|
|
||||||
return m_worldTiles;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,15 +8,11 @@
|
|||||||
#include "Collision.h"
|
#include "Collision.h"
|
||||||
#include "Player.h"
|
#include "Player.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
#include "WorldGridManager.h"
|
||||||
|
|
||||||
|
|
||||||
class WorldLevel : public Level {
|
class WorldLevel : public Level {
|
||||||
public:
|
public:
|
||||||
static const int WORLD_WIDTH = 34;
|
|
||||||
static const int WORLD_HEIGHT = 34;
|
|
||||||
|
|
||||||
static const int TILE_WIDTH = 50;
|
|
||||||
static const int TILE_HEIGHT = 50;
|
|
||||||
|
|
||||||
WorldLevel(Camera* camera, Rectf viewport);
|
WorldLevel(Camera* camera, Rectf viewport);
|
||||||
~WorldLevel() override;
|
~WorldLevel() override;
|
||||||
@@ -27,22 +23,14 @@ public:
|
|||||||
void MouseMove(const Point2f& mousePos) override;
|
void MouseMove(const Point2f& mousePos) override;
|
||||||
void ProcessImGui() override;
|
void ProcessImGui() override;
|
||||||
|
|
||||||
WorldTile* GetTileAt(const Point2f& pos) const;
|
WorldGridManager& GetGridManager() { return m_gridManager; }
|
||||||
void SetTileAt(const Point2f& pos, WorldTile* tile);
|
|
||||||
|
|
||||||
std::array<std::array<WorldTile*, WORLD_WIDTH>, WORLD_HEIGHT> GetAllTiles() const;
|
|
||||||
|
|
||||||
std::vector<Collision::CollisionRect> m_Rects;
|
std::vector<Collision::CollisionRect> m_Rects;
|
||||||
|
|
||||||
std::array<std::array<WorldTile*, WORLD_WIDTH>, WORLD_HEIGHT> GetWorldTiles() const { return m_worldTiles; }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
||||||
std::array<std::array<WorldTile*, WORLD_WIDTH>, WORLD_HEIGHT> m_worldTiles;
|
WorldGridManager m_gridManager{};
|
||||||
|
|
||||||
Player m_player;
|
Player m_player;
|
||||||
Point2f m_mousePos{};
|
Point2f m_mousePos{};
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "pch.h"
|
#include "pch.h"
|
||||||
#include "WorldTile.h"
|
#include "WorldTile.h"
|
||||||
|
|
||||||
|
#include "colors.h"
|
||||||
#include "TextureManager.h"
|
#include "TextureManager.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
@@ -20,6 +21,11 @@ void WorldTile::Draw() const {
|
|||||||
m_pTexture->Draw(m_Position);
|
m_pTexture->Draw(m_Position);
|
||||||
//utils::SetColor(Color4f{ 0.5f, 0.5f, 0.5f, 1.0f});
|
//utils::SetColor(Color4f{ 0.5f, 0.5f, 0.5f, 1.0f});
|
||||||
//utils::FillRect(m_Position.x, m_Position.y, 50, 50);
|
//utils::FillRect(m_Position.x, m_Position.y, 50, 50);
|
||||||
|
|
||||||
|
if(m_Hightlight) {
|
||||||
|
utils::SetColor(Colors::GREEN);
|
||||||
|
utils::FillRect(m_Position, 50, 50);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Collision::TileCollisionRect WorldTile::GetCollisionRect() {
|
Collision::TileCollisionRect WorldTile::GetCollisionRect() {
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ public:
|
|||||||
|
|
||||||
Collision::TileCollisionRect GetCollisionRect();
|
Collision::TileCollisionRect GetCollisionRect();
|
||||||
|
|
||||||
|
bool m_Hightlight{ false };
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Point2f m_Position;
|
Point2f m_Position;
|
||||||
|
|||||||
Reference in New Issue
Block a user