From 99354db9db9be0f921980f081ee7ed6515d2dcdc Mon Sep 17 00:00:00 2001 From: Elisey Puzko Date: Tue, 20 Feb 2018 13:09:39 +0300 Subject: New event: SelectedBlockChanged --- src/Entity.cpp | 116 +++++++++--------- src/GameState.cpp | 51 ++++++-- src/GameState.hpp | 64 +++++----- src/GlobalState.cpp | 7 +- src/Render.cpp | 329 ++++++++++++++++++++++++++-------------------------- src/Utility.cpp | 9 +- 6 files changed, 307 insertions(+), 269 deletions(-) (limited to 'src') diff --git a/src/Entity.cpp b/src/Entity.cpp index 821d556..a3a9527 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -31,64 +31,64 @@ Entity CreateObject(ObjectType type) { Entity entity; entity.type = EntityType::Object; switch (type) { - case ObjectType::Boat: - break; - case ObjectType::ItemStack: - entity.width = 0.25; - entity.height = 0.25; - break; - case ObjectType::AreaEffectCloud: - break; - case ObjectType::Minecart: - break; - case ObjectType::ActivatedTNT: - break; - case ObjectType::EnderCrystal: - break; - case ObjectType::TippedArrow: - break; - case ObjectType::Snowball: - break; - case ObjectType::Egg: - break; - case ObjectType::FireBall: - break; - case ObjectType::FireCharge: - break; - case ObjectType::ThrownEnderpearl: - break; - case ObjectType::WitherSkull: - break; - case ObjectType::ShulkerBullet: - break; - case ObjectType::LlamaSpit: - break; - case ObjectType::FallingObjects: - break; - case ObjectType::Itemframes: - break; - case ObjectType::EyeOfEnder: - break; - case ObjectType::ThrownPotion: - break; - case ObjectType::ThrownExpBottle: - break; - case ObjectType::FireworkRocket: - break; - case ObjectType::LeashKnot: - break; - case ObjectType::ArmorStand: - break; - case ObjectType::EvocationFangs: - break; - case ObjectType::FishingHook: - break; - case ObjectType::SpectralArrow: - break; - case ObjectType::DragonFireball: - break; - default: - break; + case ObjectType::Boat: + break; + case ObjectType::ItemStack: + entity.width = 0.25; + entity.height = 0.25; + break; + case ObjectType::AreaEffectCloud: + break; + case ObjectType::Minecart: + break; + case ObjectType::ActivatedTNT: + break; + case ObjectType::EnderCrystal: + break; + case ObjectType::TippedArrow: + break; + case ObjectType::Snowball: + break; + case ObjectType::Egg: + break; + case ObjectType::FireBall: + break; + case ObjectType::FireCharge: + break; + case ObjectType::ThrownEnderpearl: + break; + case ObjectType::WitherSkull: + break; + case ObjectType::ShulkerBullet: + break; + case ObjectType::LlamaSpit: + break; + case ObjectType::FallingObjects: + break; + case ObjectType::Itemframes: + break; + case ObjectType::EyeOfEnder: + break; + case ObjectType::ThrownPotion: + break; + case ObjectType::ThrownExpBottle: + break; + case ObjectType::FireworkRocket: + break; + case ObjectType::LeashKnot: + break; + case ObjectType::ArmorStand: + break; + case ObjectType::EvocationFangs: + break; + case ObjectType::FishingHook: + break; + case ObjectType::SpectralArrow: + break; + case ObjectType::DragonFireball: + break; + default: + break; } return entity; } diff --git a/src/GameState.cpp b/src/GameState.cpp index 6182d20..3166f1a 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -18,7 +18,7 @@ void GameState::Update(float deltaTime) { player->yaw, player->pitch, player->onGround); auto packet = std::static_pointer_cast(packetToSend); - PUSH_EVENT("SendPacket",packet); + PUSH_EVENT("SendPacket", packet); timeOfPreviousSendedPacket = clock.now(); } @@ -30,7 +30,7 @@ void GameState::Update(float deltaTime) { player->pos.z, player->onGround); auto packet = std::static_pointer_cast(updatePacket); - PUSH_EVENT("SendPacket",packet); + PUSH_EVENT("SendPacket", packet); } @@ -43,15 +43,26 @@ void GameState::Update(float deltaTime) { direction.z = sin(glm::radians(playerYaw)) * cos(glm::radians(playerPitch)); RaycastResult raycast = world.Raycast(player->pos + player->EyeOffset, direction); - selectedBlock = raycast.isHit ? raycast.hitBlock : Vector(0,0,0); - distanceToSelectedBlock = raycast.isHit ? (player->pos - raycast.hitPos).GetLength() : 0.0f; + if (raycast.isHit != isBlockSelected || ((raycast.isHit == true && isBlockSelected == true) && + selectedBlock != raycast.hitBlock)) { + PUSH_EVENT("SelectedBlockChanged", 0); + } + + if (raycast.isHit) { + selectedBlock = raycast.hitBlock; + distanceToSelectedBlock = (player->pos - raycast.hitPos).GetLength(); + } else { + selectedBlock = Vector(0, 0, 0); + distanceToSelectedBlock = 0.0f; + } + + isBlockSelected = raycast.isHit; raycastHit = raycast.hitPos; } } -void GameState::UpdatePacket(std::shared_ptr ptr) -{ - switch ((PacketNamePlayCB)ptr->GetPacketId()) { +void GameState::UpdatePacket(std::shared_ptr ptr) { + switch ((PacketNamePlayCB) ptr->GetPacketId()) { case SpawnObject: { auto packet = std::static_pointer_cast(ptr); Entity entity = CreateObject(static_cast(packet->Type)); @@ -300,7 +311,7 @@ void GameState::UpdatePacket(std::shared_ptr ptr) player->pitch += packet->Pitch; } else { player->pitch = packet->Pitch; - }; + } if ((packet->Flags & 0x08) != 0) { player->yaw += packet->Yaw; @@ -543,13 +554,31 @@ glm::mat4 GameState::GetViewMatrix() { return glm::lookAt(eyePos, eyePos + front, up); } +// TODO: it should actually be something like this: +// function start_digging(): +// send_packet(packet_type=start_digging_packet) +// delay(time=selected_block_dig_time, action=finish_digging) void GameState::StartDigging() { auto packetStart = std::make_shared(0,selectedBlock,1); - auto packetStop = std::make_shared(2,selectedBlock,1); auto packet = std::static_pointer_cast(packetStart); PUSH_EVENT("SendPacket",packet); - packet = std::static_pointer_cast(packetStop); + + FinishDigging(); +} + +void GameState::FinishDigging() { + auto packetFinish = std::make_shared(2,selectedBlock,1); + auto packet = std::static_pointer_cast(packetFinish); PUSH_EVENT("SendPacket",packet); } -void GameState::StopDigging() {} +// TODO: it should actually be something like this: +// function cancel_digging(): +// if finish_digging is in delayed_actions: +// send_packet(packet_type=start_digging_packet) +// remove_delayed_action(finish_digging) +void GameState::CancelDigging() { + auto packetCancel = std::make_shared(1,selectedBlock,1); + auto packet = std::static_pointer_cast(packetCancel); + PUSH_EVENT("SendPacket", packet); +} diff --git a/src/GameState.hpp b/src/GameState.hpp index fac9923..7183e64 100644 --- a/src/GameState.hpp +++ b/src/GameState.hpp @@ -20,40 +20,41 @@ public: ~GameState() = default; - void Update(float deltaTime); + void Update(float deltaTime); void UpdatePacket(std::shared_ptr ptr); - enum Direction { - FORWARD, BACKWARD, LEFT, RIGHT, JUMP - }; - void StartDigging(); - void StopDigging(); - void HandleMovement(GameState::Direction direction, float deltaTime); - void HandleRotation(double yaw, double pitch); - glm::mat4 GetViewMatrix(); + enum Direction { + FORWARD, BACKWARD, LEFT, RIGHT, JUMP + }; + void StartDigging(); + void FinishDigging(); + void CancelDigging(); + void HandleMovement(GameState::Direction direction, float deltaTime); + void HandleRotation(double yaw, double pitch); + glm::mat4 GetViewMatrix(); Entity* player; - World world; + World world; - std::string g_PlayerUuid = ""; - std::string g_PlayerName = ""; - bool g_IsGameStarted = false; - int g_PlayerEid = 0; - int g_Gamemode = 0; - int g_Dimension = 0; - unsigned char g_Difficulty = 0; - unsigned char g_MaxPlayers = 0; - std::string g_LevelType = ""; - bool g_ReducedDebugInfo = false; - Vector g_SpawnPosition; - bool g_PlayerInvulnerable = false; - bool g_PlayerFlying = false; - bool g_PlayerAllowFlying = false; - bool g_PlayerCreativeMode = false; - float g_PlayerFlyingSpeed = 0; - float g_PlayerFovModifier = 0; - float g_PlayerHealth = 0; + std::string g_PlayerUuid = ""; + std::string g_PlayerName = ""; + bool g_IsGameStarted = false; + int g_PlayerEid = 0; + int g_Gamemode = 0; + int g_Dimension = 0; + unsigned char g_Difficulty = 0; + unsigned char g_MaxPlayers = 0; + std::string g_LevelType = ""; + bool g_ReducedDebugInfo = false; + Vector g_SpawnPosition; + bool g_PlayerInvulnerable = false; + bool g_PlayerFlying = false; + bool g_PlayerAllowFlying = false; + bool g_PlayerCreativeMode = false; + float g_PlayerFlyingSpeed = 0; + float g_PlayerFovModifier = 0; + float g_PlayerHealth = 0; long long WorldAge = 0; long long TimeOfDay = 0; @@ -61,7 +62,8 @@ public: Window playerInventory; std::vector openedWindows; - Vector selectedBlock; - float distanceToSelectedBlock; - VectorF raycastHit; + bool isBlockSelected; + Vector selectedBlock; + float distanceToSelectedBlock; + VectorF raycastHit; }; diff --git a/src/GlobalState.cpp b/src/GlobalState.cpp index 4490862..73a34cb 100644 --- a/src/GlobalState.cpp +++ b/src/GlobalState.cpp @@ -160,7 +160,12 @@ void PhysExec() { }); listener.RegisterHandler("LmbReleased",[](const Event& eventData) { - gs->StopDigging(); + gs->CancelDigging(); + }); + + listener.RegisterHandler("SelectedBlockChanged", [](const Event& eventData) { + //TODO: + //gs->CancelDigging(); }); LoopExecutionTimeController timer(std::chrono::milliseconds(8)); diff --git a/src/Render.cpp b/src/Render.cpp index d126326..c5c295d 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -186,10 +186,10 @@ void Render::HandleEvents() { switch (event.key.keysym.scancode) { case SDL_SCANCODE_ESCAPE: { auto state = GlobalState::GetState(); - if (state == State::Playing || - state == State::Inventory) { + if (state == State::Playing) { GlobalState::SetState(State::Paused); } else if (state == State::Paused || + state == State::Inventory || state == State::Chat) { GlobalState::SetState(State::Playing); } else if (state == State::MainMenu) { @@ -216,10 +216,8 @@ void Render::HandleEvents() { auto state = GlobalState::GetState(); if (state == State::Playing) { GlobalState::SetState(State::Chat); - SetMouseCapture(false); } else if (state == State::Chat) { GlobalState::SetState(State::Playing); - SetMouseCapture(true); } } @@ -286,7 +284,6 @@ void Render::SetMouseCapture(bool IsCaptured) { } void Render::Update() { - HandleEvents(); if (HasFocus && GlobalState::GetState() == State::Playing) UpdateKeyboard(); if (isMouseCaptured) HandleMouseCapture(); @@ -359,192 +356,198 @@ void Render::RenderGui() { switch (GlobalState::GetState()) { - case State::MainMenu: { - ImGui::SetNextWindowPosCenter(); - ImGui::Begin("Menu", 0, windowFlags); - static char buff[512] = "127.0.0.1"; - static int port = 25565; - static char buffName[512] = "HelloOne"; - if (ImGui::Button("Connect")) { - PUSH_EVENT("ConnectToServer", std::make_tuple(std::string(buff), - (unsigned short) port, std::string(buffName))); - } - ImGui::InputText("Username", buffName, 512); - ImGui::InputText("Address", buff, 512); - ImGui::InputInt("Port", &port); - ImGui::Separator(); - if (ImGui::Button("Exit")) - PUSH_EVENT("Exit",0); - ImGui::End(); - break; - } - case State::Loading: - break; - case State::Chat: { - ImGui::SetNextWindowPosCenter(); - ImGui::Begin("Chat", 0, windowFlags); - for (const auto& msg : chatMessages) { - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1,1,1,1)); - ImGui::TextWrapped("%s", msg.c_str()); - ImGui::PopStyleColor(); - } - static char buff[256]; - ImGui::InputText("", buff, 256); - ImGui::SameLine(); - if (ImGui::Button("Send")) { - PUSH_EVENT("SendChatMessage", std::string(buff)); + case State::MainMenu: { + ImGui::SetNextWindowPosCenter(); + ImGui::Begin("Menu", 0, windowFlags); + static char buff[512] = "127.0.0.1"; + static int port = 25565; + static char buffName[512] = "HelloOne"; + if (ImGui::Button("Connect")) { + PUSH_EVENT("ConnectToServer", std::make_tuple(std::string(buff), + (unsigned short) port, std::string(buffName))); + } + ImGui::InputText("Username", buffName, 512); + ImGui::InputText("Address", buff, 512); + ImGui::InputInt("Port", &port); + ImGui::Separator(); + if (ImGui::Button("Exit")) + PUSH_EVENT("Exit",0); + ImGui::End(); + break; } - ImGui::End(); - break; - } - case State::Inventory: { - auto renderSlot = [](const SlotDataType &slot, int i) -> bool { - return ImGui::Button(((slot.BlockId == -1 ? " ##" : - AssetManager::Instance().GetAssetNameByBlockId(BlockId{ (unsigned short)slot.BlockId,0 }) + " x" + std::to_string(slot.ItemCount) + "##") - + std::to_string(i)).c_str()); - }; - ImGui::SetNextWindowPosCenter(); - ImGui::Begin("Inventory", 0, windowFlags); - Window& inventory = world->GameStatePtr()->playerInventory; - //Hand and drop slots - if (renderSlot(inventory.handSlot, -1)) { - } - ImGui::SameLine(); - if (ImGui::Button("Drop")) { - inventory.MakeClick(-1, true, true); - } - ImGui::SameLine(); - ImGui::Text("Hand slot and drop mode"); - ImGui::Separator(); - //Crafting - if (renderSlot(inventory.slots[1], 1)) { - inventory.MakeClick(1, true); - } - ImGui::SameLine(); - if (renderSlot(inventory.slots[2], 2)) { - inventory.MakeClick(2, true); - } - //Crafting result - ImGui::SameLine(); - ImGui::Text("Result"); - ImGui::SameLine(); - if (renderSlot(inventory.slots[0], 0)) { - inventory.MakeClick(0, true); - } - //Crafting second line - if (renderSlot(inventory.slots[3], 3)) { - inventory.MakeClick(3, true); - } - ImGui::SameLine(); - if (renderSlot(inventory.slots[4], 4)) { - inventory.MakeClick(4, true); - } - ImGui::Separator(); - //Armor and offhand - for (int i = 5; i < 8 + 1; i++) { - if (renderSlot(inventory.slots[i], i)) { - inventory.MakeClick(i, true); + case State::Loading: + break; + + case State::Chat: { + ImGui::SetNextWindowPosCenter(); + ImGui::Begin("Chat", 0, windowFlags); + for (const auto& msg : chatMessages) { + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1,1,1,1)); + ImGui::TextWrapped("%s", msg.c_str()); + ImGui::PopStyleColor(); } + static char buff[256]; + ImGui::InputText("", buff, 256); ImGui::SameLine(); + if (ImGui::Button("Send")) { + PUSH_EVENT("SendChatMessage", std::string(buff)); + } + ImGui::End(); + break; } - if (renderSlot(inventory.slots[45], 45)) { - inventory.MakeClick(45, true); - } - ImGui::SameLine(); - ImGui::Text("Armor and offhand"); - ImGui::Separator(); - for (int i = 36; i < 44 + 1; i++) { - if (renderSlot(inventory.slots[i], i)) { - inventory.MakeClick(i, true); + + case State::Inventory: { + auto renderSlot = [](const SlotDataType &slot, int i) -> bool { + return ImGui::Button(((slot.BlockId == -1 ? " ##" : + AssetManager::Instance().GetAssetNameByBlockId(BlockId{ (unsigned short)slot.BlockId,0 }) + " x" + std::to_string(slot.ItemCount) + "##") + + std::to_string(i)).c_str()); + }; + ImGui::SetNextWindowPosCenter(); + ImGui::Begin("Inventory", 0, windowFlags); + Window& inventory = world->GameStatePtr()->playerInventory; + //Hand and drop slots + if (renderSlot(inventory.handSlot, -1)) { + } ImGui::SameLine(); - } - ImGui::Text("Hotbar"); - ImGui::Separator(); - ImGui::Text("Main inventory"); - for (int i = 9; i < 17 + 1; i++) { - if (renderSlot(inventory.slots[i], i)) { - inventory.MakeClick(i, true); + if (ImGui::Button("Drop")) { + inventory.MakeClick(-1, true, true); } ImGui::SameLine(); - } - ImGui::Text(""); - for (int i = 18; i < 26 + 1; i++) { - if (renderSlot(inventory.slots[i], i)) { - inventory.MakeClick(i, true); + ImGui::Text("Hand slot and drop mode"); + ImGui::Separator(); + //Crafting + if (renderSlot(inventory.slots[1], 1)) { + inventory.MakeClick(1, true); } ImGui::SameLine(); - } - ImGui::Text(""); - for (int i = 27; i < 35 + 1; i++) { - if (renderSlot(inventory.slots[i], i)) { - inventory.MakeClick(i, true); + if (renderSlot(inventory.slots[2], 2)) { + inventory.MakeClick(2, true); } + //Crafting result ImGui::SameLine(); - } - ImGui::End(); + ImGui::Text("Result"); + ImGui::SameLine(); + if (renderSlot(inventory.slots[0], 0)) { + inventory.MakeClick(0, true); + } + //Crafting second line + if (renderSlot(inventory.slots[3], 3)) { + inventory.MakeClick(3, true); + } + ImGui::SameLine(); + if (renderSlot(inventory.slots[4], 4)) { + inventory.MakeClick(4, true); + } + ImGui::Separator(); + //Armor and offhand + for (int i = 5; i < 8 + 1; i++) { + if (renderSlot(inventory.slots[i], i)) { + inventory.MakeClick(i, true); + } + ImGui::SameLine(); + } + if (renderSlot(inventory.slots[45], 45)) { + inventory.MakeClick(45, true); + } + ImGui::SameLine(); + ImGui::Text("Armor and offhand"); + ImGui::Separator(); + for (int i = 36; i < 44 + 1; i++) { + if (renderSlot(inventory.slots[i], i)) { + inventory.MakeClick(i, true); + } + ImGui::SameLine(); + } + ImGui::Text("Hotbar"); + ImGui::Separator(); + ImGui::Text("Main inventory"); + for (int i = 9; i < 17 + 1; i++) { + if (renderSlot(inventory.slots[i], i)) { + inventory.MakeClick(i, true); + } + ImGui::SameLine(); + } + ImGui::Text(""); + for (int i = 18; i < 26 + 1; i++) { + if (renderSlot(inventory.slots[i], i)) { + inventory.MakeClick(i, true); + } + ImGui::SameLine(); + } + ImGui::Text(""); + for (int i = 27; i < 35 + 1; i++) { + if (renderSlot(inventory.slots[i], i)) { + inventory.MakeClick(i, true); + } + ImGui::SameLine(); + } + ImGui::End(); - break; - } - case State::Paused: { - ImGui::SetNextWindowPosCenter(); - ImGui::Begin("Pause Menu", 0, windowFlags); - if (ImGui::Button("Continue")) { - GlobalState::SetState(State::Playing); + break; } - ImGui::Separator(); - static float distance = world->MaxRenderingDistance; - ImGui::SliderFloat("Render distance", &distance, 1.0f, 16.0f); - static float sense = sensetivity; - ImGui::SliderFloat("Sensetivity", &sense, 0.01f, 1.0f); + case State::Paused: { + ImGui::SetNextWindowPosCenter(); + ImGui::Begin("Pause Menu", 0, windowFlags); + if (ImGui::Button("Continue")) { + GlobalState::SetState(State::Playing); + } + ImGui::Separator(); + static float distance = world->MaxRenderingDistance; + ImGui::SliderFloat("Render distance", &distance, 1.0f, 16.0f); + + static float sense = sensetivity; + ImGui::SliderFloat("Sensetivity", &sense, 0.01f, 1.0f); - static float targetFps = 60.0f; - ImGui::SliderFloat("Target FPS", &targetFps, 1.0f, 300.0f); + static float targetFps = 60.0f; + ImGui::SliderFloat("Target FPS", &targetFps, 1.0f, 300.0f); - static bool wireframe = isWireframe; + static bool wireframe = isWireframe; - ImGui::Checkbox("Wireframe", &wireframe); + ImGui::Checkbox("Wireframe", &wireframe); - static bool vsync = false; + static bool vsync = false; - ImGui::Checkbox("VSync", &vsync); + ImGui::Checkbox("VSync", &vsync); - if (ImGui::Button("Apply settings")) { - if (distance != world->MaxRenderingDistance) { - world->MaxRenderingDistance = distance; - PUSH_EVENT("UpdateSectionsRender", 0); - } + if (ImGui::Button("Apply settings")) { + if (distance != world->MaxRenderingDistance) { + world->MaxRenderingDistance = distance; + PUSH_EVENT("UpdateSectionsRender", 0); + } - if (sense != sensetivity) - sensetivity = sense; + if (sense != sensetivity) + sensetivity = sense; - isWireframe = wireframe; - timer.SetDelayLength(std::chrono::duration(1.0 / targetFps * 1000.0)); - if (vsync) { - timer.SetDelayLength(std::chrono::milliseconds(0)); - SDL_GL_SetSwapInterval(1); - } else - SDL_GL_SetSwapInterval(0); + isWireframe = wireframe; + timer.SetDelayLength(std::chrono::duration(1.0 / targetFps * 1000.0)); + if (vsync) { + timer.SetDelayLength(std::chrono::milliseconds(0)); + SDL_GL_SetSwapInterval(1); + } else + SDL_GL_SetSwapInterval(0); + } + ImGui::Separator(); + + if (ImGui::Button("Disconnect")) { + PUSH_EVENT("Disconnect", std::string("Disconnected by user")); + } + ImGui::End(); + break; } - ImGui::Separator(); - if (ImGui::Button("Disconnect")) { - PUSH_EVENT("Disconnect", std::string("Disconnected by user")); + case State::InitialLoading: + break; + + case State::Playing: { + ImGui::SetNextWindowPosCenter(); + ImGui::Begin("",0,windowFlags); + ImGui::End(); + break; } - ImGui::End(); - break; - } - case State::InitialLoading: - break; - case State::Playing: { - ImGui::SetNextWindowPosCenter(); - ImGui::Begin("",0,windowFlags); - ImGui::End(); - break; - } } ImGui::Render(); diff --git a/src/Utility.cpp b/src/Utility.cpp index 0fb10cf..c498fa3 100644 --- a/src/Utility.cpp +++ b/src/Utility.cpp @@ -37,7 +37,8 @@ GLenum glCheckError_(const char *file, int line) { return errorCode; } -LoopExecutionTimeController::LoopExecutionTimeController(duration delayLength) : delayLength(delayLength) { +LoopExecutionTimeController::LoopExecutionTimeController(duration delayLength) + : delayLength(delayLength) { previousUpdate = clock::now(); } @@ -77,13 +78,11 @@ double LoopExecutionTimeController::GetDeltaS() { return delta.count(); } -double LoopExecutionTimeController::GetRealDeltaS() -{ +double LoopExecutionTimeController::GetRealDeltaS() { return std::chrono::duration>(previousUpdate - previousPreviousUpdate).count(); } -double LoopExecutionTimeController::RemainTimeMs() -{ +double LoopExecutionTimeController::RemainTimeMs() { auto remain = delayLength - GetDelta(); return remain.count(); } -- cgit v1.2.3