Skip to content

Commit

Permalink
woopsie
Browse files Browse the repository at this point in the history
  • Loading branch information
afaure42 authored and SaumonDesMers committed Jun 1, 2024
1 parent 3189d5f commit d1a8a62
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 113 deletions.
14 changes: 4 additions & 10 deletions src/app/threads/update/UpdateThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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,
Expand All @@ -197,7 +190,7 @@ void UpdateThread::movePlayer()
m_move_right,
m_jump,
m_sneak,
static_cast<double>(m_delta_time.count()) / 1e9
static_cast<double>(std::chrono::duration_cast<std::chrono::milliseconds>(now - last_time).count() / 1000.0)
);

// m_world.applyPlayerMovement(m_world.m_my_player_id, displacement);
Expand All @@ -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()
Expand Down
118 changes: 15 additions & 103 deletions src/app/world/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,23 +570,11 @@ std::pair<glm::vec3, glm::vec3> World::calculatePlayerMovement(
return result;
}

void World::updatePlayerCamera(
const uint64_t player_id,
const double x_offset,
const double y_offset
)
{
std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(m_players.get(player_id));
std::unique_lock<std::mutex> guard(player->mutex);

player->moveDirection(x_offset, y_offset);
}

void World::updatePlayerTargetBlock(
const uint64_t player_id
)
{
std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(m_players.get(player_id));
std::shared_ptr<Player> player = m_players.at(player_id);
std::lock_guard<std::mutex> guard(player->mutex);

glm::dvec3 position = player->transform.position + player->eyePosition();
Expand All @@ -606,7 +594,7 @@ void World::playerAttack(
bool attack
)
{
std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(m_players.get(player_id));
std::shared_ptr<Player> player = m_players.at(player_id);
std::lock_guard<std::mutex> guard(player->mutex);

if (!attack || !player->canAttack())
Expand Down Expand Up @@ -634,7 +622,7 @@ void World::playerUse(
bool use
)
{
std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(m_players.get(player_id));
std::shared_ptr<Player> player = m_players.at(player_id);
std::lock_guard<std::mutex> guard(player->mutex);

if (!use || !player->canUse())
Expand Down Expand Up @@ -665,109 +653,32 @@ void World::updatePlayerCamera(
const double y_offset
)
{
std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(m_players.get(player_id));
std::shared_ptr<Player> player = m_players.at(player_id);
std::unique_lock<std::mutex> guard(player->mutex);

player->moveDirection(x_offset, y_offset);
}

void World::updatePlayerTargetBlock(
const uint64_t player_id
)
{
std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(m_players.get(player_id));
std::lock_guard<std::mutex> 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<glm::vec3> 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> player = std::dynamic_pointer_cast<Player>(m_players.get(player_id));
std::lock_guard<std::mutex> 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<std::mutex> 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> player = std::dynamic_pointer_cast<Player>(m_players.get(player_id));
std::lock_guard<std::mutex> 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<std::mutex> 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<void(Player &)> update
)
{
std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(m_players.at(player_id));
std::shared_ptr<Player> player = m_players.at(player_id);
std::lock_guard<std::mutex> lock(player->mutex);
update(*player);
}

Camera World::getCamera(const uint64_t player_id)
{
std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(m_players.at(player_id));
std::shared_ptr<Player> player = m_players.at(player_id);
std::lock_guard<std::mutex> lock(player->mutex);
return player->camera();
}

glm::dvec3 World::getPlayerPosition(const uint64_t player_id)
{
std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(m_players.at(player_id));
std::shared_ptr<Player> player = m_players.at(player_id);
std::lock_guard<std::mutex> lock(player->mutex);
return player->transform.position;
}
Expand Down Expand Up @@ -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();
}
}

Expand Down

0 comments on commit d1a8a62

Please sign in to comment.