FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_tilelayer.hpp
1/*
2 * Copyright 2025 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_TILELAYER_HPP_INCLUDED
18#define _FGE_C_TILELAYER_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/C_matrix.hpp"
22#include "FastEngine/C_tileset.hpp"
23#include "FastEngine/graphic/C_color.hpp"
24#include "FastEngine/graphic/C_drawable.hpp"
25#include "FastEngine/graphic/C_transformable.hpp"
26#include "FastEngine/vulkan/C_vertexBuffer.hpp"
27#include "json.hpp"
28#include <span>
29
30#define FGE_LAYER_BAD_ID 0
31
32namespace fge
33{
34
35using GlobalTileId = int32_t;
36using LocalTileId = int32_t;
37
38#ifdef FGE_DEF_SERVER
39class FGE_API BaseLayer : public fge::Transformable
40#else
41class FGE_API BaseLayer : public fge::Transformable, public fge::Drawable
42#endif
43{
44public:
45 enum class Types
46 {
47 TILE_LAYER,
48 OBJECT_GROUP
49 };
50
51 BaseLayer() = default;
52 ~BaseLayer() override = default;
53
54 [[nodiscard]] virtual Types getType() const = 0;
55
56 virtual void clear();
57
63 void setId(GlobalTileId id);
69 [[nodiscard]] GlobalTileId getId() const;
70
76 void setName(std::string_view name);
82 [[nodiscard]] std::string const& getName() const;
83
84 virtual void save(nlohmann::json& jsonObject);
85 virtual void load(nlohmann::json const& jsonObject, std::filesystem::path const& filePath);
86
87 [[nodiscard]] static std::shared_ptr<BaseLayer> loadLayer(nlohmann::json const& jsonObject,
88 std::filesystem::path const& filePath);
89
90 template<class T>
91 constexpr T const* as() const
92 {
93 static_assert(std::is_base_of_v<BaseLayer, T>, "T must inherit from BaseLayer");
94 return static_cast<T*>(this);
95 }
96 template<class T>
97 constexpr T* as()
98 {
99 static_assert(std::is_base_of_v<BaseLayer, T>, "T must inherit from BaseLayer");
100 return static_cast<T*>(this);
101 }
102
103private:
104 std::string g_name;
105 GlobalTileId g_id{FGE_LAYER_BAD_ID};
106};
107
115class FGE_API TileLayer : public BaseLayer
116{
117public:
123 class FGE_API Tile
124 {
125 public:
126 Tile();
127
135 void setGid(GlobalTileId gid);
141 [[nodiscard]] GlobalTileId getGid() const;
142
148 void setPosition(Vector2f const& position);
154 [[nodiscard]] Vector2f const& getPosition() const;
155
161 void setColor(Color const& color);
167 [[nodiscard]] Color getColor() const;
168
176 void setTileSet(std::shared_ptr<TileSet> const& tileSet);
182 [[nodiscard]] std::shared_ptr<TileSet> getTileSet() const;
183
184 [[nodiscard]] TileData const* getTileData() const;
185
186 private:
187 void updatePositions();
188 void updateTexCoords();
189
190 GlobalTileId g_gid{0};
191 std::weak_ptr<TileSet> g_tileSet;
192 vulkan::VertexBuffer g_vertexBuffer;
193 Vector2f g_position;
194
195 friend TileLayer;
196 };
197
198 TileLayer() = default;
199
200#ifndef FGE_DEF_SERVER
201 void draw(fge::RenderTarget& target, fge::RenderStates const& states) const override;
202#endif
203
204 [[nodiscard]] Types getType() const override;
205
206 void clear() override;
207
213 [[nodiscard]] fge::Matrix<Tile> const& getTiles() const;
221 void setGid(fge::Vector2size position, std::span<std::shared_ptr<TileSet>> tileSets, GlobalTileId gid);
222 [[nodiscard]] GlobalTileId getGid(fge::Vector2size position) const;
223 [[nodiscard]] std::optional<fge::Vector2size> getGridPosition(fge::Vector2f position) const;
230 void setGid(fge::Vector2size position, GlobalTileId gid);
236 void setGridSize(fge::Vector2size size);
237
243 void refreshTextures(std::span<std::shared_ptr<TileSet>> tileSets);
244 [[nodiscard]] static std::shared_ptr<TileSet>
245 retrieveAssociatedTileSet(std::span<std::shared_ptr<TileSet>> tileSets, GlobalTileId gid);
246
247 [[nodiscard]] fge::RectFloat getGlobalBounds() const;
248 [[nodiscard]] fge::RectFloat getLocalBounds() const;
249
250 void save(nlohmann::json& jsonObject) override;
251 void load(nlohmann::json const& jsonObject, std::filesystem::path const& filePath) override;
252
253private:
254 fge::Matrix<Tile> g_tiles;
255};
256
264class FGE_API ObjectGroupLayer : public BaseLayer
265{
266public:
267 ObjectGroupLayer() = default;
268
269 struct Object
270 {
271 fge::Vector2f _position;
272 fge::Vector2f _size;
273 std::string _name;
274 LocalTileId _id;
275 float _rotation;
276 bool _point;
277 };
278
279#ifndef FGE_DEF_SERVER
280 void draw(fge::RenderTarget& target, fge::RenderStates const& states) const override;
281#endif
282
283 [[nodiscard]] Types getType() const override;
284
285 void clear() override;
286
287 [[nodiscard]] std::vector<Object> const& getObjects() const;
288 [[nodiscard]] std::vector<Object>& getObjects();
289
290 [[nodiscard]] Object* findObjectName(std::string_view name);
291 [[nodiscard]] Object const* findObjectName(std::string_view name) const;
292
293 void save(nlohmann::json& jsonObject) override;
294 void load(nlohmann::json const& jsonObject, std::filesystem::path const& filePath) override;
295
296private:
297 std::vector<Object> g_objects;
298};
299
300} // namespace fge
301
302#endif // _FGE_C_TILELAYER_HPP_INCLUDED
Definition C_tilelayer.hpp:43
GlobalTileId getId() const
Get the id of the layer.
void setId(GlobalTileId id)
Set the id of the layer (mostly for "Tiled" map editor compatibility)
std::string const & getName() const
Get the name of the layer.
void setName(std::string_view name)
Set the name of the layer.
Definition C_color.hpp:35
Definition C_drawable.hpp:36
A container to store a 2D matrix of any type.
Definition C_matrix.hpp:40
The RenderStates class contains all the information needed to render something.
Definition C_renderStates.hpp:412
Definition C_renderTarget.hpp:56
Color getColor() const
Get the color of the tile.
GlobalTileId getGid() const
Get the global id of the tile.
std::shared_ptr< TileSet > getTileSet() const
Get the associated tileset pointer.
Vector2f const & getPosition() const
Get the local position of the tile.
void setTileSet(std::shared_ptr< TileSet > const &tileSet)
Set the associated tileset pointer.
void setGid(GlobalTileId gid)
Set the global id of the tile.
void setPosition(Vector2f const &position)
Set the local position of the tile.
void setColor(Color const &color)
Set the color of the tile.
A tile layer contain a matrix of global tile id and a list of TileSet.
Definition C_tilelayer.hpp:116
void refreshTextures(std::span< std::shared_ptr< TileSet > > tileSets)
Refresh all tiles with a list of tilesets.
void setGid(fge::Vector2size position, GlobalTileId gid)
Shortcut to set a global tile id.
fge::Matrix< Tile > const & getTiles() const
Get the matrix of tiles.
void setGid(fge::Vector2size position, std::span< std::shared_ptr< TileSet > > tileSets, GlobalTileId gid)
Shortcut to set a global tile id and a new tileset.
void setGridSize(fge::Vector2size size)
Set the tiles matrix size.
Definition C_transformable.hpp:34
Definition C_vertexBuffer.hpp:45
Definition C_tilelayer.hpp:270
A tile structure that contain mostly the texture rectangle and data.
Definition C_tileset.hpp:42