140 lines
4.0 KiB
C++
140 lines
4.0 KiB
C++
#include <iostream>
|
|
|
|
#include <td/protocol/packet/PacketSerialize.h>
|
|
#include <fstream>
|
|
#include <td/game/World.h>
|
|
#include <td/input/Display.h>
|
|
#include <td/protocol/packet/Packets.h>
|
|
#include <td/render/renderer/EntityRenderer.h>
|
|
#include <td/render/renderer/TowerRenderer.h>
|
|
#include <td/render/renderer/WorldRenderer.h>
|
|
#include <td/simulation/ClientSimulation.h>
|
|
|
|
#include <sp/common/DataBuffer.h>
|
|
#include <sp/extensions/Compress.h>
|
|
#include <sp/io/MessageStream.h>
|
|
#include <sp/io/StdIo.h>
|
|
|
|
class WorldApply : public td::protocol::PacketHandler {
|
|
private:
|
|
td::game::World& m_World;
|
|
using td::protocol::PacketHandler::Handle;
|
|
|
|
public:
|
|
WorldApply(td::game::World& a_World) : m_World(a_World) {}
|
|
|
|
void Handle(const td::protocol::packets::WorldHeaderPacket& a_Header) override {
|
|
m_World.LoadMap(*a_Header);
|
|
}
|
|
|
|
void Handle(const td::protocol::packets::WorldDataPacket& a_Data) override {
|
|
m_World.LoadMap(*a_Data);
|
|
}
|
|
};
|
|
|
|
void Save(const td::protocol::PacketBase& header, const td::protocol::PacketBase& data) {
|
|
auto comp = std::make_shared<sp::ZlibCompress>();
|
|
|
|
std::ofstream fStream("test/tdmap.tdmap3");
|
|
auto out = std::make_shared<sp::StdOuput>(fStream);
|
|
|
|
sp::MessageStream<td::protocol::PacketFactory> stream(std::move(out), std::move(comp));
|
|
|
|
stream.WriteMessage(header, false);
|
|
stream.WriteMessage(data, false);
|
|
}
|
|
|
|
td::game::World GetWorld() {
|
|
auto comp = std::make_shared<sp::ZlibCompress>();
|
|
|
|
std::ifstream fStream("test/tdmap.tdmap2");
|
|
auto out = std::make_shared<sp::StdInput>(fStream);
|
|
|
|
sp::MessageStream<td::protocol::PacketFactory> stream(std::move(out), std::move(comp));
|
|
|
|
auto header = stream.ReadMessage(td::protocol::PacketID::WorldHeader);
|
|
auto data = stream.ReadMessage(td::protocol::PacketID::WorldData);
|
|
|
|
td::game::World w;
|
|
auto wa = std::make_shared<WorldApply>(w);
|
|
|
|
td::protocol::PacketDispatcher d;
|
|
d.RegisterHandler(wa);
|
|
|
|
d.Dispatch(*header);
|
|
d.Dispatch(*data);
|
|
|
|
Save(*header, *data);
|
|
|
|
return w;
|
|
}
|
|
|
|
void FastForward(td::game::World& a_World, const td::sim::GameHistory& a_LockSteps) {
|
|
const td::FpFloat delta = td::FpFloat(1) / td::FpFloat(75);
|
|
for (const auto& lockstep : a_LockSteps) {
|
|
a_World.Tick(lockstep, delta);
|
|
}
|
|
}
|
|
|
|
td::sim::GameHistory GetCustomHistory() {
|
|
constexpr std::size_t MAX_COUNT = 20 * 60 * 40;
|
|
|
|
td::sim::GameHistory gh(MAX_COUNT);
|
|
|
|
auto spawn = td::protocol::CommandPtr(
|
|
std::make_shared<td::protocol::commands::SpawnTroopCommand>(td::EntityType::Zombie, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0));
|
|
gh[0].push_back(spawn);
|
|
|
|
auto tower = td::protocol::CommandPtr(
|
|
std::make_shared<td::protocol::commands::PlaceTowerCommand>(td::TowerType::Archer, 0, td::TowerCoords{77, 13}));
|
|
gh[0].push_back(tower);
|
|
|
|
return gh;
|
|
}
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
td::game::World w = GetWorld();
|
|
|
|
// init GL context
|
|
td::Display display(1920, 1080, "Tower-Defense 2");
|
|
|
|
td::render::Camera cam;
|
|
|
|
display.OnAspectRatioChange.Connect([&cam](float a_AspectRatio) { cam.UpdatePerspective(a_AspectRatio); });
|
|
|
|
td::sim::GameHistory gh = GetCustomHistory();
|
|
|
|
td::render::RenderPipeline renderer;
|
|
renderer.AddRenderer<td::render::WorldRenderer>(cam, w);
|
|
renderer.AddRenderer<td::render::EntityRenderer>(cam, w);
|
|
renderer.AddRenderer<td::render::TowerRenderer>(cam, w);
|
|
|
|
cam.SetCamPos({77, 7, 13});
|
|
cam.UpdatePerspective(display.GetAspectRatio());
|
|
|
|
td::sim::ClientSimulation simulation(w, 50);
|
|
|
|
display.OnKeyDown.Connect([&simulation](SDL_Keycode key) {
|
|
static int counter = 0;
|
|
if (key == SDLK_A) {
|
|
auto spawn = td::protocol::CommandPtr(
|
|
std::make_shared<td::protocol::commands::SpawnTroopCommand>(td::EntityType::Zombie, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0));
|
|
std::array<td::protocol::LockStep, LOCKSTEP_BUFFER_SIZE> steps{};
|
|
steps[0].push_back(spawn);
|
|
td::protocol::packets::LockStepsPacket packet{counter * LOCKSTEP_BUFFER_SIZE * 3, steps};
|
|
simulation.Handle(packet);
|
|
counter++;
|
|
}
|
|
});
|
|
|
|
while (!display.IsCloseRequested()) {
|
|
display.PollEvents();
|
|
float lerp = simulation.Update();
|
|
renderer.Render(lerp);
|
|
display.Update();
|
|
}
|
|
|
|
return 0;
|
|
}
|