too many things

This commit is contained in:
2025-07-18 13:11:18 +02:00
parent b788caafa6
commit 6d0e56eb46
26 changed files with 529 additions and 191 deletions

View File

@@ -18,7 +18,7 @@ struct Vec2 {
T g;
};
constexpr Vec2(T X = 0, T Y = 0) : x(X), y(Y) {}
constexpr Vec2(T X = T(0), T Y = T(0)) : x(X), y(Y) {}
};
template <typename T>

View File

@@ -2,6 +2,11 @@
#include <cstdint>
#include <fpm/fixed.hpp>
#include <td/Maths.h>
namespace sp {
class DataBuffer;
} // namespace sp
namespace td {
@@ -31,7 +36,7 @@ enum class EntityType : std::uint8_t {
Zombie = 0,
Spider,
Pigman,
Skeleton,
Skelon,
Creeper,
Silverfish,
Blaze,
@@ -61,10 +66,7 @@ struct TowerCoords {
std::int16_t y;
};
struct EntityCoords {
FpFloat x;
FpFloat y;
};
using EntityCoords = Vec2<FpFloat>;
using PeerID = std::uint16_t;
@@ -75,4 +77,10 @@ enum class Direction : std::uint8_t {
NegativeY = 1 << 3,
};
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const EntityCoords& a_Coords);
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const FpFloat& a_Float);
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, EntityCoords& a_Coords);
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, FpFloat& a_Float);
} // namespace td

View File

