#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); m_Renderer.AddRenderer(*gameState); 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 = Camera{{0}}; m_Camera.position = (Vector3){77.0f, 7.0f, 13.0f}; // Camera position m_Camera.target = (Vector3){0.0f, 1.0f, -1.0f}; // Camera looking at point m_Camera.up = (Vector3){0.0f, 1.0f, 0.0f}; // Camera up vector (rotation towards target) m_Camera.fovy = 45.0f; // Camera field-of-view Y m_Camera.projection = CAMERA_PERSPECTIVE; // Camera projection type // m_Camera.SetCamPos({77, 7, 13}); // m_Camera.UpdatePerspective(m_StateMachine.GetAspectRatio()); } void DebugWorldState::Update(float a_Delta) { m_Server->Update(a_Delta * m_PlaySpeed); m_Client->Update(a_Delta * m_PlaySpeed); UpdateCamera(&m_Camera, CAMERA_FREE); if (m_ClientState) { BeginMode3D(m_Camera); m_Renderer.Render(m_ClientState->GetCurrentLerp()); EndMode3D(); } constexpr int SECONDS = 10; if (IsKeyPressed(KEY_Q)) { m_Client->SendPacket(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1)); } if (IsKeyPressed(KEY_Z)) m_Client->SendPacket(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13))); if (IsKeyPressed(KEY_F)) { m_Server->Update(SECONDS); m_Client->Update(SECONDS); } if (IsKeyPressed(KEY_P)) m_PlaySpeed = 1 - m_PlaySpeed; } void DebugWorldState::OnAspectRatioChange(float a_Ratio) { // m_Camera.UpdatePerspective(a_Ratio); } void DebugWorldState::OnKeyDown(int a_Key) { // temporary tests } DebugWorldState::~DebugWorldState() {} } // namespace td