Skip to content

Commit

Permalink
Merge branch 'TilemapCleaning'
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathSpirit committed Jan 5, 2025
2 parents 1b7d45f + a83e355 commit e9c73e8
Show file tree
Hide file tree
Showing 10 changed files with 526 additions and 172 deletions.
4 changes: 2 additions & 2 deletions examples/tileMapAndPathFinding_001/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class PathFinder : public fge::Object
this->g_pathGenerator.clearCollisions();

//Get the front tile layer
auto tileLayer = tileMap->getTileLayers().front();
auto tileLayer = tileMap->getTileLayers().front()->as<fge::TileLayer>();

//For every tiles
for (std::size_t x = 0; x < tileLayer->getTiles().getSizeX(); ++x)
Expand Down Expand Up @@ -207,7 +207,7 @@ class MainScene : public fge::Scene
tileMap->loadFromFile("resources/tilemaps/tilemap_basic_1.json", false);

//Get the tileMap size
auto tileMapSize = tileMap->getTileLayers().front()->getTiles().getSize();
auto tileMapSize = tileMap->getTileLayers().front()->as<fge::TileLayer>()->getTiles().getSize();

//Create a pathfinder object
auto* pathFinder = this->newObject<PathFinder>({FGE_SCENE_PLAN_TOP});
Expand Down
1 change: 1 addition & 0 deletions includes/FastEngine/C_rect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Rect
[[nodiscard]] bool operator!=(Rect<T> const& right) const;

[[nodiscard]] bool contains(Vector2<T> const& point) const;
[[nodiscard]] bool contains(Rect<T> const& rectangle) const;
[[nodiscard]] std::optional<Rect<T>> findIntersection(Rect<T> const& rectangle) const;

[[nodiscard]] Vector2<T> getPosition() const;
Expand Down
24 changes: 24 additions & 0 deletions includes/FastEngine/C_rect.inl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ bool Rect<T>::contains(Vector2<T> const& point) const

return (point.x >= minX) && (point.x < maxX) && (point.y >= minY) && (point.y < maxY);
}
template<class T>
bool Rect<T>::contains(Rect<T> const& rectangle) const
{
// Rectangles with negative dimensions are allowed, so we must handle them correctly
T const r1FarX = static_cast<T>(this->_x + this->_width);
T const r1FarY = static_cast<T>(this->_y + this->_height);

T const r2FarX = static_cast<T>(rectangle._x + rectangle._width);
T const r2FarY = static_cast<T>(rectangle._y + rectangle._height);

// Compute the min and max of the first rectangle on both axes
T const r1MinX = (this->_x < r1FarX) ? this->_x : r1FarX;
T const r1MaxX = (this->_x > r1FarX) ? this->_x : r1FarX;
T const r1MinY = (this->_y < r1FarY) ? this->_y : r1FarY;
T const r1MaxY = (this->_y > r1FarY) ? this->_y : r1FarY;

// Compute the min and max of the second rectangle on both axes
T const r2MinX = (rectangle._x < r2FarX) ? rectangle._x : r2FarX;
T const r2MaxX = (rectangle._x > r2FarX) ? rectangle._x : r2FarX;
T const r2MinY = (rectangle._y < r2FarY) ? rectangle._y : r2FarY;
T const r2MaxY = (rectangle._y > r2FarY) ? rectangle._y : r2FarY;

return (r1MinX <= r2MinX) && (r1MaxX >= r2MaxX) && (r1MinY <= r2MinY) && (r1MaxY >= r2MaxY);
}

template<class T>
std::optional<Rect<T>> Rect<T>::findIntersection(Rect<T> const& rectangle) const
Expand Down
201 changes: 144 additions & 57 deletions includes/FastEngine/C_tilelayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,85 @@
#include "FastEngine/graphic/C_transformable.hpp"
#include "FastEngine/vulkan/C_vertexBuffer.hpp"
#include "json.hpp"
#include <span>

#define FGE_LAYER_BAD_ID 0

