201 lines
6.9 KiB
C++
201 lines
6.9 KiB
C++
#include "pch.h"
|
|
#include "WorldTile.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include "Camera.h"
|
|
#include "colors.h"
|
|
#include "../TextureManager.h"
|
|
#include "utils.h"
|
|
#include "WorldGridManager.h"
|
|
|
|
|
|
WorldTile::WorldTile(const Vector2f& 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/bottomLeft.png");
|
|
m_pBottomRightTexture = pTextureManager->GetTexture("tiles/dirt/sidepieces/bottomRight.png");
|
|
m_pTopLeftTexture = pTextureManager->GetTexture("tiles/dirt/sidepieces/topLeft.png");
|
|
m_pTopRightTexture = pTextureManager->GetTexture("tiles/dirt/sidepieces/topRight.png");
|
|
|
|
m_pMiddleBottomTexture = pTextureManager->GetTexture("tiles/dirt/sidepieces/middleBottom.png");
|
|
m_pMiddleTopTexture = pTextureManager->GetTexture("tiles/dirt/sidepieces/middleTop.png");
|
|
m_pMiddleLeftTexture = pTextureManager->GetTexture("tiles/dirt/sidepieces/middleLeft.png");
|
|
m_pMiddleRightTexture = pTextureManager->GetTexture("tiles/dirt/sidepieces/middleRight.png");
|
|
|
|
m_pAllTexture = pTextureManager->GetTexture("tiles/dirt/sidepieces/all.png");
|
|
}
|
|
WorldTile::~WorldTile() {
|
|
delete m_pTexture;
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
//check if it's all around dirt
|
|
bool allDirt = true;
|
|
for (int i = 0; i < 8; i++) {
|
|
WorldTile* tile = m_SurroundingTiles.GetTile(static_cast<TileDirection>(i));
|
|
if(tile != nullptr) { //Tile exists
|
|
GroundTileTypes type = tile->GetTileType()->getType();
|
|
if(type != Tiles::DIRT->getType()) {
|
|
allDirt = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if(allDirt) {
|
|
m_pAllTexture->Draw(m_Position);
|
|
return;
|
|
}
|
|
|
|
if(*m_GroundTileType == Tiles::AIR) {
|
|
WorldTile* topLeft = m_SurroundingTiles.GetTile(TileDirection::TopLeft);
|
|
if(topLeft != nullptr) {
|
|
GroundTileTypes type = topLeft->GetTileType()->getType();
|
|
if(type != Tiles::AIR->getType()) {
|
|
m_pTopLeftTexture->Draw(m_Position);
|
|
}
|
|
}
|
|
|
|
WorldTile* topRight = m_SurroundingTiles.GetTile(TileDirection::TopRight);
|
|
if(topRight != nullptr) {
|
|
GroundTileTypes type = topRight->GetTileType()->getType();
|
|
if(type != Tiles::AIR->getType()) {
|
|
m_pTopRightTexture->Draw(m_Position);
|
|
}
|
|
}
|
|
|
|
WorldTile* bottomLeft = m_SurroundingTiles.GetTile(TileDirection::BottomLeft);
|
|
if(bottomLeft != nullptr) {
|
|
GroundTileTypes type = bottomLeft->GetTileType()->getType();
|
|
if(type != Tiles::AIR->getType()) {
|
|
m_pBottomLeftTexture->Draw(m_Position);
|
|
}
|
|
}
|
|
|
|
WorldTile* bottomRight = m_SurroundingTiles.GetTile(TileDirection::BottomRight);
|
|
if(bottomRight != nullptr) {
|
|
GroundTileTypes type = bottomRight->GetTileType()->getType();
|
|
if(type != Tiles::AIR->getType()) {
|
|
m_pBottomRightTexture->Draw(m_Position);
|
|
}
|
|
}
|
|
|
|
WorldTile* middleTop = m_SurroundingTiles.GetTile(TileDirection::TopMiddle);
|
|
if(middleTop != nullptr) {
|
|
GroundTileTypes type = middleTop->GetTileType()->getType();
|
|
if(type != Tiles::AIR->getType()) {
|
|
m_pMiddleTopTexture->Draw(m_Position);
|
|
}
|
|
}
|
|
|
|
WorldTile* middleBottom = m_SurroundingTiles.GetTile(TileDirection::BottomMiddle);
|
|
if(middleBottom != nullptr) {
|
|
GroundTileTypes type = middleBottom->GetTileType()->getType();
|
|
if(type != Tiles::AIR->getType()) {
|
|
m_pMiddleBottomTexture->Draw(m_Position);
|
|
}
|
|
}
|
|
|
|
WorldTile* middleLeft = m_SurroundingTiles.GetTile(TileDirection::MiddleLeft);
|
|
if(middleLeft != nullptr) {
|
|
GroundTileTypes type = middleLeft->GetTileType()->getType();
|
|
if(type != Tiles::AIR->getType()) {
|
|
m_pMiddleLeftTexture->Draw(m_Position);
|
|
}
|
|
}
|
|
|
|
WorldTile* middleRight = m_SurroundingTiles.GetTile(TileDirection::MiddleRight);
|
|
if(middleRight != nullptr) {
|
|
GroundTileTypes type = middleRight->GetTileType()->getType();
|
|
if(type != Tiles::AIR->getType()) {
|
|
m_pMiddleRightTexture->Draw(m_Position);
|
|
}
|
|
}
|
|
|
|
WorldTile* bottomMiddle = m_SurroundingTiles.GetTile(TileDirection::BottomMiddle);
|
|
if(bottomMiddle != nullptr) {
|
|
GroundTileTypes type = bottomMiddle->GetTileType()->getType();
|
|
if(type != Tiles::AIR->getType()) {
|
|
m_pMiddleBottomTexture->Draw(m_Position);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
// 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(), Vector2f{50,50}});
|
|
// continue;
|
|
// }
|
|
// if(type != Tiles::AIR->getType()) {
|
|
// utils::SetColor(Colors::YELLOW);
|
|
// utils::FillRect(Rectf{tile->GetPosition(), Vector2f{50,50}});
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
//Tile is air, So check 8 tiles around
|
|
// Check the 4 tiles diagonally
|
|
|
|
// 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(), Vector2f{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) {
|
|
Vector2f CurrentIndex = m_pGridManager->GetIndexFromPosition(m_Position);
|
|
m_SurroundingTiles = m_pGridManager->GetSurroundingTiles(this);
|
|
Vector2f mousePos = camera->TransformMouse(Vector2f{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 ) };
|
|
}
|