mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-17 01:21:49 +01:00
Fix precompiled headers
Added edge detection (if it works :/)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "WorldGridManager.h"
|
||||
#include "pch.h"
|
||||
#include "WorldGridManager.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
@@ -15,31 +16,27 @@ WorldGridManager::WorldGridManager() {
|
||||
WorldGridManager::~WorldGridManager() {
|
||||
|
||||
}
|
||||
std::vector<WorldTile*> WorldGridManager::GetSurroundingTiles(const WorldTile* world_tile) {
|
||||
std::vector<WorldTile*> tiles;
|
||||
surroundingTiles WorldGridManager::GetSurroundingTiles(const WorldTile* world_tile) {
|
||||
surroundingTiles 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tiles.SetTile(TileDirection::TopLeft, this->GetTileAtIndex(x - 1, y - 1));
|
||||
tiles.SetTile(TileDirection::TopMiddle, this->GetTileAtIndex(x, y - 1));
|
||||
tiles.SetTile(TileDirection::TopRight, this->GetTileAtIndex(x + 1, y - 1));
|
||||
|
||||
tiles.SetTile(TileDirection::MiddleLeft, this->GetTileAtIndex(x - 1, y));
|
||||
tiles.SetTile(TileDirection::MiddleRight, this->GetTileAtIndex(x + 1, y));
|
||||
|
||||
tiles.SetTile(TileDirection::BottomLeft, this->GetTileAtIndex(x - 1, y + 1));
|
||||
tiles.SetTile(TileDirection::BottomMiddle, this->GetTileAtIndex(x, y + 1));
|
||||
tiles.SetTile(TileDirection::BottomRight, this->GetTileAtIndex(x + 1, y + 1));
|
||||
|
||||
return tiles;
|
||||
|
||||
}
|
||||
Point2f WorldGridManager::GetIndexFromPosition(Point2f position) {
|
||||
int x = int(position.x / TILE_WIDTH + WORLD_WIDTH / 2);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "structs.h"
|
||||
@@ -12,17 +13,32 @@ static const int TILE_HEIGHT = 50;
|
||||
|
||||
class WorldTile;
|
||||
|
||||
enum TileDirection
|
||||
{
|
||||
TopLeft,
|
||||
TopMiddle,
|
||||
TopRight,
|
||||
|
||||
MiddleLeft,
|
||||
//me
|
||||
MiddleRight,
|
||||
|
||||
BottomLeft,
|
||||
BottomMiddle,
|
||||
BottomRight
|
||||
};
|
||||
|
||||
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;
|
||||
std::map<TileDirection, WorldTile *> m_tiles;
|
||||
|
||||
void SetTile(TileDirection direction, WorldTile* tile) {
|
||||
m_tiles[direction] = tile;
|
||||
}
|
||||
|
||||
WorldTile * GetTile(int x, int y) {
|
||||
return tiles[x + y * 3];
|
||||
WorldTile* GetTile(TileDirection direction) {
|
||||
return m_tiles[direction];
|
||||
}
|
||||
};
|
||||
|
||||
@@ -31,7 +47,7 @@ class WorldGridManager
|
||||
public:
|
||||
WorldGridManager();
|
||||
~WorldGridManager();
|
||||
std::vector<WorldTile*> GetSurroundingTiles(const WorldTile* world_tile);
|
||||
surroundingTiles GetSurroundingTiles(const WorldTile* world_tile);
|
||||
Point2f GetIndexFromPosition(Point2f position);
|
||||
|
||||
WorldGridManager(const WorldGridManager& other) = default;
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "WorldTile.h"
|
||||
#include "pch.h"
|
||||
#include "WorldTile.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "Camera.h"
|
||||
#include "colors.h"
|
||||
#include "../TextureManager.h"
|
||||
#include "utils.h"
|
||||
@@ -21,56 +23,78 @@ WorldTile::WorldTile(const Point2f& position, GroundTileType* groundTileType, Te
|
||||
WorldTile::~WorldTile() {
|
||||
delete m_pTexture;
|
||||
}
|
||||
void WorldTile::Draw() const {
|
||||
void WorldTile::Draw() {
|
||||
if (*m_GroundTileType != Tiles::AIR) {
|
||||
m_pTexture->Draw(m_Position);
|
||||
if (m_Hightlight) {
|
||||
utils::SetColor(Colors::GREEN);
|
||||
utils::FillRect(m_Position, 50, 50);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if(m_Hightlight) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
WorldTile* tile = m_SurroundingTiles.GetTile(static_cast<TileDirection>(i));
|
||||
if(tile != nullptr) { //Tile exists
|
||||
//TODO: Wow Big mess
|
||||
GroundTileTypes type = tile->GetTileType()->getType();
|
||||
if(type == Tiles::AIR->getType()) {
|
||||
utils::SetColor(Colors::BLACK);
|
||||
utils::FillRect(Rectf{tile->GetPosition(), Point2f{50,50}});
|
||||
continue;
|
||||
}
|
||||
if(type != Tiles::AIR->getType()) {
|
||||
utils::SetColor(Colors::YELLOW);
|
||||
utils::FillRect(Rectf{tile->GetPosition(), Point2f{50,50}});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//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} });
|
||||
// m_SurroundingTiles = m_pGridManager->GetSurroundingTiles(this);
|
||||
//
|
||||
// //check if all tiles are air
|
||||
// bool allAir = true;
|
||||
// for (int i = 0; i < 8; i++) {
|
||||
// WorldTile* tile = m_SurroundingTiles.GetTile(static_cast<TileDirection>(i));
|
||||
// if(tile != nullptr) { //Tile exists
|
||||
// if(tile->GetTileType() != Tiles::AIR) {
|
||||
// allAir = false;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if (allAir) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// WorldTile* topLeft = m_SurroundingTiles.GetTile(TileDirection::TopLeft); //TODO: ask if draw needs to be const
|
||||
// GroundTileType* topLeftType = topLeft != nullptr ? topLeft->GetTileType() : Tiles::AIR;
|
||||
// if(topLeftType != Tiles::AIR) {
|
||||
// m_pTopLeftTexture->Draw(m_Position);
|
||||
// utils::SetColor(Colors::YELLOW);
|
||||
// utils::FillRect(Rectf{topLeft->GetPosition(), Point2f{50,50}});
|
||||
// }
|
||||
//
|
||||
// WorldTile* topRight = m_SurroundingTiles.GetTile(TileDirection::TopRight);
|
||||
// GroundTileType* topRightType = topRight != nullptr ? topRight->GetTileType() : Tiles::AIR;
|
||||
// if(topRightType != Tiles::AIR) {
|
||||
// m_pTopRightTexture->Draw(m_Position);
|
||||
// }
|
||||
//
|
||||
}
|
||||
void WorldTile::Update(Camera* camera) {
|
||||
Point2f CurrentIndex = m_pGridManager->GetIndexFromPosition(m_Position);
|
||||
m_SurroundingTiles = m_pGridManager->GetSurroundingTiles(this);
|
||||
Point2f mousePos = camera->TransformMouse(Point2f{utils::GetMousePos().x, 500 - utils::GetMousePos().y});
|
||||
m_Hightlight = utils::IsPointInRect(mousePos, Rectf{GetCollisionRect().pos, GetCollisionRect().size});
|
||||
if(CurrentIndex.x == 1 && CurrentIndex.y == 1) {
|
||||
std::cout << "Hey" << std::endl;
|
||||
}
|
||||
}
|
||||
Collision::TileCollisionRect WorldTile::GetCollisionRect() {
|
||||
return Collision::TileCollisionRect { m_Position, GetSize(), ( this ) };
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "WorldGridManager.h"
|
||||
#include "../TextureManager.h"
|
||||
|
||||
|
||||
class Camera;
|
||||
|
||||
enum class GroundTileTypes
|
||||
{
|
||||
@@ -98,8 +98,8 @@ public:
|
||||
WorldTile(const Point2f& position, GroundTileType* groundTileType, TextureManager* pTextureManager, WorldGridManager* pGridManager);
|
||||
~WorldTile();
|
||||
|
||||
void Draw() const;
|
||||
void Update();
|
||||
void Draw();
|
||||
void Update(Camera* camera); //TODO: no use
|
||||
|
||||
Point2f GetPosition() const {
|
||||
return m_Position;
|
||||
@@ -133,6 +133,8 @@ private:
|
||||
|
||||
WorldGridManager* m_pGridManager;
|
||||
|
||||
surroundingTiles m_SurroundingTiles;
|
||||
|
||||
Texture* m_pTopLeftTexture;
|
||||
Texture* m_pTopRightTexture;
|
||||
Texture* m_pBottomLeftTexture;
|
||||
|
||||
Reference in New Issue
Block a user