Rework Tile detection system

This commit is contained in:
Bram Verhulst
2024-03-28 18:53:36 +01:00
parent d441222173
commit 3b9c96ea8d
15 changed files with 252 additions and 41 deletions

View File

@@ -47,8 +47,6 @@ void Game::Update(float elapsedSec) {
void Game::Draw() const {
utils::ClearBackground(Color4f(0.0f, 0.0f, 0.3f, 1.0f));
m_WorldLevel.Draw();
TextureManager::GetInstance()->GetTexture("dingus.png")->Draw(Point2f{0,0}, {}, true);
TextureManager::GetInstance()->GetTexture("dingus.png")->Draw(Point2f{0,300}, {}, false);
}
void Game::ProcessKeyDownEvent(const SDL_KeyboardEvent& e) {
@@ -76,7 +74,7 @@ void Game::ProcessMouseUpEvent(const SDL_MouseButtonEvent& e) {
//case SDL_BUTTON_LEFT:
// std::cout << " left button " << std::endl;
// break;
//case SDL_BUTTON_RIGHT:
//case SDL_BUTTON_RIGHT:k
// std::cout << " right button " << std::endl;
// break;
//case SDL_BUTTON_MIDDLE:

View File

@@ -315,9 +315,6 @@ xcopy "$(SolutionDir)Resources\*.*" "$(TargetDir)" /y /d /s</Command>
<ClInclude Include="WorldLevel.h" />
<ClInclude Include="WorldTile.h" />
</ItemGroup>
<ItemGroup>
<Folder Include="Level\" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

@@ -67,7 +67,6 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
m_ContactMap[Collision::CollisionDirection::Bottom] = nullptr;
}
}
}
if(utils::isKeyDown(SDL_SCANCODE_A)) {
@@ -93,7 +92,7 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
if(m_DidJustDigRight) {
if(!utils::isKeyDown(SDL_SCANCODE_D)) {
m_DidJustDigRight = false;
}
}
}
m_ContactMap[Collision::CollisionDirection::Top] = nullptr;
@@ -112,9 +111,10 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
for(int y { 0 }; y < WORLD_HEIGHT; ++y) {
if(gridManager.GetTileAtIndex(x,y)->GetTileType() == Tiles::DIRT) {
gridManager.GetTileAtIndex(x,y)->m_Hightlight = false;
if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, gridManager.GetTileAtIndex(x, y)->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
WorldTile* tile = gridManager.GetTileAtIndex(x,y);
if(*tile->GetTileType() != Tiles::AIR) {
tile->m_Hightlight = false;
if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, tile->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
contactTimes.push_back(std::pair<int, float>{x + y * WORLD_WIDTH, t});
}
}

View File

@@ -15,6 +15,4 @@ Texture* TextureManager::GetTexture(const std::string& name) {
Texture* pTexture = new Texture(name);
m_Textures[name] = pTexture;
return pTexture;
}
}

View File

