#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace td { DebugWorldState::DebugWorldState(Display& a_Display) : DisplayState(a_Display) { // server m_ServerSocket = std::make_shared(); m_Server = std::make_unique(m_ServerSocket); // client auto clientFakeSocket = client::FakeSocket::Connect(m_ServerSocket); m_Client = std::make_unique(clientFakeSocket); // TODO: make it better m_Client->OnStateChange.Connect([this](client::Client::State& a_State) { if (auto gameState = dynamic_cast(&a_State)) { // render auto clientWorld = gameState->GetWorld(); m_Renderer.AddRenderer(m_Camera, clientWorld); m_Renderer.AddRenderer(m_Camera, clientWorld); m_Renderer.AddRenderer(m_Camera, clientWorld); auto& list = m_Renderer.AddRenderer(m_Client->GetPlayers()); list.OnPlayerCreate.Connect([this]() { auto newSocket = client::FakeSocket::Connect(m_ServerSocket); auto newClient = std::make_unique(newSocket); newClient->ChangeState("Bot"); m_FakeClients.push_back(std::move(newClient)); }); list.OnPlayerKick.Connect([this](PlayerID a_Player) { auto it = std::find_if(m_FakeClients.begin(), m_FakeClients.end(), [a_Player](auto& clientPtr){ if (!clientPtr->GetId().has_value()) return false; return clientPtr->GetId().value() == a_Player; }); m_FakeClients.erase(it); }); // update state m_ClientState = gameState; } }); m_Client->ChangeState("Player0"); // camera m_Camera.SetCamPos({77, 7, 13}); m_Camera.UpdatePerspective(m_StateMachine.GetAspectRatio()); } void DebugWorldState::Update(float a_Delta) { m_Server->Update(a_Delta); m_Client->Update(a_Delta); if (m_ClientState) m_Renderer.Render(m_ClientState->GetCurrentLerp()); } void DebugWorldState::OnAspectRatioChange(float a_Ratio) { m_Camera.UpdatePerspective(a_Ratio); } void DebugWorldState::OnKeyDown(SDL_Keycode a_Key) { // temporary tests switch (a_Key) { case SDLK_A: m_Client->SendPacket(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1)); break; case SDLK_Z: m_Client->SendPacket(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13))); break; default: break; } } DebugWorldState::~DebugWorldState() {} } // namespace td