Skip to content

Commit

Permalink
TileLayer: add BaseLayer class, implement ObjectGroupLayer from Tiled…
Browse files Browse the repository at this point in the history
…, add TileMap::findLayerName()
  • Loading branch information
JonathSpirit committed Dec 27, 2024
1 parent 9f2dd29 commit a83e355
Show file tree
Hide file tree
Showing 5 changed files with 354 additions and 100 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
162 changes: 122 additions & 40 deletions includes/FastEngine/C_tilelayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,92 @@
#include "json.hpp"
#include <span>

#define FGE_LAYER_BAD_ID 0

namespace fge
{

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
* \brief A tile layer contain a matrix of global tile id and a list of TileSet
* \ingroup graphics
*
* 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 Down Expand Up @@ -133,36 +201,9 @@ 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();
[[nodiscard]] Types getType() const override;

/**
* \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 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
Expand All @@ -178,8 +219,8 @@ class FGE_API TileLayer : public fge::Transformable, public fge::Drawable
* \param gid The global tile id
*/
void setGid(fge::Vector2size position, std::span<std::shared_ptr<TileSet>> tileSets, GlobalTileId gid);
[[nodiscard]] GlobalTileId getGid(fge::Vector2size position);
[[nodiscard]] GlobalTileId getGid(fge::Vector2f position);
[[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
*
Expand All @@ -206,14 +247,55 @@ class FGE_API TileLayer : public fge::Transformable, public fge::Drawable
[[nodiscard]] fge::RectFloat getGlobalBounds() const;
[[nodiscard]] fge::RectFloat getLocalBounds() const;

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

private:
GlobalTileId g_id{1};
std::string g_name;
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
15 changes: 9 additions & 6 deletions includes/FastEngine/object/C_objTilemap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
namespace fge
{

using TileSetList = std::vector<std::shared_ptr<fge::TileSet>>;
using TileLayerList = std::vector<std::shared_ptr<fge::TileLayer>>;
using TileSetList = std::vector<std::shared_ptr<TileSet>>;
using TileLayerList = std::vector<std::shared_ptr<BaseLayer>>;

class FGE_API ObjTileMap : public fge::Object
{
Expand All @@ -41,11 +41,14 @@ class FGE_API ObjTileMap : public fge::Object

void clear();

TileSetList& getTileSets();
TileSetList const& getTileSets() const;
[[nodiscard]] TileSetList& getTileSets();
[[nodiscard]] TileSetList const& getTileSets() const;

TileLayerList& getTileLayers();
TileLayerList const& getTileLayers() const;
[[nodiscard]] TileLayerList& getTileLayers();
[[nodiscard]] TileLayerList const& getTileLayers() const;

[[nodiscard]] TileLayerList::value_type* findLayerName(std::string_view name);
[[nodiscard]] TileLayerList::value_type const* findLayerName(std::string_view name) const;

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

0 comments on commit a83e355

Please sign in to comment.