mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-17 01:21:49 +01:00
Reformat + Basic animation system
General fixes
This commit is contained in:
29
Game/GridSystem/WorldGridManager.cpp
Normal file
29
Game/GridSystem/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;
|
||||
}
|
||||
45
Game/GridSystem/WorldGridManager.h
Normal file
45
Game/GridSystem/WorldGridManager.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#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;
|
||||
|
||||
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:
|
||||
WorldGridManager();
|
||||
~WorldGridManager();
|
||||
|
||||
WorldGridManager(const WorldGridManager& other) = default;
|
||||
|
||||
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;
|
||||
|
||||
|
||||
};
|
||||
27
Game/GridSystem/WorldTile.cpp
Normal file
27
Game/GridSystem/WorldTile.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "WorldTile.h"
|
||||
|
||||
#include "colors.h"
|
||||
#include "../TextureManager.h"
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
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());
|
||||
}
|
||||
WorldTile::~WorldTile() {
|
||||
delete m_pTexture;
|
||||
}
|
||||
void WorldTile::Draw() const {
|
||||
if (*m_GroundTileType != Tiles::AIR) {
|
||||
m_pTexture->Draw(m_Position);
|
||||
if (m_Hightlight) {
|
||||
utils::SetColor(Colors::GREEN);
|
||||
utils::FillRect(m_Position, 50, 50);
|
||||
}
|
||||
}
|
||||
}
|
||||
Collision::TileCollisionRect WorldTile::GetCollisionRect() {
|
||||
return Collision::TileCollisionRect { m_Position, GetSize(), ( this ) };
|
||||
}
|
||||
134
Game/GridSystem/WorldTile.h
Normal file
134
Game/GridSystem/WorldTile.h
Normal file
@@ -0,0 +1,134 @@
|
||||
#pragma once
|
||||
#include "Collision.h"
|
||||
#include "Texture.h"
|
||||
#include "../TextureManager.h"
|
||||
|
||||
|
||||
|
||||
enum class GroundTileTypes
|
||||
{
|
||||
Air,
|
||||
Dirt,
|
||||
Stone,
|
||||
Iron
|
||||
};
|
||||
|
||||
class GroundTileType
|
||||
{
|
||||
public:
|
||||
GroundTileType(const std::string& filePath, GroundTileTypes type): m_filePath(filePath), m_type(type) {
|
||||
}
|
||||
virtual ~GroundTileType() = default;
|
||||
bool operator==(const GroundTileType& rhs) const {
|
||||
return m_type == rhs.m_type;
|
||||
}
|
||||
bool operator!=(const GroundTileType& rhs) const {
|
||||
return m_type != rhs.m_type;
|
||||
}
|
||||
|
||||
virtual bool operator==(const GroundTileType* rhs) const {
|
||||
return rhs->m_type == m_type;
|
||||
}
|
||||
virtual bool operator!=(const GroundTileType* rhs) const {
|
||||
return rhs->m_type != m_type;
|
||||
}
|
||||
|
||||
virtual std::string getPath() const {
|
||||
return m_filePath;
|
||||
}
|
||||
|
||||
virtual GroundTileTypes getType() const {
|
||||
return m_type;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::string m_filePath;
|
||||
|
||||
private:
|
||||
GroundTileTypes m_type;
|
||||
};
|
||||
|
||||
class RandomGroundTile : public GroundTileType
|
||||
{
|
||||
public:
|
||||
RandomGroundTile(const std::string& filePath, GroundTileTypes type, int maxRandom): GroundTileType(filePath, type), m_maxRandom(maxRandom) {
|
||||
}
|
||||
|
||||
~RandomGroundTile() override = default;
|
||||
|
||||
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(utils::randRange(1, m_maxRandom));
|
||||
|
||||
size_t found = m_filePath.find(toReplace);
|
||||
std::string newFilePath { m_filePath };
|
||||
|
||||
if (found != std::string::npos) {
|
||||
newFilePath.replace(found, 3, replacement);
|
||||
}
|
||||
|
||||
return newFilePath;
|
||||
}
|
||||
|
||||
private:
|
||||
int m_maxRandom;
|
||||
};
|
||||
|
||||
namespace Tiles
|
||||
{
|
||||
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();
|
||||
|
||||
void Draw() const;
|
||||
|
||||
Point2f GetPosition() const {
|
||||
return m_Position;
|
||||
}
|
||||
void SetPosition(const Point2f& position) {
|
||||
m_Position = position;
|
||||
}
|
||||
|
||||
Point2f GetSize() const {
|
||||
return Point2f { 50, 50 };
|
||||
}
|
||||
|
||||
GroundTileType * GetTileType() const {
|
||||
return m_GroundTileType;
|
||||
}
|
||||
void SetTileType(GroundTileType* type) {
|
||||
m_GroundTileType = type;
|
||||
}
|
||||
|
||||
Collision::TileCollisionRect GetCollisionRect();
|
||||
|
||||
bool m_Hightlight { false };
|
||||
|
||||
private:
|
||||
Point2f m_Position;
|
||||
GroundTileType* m_GroundTileType;
|
||||
|
||||
Texture* m_pTexture;
|
||||
|
||||
Collision::CollisionRect m_CollisionRect;
|
||||
|
||||
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user