namespace fge
{

using TileId = int32_t;
using TileSetList = std::vector<std::shared_ptr<fge::TileSet>>;
using GlobalTileId = int32_t;
using LocalTileId = int32_t;

#ifdef FGE_DEF_SERVER
class FGE_API BaseLayer : public fge::Transformable
#else
class FGE_API BaseLayer : public fge::Transformable, public fge::Drawable
#endif
{
public:
enum class Types
{
TILE_LAYER,
OBJECT_GROUP
};

BaseLayer() = default;
~BaseLayer() override = default;

[[nodiscard]] virtual Types getType() const = 0;

virtual void clear();

/**
* \brief Set the id of the layer (mostly for "Tiled" map editor compatibility)
*
* \param id The id of the layer
*/
void setId(GlobalTileId id);
/**
* \brief Get the id of the layer
*
* \return The id of the layer
*/
[[nodiscard]] GlobalTileId getId() const;

/**
* \brief Set the name of the layer
*
* \param name The name of the layer
*/
void setName(std::string_view name);
/**
* \brief Get the name of the layer
*
* \return The name of the layer
*/
[[nodiscard]] std::string const& getName() const;

virtual void save(nlohmann::json& jsonObject);
virtual void load(nlohmann::json const& jsonObject, std::filesystem::path const& filePath);

[[nodiscard]] static std::shared_ptr<BaseLayer> loadLayer(nlohmann::json const& jsonObject,
std::filesystem::path const& filePath);

template<class T>
constexpr T const* as() const
{
static_assert(std::is_base_of_v<BaseLayer, T>, "T must inherit from BaseLayer");
return static_cast<T*>(this);
}
template<class T>
constexpr T* as()
{
static_assert(std::is_base_of_v<BaseLayer, T>, "T must inherit from BaseLayer");
return static_cast<T*>(this);
}

private:
std::string g_name;
GlobalTileId g_id{FGE_LAYER_BAD_ID};
};

/**
* \class TileLayer
Expand All @@ -39,11 +112,7 @@ using TileSetList = std::vector<std::shared_ptr<fge::TileSet>>;
*
* This class is compatible with the "Tiled" map editor.
*/
#ifdef FGE_DEF_SERVER
class FGE_API TileLayer : public fge::Transformable
#else
class FGE_API TileLayer : public fge::Transformable, public fge::Drawable
#endif
class FGE_API TileLayer : public BaseLayer
{
public:
/**
Expand All @@ -63,13 +132,13 @@ class FGE_API TileLayer : public fge::Transformable, public fge::Drawable
*
* \param gid The global id of the tile
*/
void setGid(TileId gid);
void setGid(GlobalTileId gid);
/**
* \brief Get the global id of the tile
*
* \return The global id of the tile
*/
[[nodiscard]] TileId getGid() const;
[[nodiscard]] GlobalTileId getGid() const;

/**
* \brief Set the local position of the tile
Expand Down Expand Up @@ -112,11 +181,13 @@ class FGE_API TileLayer : public fge::Transformable, public fge::Drawable
*/
[[nodiscard]] std::shared_ptr<fge::TileSet> const& getTileSet() const;

[[nodiscard]] TileData const* getTileData() const;

private:
void updatePositions();
void updateTexCoords();

TileId g_gid{0};
GlobalTileId g_gid{0};
std::shared_ptr<fge::TileSet> g_tileSet;
fge::vulkan::VertexBuffer g_vertexBuffer;
fge::Vector2f g_position;
Expand All @@ -130,85 +201,101 @@ class FGE_API TileLayer : public fge::Transformable, public fge::Drawable
void draw(fge::RenderTarget& target, fge::RenderStates const& states) const override;
#endif

/**
* \brief Clear the matrix of tiles
*/
void clear();

/**
* \brief Set the id of the layer (mostly for "Tiled" map editor compatibility)
*
* \param id The id of the layer
*/
void setId(TileId id);
/**
* \brief Get the id of the layer
*
* \return The id of the layer
*/
[[nodiscard]] TileId getId() const;
[[nodiscard]] Types getType() const override;

/**
* \brief Set the name of the layer
*
* \param name The name of the layer
*/
void setName(std::string name);
/**
* \brief Get the name of the layer
*
* \return The name of the layer
*/
[[nodiscard]] std::string const& getName() const;
void clear() override;

/**
* \brief Get the matrix of tiles
*
* \return The matrix of tiles
*/
[[nodiscard]] fge::Matrix<TileLayer::Tile> const& getTiles() const;
[[nodiscard]] fge::Matrix<Tile> const& getTiles() const;
/**
* \brief Shortcut to set a global tile id and a new tileset
*
* \param x The x position of the tile
* \param y The y position of the tile
* \param position The position of the tile
* \param tileSets The list of tilesets
* \param gid The global tile id
*/
void setGid(std::size_t x, std::size_t y, TileSetList const& tileSets, TileId gid);
void setGid(fge::Vector2size position, std::span<std::shared_ptr<TileSet>> tileSets, GlobalTileId gid);
[[nodiscard]] GlobalTileId getGid(fge::Vector2size position) const;
[[nodiscard]] std::optional<fge::Vector2size> getGridPosition(fge::Vector2f position) const;
/**
* \brief Shortcut to set a global tile id
*
* \param x The x position of the tile
* \param y The y position of the tile
* \param position The position of the tile
* \param gid The global tile id
*/
void setGid(std::size_t x, std::size_t y, TileId gid);
void setGid(fge::Vector2size position, GlobalTileId gid);
/**
* \brief Set the tiles matrix size
*
* \param x The x size of the matrix
* \param y The y size of the matrix
* \param size The size of the matrix
*/
void setGridSize(std::size_t x, std::size_t y);
void setGridSize(fge::Vector2size size);

/**
* \brief Refresh all tiles with a list of tilesets
*
* \param tileSets The list of tilesets
*/
void refreshTextures(TileSetList const& tileSets);
void refreshTextures(std::span<std::shared_ptr<TileSet>> tileSets);
[[nodiscard]] static std::shared_ptr<TileSet>
retrieveAssociatedTileSet(std::span<std::shared_ptr<TileSet>> tileSets, GlobalTileId gid);

private:
static std::shared_ptr<fge::TileSet> retrieveAssociatedTileSet(TileSetList const& tileSets, TileId gid);
[[nodiscard]] fge::RectFloat getGlobalBounds() const;
[[nodiscard]] fge::RectFloat getLocalBounds() const;

TileId g_id{1};
std::string g_name;
fge::Matrix<TileLayer::Tile> g_data;
void save(nlohmann::json& jsonObject) override;
void load(nlohmann::json const& jsonObject, std::filesystem::path const& filePath) override;

private:
fge::Matrix<Tile> g_tiles;
};

FGE_API void to_json(nlohmann::json& j, fge::TileLayer const& p);
FGE_API void from_json(nlohmann::json const& j, fge::TileLayer& p);
/**
* \class ObjectGroupLayer
* \brief An object group layer contain some objects defined by the user
* \ingroup graphics
*
* This class is compatible with the "Tiled" map editor.
*/
class FGE_API ObjectGroupLayer : public BaseLayer
{
public:
ObjectGroupLayer() = default;

struct Object
{
fge::Vector2f _position;
fge::Vector2f _size;
std::string _name;
LocalTileId _id;
float _rotation;
bool _point;
};

#ifndef FGE_DEF_SERVER
void draw(fge::RenderTarget& target, fge::RenderStates const& states) const override;
#endif

[[nodiscard]] Types getType() const override;

void clear() override;

[[nodiscard]] std::vector<Object> const& getObjects() const;
[[nodiscard]] std::vector<Object>& getObjects();

[[nodiscard]] Object* findObjectName(std::string_view name);
[[nodiscard]] Object const* findObjectName(std::string_view name) const;

void save(nlohmann::json& jsonObject) override;
void load(nlohmann::json const& jsonObject, std::filesystem::path const& filePath) override;

private:
std::vector<Object> g_objects;
};

} // namespace fge

Expand Down
Loading

0 comments on commit e9c73e8

Please sign in to comment.