diff options
Diffstat (limited to '')
-rw-r--r-- | src/GameState.cpp | 16 | ||||
-rw-r--r-- | src/GameState.hpp | 4 | ||||
-rw-r--r-- | src/GlobalState.cpp | 8 | ||||
-rw-r--r-- | src/Render.cpp | 28 |
4 files changed, 46 insertions, 10 deletions
diff --git a/src/GameState.cpp b/src/GameState.cpp index de9f1d7..20926e0 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -27,6 +27,7 @@ void GameState::Update(float deltaTime) { PUSH_EVENT("SendPacket",packet); } + selectedBlock = Vector(Vector(player->pos.x,player->pos.y,player->pos.z) - Vector(-1,0,0)); } } @@ -470,4 +471,17 @@ glm::mat4 GameState::GetViewMatrix() { glm::vec3 eyePos = player->pos.glm(); eyePos += player->EyeOffset.glm(); return glm::lookAt(eyePos, eyePos + front, up); -}
\ No newline at end of file +} + +void GameState::StartDigging() { + auto packetStart = std::make_shared<PacketPlayerDigging>(0,selectedBlock,1); + auto packetStop = std::make_shared<PacketPlayerDigging>(2,selectedBlock,1); + auto packet = std::static_pointer_cast<Packet>(packetStart); + PUSH_EVENT("SendPacket",packet); + packet = std::static_pointer_cast<Packet>(packetStop); + PUSH_EVENT("SendPacket",packet); +} + +void GameState::StopDigging() { + +} diff --git a/src/GameState.hpp b/src/GameState.hpp index 7145df5..0bc8cfd 100644 --- a/src/GameState.hpp +++ b/src/GameState.hpp @@ -27,6 +27,8 @@ public: 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(); @@ -58,4 +60,6 @@ public: Window playerInventory; std::vector<Window> openedWindows; + + Vector selectedBlock; }; diff --git a/src/GlobalState.cpp b/src/GlobalState.cpp index 80a3342..ce8cb05 100644 --- a/src/GlobalState.cpp +++ b/src/GlobalState.cpp @@ -154,6 +154,14 @@ void PhysExec() { gs->UpdatePacket(packet); }); + listener.RegisterHandler("LmbPressed",[](const Event& eventData) { + gs->StartDigging(); + }); + + listener.RegisterHandler("LmbReleased",[](const Event& eventData) { + gs->StopDigging(); + }); + LoopExecutionTimeController timer(std::chrono::milliseconds(8)); while (isPhysRunning) { diff --git a/src/Render.cpp b/src/Render.cpp index 9c5cd49..f53cc91 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -35,7 +35,7 @@ Render::~Render() { void Render::InitSdl(unsigned int WinWidth, unsigned int WinHeight, std::string WinTitle) { LOG(INFO) << "Creating window: " << WinWidth << "x" << WinHeight << " \"" << WinTitle << "\""; - + if (SDL_Init(SDL_INIT_VIDEO) < 0) throw std::runtime_error("SDL initalization failed: " + std::string(SDL_GetError())); @@ -52,7 +52,7 @@ void Render::InitSdl(unsigned int WinWidth, unsigned int WinHeight, std::string if (!glContext) throw std::runtime_error("OpenGl context creation failed: " + std::string(SDL_GetError())); - SetMouseCapture(false); + SetMouseCapture(false); renderState.WindowWidth = WinWidth; renderState.WindowHeight = WinHeight; @@ -115,7 +115,7 @@ void Render::UpdateKeyboard() { } } -void Render::RenderFrame() { +void Render::RenderFrame() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (isWireframe) @@ -159,7 +159,7 @@ void Render::HandleEvents() { HasFocus = true; break; case SDL_WINDOWEVENT_FOCUS_LOST: - HasFocus = false; + HasFocus = false; if (GlobalState::GetState() == State::Inventory || GlobalState::GetState() == State::Playing || GlobalState::GetState() == State::Chat) GlobalState::SetState(State::Paused); break; @@ -209,22 +209,31 @@ void Render::HandleEvents() { } break; } - break; + break; case SDL_MOUSEMOTION: if (isMouseCaptured) { double deltaX = event.motion.xrel; - double deltaY = event.motion.yrel; + double deltaY = event.motion.yrel; deltaX *= sensetivity; deltaY *= sensetivity * -1; PUSH_EVENT("MouseMove", std::make_tuple(deltaX, deltaY)); } + break; + case SDL_MOUSEBUTTONDOWN: + if (event.button.button == SDL_BUTTON_LEFT && !ImGui::GetIO().WantCaptureMouse) + PUSH_EVENT("LmbPressed",0); + break; + case SDL_MOUSEBUTTONUP: + if (event.button.button == SDL_BUTTON_LEFT && !ImGui::GetIO().WantCaptureMouse) + PUSH_EVENT("LmbReleased",0); + break; default: break; } } } -void Render::HandleMouseCapture() { +void Render::HandleMouseCapture() { } void Render::SetMouseCapture(bool IsCaptured) { @@ -235,7 +244,7 @@ void Render::SetMouseCapture(bool IsCaptured) { if (isMouseCaptured) { SDL_GetGlobalMouseState(&prevMouseX, &prevMouseY); } - + SDL_CaptureMouse(IsCaptured ? SDL_TRUE : SDL_FALSE); SDL_SetRelativeMouseMode(IsCaptured ? SDL_TRUE : SDL_FALSE); @@ -282,6 +291,7 @@ void Render::RenderGui() { ImGui::Text("Player pos: %.1f %.1f %.1f OnGround=%d", world->GameStatePtr()->player->pos.x, world->GameStatePtr()->player->pos.y, world->GameStatePtr()->player->pos.z, world->GameStatePtr()->player->onGround); ImGui::Text("Player vel: %.1f %.1f %.1f", world->GameStatePtr()->player->vel.x, world->GameStatePtr()->player->vel.y, world->GameStatePtr()->player->vel.z); ImGui::Text("Player health: %.1f/%.1f", world->GameStatePtr()->g_PlayerHealth, 20.0f); + ImGui::Text("Selected block: %d %d %d",world->GameStatePtr()->selectedBlock.x,world->GameStatePtr()->selectedBlock.y,world->GameStatePtr()->selectedBlock.z); } ImGui::End(); @@ -368,7 +378,7 @@ void Render::RenderGui() { inventory.MakeClick(4, true); } ImGui::Separator(); - //Armor and offhand + //Armor and offhand for (int i = 5; i < 8 + 1; i++) { if (renderSlot(inventory.slots[i], i)) { inventory.MakeClick(i, true); |