Compare commits
3 Commits
550ff3aeec
...
701dd6b120
| Author | SHA1 | Date | |
|---|---|---|---|
|
701dd6b120
|
|||
|
2225151f72
|
|||
|
aef0cf4d95
|
@@ -25,8 +25,8 @@ void GameState::Update(float a_Delta) {
|
|||||||
// TODO: don't make STEP_TIME constant
|
// TODO: don't make STEP_TIME constant
|
||||||
static const float stepTimeSecond = static_cast<float>(STEP_TIME) / 1000.0f;
|
static const float stepTimeSecond = static_cast<float>(STEP_TIME) / 1000.0f;
|
||||||
m_Time += a_Delta;
|
m_Time += a_Delta;
|
||||||
if (m_Time > stepTimeSecond) {
|
while (m_Time > stepTimeSecond) {
|
||||||
m_Time = std::fmod(m_Time, stepTimeSecond);
|
m_Time -= stepTimeSecond;
|
||||||
auto lockStepPacket = m_Simulation.Update();
|
auto lockStepPacket = m_Simulation.Update();
|
||||||
BroadcastPacket(lockStepPacket);
|
BroadcastPacket(lockStepPacket);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ void DebugWorldState::OnAspectRatioChange(float a_Ratio) {
|
|||||||
|
|
||||||
void DebugWorldState::OnKeyDown(SDL_Keycode a_Key) {
|
void DebugWorldState::OnKeyDown(SDL_Keycode a_Key) {
|
||||||
// temporary tests
|
// temporary tests
|
||||||
|
constexpr int SECONDS = 10;
|
||||||
switch (a_Key) {
|
switch (a_Key) {
|
||||||
case SDLK_A:
|
case SDLK_A:
|
||||||
m_Client->SendPacket(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1));
|
m_Client->SendPacket(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1));
|
||||||
@@ -94,6 +95,11 @@ void DebugWorldState::OnKeyDown(SDL_Keycode a_Key) {
|
|||||||
m_Client->SendPacket(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13)));
|
m_Client->SendPacket(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13)));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case SDLK_F:
|
||||||
|
m_Server->Update(SECONDS);
|
||||||
|
m_Client->Update(SECONDS);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
|
#include <cassert>
|
||||||
|
#include <td/Maths.h>
|
||||||
#include <td/game/World.h>
|
#include <td/game/World.h>
|
||||||
|
#include <td/game/WorldTypes.h>
|
||||||
|
|
||||||
#include <td/simulation/WorldTicker.h>
|
|
||||||
#include <td/protocol/packet/PacketSerialize.h>
|
#include <td/protocol/packet/PacketSerialize.h>
|
||||||
|
#include <td/simulation/WorldTicker.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace game {
|
namespace game {
|
||||||
@@ -89,5 +92,14 @@ void World::ResetSnapshots(std::shared_ptr<sim::WorldSnapshot>& a_Current, std::
|
|||||||
m_NextState = a_Next;
|
m_NextState = a_Next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TilePtr World::GetTile(std::int32_t x, std::int32_t y) const {
|
||||||
|
ChunkCoord coords{static_cast<std::int16_t>(x / Chunk::ChunkWidth), static_cast<std::int16_t>(y / Chunk::ChunkHeight)};
|
||||||
|
auto it = m_Chunks.find(coords);
|
||||||
|
assert(it != m_Chunks.end());
|
||||||
|
auto chunk = it->second;
|
||||||
|
Vec2i inchunkCoords{x % Chunk::ChunkWidth, y % Chunk::ChunkHeight};
|
||||||
|
return GetTilePtr(chunk->GetTileIndex(inchunkCoords.y * Chunk::ChunkWidth + inchunkCoords.x));
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace game
|
} // namespace game
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ void EntityRenderer::Render(float a_Lerp) {
|
|||||||
float x = Lerp<game::Mob>(*mob, a_Lerp, [](const game::Mob& a_Mob) { return static_cast<float>(a_Mob.m_Position.x); });
|
float x = Lerp<game::Mob>(*mob, a_Lerp, [](const game::Mob& a_Mob) { return static_cast<float>(a_Mob.m_Position.x); });
|
||||||
float z = Lerp<game::Mob>(*mob, a_Lerp, [](const game::Mob& a_Mob) { return static_cast<float>(a_Mob.m_Position.y); });
|
float z = Lerp<game::Mob>(*mob, a_Lerp, [](const game::Mob& a_Mob) { return static_cast<float>(a_Mob.m_Position.y); });
|
||||||
|
|
||||||
m_Shader->SetModelPos({x, 1, z});
|
m_Shader->SetModelPos({x, .001, z});
|
||||||
Renderer::Render(*m_EntityVao);
|
Renderer::Render(*m_EntityVao);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ TowerRenderer::~TowerRenderer() {}
|
|||||||
void TowerRenderer::Render(float a_Lerp) {
|
void TowerRenderer::Render(float a_Lerp) {
|
||||||
m_Shader->Start();
|
m_Shader->Start();
|
||||||
for (const auto& tower : m_World->GetTowers()) {
|
for (const auto& tower : m_World->GetTowers()) {
|
||||||
m_Shader->SetModelPos({tower->GetCenterX(), 1, tower->GetCenterY()});
|
m_Shader->SetModelPos({tower->GetCenterX(), .001, tower->GetCenterY()});
|
||||||
Renderer::Render(*m_EntityVao);
|
Renderer::Render(*m_EntityVao);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,8 +51,8 @@ float ClientSimulation::Update(float a_Delta) {
|
|||||||
// TODO: handle freezes (m_CurrentTime > 2 * m_StepTime)
|
// TODO: handle freezes (m_CurrentTime > 2 * m_StepTime)
|
||||||
static const float stepTimeSecond = static_cast<float>(m_StepTime) / 1000.0f;
|
static const float stepTimeSecond = static_cast<float>(m_StepTime) / 1000.0f;
|
||||||
m_CurrentTime += a_Delta;
|
m_CurrentTime += a_Delta;
|
||||||
if (m_CurrentTime > stepTimeSecond) {
|
while (m_CurrentTime > stepTimeSecond) {
|
||||||
m_CurrentTime = std::fmod(m_CurrentTime, stepTimeSecond);
|
m_CurrentTime -= stepTimeSecond;
|
||||||
Step();
|
Step();
|
||||||
}
|
}
|
||||||
return (float)m_CurrentTime / stepTimeSecond;
|
return (float)m_CurrentTime / stepTimeSecond;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include <td/Types.h>
|
||||||
#include <td/simulation/CommandApply.h>
|
#include <td/simulation/CommandApply.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
@@ -24,6 +25,10 @@ void CommandApply::Handle(const protocol::commands::PlayerJoinCommand& a_PlayerJ
|
|||||||
void CommandApply::Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) {
|
void CommandApply::Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) {
|
||||||
auto zombie = std::make_shared<game::Zombie>();
|
auto zombie = std::make_shared<game::Zombie>();
|
||||||
zombie->m_Position = a_SpawnTroop->m_Position;
|
zombie->m_Position = a_SpawnTroop->m_Position;
|
||||||
|
|
||||||
|
// TODO: make it spawn dependant
|
||||||
|
zombie->m_Direction = Direction::PositiveY;
|
||||||
|
|
||||||
m_Snapshot.m_Mobs.push_back(zombie);
|
m_Snapshot.m_Mobs.push_back(zombie);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,61 @@
|
|||||||
|
#include <td/Maths.h>
|
||||||
|
#include <td/Types.h>
|
||||||
|
#include <td/game/WorldTypes.h>
|
||||||
#include <td/simulation/system/EntityMove.h>
|
#include <td/simulation/system/EntityMove.h>
|
||||||
|
|
||||||
|
#include <td/game/World.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace sim {
|
namespace sim {
|
||||||
|
|
||||||
|
static Vec2i GetUnitDirection(Direction a_Direction) {
|
||||||
|
switch (a_Direction) {
|
||||||
|
case Direction::PositiveX:
|
||||||
|
return {1, 0};
|
||||||
|
case Direction::NegativeX:
|
||||||
|
return {-1, 0};
|
||||||
|
case Direction::PositiveY:
|
||||||
|
return {0, 1};
|
||||||
|
case Direction::NegativeY:
|
||||||
|
return {0, -1};
|
||||||
|
}
|
||||||
|
return {0, 0};
|
||||||
|
}
|
||||||
|
|
||||||
|
class DirectionTileVisitor : public game::TileHandler {
|
||||||
|
private:
|
||||||
|
Direction m_Direction;
|
||||||
|
|
||||||
|
public:
|
||||||
|
DirectionTileVisitor() {}
|
||||||
|
|
||||||
|
virtual void Handle(const game::EmptyTile& a_Tile) override {}
|
||||||
|
|
||||||
|
virtual void Handle(const game::TowerTile& a_Tile) override {}
|
||||||
|
|
||||||
|
virtual void Handle(const game::DecorationTile& a_Tile) override {}
|
||||||
|
|
||||||
|
virtual void Handle(const game::WalkableTile& a_Tile) override {
|
||||||
|
m_Direction = a_Tile->m_Direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Direction GetDirection() {
|
||||||
|
return m_Direction;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void EntityMove::Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloat a_Delta) {
|
void EntityMove::Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloat a_Delta) {
|
||||||
for (auto& mob : a_State.m_Mobs) {
|
for (auto& mob : a_State.m_Mobs) {
|
||||||
mob->m_Position.x += a_Delta;
|
auto tile = a_World.GetTile(static_cast<std::int32_t>(mob->m_Position.x), static_cast<std::int32_t>(mob->m_Position.y));
|
||||||
|
Direction direction = mob->m_Direction;
|
||||||
|
if (tile) {
|
||||||
|
DirectionTileVisitor visitor;
|
||||||
|
tile->Dispatch(visitor);
|
||||||
|
direction = visitor.GetDirection();
|
||||||
|
}
|
||||||
|
auto directVector = GetUnitDirection(direction);
|
||||||
|
mob->m_Position.x += directVector.x * a_Delta;
|
||||||
|
mob->m_Position.y += directVector.y * a_Delta;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user