#include #include #include #include #include #include #include #include #include #include #include #include #include #include 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(); std::ofstream fStream("test/tdmap.tdmap3"); auto out = std::make_shared(fStream); sp::MessageStream stream(std::move(out), std::move(comp)); stream.WriteMessage(header, false); stream.WriteMessage(data, false); } td::game::World GetWorld() { auto comp = std::make_shared(); std::ifstream fStream("test/tdmap.tdmap2"); auto out = std::make_shared(fStream); sp::MessageStream 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(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::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::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(cam, w); renderer.AddRenderer(cam, w); renderer.AddRenderer(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::EntityType::Zombie, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0)); std::array 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; }