@@ -10,8 +10,11 @@
#include <vector>
namespace td {
using Vec2fp = Vec2<FpFloat>;
namespace game {
struct WalkableTile;
enum class EffectType : std::uint8_t {
@@ -62,11 +65,12 @@ const MobStats* GetMobStats(MobType type, std::uint8_t level);
const TowerImmunities& GetMobTowerImmunities(MobType type, std::uint8_t level);
const EffectImmunities& GetMobEffectImmunities(MobType type, std::uint8_t level);
class Mob : public utils::shape::Rectangle {
class Mob {
protected:
float m_Health;
private:
Vec2fp m_Position;
MobID m_ID;
PlayerID m_Sender;
MobLevel m_Level;
@@ -89,12 +93,14 @@ class Mob : public utils::shape::Rectangle {
virtual MobType GetType() const = 0;
virtual void Tick(std::uint64_t delta, World* world) {}
virtual bool OnDeath(World* world) {
return true;
}
Vec2fp& GetPosition() {
return m_Position;
}
MobID GetMobID() const {
return m_ID;
}
@@ -156,15 +162,15 @@ class Mob : public utils::shape::Rectangle {
return m_HitCooldown > 0;
}
// returns a float between 0 and 1 excluded
float GetTileX() {
return GetCenterX() - static_cast<float>(static_cast<std::int32_t>(GetCenterX()));
}
// // returns a float between 0 and 1 excluded
// float GetTileX() {
// return GetCenterX() - static_cast<float>(static_cast<std::int32_t>(GetCenterX()));
// }
// returns a float between 0 and 1 excluded
float GetTileY() {
return GetCenterY() - static_cast<float>(static_cast<std::int32_t>(GetCenterY()));
}
// // returns a float between 0 and 1 excluded
// float GetTileY() {
// return GetCenterY() - static_cast<float>(static_cast<std::int32_t>(GetCenterY()));
// }
Direction GetDirection() const {
return m_Direction;
@@ -176,7 +182,7 @@ class Mob : public utils::shape::Rectangle {
protected:
void InitMob() {
m_Health = static_cast<float>(GetStats()->m_MaxLife);
SetSize(GetStats()->m_Size.x, GetStats()->m_Size.y);
// SetSize(GetStats()->m_Size.x, GetStats()->m_Size.y);
}
private:
@@ -201,9 +207,7 @@ class ConcreteMob : public Mob {
virtual ~ConcreteMob() {}
virtual void Tick(std::uint64_t delta, World* world) {}
virtual constexpr MobType GetType() const {
virtual constexpr MobType GetType() const override {
return MT;
}
};

View File

@@ -1,5 +1,6 @@
#pragma once
#include <td/simulation/WorldTicker.h>
#include <td/game/WorldTypes.h>
#include <td/protocol/packet/Packets.h>
@@ -19,11 +20,10 @@ class World {
TilePalette m_TilePalette;
MobList m_Mobs;
sim::WorldSnapshot m_CurrentState;
TowerList m_Towers;
TeamList m_Teams;
private:
sim::WorldTicker m_Ticker;
public:
World();
@@ -34,8 +34,6 @@ class World {
bool LoadMapFromFile(const std::string& fileName);
bool SaveMap(const std::string& fileName) const;
void Tick(std::uint64_t delta);
void SpawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID sender, float x, float y, Direction dir);
TowerPtr PlaceTowerAt(TowerID id, TowerType type, std::int32_t x, std::int32_t y, PlayerID builder);
@@ -83,47 +81,49 @@ class World {
}
const MobList& GetMobList() const {
return m_Mobs;
return m_CurrentState.m_Mobs;
}
MobList& GetMobList() {
return m_Mobs;
return m_CurrentState.m_Mobs;
}
const Color* GetTileColor(TilePtr tile) const;
Team& GetRedTeam() {
return m_Teams[static_cast<std::uint8_t>(TeamColor::Red)];
return m_CurrentState.m_Teams[static_cast<std::uint8_t>(TeamColor::Red)];
}
const Team& GetRedTeam() const {
return m_Teams[static_cast<std::uint8_t>(TeamColor::Red)];
return m_CurrentState.m_Teams[static_cast<std::uint8_t>(TeamColor::Red)];
}
Team& GetBlueTeam() {
return m_Teams[static_cast<std::uint8_t>(TeamColor::Blue)];
return m_CurrentState.m_Teams[static_cast<std::uint8_t>(TeamColor::Blue)];
}
const Team& GetBlueTeam() const {
return m_Teams[static_cast<std::uint8_t>(TeamColor::Red)];
return m_CurrentState.m_Teams[static_cast<std::uint8_t>(TeamColor::Red)];
}
Team& GetTeam(TeamColor team) {
return m_Teams[static_cast<std::uint8_t>(team)];
return m_CurrentState.m_Teams[static_cast<std::uint8_t>(team)];
}
const Team& GetTeam(TeamColor team) const {
return m_Teams[static_cast<std::uint8_t>(team)];
return m_CurrentState.m_Teams[static_cast<std::uint8_t>(team)];
}
const TeamList& GetTeams() const {
return m_Teams;
return m_CurrentState.m_Teams;
}
const TowerList& GetTowers() const {
return m_Towers;
return m_CurrentState.m_Towers;
}
TowerPtr GetTowerById(TowerID tower);
const Player* GetPlayerById(PlayerID id) const;
void Tick(const protocol::LockStep& a_LockStep, FpFloat a_Delta);
private:
void TickMobs(std::uint64_t delta);
void CleanDeadMobs();

View File

@@ -102,6 +102,8 @@ typedef std::array<Color, 2> SpawnColorPalette;
typedef std::vector<TowerPtr> TowerList;
sp::DataBuffer& operator>>(sp::DataBuffer& buffer, TilePtr& tile);
} // namespace game

View File

@@ -3,11 +3,14 @@
#include <string>
#include <SDL3/SDL_video.h>
#include <td/misc/Signal.h>
namespace td {
class Display {
public:
utils::Signal<float> OnAspectRatioChange;
Display(int a_Width, int a_Height, const std::string& a_Title);
~Display();

View File

@@ -21,8 +21,10 @@ struct UpgradeTower {
sp::BitField<std::uint8_t, 4> m_Upgrade;
};
using EntityTypeInt = std::uint8_t;
struct SpawnTroop {
sp::BitField<EntityType, 5> m_Type;
sp::BitField<EntityTypeInt, 5> m_Type;
sp::BitField<std::uint8_t, 3> m_Level;
EntityCoords m_Position;
PlayerID m_Sender;

View File

@@ -0,0 +1,18 @@
#pragma once
#include <td/protocol/packet/PacketData.h>
namespace sp {
class DataBuffer;
namespace details {
template <>
void ReadMessage(DataBuffer& a_Buffer, td::protocol::pdata::WorldHeader& a_Header);
template <>
void ReadMessage(DataBuffer& a_Buffer, td::protocol::pdata::WorldData& a_WorldData);
} // namespace details
} // namespace sp

View File

@@ -0,0 +1,21 @@
#pragma once
#include <td/game/World.h>
#include <td/protocol/command/Commands.h>
namespace td {
namespace sim {
class CommandApply : public protocol::CommandHandler {
private:
const game::World& m_World;
WorldSnapshot& m_Snapshot;
public:
CommandApply(const game::World& a_World, WorldSnapshot& a_Snapshot);
virtual void Handle(const protocol::cdata::SpawnTroop& a_SpawnTroop) override;
};
} // namespace sim
} // namespace td

View File

@@ -0,0 +1,26 @@
#pragma once
#include <td/game/World.h>
namespace td {
namespace sim {
using GameHistory = std::vector<td::protocol::LockStep>;
class RealTimeSimulation {
private:
std::uint64_t m_StepTime;
game::World& m_World;
GameHistory m_History;
std::uint64_t m_CurrentTime;
std::uint64_t m_LastTime;
std::size_t m_CurrentStep;
public:
RealTimeSimulation(game::World& a_World, GameHistory&& a_History, std::uint64_t a_StepTime);
void Update();
};
} // namespace sim
} // namespace td

View File

@@ -0,0 +1,17 @@
#pragma once
#include <td/game/WorldTypes.h>
namespace td {
namespace sim {
struct WorldSnapshot {
game::MobList m_Mobs;
game::TowerList m_Towers;
game::TeamList m_Teams;
};
} // namespace sim
} // namespace td

View File

@@ -0,0 +1,40 @@
#pragma once
#include <td/protocol/command/Commands.h>
#include <td/simulation/WorldSnapshot.h>
namespace td {
namespace game {
class World;
}
namespace sim {
class IWorldSystem {
public:
virtual void Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloat a_Delta) = 0;
};
class WorldTicker {
private:
std::vector<std::unique_ptr<IWorldSystem>> m_Systems;
public:
WorldTicker();
WorldSnapshot NextStep(
const game::World& a_World, const WorldSnapshot& a_PreviousState, const protocol::LockStep& a_LockStep, FpFloat a_Delta);
private:
void ApplySteps(const game::World& a_World, WorldSnapshot& a_State, const protocol::LockStep& a_LockStep);
void Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloat a_Delta);
WorldSnapshot CreateNext(const WorldSnapshot& a_PreviousState);
template <typename T>
void AddSystem() {
m_Systems.push_back(std::move(std::make_unique<T>()));
}
};
} // namespace sim
} // namespace td

View File

@@ -0,0 +1,14 @@
#pragma once
#include <td/simulation/WorldTicker.h>
namespace td {
namespace sim {
class EntityMove : public IWorldSystem {
public:
virtual void Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloat a_Delta) override;
};
} // namespace sim
} // namespace td