pupush
This commit is contained in:
5
assets/stats.json
Normal file
5
assets/stats.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"Zombie" : {
|
||||
|
||||
}
|
||||
}
|
||||
10
include/td/Constants.h
Normal file
10
include/td/Constants.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace td {
|
||||
|
||||
constexpr int TPS = 20;
|
||||
constexpr float STEP_PERIOD = 1.0f / static_cast<float>(TPS);
|
||||
|
||||
} // namespace td
|
||||
@@ -66,5 +66,6 @@ struct EntityCoords {
|
||||
};
|
||||
|
||||
using PeerID = std::uint16_t;
|
||||
using StepsType = std::uint16_t;
|
||||
|
||||
} // namespace td
|
||||
|
||||
@@ -9,12 +9,10 @@ namespace game {
|
||||
|
||||
class GameHistory {
|
||||
private:
|
||||
using HistorySizeType = std::uint16_t;
|
||||
using HistorySizeType = StepsType;
|
||||
|
||||
std::vector<std::optional<protocol::LockStep>> m_History;
|
||||
|
||||
HistorySizeType m_Cursor;
|
||||
|
||||
public:
|
||||
GameHistory();
|
||||
|
||||
@@ -24,10 +22,6 @@ class GameHistory {
|
||||
|
||||
bool HasLockStep(HistorySizeType a_Index) const;
|
||||
|
||||
const protocol::LockStep& GetNextStep();
|
||||
|
||||
bool HasNextStep() const;
|
||||
|
||||
void FromPacket(td::protocol::pdata::LockSteps&& a_Steps);
|
||||
|
||||
td::protocol::packets::LockSteps ToPacket(HistorySizeType a_StartIndex);
|
||||
|
||||
27
include/td/game/World.h
Normal file
27
include/td/game/World.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/game/GameHistory.h>
|
||||
#include <td/game/WorldState.h>
|
||||
#include <vector>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
class World {
|
||||
private:
|
||||
std::vector<WorldState> m_WorldStates;
|
||||
GameHistory m_GameHistory;
|
||||
|
||||
float m_OffsetTime;
|
||||
|
||||
public:
|
||||
World();
|
||||
|
||||
void Tick(float a_Delta);
|
||||
|
||||
private:
|
||||
std::uint16_t GetCursorPos();
|
||||
};
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
21
include/td/game/WorldState.h
Normal file
21
include/td/game/WorldState.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/protocol/command/Commands.h>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
class WorldState {
|
||||
private:
|
||||
// list of players, mobs, towers, castles
|
||||
public:
|
||||
WorldState() {}
|
||||
|
||||
WorldState GetNextState(const protocol::LockStep& a_Step);
|
||||
|
||||
private:
|
||||
void Tick(float a_Delta);
|
||||
};
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
1
include/td/network/EnetClient.h
Normal file
1
include/td/network/EnetClient.h
Normal file
@@ -0,0 +1 @@
|
||||
#pragma once
|
||||
1
include/td/network/EnetConnection.h
Normal file
1
include/td/network/EnetConnection.h
Normal file
@@ -0,0 +1 @@
|
||||
#pragma once
|
||||
1
include/td/network/EnetServer.h
Normal file
1
include/td/network/EnetServer.h
Normal file
@@ -0,0 +1 @@
|
||||
#pragma once
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
GameHistory::GameHistory() : m_History(std::numeric_limits<HistorySizeType>::max()), m_Cursor(0) {}
|
||||
GameHistory::GameHistory() : m_History(std::numeric_limits<HistorySizeType>::max()) {}
|
||||
|
||||
void GameHistory::SetLockStep(HistorySizeType a_Index, protocol::LockStep&& a_LockStep) {
|
||||
m_History[a_Index] = std::move(a_LockStep);
|
||||
@@ -17,14 +17,6 @@ bool GameHistory::HasLockStep(HistorySizeType a_Index) const {
|
||||
return m_History[a_Index].has_value();
|
||||
}
|
||||
|
||||
const protocol::LockStep& GameHistory::GetNextStep() {
|
||||
return GetLockStep(m_Cursor++);
|
||||
}
|
||||
|
||||
bool GameHistory::HasNextStep() const {
|
||||
return HasLockStep(m_Cursor);
|
||||
}
|
||||
|
||||
void GameHistory::FromPacket(td::protocol::pdata::LockSteps&& a_Steps) {
|
||||
for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) {
|
||||
protocol::LockStep& step = a_Steps.m_LockSteps[i];
|
||||
|
||||
31
src/td/game/World.cpp
Normal file
31
src/td/game/World.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <td/game/World.h>
|
||||
|
||||
#include <td/Constants.h>
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
World::World() : m_WorldStates(std::numeric_limits<StepsType>::max()), m_OffsetTime(0) {}
|
||||
|
||||
void World::Tick(float a_Delta) {
|
||||
m_OffsetTime += a_Delta;
|
||||
|
||||
if (GetCursorPos() > m_WorldStates.size()) {
|
||||
if (!m_GameHistory.HasLockStep(GetCursorPos())) {
|
||||
// oupsi
|
||||
return;
|
||||
}
|
||||
WorldState& lastState = m_WorldStates.back();
|
||||
WorldState nextState = lastState.GetNextState(m_GameHistory.GetLockStep(GetCursorPos()));
|
||||
m_WorldStates.push_back(std::move(nextState));
|
||||
}
|
||||
}
|
||||
|
||||
std::uint16_t World::GetCursorPos() {
|
||||
return (std::uint16_t) m_OffsetTime * TPS;
|
||||
}
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
43
src/td/game/WorldState.cpp
Normal file
43
src/td/game/WorldState.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <td/game/WorldState.h>
|
||||
|
||||
#include <td/Constants.h>
|
||||
|
||||
#include <td/protocol/command/CommandVisitor.h>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
class CommandHandler : public protocol::CommandVisitor {
|
||||
private:
|
||||
WorldState& m_WorldState;
|
||||
|
||||
public:
|
||||
CommandHandler(WorldState& a_WorldState) : m_WorldState(a_WorldState) {}
|
||||
|
||||
void Visit(const protocol::commands::End&) override {}
|
||||
void Visit(const protocol::commands::PlaceTower&) override {}
|
||||
void Visit(const protocol::commands::PlayerJoin&) override {}
|
||||
void Visit(const protocol::commands::SpawnTroop&) override {}
|
||||
void Visit(const protocol::commands::TeamChange&) override {}
|
||||
void Visit(const protocol::commands::UpgradeTower&) override {}
|
||||
void Visit(const protocol::commands::UseItem&) override {}
|
||||
};
|
||||
|
||||
WorldState WorldState::GetNextState(const protocol::LockStep& a_Step) {
|
||||
WorldState newState = *this;
|
||||
CommandHandler handler(*this);
|
||||
for (auto command : a_Step) {
|
||||
handler.Check(*command);
|
||||
}
|
||||
newState.Tick(STEP_PERIOD);
|
||||
return newState;
|
||||
}
|
||||
|
||||
void WorldState::Tick(float a_Delta) {
|
||||
// move mobs
|
||||
// process towers
|
||||
// process damages
|
||||
}
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
Reference in New Issue
Block a user