This commit is contained in:
2024-08-23 20:58:40 +02:00
parent 57a46db439
commit ad7b48d1fb
8 changed files with 137 additions and 15200 deletions

File diff suppressed because one or more lines are too long

View File

@@ -6,7 +6,7 @@
namespace blitz {
class FirstPersonPlayer : public Player {
GDCLASS(FirstPersonPlayer, godot::CharacterBody3D)
GDCLASS(FirstPersonPlayer, godot::Node)
protected:
static void _bind_methods();
@@ -17,11 +17,10 @@ class FirstPersonPlayer : public Player {
// Godot overrides
void _unhandled_input(const godot::Ref<godot::InputEvent>&);
void _physics_process(float delta) override;
void _ready() override;
void _ready();
private:
godot::Camera3D* m_Camera;
godot::Node3D* m_Head;
float m_BobTime;
float m_Speed;
@@ -29,8 +28,6 @@ class FirstPersonPlayer : public Player {
void UpdateFOV(float delta);
void UpdateCamera(const godot::InputEventMouseMotion&);
void UpdatePosition(float delta);
void UpdateAnimation(float delta);
};
} // namespace blitz

View File

@@ -9,9 +9,9 @@ namespace blitz {
class World;
class Player : public godot::CharacterBody3D {
class Player : public godot::Node {
GDCLASS(Player, godot::CharacterBody3D);
GDCLASS(Player, godot::Node);
protected:
static void _bind_methods();
@@ -22,7 +22,13 @@ class Player : public godot::CharacterBody3D {
void _ready() override;
virtual void _physics_process(float delta);
void animate(float delta);
godot::Vector3 GetPosition() const;
void SetPosition(const godot::Vector3& a_Position);
godot::Vector3 GetVelocity() const;
void SetVelocity(const godot::Vector3& a_Velocity);
godot::Vector3 GetCameraRotation() const;
void SetCameraRotation(const godot::Vector3& a_Rotation);
@@ -33,11 +39,17 @@ class Player : public godot::CharacterBody3D {
protected:
godot::Node3D* m_Mesh;
godot::Node3D* m_Head;
godot::AnimationTree* m_AnimationTree;
godot::CharacterBody3D* m_Player;
godot::Vector3 m_SnapVector;
PeerID m_PeerId;
void SetModelVisible(bool visible);
void UpdateAnimation(float delta);
void BlendAnimation(const godot::String& a_AnimationName, float a_Goal, float a_Delta);
friend class World;
};
} // namespace blitz

View File

@@ -14,8 +14,7 @@ using namespace godot;
namespace blitz {
static const char FirstPersonPlayerScenePath[] = "res://Scenes/Characters/first_person_player.tscn";
static const char PlayerScenePath[] = "res://Scenes/Characters/player.tscn";
static const char PlayerScenePath[] = "res://Scenes/Characters/remy.tscn";
void World::_bind_methods() {}
@@ -67,16 +66,26 @@ void World::HandlePacket(const protocol::packets::PlayerLeave& a_PlayerLeave) {
void World::AddPlayer(PlayerID a_PlayerId, String a_PlayerName) {
UtilityFunctions::print("New Player with id : ", a_PlayerId, " and name ", a_PlayerName);
if (a_PlayerId == get_multiplayer()->get_unique_id()) {
Ref<PackedScene> serverScene = ResourceLoader::get_singleton()->load(FirstPersonPlayerScenePath);
FirstPersonPlayer* player = Object::cast_to<FirstPersonPlayer>(serverScene->instantiate());
Ref<PackedScene> serverScene = ResourceLoader::get_singleton()->load(PlayerScenePath);
Node* playerContent = serverScene->instantiate();
FirstPersonPlayer* player = memnew(FirstPersonPlayer);
player->set_name(UtilityFunctions::var_to_str(a_PlayerId));
player->m_PeerId = a_PlayerId;
player->add_child(playerContent);
m_Players->add_child(player);
} else {
Ref<PackedScene> serverScene = ResourceLoader::get_singleton()->load(PlayerScenePath);
Player* player = Object::cast_to<Player>(serverScene->instantiate());
Node* playerContent = serverScene->instantiate();
Player* player = memnew(Player);
player->set_name(UtilityFunctions::var_to_str(a_PlayerId));
player->m_PeerId = a_PlayerId;
player->add_child(playerContent);
m_Players->add_child(player);
}
}
@@ -93,9 +102,9 @@ void World::SetPlayerPositionAndRotation(
PlayerID a_PlayerId, const Vector3& a_Position, const Vector3& a_Rotation, const godot::Vector3& a_Velocity) {
Player* player = GetPlayerById(a_PlayerId);
if (player) {
player->set_position(a_Position);
player->SetPosition(a_Position);
player->SetCameraRotation(a_Rotation);
player->set_velocity(a_Velocity);
player->SetVelocity(a_Velocity);
}
}

View File

@@ -36,7 +36,7 @@ void ClientWorld::UpdatePlayerPos() {
Player* player = GetPlayerById(get_multiplayer()->get_unique_id());
if (player) {
m_NetworkInterface->BroadcastPacket(protocol::packets::PlayerPositionAndRotation(
{get_multiplayer()->get_unique_id(), player->get_position(), player->GetCameraRotation(), player->get_velocity()}));
{get_multiplayer()->get_unique_id(), player->GetPosition(), player->GetCameraRotation(), player->GetVelocity()}));
}
}
@@ -44,7 +44,7 @@ void ClientWorld::HandlePacket(const protocol::packets::PlayerPositionAndRotatio
const auto& data = a_PlayerPos.m_Data;
if (data.m_Player == get_multiplayer()->get_unique_id()) {
Player* player = GetPlayerById(get_multiplayer()->get_unique_id());
if (player && (a_PlayerPos.m_Data.m_Position - player->get_position()).length() > 10) {
if (player && (a_PlayerPos.m_Data.m_Position - player->GetPosition()).length() > 10) {
SetPlayerPositionAndRotation(data.m_Player, data.m_Position, data.m_Rotation, data.m_Velocity);
godot::UtilityFunctions::print("Teleported to : ", data.m_Position);
}

View File

@@ -44,17 +44,22 @@ FirstPersonPlayer::FirstPersonPlayer() : Player(), m_BobTime(0) {}
FirstPersonPlayer::~FirstPersonPlayer() {}
void FirstPersonPlayer::_ready() {
Player::_ready();
InputMap::get_singleton()->load_from_project_settings();
if (!Engine::get_singleton()->is_editor_hint()) {
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
} else {
return;
}
m_Head = Object::cast_to<Node3D>(find_child("Head"));
m_Camera = Object::cast_to<Camera3D>(m_Head->find_child("Camera"));
m_AnimationTree = Object::cast_to<AnimationTree>(find_child("AnimationTree"));
m_Mesh = Object::cast_to<Node3D>(find_child("Mesh"));
set_position({0, 0, 0});
set_velocity({0, 0, 0});
m_Camera = memnew(Camera3D);
m_Camera->set_name("FirstPersonCamera");
m_Head->add_child(m_Camera);
m_Camera->make_current();
SetPosition({0, 100, 0});
SetVelocity({0, 0, 0});
}
void FirstPersonPlayer::_unhandled_input(const godot::Ref<godot::InputEvent>& a_Event) {
@@ -79,11 +84,11 @@ void FirstPersonPlayer::_physics_process(float a_Delta) {
auto* Input = Input::get_singleton();
if (!is_on_floor())
set_velocity(get_velocity() - Vector3{0, GRAVITY * a_Delta, 0});
if (!m_Player->is_on_floor())
SetVelocity(GetVelocity() - Vector3{0, GRAVITY * a_Delta, 0});
if (Input->is_action_pressed("jump") && is_on_floor())
set_velocity({get_velocity().x, JUMP_VELOCITY, get_velocity().z});
if (Input->is_action_pressed("jump") && m_Player->is_on_floor())
SetVelocity({GetVelocity().x, JUMP_VELOCITY, GetVelocity().z});
m_Speed = Input->is_action_pressed("sprint") ? SPRINT_SPEED : WALK_SPEED;
@@ -92,88 +97,58 @@ void FirstPersonPlayer::_physics_process(float a_Delta) {
UpdateFOV(a_Delta);
UpdateBobbing(a_Delta);
move_and_slide();
m_Player->move_and_slide();
UpdateAnimation(a_Delta);
}
void FirstPersonPlayer::UpdateBobbing(float a_Delta) {
m_BobTime += a_Delta * get_velocity().length() * is_on_floor();
m_BobTime += a_Delta * GetVelocity().length() * m_Player->is_on_floor();
Vector3 newPos{static_cast<float>(Math::cos(m_BobTime * BOB_FREQ / 2.0) * BOB_AMP),
static_cast<float>(Math::sin(m_BobTime * BOB_FREQ) * BOB_AMP), 0};
m_Camera->set_transform({m_Camera->get_transform().basis, newPos});
// m_Camera->set_transform({m_Camera->get_transform().basis, newPos});
}
void FirstPersonPlayer::UpdateCamera(const InputEventMouseMotion& a_Event) {
m_Head->rotate_y(-a_Event.get_relative().x * SENSITIVITY);
m_Mesh->rotate_y(-a_Event.get_relative().x * SENSITIVITY);
m_Player->rotate_y(-a_Event.get_relative().x * SENSITIVITY);
m_Camera->rotate_x(-a_Event.get_relative().y * SENSITIVITY);
float rotationX = m_Camera->get_rotation().x;
rotationX = CLAMP(rotationX, Math::deg_to_rad(-80.0), Math::deg_to_rad(80.0));
m_Camera->set_rotation({rotationX, get_rotation().y, get_rotation().z});
m_Camera->set_rotation({rotationX, m_Camera->get_rotation().y, m_Camera->get_rotation().z});
}
void FirstPersonPlayer::UpdatePosition(float delta) {
auto* Input = Input::get_singleton();
Vector2 inputDirection = Input->get_vector("move_left", "move_right", "move_forwards", "move_backwards");
Vector3 direction = (m_Head->get_transform().basis.xform(Vector3(inputDirection.x, 0, inputDirection.y))).normalized();
Vector3 direction = (m_Player->get_transform().basis.xform(Vector3(inputDirection.x, 0, inputDirection.y))).normalized();
if (is_on_floor()) {
if (m_Player->is_on_floor()) {
if (!direction.is_zero_approx()) {
set_velocity({direction.x * m_Speed, get_velocity().y, direction.z * m_Speed});
SetVelocity({direction.x * m_Speed, GetVelocity().y, direction.z * m_Speed});
} else {
set_velocity({Math::lerp(static_cast<float>(get_velocity().x), static_cast<float>(direction.x * m_Speed),
static_cast<float>(delta * GROUND_FRICTION)),
get_velocity().y,
Math::lerp(static_cast<float>(get_velocity().z), static_cast<float>(direction.z * m_Speed),
SetVelocity({Math::lerp(static_cast<float>(GetVelocity().x), static_cast<float>(direction.x * m_Speed),
static_cast<float>(delta * GROUND_FRICTION)),
GetVelocity().y,
Math::lerp(static_cast<float>(GetVelocity().z), static_cast<float>(direction.z * m_Speed),
static_cast<float>(delta * GROUND_FRICTION))});
}
} else {
set_velocity({Math::lerp(static_cast<float>(get_velocity().x), static_cast<float>(direction.x * m_Speed),
static_cast<float>(delta * AIR_MOVEMENT)),
get_velocity().y,
Math::lerp(static_cast<float>(get_velocity().z), static_cast<float>(direction.z * m_Speed),
SetVelocity({Math::lerp(static_cast<float>(GetVelocity().x), static_cast<float>(direction.x * m_Speed),
static_cast<float>(delta * AIR_MOVEMENT)),
GetVelocity().y,
Math::lerp(static_cast<float>(GetVelocity().z), static_cast<float>(direction.z * m_Speed),
static_cast<float>(delta * AIR_MOVEMENT))});
}
if (!direction.is_zero_approx()) {
godot::Vector3 newRotation = m_Mesh->get_rotation();
newRotation.y = godot::UtilityFunctions::lerp_angle(
newRotation.y, godot::UtilityFunctions::atan2(get_velocity().x, get_velocity().z), LerpValue);
m_Mesh->set_rotation(newRotation);
}
}
void FirstPersonPlayer::UpdateFOV(float a_Delta) {
float velocityClamped = Math::clamp(get_velocity().length(), MIN_FOV_VELOCITY, MAX_FOV_VELOCITY);
float velocityClamped = Math::clamp(GetVelocity().length(), MIN_FOV_VELOCITY, MAX_FOV_VELOCITY);
float targetFOV = BASE_FOV + FOV_CHANGE * velocityClamped;
m_Camera->set_fov(Math::lerp(static_cast<float>(m_Camera->get_fov()), targetFOV, a_Delta * FOV_TRANSITION));
}
void FirstPersonPlayer::UpdateAnimation(float delta) {
if (is_on_floor()) {
m_AnimationTree->set("parameters/ground_air_transition/transition_request", "grounded");
if (get_velocity().length() > 0.2f) {
if (m_Speed == SPRINT_SPEED) {
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
UtilityFunctions::lerp(m_AnimationTree->get("parameters/iwr_blend/blend_amount"), 1.0, delta * AnimationBlend));
} else {
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
UtilityFunctions::lerp(m_AnimationTree->get("parameters/iwr_blend/blend_amount"), 0.0, delta * AnimationBlend));
}
} else {
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
UtilityFunctions::lerp(m_AnimationTree->get("parameters/iwr_blend/blend_amount"), -1.0, delta * AnimationBlend));
}
} else {
m_AnimationTree->set("parameters/ground_air_transition/transition_request", "air");
}
}
} // namespace blitz

View File

@@ -20,63 +20,92 @@ using namespace godot;
void Player::_bind_methods() {}
Player::Player() : m_PeerId(0) {
// we set the player to an invalid position
set_position({-99999, -999999, -999999});
}
Player::Player() : m_PeerId(0) {}
Player::~Player() {}
void Player::_ready() {
godot::InputMap::get_singleton()->load_from_project_settings();
m_Mesh = Object::cast_to<godot::Node3D>(find_child("Mesh"));
m_AnimationTree = Object::cast_to<godot::AnimationTree>(find_child("AnimationTree"));
DEV_ASSERT(m_Mesh);
DEV_ASSERT(m_AnimationTree);
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
animate(0);
m_Player = get_node<CharacterBody3D>("Player");
DEV_ASSERT(m_Player);
// we set the player to an invalid position
m_Player->set_position({-99999, -999999, -999999});
m_Head = get_node<Node3D>("Player/Head");
DEV_ASSERT(m_Head);
m_Mesh = get_node<Node3D>("Player/Armature");
DEV_ASSERT(m_Mesh);
m_AnimationTree = get_node<AnimationTree>("Player/AnimationTree");
DEV_ASSERT(m_AnimationTree);
}
void Player::_physics_process(float delta) {
if (godot::Engine::get_singleton()->is_editor_hint())
return;
move_and_slide();
animate(delta);
m_Player->move_and_slide();
UpdateAnimation(delta);
}
void Player::animate(float delta) {
if (is_on_floor()) {
m_AnimationTree->set("parameters/ground_air_transition/transition_request", "grounded");
Vector3 Player::GetPosition() const {
return m_Player->get_position();
}
float speed = get_velocity().length();
void Player::SetPosition(const Vector3& a_Position) {
m_Player->set_position(a_Position);
}
if (speed > 0.2f) {
if (speed >= RunSpeed) {
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
godot::UtilityFunctions::lerp(
m_AnimationTree->get("parameters/iwr_blend/blend_amount"), 1.0, delta * AnimationBlend));
} else {
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
godot::UtilityFunctions::lerp(
m_AnimationTree->get("parameters/iwr_blend/blend_amount"), 0.0, delta * AnimationBlend));
}
} else {
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
godot::UtilityFunctions::lerp(
m_AnimationTree->get("parameters/iwr_blend/blend_amount"), -1.0, delta * AnimationBlend));
}
Vector3 Player::GetVelocity() const {
return m_Player->get_velocity();
}
void Player::SetVelocity(const Vector3& a_Velocity) {
m_Player->set_velocity(a_Velocity);
}
void Player::UpdateAnimation(float a_Delta) {
Vector3 velocity = m_Player->get_velocity();
float angle = m_Player->get_rotation().y;
Vector3 direction = velocity.rotated({0, 1, 0}, -angle);
if (direction.length() < 1.0f) {
direction.zero();
} else {
m_AnimationTree->set("parameters/ground_air_transition/transition_request", "air");
direction.normalize();
}
Vector2 inputDirection = Input::get_singleton()->get_vector("move_left", "move_right", "move_forwards", "move_backwards");
BlendAnimation("parameters/Movement/Side/blend_amount", direction.x, a_Delta);
BlendAnimation("parameters/Movement/Straight/blend_amount", direction.z, a_Delta);
float ratio = 0.5f - (UtilityFunctions::absf(direction.z) - UtilityFunctions::absf(direction.x)) * 0.5f;
BlendAnimation("parameters/Movement/Walking/blend_amount", ratio, a_Delta);
m_AnimationTree->set("parameters/conditions/jump", !m_Player->is_on_floor() && m_Player->get_velocity().y > 0.0f);
m_AnimationTree->set("parameters/conditions/is_on_floor", m_Player->is_on_floor());
}
void Player::BlendAnimation(const godot::String& a_AnimationName, float a_Goal, float a_Delta) {
m_AnimationTree->set(
a_AnimationName, UtilityFunctions::lerp(m_AnimationTree->get(a_AnimationName), a_Goal, a_Delta * AnimationBlend));
}
void Player::SetModelVisible(bool visible) {}
Vector3 Player::GetCameraRotation() const {
return m_Mesh->get_rotation();
return m_Player->get_rotation();
}
void Player::SetCameraRotation(const Vector3& a_Rotation) {
m_Mesh->set_rotation(a_Rotation);
m_Player->set_rotation(a_Rotation);
}
} // namespace blitz

View File

@@ -33,7 +33,7 @@ void ServerWorld::SyncPlayersPos() {
Player* player = Object::cast_to<Player>(m_Players->get_child(i));
DEV_ASSERT(player);
m_NetworkInterface->BroadcastPacket(protocol::packets::PlayerPositionAndRotation(
{player->GetId(), player->get_position(), player->GetCameraRotation(), player->get_velocity()}));
{player->GetId(), player->GetPosition(), player->GetCameraRotation(), player->GetVelocity()}));
}
}
@@ -46,9 +46,9 @@ void ServerWorld::HandlePacket(const protocol::packets::PlayerPositionAndRotatio
if (!player)
return;
if ((data.m_Position - player->get_position()).length() > 10) {
if ((data.m_Position - player->GetPosition()).length() > 10) {
UtilityFunctions::print(
"Player ", data.m_Player, " moved too fast ! (from ", player->get_position(), " to ", data.m_Position, ")");
"Player ", data.m_Player, " moved too fast ! (from ", player->GetPosition(), " to ", data.m_Position, ")");
return;
}
@@ -58,7 +58,7 @@ void ServerWorld::HandlePacket(const protocol::packets::PlayerPositionAndRotatio
void ServerWorld::AddPlayer(PlayerID a_PlayerId, godot::String a_PlayerName) {
World::AddPlayer(a_PlayerId, a_PlayerName);
Player* player = GetPlayerById(a_PlayerId);
player->set_position({0, 0, 0});
player->SetPosition({0, 0, 0});
}
} // namespace blitz