diff --git a/src/app/threads/update/UpdateThread.cpp b/src/app/threads/update/UpdateThread.cpp index c735838b..75ffa0f3 100644 --- a/src/app/threads/update/UpdateThread.cpp +++ b/src/app/threads/update/UpdateThread.cpp @@ -13,7 +13,6 @@ UpdateThread::UpdateThread( VulkanAPI & vulkan_api, std::chrono::nanoseconds start_time ): - m_client(client), m_settings(settings), m_window(window), m_world_scene(world_scene), @@ -69,16 +68,11 @@ void UpdateThread::loop() readInput(); //only move player every 20 ms static auto last_move = std::chrono::steady_clock::now(); - if (std::chrono::steady_clock::now() - last_move > std::chrono::milliseconds(1)) - { - //only move player every 20 ms - static auto last_move = std::chrono::steady_clock::now(); if (std::chrono::steady_clock::now() - last_move > std::chrono::milliseconds(20)) + { movePlayer(); last_move = std::chrono::steady_clock::now(); } - - handlePackets(); handlePackets(); } @@ -188,7 +182,6 @@ void UpdateThread::movePlayer() m_last_target_block_update_time = m_current_time; } - auto [position, displacement] = m_world.calculatePlayerMovement( auto [position, displacement] = m_world.calculatePlayerMovement( m_world.m_my_player_id, m_move_forward, @@ -197,7 +190,7 @@ void UpdateThread::movePlayer() m_move_right, m_jump, m_sneak, - static_cast(m_delta_time.count()) / 1e9 + static_cast(std::chrono::duration_cast(now - last_time).count() / 1000.0) ); // m_world.applyPlayerMovement(m_world.m_my_player_id, displacement); @@ -207,13 +200,14 @@ void UpdateThread::movePlayer() m_client.sendPacket(packet); } - m_world.updatePlayer( + m_world.updatePlayerCamera( m_world.m_my_player_id, look.x, look.y ); m_world_scene.camera() = m_world.getCamera(m_world.m_my_player_id); + last_time = now; } void UpdateThread::handlePackets() diff --git a/src/app/world/World.cpp b/src/app/world/World.cpp index 1f22a4da..bfbabd15 100644 --- a/src/app/world/World.cpp +++ b/src/app/world/World.cpp @@ -570,23 +570,11 @@ std::pair World::calculatePlayerMovement( return result; } -void World::updatePlayerCamera( - const uint64_t player_id, - const double x_offset, - const double y_offset -) -{ - std::shared_ptr player = std::dynamic_pointer_cast(m_players.get(player_id)); - std::unique_lock guard(player->mutex); - - player->moveDirection(x_offset, y_offset); -} - void World::updatePlayerTargetBlock( const uint64_t player_id ) { - std::shared_ptr player = std::dynamic_pointer_cast(m_players.get(player_id)); + std::shared_ptr player = m_players.at(player_id); std::lock_guard guard(player->mutex); glm::dvec3 position = player->transform.position + player->eyePosition(); @@ -606,7 +594,7 @@ void World::playerAttack( bool attack ) { - std::shared_ptr player = std::dynamic_pointer_cast(m_players.get(player_id)); + std::shared_ptr player = m_players.at(player_id); std::lock_guard guard(player->mutex); if (!attack || !player->canAttack()) @@ -634,7 +622,7 @@ void World::playerUse( bool use ) { - std::shared_ptr player = std::dynamic_pointer_cast(m_players.get(player_id)); + std::shared_ptr player = m_players.at(player_id); std::lock_guard guard(player->mutex); if (!use || !player->canUse()) @@ -665,109 +653,32 @@ void World::updatePlayerCamera( const double y_offset ) { - std::shared_ptr player = std::dynamic_pointer_cast(m_players.get(player_id)); + std::shared_ptr player = m_players.at(player_id); std::unique_lock guard(player->mutex); player->moveDirection(x_offset, y_offset); } -void World::updatePlayerTargetBlock( - const uint64_t player_id -) -{ - std::shared_ptr player = std::dynamic_pointer_cast(m_players.get(player_id)); - std::lock_guard guard(player->mutex); - - glm::dvec3 position = player->transform.position + player->eyePosition(); - glm::dvec3 direction = player->direction(); - - RayCastOnBlockResult raycast = rayCastOnBlock(position, direction, 5.0); - - std::optional target_block = raycast.hit ? std::make_optional(raycast.block_position) : std::nullopt; - - m_worldScene.setTargetBlock(target_block); - - player->targeted_block = raycast; -} - -void World::playerAttack( - const uint64_t player_id, - bool attack -) -{ - std::shared_ptr player = std::dynamic_pointer_cast(m_players.get(player_id)); - std::lock_guard guard(player->mutex); - - if (!attack || !player->canAttack()) - return; - player->startAttack(); - - if (player->targeted_block.hit) - { - // LOG_DEBUG("Block hit: " - // << player->targeted_block.block_position.x << " " << player->targeted_block.block_position.y << " " << player->targeted_block.block_position.z - // << " = " << int(player->targeted_block.block) - // ); - - std::lock_guard lock(m_blocks_to_set_mutex); - m_blocks_to_set.push({player->targeted_block.block_position, Block::Air.id}); - } - // else - // { - // LOG_DEBUG("No block hit"); - // } -} - -void World::playerUse( - const uint64_t player_id, - bool use -) -{ - std::shared_ptr player = std::dynamic_pointer_cast(m_players.get(player_id)); - std::lock_guard guard(player->mutex); - - if (!use || !player->canUse()) - return; - player->startUse(); - - if (player->targeted_block.hit) - { - glm::vec3 block_placed_position = player->targeted_block.block_position + player->targeted_block.normal; - - // check collision with player - if (isColliding( - player->hitbox, - player->transform.position, - Block::getData(player->targeted_block.block).hitbox, - block_placed_position - )) - return; - - std::lock_guard lock(m_blocks_to_set_mutex); - m_blocks_to_set.push({block_placed_position, Block::Stone.id}); - } -} - void World::updatePlayer( const uint64_t player_id, std::function update ) { - std::shared_ptr player = std::dynamic_pointer_cast(m_players.at(player_id)); + std::shared_ptr player = m_players.at(player_id); std::lock_guard lock(player->mutex); update(*player); } Camera World::getCamera(const uint64_t player_id) { - std::shared_ptr player = std::dynamic_pointer_cast(m_players.at(player_id)); + std::shared_ptr player = m_players.at(player_id); std::lock_guard lock(player->mutex); return player->camera(); } glm::dvec3 World::getPlayerPosition(const uint64_t player_id) { - std::shared_ptr player = std::dynamic_pointer_cast(m_players.at(player_id)); + std::shared_ptr player = m_players.at(player_id); std::lock_guard lock(player->mutex); return player->transform.position; } @@ -921,15 +832,16 @@ void World::addPlayer(const uint64_t player_id, const glm::vec3 & position) { // auto world_scene_lock = m_worldScene.entity_mesh_list.lock(); m_worldScene.entity_mesh_list.insert( - player_id, { - m_vulkanAPI.cube_mesh_id, {} + player_id, + { + m_vulkanAPI.cube_mesh_id, + Transform( + player->transform.position + player->hitbox.position, + glm::vec3(0.0f), + player->hitbox.size + ).model() } ); - m_worldScene.entity_mesh_list.at(player_id).model = Transform( - player->transform.position + player->hitbox.position, - glm::vec3(0.0f), - player->hitbox.size - ).model(); } }