mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-16 03:41:48 +01:00
Add more digging logic
This commit is contained in:
@@ -149,6 +149,7 @@ xcopy "$(SolutionDir)Resources\*.*" "$(TargetDir)" /y /d /s</Command>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Camera.cpp" />
|
||||
<ClCompile Include="Game.cpp" />
|
||||
<ClCompile Include="WorldGridManager.cpp" />
|
||||
<ClCompile Include="Level.cpp">
|
||||
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
|
||||
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
|
||||
@@ -306,6 +307,7 @@ xcopy "$(SolutionDir)Resources\*.*" "$(TargetDir)" /y /d /s</Command>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Camera.h" />
|
||||
<ClInclude Include="Game.h" />
|
||||
<ClInclude Include="WorldGridManager.h" />
|
||||
<ClInclude Include="Level.h" />
|
||||
<ClInclude Include="pch.h" />
|
||||
<ClInclude Include="Player.h" />
|
||||
|
||||
@@ -40,6 +40,11 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
||||
if(m_Grounded) {
|
||||
if(m_ContactMap[Collision::CollisionDirection::Bottom] != nullptr) {
|
||||
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};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -56,12 +61,15 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
||||
Point2f intersectionPoint, normal;
|
||||
|
||||
std::vector<std::pair<int, float>> contactTimes{};
|
||||
|
||||
WorldGridManager& gridManager = level.GetGridManager();
|
||||
|
||||
for (size_t x { 0 }; x < level.WORLD_WIDTH; ++x) {
|
||||
for(size_t y { 0 }; y < level.WORLD_HEIGHT; ++y) {
|
||||
if(level.GetTileAt(Point2f{(float)x,(float)y})->GetTileType() == GroundTileTypes::Dirt) {
|
||||
if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, level.GetTileAt(Point2f{(float)x, (float)y})->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
|
||||
contactTimes.push_back(std::pair<int, float>{x + y * level.WORLD_WIDTH, t});
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for(size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
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,19 +80,43 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
||||
});
|
||||
|
||||
for (std::pair<int, float> contact_time : contactTimes) {
|
||||
int x = contact_time.first % WorldLevel::WORLD_WIDTH;
|
||||
int y = contact_time.first / WorldLevel::WORLD_WIDTH;
|
||||
WorldTile* world_tile = level.GetTileAt(Point2f{(float) x, (float) y});
|
||||
//check if tile is next to player
|
||||
int x = contact_time.first % WORLD_WIDTH;
|
||||
int y = contact_time.first / WORLD_WIDTH;
|
||||
WorldTile* world_tile = gridManager.GetTileAtIndex(x,y);
|
||||
world_tile->m_Hightlight = true;
|
||||
|
||||
//check if the collision happend beneeth the player
|
||||
if(world_tile->GetCollisionRect().pos.y < m_Position.y) {
|
||||
m_Grounded = true;
|
||||
m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
|
||||
bool isDiggingCandidate = true;
|
||||
Collision::CollisionRect tileRect = world_tile->GetCollisionRect().getCollisionRect();
|
||||
//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_Grounded = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
|
||||
//m_Grounded = true;
|
||||
|
||||
Collision::CollisionRect rect = world_tile->GetCollisionRect().getCollisionRect(); //TODO: fix this mess
|
||||
Collision::ResolvePlayerVsRect(*this, elapsedTime, &rect);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ public:
|
||||
auto GetContactMap(){ return m_ContactMap; }
|
||||
void SetContactMap(Collision::CollisionDirection dir, WorldTile* tile) { m_ContactMap[dir] = tile; }
|
||||
|
||||
void ProcessImGui();
|
||||
|
||||
private:
|
||||
Point2f m_Position;
|
||||
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),
|
||||
m_player(Player { Point2f { 0, 100 } }),
|
||||
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
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
int actualX = x - WORLD_WIDTH / 2;
|
||||
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 y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
if(m_worldTiles[x][y]->GetCollisionRect().Contains(m_mousePos)) {
|
||||
selectedTile = m_worldTiles[x][y];
|
||||
if(m_gridManager.GetTileAtIndex(x,y)->GetCollisionRect().Contains(m_mousePos)) {
|
||||
selectedTile = m_gridManager.GetTileAtIndex(x,y);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,6 +57,11 @@ void WorldLevel::Update(float elapsedSec) {
|
||||
|
||||
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 newCameraPos = playerPos;
|
||||
//m_pCamera->SetPosition(newCameraPos);
|
||||
@@ -81,15 +87,15 @@ void WorldLevel::Draw() const {
|
||||
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
m_worldTiles[x][y]->Draw();
|
||||
m_gridManager.GetTileAtIndex(x,y)->Draw();
|
||||
}
|
||||
}
|
||||
|
||||
//loop over worldtiles
|
||||
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for (int y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
if(m_worldTiles[x][y]->GetTileType() == GroundTileTypes::Dirt) {
|
||||
Collision::CollisionRect rect = m_worldTiles[x][y]->GetCollisionRect().getCollisionRect();
|
||||
if(m_gridManager.GetTileAtIndex(x,y)->GetTileType() == GroundTileTypes::Dirt) {
|
||||
Collision::CollisionRect rect = m_gridManager.GetTileAtIndex(x,y)->GetCollisionRect().getCollisionRect();
|
||||
utils::SetColor(Colors::GREEN);
|
||||
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::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 "Player.h"
|
||||
#include "utils.h"
|
||||
#include "WorldGridManager.h"
|
||||
|
||||
|
||||
class WorldLevel : public Level {
|
||||
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() override;
|
||||
@@ -27,22 +23,14 @@ public:
|
||||
void MouseMove(const Point2f& mousePos) override;
|
||||
void ProcessImGui() override;
|
||||
|
||||
WorldTile* GetTileAt(const Point2f& pos) const;
|
||||
void SetTileAt(const Point2f& pos, WorldTile* tile);
|
||||
|
||||
std::array<std::array<WorldTile*, WORLD_WIDTH>, WORLD_HEIGHT> GetAllTiles() const;
|
||||
WorldGridManager& GetGridManager() { return m_gridManager; }
|
||||
|
||||
std::vector<Collision::CollisionRect> m_Rects;
|
||||
|
||||
std::array<std::array<WorldTile*, WORLD_WIDTH>, WORLD_HEIGHT> GetWorldTiles() const { return m_worldTiles; }
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
|
||||
std::array<std::array<WorldTile*, WORLD_WIDTH>, WORLD_HEIGHT> m_worldTiles;
|
||||
|
||||
WorldGridManager m_gridManager{};
|
||||
Player m_player;
|
||||
Point2f m_mousePos{};
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "pch.h"
|
||||
#include "WorldTile.h"
|
||||
|
||||
#include "colors.h"
|
||||
#include "TextureManager.h"
|
||||
#include "utils.h"
|
||||
|
||||
@@ -20,6 +21,11 @@ void WorldTile::Draw() const {
|
||||
m_pTexture->Draw(m_Position);
|
||||
//utils::SetColor(Color4f{ 0.5f, 0.5f, 0.5f, 1.0f});
|
||||
//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() {
|
||||
|
||||
@@ -26,6 +26,8 @@ public:
|
||||
void SetTileType(GroundTileTypes type) { m_GroundTileType = type; }
|
||||
|
||||
Collision::TileCollisionRect GetCollisionRect();
|
||||
|
||||
bool m_Hightlight{ false };
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user