mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-17 01:21:49 +01:00
Added inheritance for the screen system
basic edge detection for tile rendering
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
#include "WorldGridManager.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "WorldTile.h"
|
||||
|
||||
WorldGridManager::WorldGridManager() {
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
@@ -9,10 +14,42 @@ WorldGridManager::WorldGridManager() {
|
||||
}
|
||||
WorldGridManager::~WorldGridManager() {
|
||||
|
||||
}
|
||||
std::vector<WorldTile*> WorldGridManager::GetSurroundingTiles(const WorldTile* world_tile) {
|
||||
std::vector<WorldTile*> tiles;
|
||||
Point2f pos = world_tile->GetPosition();
|
||||
Point2f gridCoords = this->GetIndexFromPosition(pos);
|
||||
int x = gridCoords.x;
|
||||
//TODO: Stupid fix, fix this
|
||||
int y = gridCoords.y - 1;
|
||||
if (x < 0 || x >= WORLD_WIDTH || y < 0 || y >= WORLD_HEIGHT) {
|
||||
return tiles;
|
||||
}
|
||||
for (int i { -1 }; i <= 1; ++i) {
|
||||
for (int j { -1 }; j <= 1; ++j) {
|
||||
if (i == 0 && j == 0) {
|
||||
continue;
|
||||
}
|
||||
if(x + i < 0 || x + i >= WORLD_WIDTH || y + j < 0 || y + j >= WORLD_HEIGHT) {
|
||||
continue;
|
||||
}
|
||||
WorldTile* tile = GetTileAtIndex(x + i, y + j);
|
||||
if (tile != nullptr) {
|
||||
tiles.push_back(tile);
|
||||
}
|
||||
}
|
||||
}
|
||||
return tiles;
|
||||
}
|
||||
Point2f WorldGridManager::GetIndexFromPosition(Point2f position) {
|
||||
int x = int(position.x / TILE_WIDTH + WORLD_WIDTH / 2);
|
||||
int y = int(-position.y / TILE_HEIGHT);
|
||||
return Point2f{ float(x), float(y) };
|
||||
}
|
||||
WorldTile * WorldGridManager::GetTileAtIndex(const int x, const int y) const {
|
||||
if (x < 0 || x >= WORLD_WIDTH || y < 0 || y >= WORLD_HEIGHT) {
|
||||
return nullptr;
|
||||
//TODO: see if this can be Math'ed out
|
||||
}
|
||||
return m_worldTiles[x][y];
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <vector>
|
||||
|
||||
#include "structs.h"
|
||||
|
||||
@@ -30,6 +31,8 @@ class WorldGridManager
|
||||
public:
|
||||
WorldGridManager();
|
||||
~WorldGridManager();
|
||||
std::vector<WorldTile*> GetSurroundingTiles(const WorldTile* world_tile);
|
||||
Point2f GetIndexFromPosition(Point2f position);
|
||||
|
||||
WorldGridManager(const WorldGridManager& other) = default;
|
||||
|
||||
|
||||
@@ -1,14 +1,22 @@
|
||||
#include "WorldTile.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "colors.h"
|
||||
#include "../TextureManager.h"
|
||||
#include "utils.h"
|
||||
#include "WorldGridManager.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, WorldGridManager* pGridManager) : m_Position { position }, m_GroundTileType { groundTileType }, m_pGridManager { pGridManager } {
|
||||
// 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_pBottomLeftTexture = pTextureManager->GetTexture("tiles/dirt/sidepieces/bottomRight.png");
|
||||
m_pBottomRightTexture = pTextureManager->GetTexture("tiles/dirt/sidepieces/bottomLeft.png");
|
||||
m_pTopLeftTexture = pTextureManager->GetTexture("tiles/dirt/sidepieces/topRight.png");
|
||||
m_pTopRightTexture = pTextureManager->GetTexture("tiles/dirt/sidepieces/topLeft.png");
|
||||
}
|
||||
WorldTile::~WorldTile() {
|
||||
delete m_pTexture;
|
||||
@@ -20,7 +28,49 @@ void WorldTile::Draw() const {
|
||||
utils::SetColor(Colors::GREEN);
|
||||
utils::FillRect(m_Position, 50, 50);
|
||||
}
|
||||
return;
|
||||
}
|
||||
//Tile is air, So check 8 tiles around
|
||||
// Check the 4 tiles diagonally
|
||||
Point2f CurrentIndex = m_pGridManager->GetIndexFromPosition(m_Position);
|
||||
|
||||
WorldTile* pTopLeft = m_pGridManager->GetTileAtIndex(CurrentIndex.x - 1, CurrentIndex.y - 1);
|
||||
WorldTile* pTopRight = m_pGridManager->GetTileAtIndex(CurrentIndex.x + 1, CurrentIndex.y - 1);
|
||||
WorldTile* pBottomLeft = m_pGridManager->GetTileAtIndex(CurrentIndex.x - 1, CurrentIndex.y + 1);
|
||||
WorldTile* pBottomRight = m_pGridManager->GetTileAtIndex(CurrentIndex.x + 1, CurrentIndex.y + 1);
|
||||
|
||||
|
||||
GroundTileType* pTopLeftType = pTopLeft != nullptr ? pTopLeft->GetTileType() : Tiles::AIR;
|
||||
GroundTileType* pTopRightType = pTopRight != nullptr ? pTopRight->GetTileType() : Tiles::AIR;
|
||||
GroundTileType* pBottomLeftType = pBottomLeft != nullptr ? pBottomLeft->GetTileType() : Tiles::AIR;
|
||||
GroundTileType* pBottomRightType = pBottomRight != nullptr ? pBottomRight->GetTileType() : Tiles::AIR;
|
||||
|
||||
if(pBottomLeftType == Tiles::AIR && pBottomRightType == Tiles::AIR && pTopLeftType == Tiles::AIR && pTopRightType == Tiles::AIR) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(pTopLeftType != Tiles::AIR) {
|
||||
m_pTopLeftTexture->Draw(m_Position);
|
||||
}
|
||||
if(pTopRightType != Tiles::AIR) {
|
||||
m_pTopRightTexture->Draw(m_Position);
|
||||
}
|
||||
if(pBottomLeftType != Tiles::AIR) {
|
||||
m_pBottomLeftTexture->Draw(m_Position);
|
||||
}
|
||||
if(pBottomRightType != Tiles::AIR) {
|
||||
m_pBottomRightTexture->Draw(m_Position);
|
||||
}
|
||||
|
||||
// if(m_Hightlight) {
|
||||
// //Draw a rect over the diagonal tiles
|
||||
// utils::SetColor(Colors::GREEN);
|
||||
// utils::FillRect(m_Position, 50, 50);
|
||||
// utils::FillRect(Rectf{ pTopLeft->GetPosition(), Point2f{ 50, 50} });
|
||||
// utils::FillRect(Rectf{ pTopRight->GetPosition(), Point2f{ 50, 50} });
|
||||
// utils::FillRect(Rectf{ pBottomLeft->GetPosition(), Point2f{ 50, 50} });
|
||||
// utils::FillRect(Rectf{ pBottomRight->GetPosition(), Point2f{ 50, 50} });
|
||||
// }
|
||||
}
|
||||
Collision::TileCollisionRect WorldTile::GetCollisionRect() {
|
||||
return Collision::TileCollisionRect { m_Position, GetSize(), ( this ) };
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include "Collision.h"
|
||||
#include "Texture.h"
|
||||
#include "WorldGridManager.h"
|
||||
#include "../TextureManager.h"
|
||||
|
||||
|
||||
@@ -94,10 +95,11 @@ class WorldTile
|
||||
{
|
||||
public:
|
||||
WorldTile() = default;
|
||||
WorldTile(const Point2f& position, GroundTileType* groundTileType, TextureManager* pTextureManager);
|
||||
WorldTile(const Point2f& position, GroundTileType* groundTileType, TextureManager* pTextureManager, WorldGridManager* pGridManager);
|
||||
~WorldTile();
|
||||
|
||||
void Draw() const;
|
||||
void Update();
|
||||
|
||||
Point2f GetPosition() const {
|
||||
return m_Position;
|
||||
@@ -129,6 +131,13 @@ private:
|
||||
|
||||
Collision::CollisionRect m_CollisionRect;
|
||||
|
||||
WorldGridManager* m_pGridManager;
|
||||
|
||||
Texture* m_pTopLeftTexture;
|
||||
Texture* m_pTopRightTexture;
|
||||
Texture* m_pBottomLeftTexture;
|
||||
Texture* m_pBottomRightTexture;
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user