@@ -1,5 +1,6 @@
#pragma once
#include <array>
#include <vector>
#include "structs.h"
@@ -11,6 +12,19 @@ static const int TILE_HEIGHT = 50;
class WorldTile;
struct surroundingTiles {
std::array<WorldTile*, 8> tiles;
//Center of the surrounding tiles is 0, 0
void SetTile(int x, int y, WorldTile* tile) {
tiles[x + y * 3] = tile;
}
WorldTile* GetTile(int x, int y) {
return tiles[x + y * 3];
}
};
class WorldGridManager
{
public:

View File

@@ -23,8 +23,23 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport):
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};
GroundTileType type = rand() % 2 == 0 ? Tiles::DIRT : Tiles::DIRT;
m_gridManager.SetTileAtIndex(x,y, new WorldTile{ pos, Tiles::DIRT, TextureManager::GetInstance() });
GroundTileType* type = Tiles::AIR;
switch(utils::randRange(0,2)) {
case 0:
type = Tiles::DIRT;
break;
case 1:
type = Tiles::IRON;
break;
case 2:
//AIR
break;
default:
std::cout << "??" << std::endl;
}
m_gridManager.SetTileAtIndex(x,y, new WorldTile{ pos, type, TextureManager::GetInstance() });
}
}
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
@@ -92,7 +107,7 @@ void WorldLevel::Draw() const {
//loop over worldtiles
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
for (int y { 0 }; y < WORLD_HEIGHT; ++y) {
if(m_gridManager.GetTileAtIndex(x,y)->GetTileType() == Tiles::DIRT) {
if(*m_gridManager.GetTileAtIndex(x,y)->GetTileType() != Tiles::AIR) {
Collision::CollisionRect rect = m_gridManager.GetTileAtIndex(x,y)->GetCollisionRect().getCollisionRect();
utils::SetColor(Colors::GREEN);
utils::DrawRect(rect.pos, rect.size.x, rect.size.y);

View File

@@ -6,16 +6,16 @@
#include "utils.h"
WorldTile::WorldTile(const Point2f& position, GroundTileType groundTileType, TextureManager* pTextureManager) : m_Position { position }, m_GroundTileType { groundTileType } {
WorldTile::WorldTile(const Point2f& position, GroundTileType* groundTileType, TextureManager* pTextureManager) : m_Position { position }, m_GroundTileType { groundTileType } {
// const std::string dirtPath = + "tiles/dirt/dirt" + std::to_string(utils::randRange(1, 5)) + ".png";
// m_pTexture = new Texture(dirtPath);
m_pTexture = pTextureManager->GetTexture(groundTileType.getPath());
m_pTexture = pTextureManager->GetTexture(groundTileType->getPath());
}
WorldTile::~WorldTile() {
delete m_pTexture;
}
void WorldTile::Draw() const {
if (m_GroundTileType == Tiles::DIRT) {
if (*m_GroundTileType != Tiles::AIR) {
m_pTexture->Draw(m_Position);
if(m_Hightlight) {
utils::SetColor(Colors::GREEN);

View File

@@ -24,16 +24,20 @@ public:
return m_type != rhs.m_type;
}
bool operator== (const GroundTileType* rhs) const {
virtual bool operator== (const GroundTileType* rhs) const {
return rhs->m_type == m_type;
}
bool operator!= (const GroundTileType* rhs) const {
virtual bool operator!= (const GroundTileType* rhs) const {
return rhs->m_type != m_type;
}
virtual std::string getPath() {
virtual std::string getPath() const {
return m_filePath;
}
virtual GroundTileTypes getType() const {
return m_type;
}
protected:
std::string m_filePath;
private:
@@ -42,37 +46,48 @@ private:
class RandomGroundTile: public GroundTileType {
public:
RandomGroundTile(const std::string& filePath, GroundTileTypes type, int maxRandom): GroundTileType(filePath, type), m_variant(utils::randRange(1, maxRandom)) {
RandomGroundTile(const std::string& filePath, GroundTileTypes type, int maxRandom): GroundTileType(filePath, type), m_maxRandom(maxRandom) {
}
~RandomGroundTile() override = default;
std::string getPath() override {
bool operator== (const GroundTileType* rhs) const override{
return rhs->getType() == this->getType();
}
bool operator!= (const GroundTileType* rhs) const override{
return rhs->getType() != this->getType();
}
std::string getPath() const override {
int variant = utils::randRange(1, m_maxRandom);
std::string toReplace{ "[0]" };
std::string replacement = std::to_string(m_variant);
std::string replacement = std::to_string(utils::randRange(1, m_maxRandom));
size_t found = m_filePath.find(toReplace);
std::string newFilePath{ m_filePath };
std::string newFilePath{ m_filePath };
if(found != std::string::npos) {
newFilePath.replace(found, 3, replacement);
}
return newFilePath;
}
private:
int m_variant;
int m_maxRandom;
};
namespace Tiles
{
static GroundTileType AIR = GroundTileType("", GroundTileTypes::Air);
static GroundTileType DIRT = RandomGroundTile("tiles/dirt/dirt[0].png", GroundTileTypes::Dirt, 6);
static GroundTileType IRON = GroundTileType("tiles/ores/Ore_Ironium.png", GroundTileTypes::Iron);
static GroundTileType* AIR = new GroundTileType("", GroundTileTypes::Air);
static GroundTileType* DIRT = new RandomGroundTile("tiles/dirt/dirt[0].png", GroundTileTypes::Dirt, 5);
static GroundTileType* IRON = new GroundTileType("tiles/ores/Ore_Ironium.png", GroundTileTypes::Iron);
}
class WorldTile {
public:
WorldTile() = default;
WorldTile(const Point2f& position, GroundTileType groundTileType, TextureManager* pTextureManager);
WorldTile(const Point2f& position, GroundTileType* groundTileType, TextureManager* pTextureManager);
~WorldTile();
void Draw() const;
@@ -82,8 +97,8 @@ public:
Point2f GetSize() const { return Point2f{ 50, 50 }; }
GroundTileType GetTileType() const { return m_GroundTileType; }
void SetTileType(GroundTileType type) { m_GroundTileType = type; }
GroundTileType* GetTileType() const { return m_GroundTileType; }
void SetTileType(GroundTileType* type) { m_GroundTileType = type; }
Collision::TileCollisionRect GetCollisionRect();
@@ -92,7 +107,7 @@ public:
private:
Point2f m_Position;
GroundTileType m_GroundTileType;
GroundTileType* m_GroundTileType;
Texture* m_pTexture;