refactor tile serialize

This commit is contained in:
2025-07-31 18:02:14 +02:00
parent c8159bae6e
commit 1a455a3d6b
10 changed files with 84 additions and 117 deletions

View File

@@ -18,9 +18,6 @@ using Vec2fp = Vec2<FpFloat>;
namespace game {
struct WalkableTile;
enum class EffectType : std::uint8_t {
Slowness = 0,
Stun,
@@ -127,6 +124,8 @@ class MobListener {
virtual void OnMobCastleDamage(Mob* damager, TeamCastle* enemyCastle, float damage) {}
};
using MobList = std::vector<MobPtr>;
// typedef utils::ObjectNotifier<MobListener> MobNotifier;
} // namespace game

View File

@@ -1,10 +1,11 @@
#pragma once
#include <td/Maths.h>
#include <td/game/Mobs.h>
#include <td/game/Team.h>
#include <td/game/Towers.h>
#include <sp/io/SerializableMessage.h>
namespace td {
namespace game {
@@ -31,37 +32,44 @@ static constexpr Color RED{255, 0, 0};
static constexpr Color GREEN{0, 255, 0};
static constexpr Color BLUE{0, 0, 255};
struct Tile {
virtual TileType GetType() const = 0;
virtual ~Tile() = default;
class TileHandler;
using Tile = sp::MessageBase<TileType, TileHandler>;
template <TileType ID, typename TileData>
using ConcreteTile = sp::ConcreteMessage<TileData, Tile, ID>;
namespace data {
struct EmptyData {};
struct TowerTileData {
std::uint8_t m_ColorPaletteRef;
TeamColor m_TeamOwner;
};
struct TowerTile : Tile {
std::uint8_t color_palette_ref;
TeamColor team_owner;
virtual TileType GetType() const {
return TileType::Tower;
}
struct WalkableTileData {
Direction m_Direction;
};
struct WalkableTile : Tile {
Direction direction;
virtual TileType GetType() const {
return TileType::Walk;
}
struct DecorationTileData {
std::uint16_t m_ColorPaletteRef;
};
} // namespace data
struct DecorationTile : Tile {
std::uint16_t color_palette_ref;
using EmptyTile = ConcreteTile<TileType::None, data::EmptyData>;
using TowerTile = ConcreteTile<TileType::Tower, data::TowerTileData>;
using WalkableTile = ConcreteTile<TileType::Walk, data::WalkableTileData>;
using DecorationTile = ConcreteTile<TileType::Decoration, data::DecorationTileData>;
virtual TileType GetType() const {
return TileType::Decoration;
}
};
using AllTiles = std::tuple<EmptyTile, TowerTile, WalkableTile, DecorationTile>;
typedef std::shared_ptr<Tile> TilePtr;
using TileFactory = sp::MessageFactory<Tile, AllTiles>;
class TileHandler : public sp::GenericHandler<AllTiles>{};
using TilePtr = std::shared_ptr<sp::SerializableMessage<TileFactory>>;
// typedef std::shared_ptr<Tile> TilePtr;
typedef std::vector<std::uint16_t> ChunkPalette;
typedef std::shared_ptr<WalkableTile> WalkableTilePtr;
@@ -85,6 +93,21 @@ struct Chunk {
}
// TODO: keep data packed
/*
std::size_t startLong = static_cast<std::size_t>((tileNumber * bitsPerTile) / BITS_IN_LONG);
std::size_t startOffset = static_cast<std::size_t>((tileNumber * bitsPerTile) % BITS_IN_LONG);
std::size_t endLong = static_cast<std::size_t>(((tileNumber + 1) * bitsPerTile - 1) / BITS_IN_LONG);
std::uint64_t value = static_cast<std::uint64_t>(a_Chunk->m_Tiles[tileNumber]);
value &= individualValueMask;
chunkData[startLong] |= (value << startOffset);
if (startLong != endLong) {
chunkData[endLong] = (value >> (BITS_IN_LONG - startOffset));
}
*/
};
typedef std::shared_ptr<Chunk> ChunkPtr;
@@ -93,17 +116,12 @@ typedef std::array<Color, 2> TowerTileColorPalette;
typedef std::vector<TilePtr> TilePalette;
typedef std::vector<MobPtr> MobList;
typedef std::array<Color, 2> SpawnColorPalette;
typedef std::vector<TowerPtr> TowerList;
using ChunkList = std::unordered_map<ChunkCoord, ChunkPtr>;
sp::DataBuffer& operator>>(sp::DataBuffer& buffer, TilePtr& tile);
} // namespace game
} // namespace td

View File

@@ -13,6 +13,7 @@
#include <td/Types.h>
#include <td/common/NonCopyable.h>
#include <td/protocol/command/CommandData.h>
#include <sp/io/SerializableMessage.h>
namespace td {
namespace protocol {
@@ -32,8 +33,6 @@ class CommandHandler;
using CommandBase = sp::MessageBase<CommandID, CommandHandler>;
using CommandPtr = std::unique_ptr<CommandBase>;
template <typename TData, CommandID ID>
using CommandMessage = sp::ConcreteMessage<TData, CommandBase, ID>;
@@ -61,5 +60,7 @@ using CommandFactory = sp::MessageFactory<CommandBase, AllCommands>;
using LockStep = std::vector<std::shared_ptr<CommandBase>>;
using CommandPtr = std::unique_ptr<sp::SerializableMessage<CommandFactory>>;
} // namespace protocol
} // namespace td

View File

@@ -43,8 +43,5 @@ sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, TeamCastle& a_Castle);
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const ChunkPtr& a_Chunk);
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, ChunkPtr& a_Chunk);
sp::DataBuffer& operator<<(sp::DataBuffer& buffer, const TilePtr& tile);
sp::DataBuffer& operator>>(sp::DataBuffer& buffer, TilePtr& tile);
} // namespace game
} // namespace td

View File

@@ -1,6 +1,7 @@
#pragma once
#include <td/game/WorldTypes.h>
#include <td/game/Mobs.h>
namespace td {
namespace sim {