generated from Persson-dev/Godot-Xmake
remrem
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -6,7 +6,7 @@
|
|||||||
namespace blitz {
|
namespace blitz {
|
||||||
|
|
||||||
class FirstPersonPlayer : public Player {
|
class FirstPersonPlayer : public Player {
|
||||||
GDCLASS(FirstPersonPlayer, godot::CharacterBody3D)
|
GDCLASS(FirstPersonPlayer, godot::Node)
|
||||||
protected:
|
protected:
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
|
||||||
@@ -17,11 +17,10 @@ class FirstPersonPlayer : public Player {
|
|||||||
// Godot overrides
|
// Godot overrides
|
||||||
void _unhandled_input(const godot::Ref<godot::InputEvent>&);
|
void _unhandled_input(const godot::Ref<godot::InputEvent>&);
|
||||||
void _physics_process(float delta) override;
|
void _physics_process(float delta) override;
|
||||||
void _ready() override;
|
void _ready();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
godot::Camera3D* m_Camera;
|
godot::Camera3D* m_Camera;
|
||||||
godot::Node3D* m_Head;
|
|
||||||
float m_BobTime;
|
float m_BobTime;
|
||||||
float m_Speed;
|
float m_Speed;
|
||||||
|
|
||||||
@@ -29,8 +28,6 @@ class FirstPersonPlayer : public Player {
|
|||||||
void UpdateFOV(float delta);
|
void UpdateFOV(float delta);
|
||||||
void UpdateCamera(const godot::InputEventMouseMotion&);
|
void UpdateCamera(const godot::InputEventMouseMotion&);
|
||||||
void UpdatePosition(float delta);
|
void UpdatePosition(float delta);
|
||||||
|
|
||||||
void UpdateAnimation(float delta);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace blitz
|
} // namespace blitz
|
||||||
@@ -9,9 +9,9 @@ namespace blitz {
|
|||||||
|
|
||||||
class World;
|
class World;
|
||||||
|
|
||||||
class Player : public godot::CharacterBody3D {
|
class Player : public godot::Node {
|
||||||
|
|
||||||
GDCLASS(Player, godot::CharacterBody3D);
|
GDCLASS(Player, godot::Node);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
@@ -22,7 +22,13 @@ class Player : public godot::CharacterBody3D {
|
|||||||
|
|
||||||
void _ready() override;
|
void _ready() override;
|
||||||
virtual void _physics_process(float delta);
|
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;
|
godot::Vector3 GetCameraRotation() const;
|
||||||
void SetCameraRotation(const godot::Vector3& a_Rotation);
|
void SetCameraRotation(const godot::Vector3& a_Rotation);
|
||||||
@@ -33,11 +39,17 @@ class Player : public godot::CharacterBody3D {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
godot::Node3D* m_Mesh;
|
godot::Node3D* m_Mesh;
|
||||||
|
godot::Node3D* m_Head;
|
||||||
godot::AnimationTree* m_AnimationTree;
|
godot::AnimationTree* m_AnimationTree;
|
||||||
|
godot::CharacterBody3D* m_Player;
|
||||||
|
|
||||||
godot::Vector3 m_SnapVector;
|
godot::Vector3 m_SnapVector;
|
||||||
PeerID m_PeerId;
|
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;
|
friend class World;
|
||||||
};
|
};
|
||||||
} // namespace blitz
|
} // namespace blitz
|
||||||
|
|||||||
@@ -14,8 +14,7 @@ using namespace godot;
|
|||||||
|
|
||||||
namespace blitz {
|
namespace blitz {
|
||||||
|
|
||||||
static const char FirstPersonPlayerScenePath[] = "res://Scenes/Characters/first_person_player.tscn";
|
static const char PlayerScenePath[] = "res://Scenes/Characters/remy.tscn";
|
||||||
static const char PlayerScenePath[] = "res://Scenes/Characters/player.tscn";
|
|
||||||
|
|
||||||
void World::_bind_methods() {}
|
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) {
|
void World::AddPlayer(PlayerID a_PlayerId, String a_PlayerName) {
|
||||||
UtilityFunctions::print("New Player with id : ", a_PlayerId, " and name ", a_PlayerName);
|
UtilityFunctions::print("New Player with id : ", a_PlayerId, " and name ", a_PlayerName);
|
||||||
if (a_PlayerId == get_multiplayer()->get_unique_id()) {
|
if (a_PlayerId == get_multiplayer()->get_unique_id()) {
|
||||||
Ref<PackedScene> serverScene = ResourceLoader::get_singleton()->load(FirstPersonPlayerScenePath);
|
Ref<PackedScene> serverScene = ResourceLoader::get_singleton()->load(PlayerScenePath);
|
||||||
FirstPersonPlayer* player = Object::cast_to<FirstPersonPlayer>(serverScene->instantiate());
|
|
||||||
|
Node* playerContent = serverScene->instantiate();
|
||||||
|
|
||||||
|
FirstPersonPlayer* player = memnew(FirstPersonPlayer);
|
||||||
player->set_name(UtilityFunctions::var_to_str(a_PlayerId));
|
player->set_name(UtilityFunctions::var_to_str(a_PlayerId));
|
||||||
player->m_PeerId = a_PlayerId;
|
player->m_PeerId = a_PlayerId;
|
||||||
|
player->add_child(playerContent);
|
||||||
|
|
||||||
m_Players->add_child(player);
|
m_Players->add_child(player);
|
||||||
} else {
|
} else {
|
||||||
Ref<PackedScene> serverScene = ResourceLoader::get_singleton()->load(PlayerScenePath);
|
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->set_name(UtilityFunctions::var_to_str(a_PlayerId));
|
||||||
player->m_PeerId = a_PlayerId;
|
player->m_PeerId = a_PlayerId;
|
||||||
|
player->add_child(playerContent);
|
||||||
|
|
||||||
m_Players->add_child(player);
|
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) {
|
PlayerID a_PlayerId, const Vector3& a_Position, const Vector3& a_Rotation, const godot::Vector3& a_Velocity) {
|
||||||
Player* player = GetPlayerById(a_PlayerId);
|
Player* player = GetPlayerById(a_PlayerId);
|
||||||
if (player) {
|
if (player) {
|
||||||
player->set_position(a_Position);
|
player->SetPosition(a_Position);
|
||||||
player->SetCameraRotation(a_Rotation);
|
player->SetCameraRotation(a_Rotation);
|
||||||
player->set_velocity(a_Velocity);
|
player->SetVelocity(a_Velocity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ void ClientWorld::UpdatePlayerPos() {
|
|||||||
Player* player = GetPlayerById(get_multiplayer()->get_unique_id());
|
Player* player = GetPlayerById(get_multiplayer()->get_unique_id());
|
||||||
if (player) {
|
if (player) {
|
||||||
m_NetworkInterface->BroadcastPacket(protocol::packets::PlayerPositionAndRotation(
|
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;
|
const auto& data = a_PlayerPos.m_Data;
|
||||||
if (data.m_Player == get_multiplayer()->get_unique_id()) {
|
if (data.m_Player == get_multiplayer()->get_unique_id()) {
|
||||||
Player* player = GetPlayerById(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);
|
SetPlayerPositionAndRotation(data.m_Player, data.m_Position, data.m_Rotation, data.m_Velocity);
|
||||||
godot::UtilityFunctions::print("Teleported to : ", data.m_Position);
|
godot::UtilityFunctions::print("Teleported to : ", data.m_Position);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,17 +44,22 @@ FirstPersonPlayer::FirstPersonPlayer() : Player(), m_BobTime(0) {}
|
|||||||
FirstPersonPlayer::~FirstPersonPlayer() {}
|
FirstPersonPlayer::~FirstPersonPlayer() {}
|
||||||
|
|
||||||
void FirstPersonPlayer::_ready() {
|
void FirstPersonPlayer::_ready() {
|
||||||
|
Player::_ready();
|
||||||
|
|
||||||
InputMap::get_singleton()->load_from_project_settings();
|
InputMap::get_singleton()->load_from_project_settings();
|
||||||
if (!Engine::get_singleton()->is_editor_hint()) {
|
if (!Engine::get_singleton()->is_editor_hint()) {
|
||||||
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
|
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});
|
m_Camera = memnew(Camera3D);
|
||||||
set_velocity({0, 0, 0});
|
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) {
|
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();
|
auto* Input = Input::get_singleton();
|
||||||
|
|
||||||
if (!is_on_floor())
|
if (!m_Player->is_on_floor())
|
||||||
set_velocity(get_velocity() - Vector3{0, GRAVITY * a_Delta, 0});
|
SetVelocity(GetVelocity() - Vector3{0, GRAVITY * a_Delta, 0});
|
||||||
|
|
||||||
if (Input->is_action_pressed("jump") && is_on_floor())
|
if (Input->is_action_pressed("jump") && m_Player->is_on_floor())
|
||||||
set_velocity({get_velocity().x, JUMP_VELOCITY, get_velocity().z});
|
SetVelocity({GetVelocity().x, JUMP_VELOCITY, GetVelocity().z});
|
||||||
|
|
||||||
m_Speed = Input->is_action_pressed("sprint") ? SPRINT_SPEED : WALK_SPEED;
|
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);
|
UpdateFOV(a_Delta);
|
||||||
UpdateBobbing(a_Delta);
|
UpdateBobbing(a_Delta);
|
||||||
|
|
||||||
move_and_slide();
|
m_Player->move_and_slide();
|
||||||
|
|
||||||
UpdateAnimation(a_Delta);
|
UpdateAnimation(a_Delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FirstPersonPlayer::UpdateBobbing(float 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),
|
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};
|
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) {
|
void FirstPersonPlayer::UpdateCamera(const InputEventMouseMotion& a_Event) {
|
||||||
m_Head->rotate_y(-a_Event.get_relative().x * SENSITIVITY);
|
m_Player->rotate_y(-a_Event.get_relative().x * SENSITIVITY);
|
||||||
m_Mesh->rotate_y(-a_Event.get_relative().x * SENSITIVITY);
|
|
||||||
m_Camera->rotate_x(-a_Event.get_relative().y * SENSITIVITY);
|
m_Camera->rotate_x(-a_Event.get_relative().y * SENSITIVITY);
|
||||||
|
|
||||||
float rotationX = m_Camera->get_rotation().x;
|
float rotationX = m_Camera->get_rotation().x;
|
||||||
rotationX = CLAMP(rotationX, Math::deg_to_rad(-80.0), Math::deg_to_rad(80.0));
|
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) {
|
void FirstPersonPlayer::UpdatePosition(float delta) {
|
||||||
auto* Input = Input::get_singleton();
|
auto* Input = Input::get_singleton();
|
||||||
|
|
||||||
Vector2 inputDirection = Input->get_vector("move_left", "move_right", "move_forwards", "move_backwards");
|
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()) {
|
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 {
|
} else {
|
||||||
set_velocity({Math::lerp(static_cast<float>(get_velocity().x), static_cast<float>(direction.x * m_Speed),
|
SetVelocity({Math::lerp(static_cast<float>(GetVelocity().x), static_cast<float>(direction.x * m_Speed),
|
||||||
static_cast<float>(delta * GROUND_FRICTION)),
|
static_cast<float>(delta * GROUND_FRICTION)),
|
||||||
get_velocity().y,
|
GetVelocity().y,
|
||||||
Math::lerp(static_cast<float>(get_velocity().z), static_cast<float>(direction.z * m_Speed),
|
Math::lerp(static_cast<float>(GetVelocity().z), static_cast<float>(direction.z * m_Speed),
|
||||||
static_cast<float>(delta * GROUND_FRICTION))});
|
static_cast<float>(delta * GROUND_FRICTION))});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
set_velocity({Math::lerp(static_cast<float>(get_velocity().x), static_cast<float>(direction.x * m_Speed),
|
SetVelocity({Math::lerp(static_cast<float>(GetVelocity().x), static_cast<float>(direction.x * m_Speed),
|
||||||
static_cast<float>(delta * AIR_MOVEMENT)),
|
static_cast<float>(delta * AIR_MOVEMENT)),
|
||||||
get_velocity().y,
|
GetVelocity().y,
|
||||||
Math::lerp(static_cast<float>(get_velocity().z), static_cast<float>(direction.z * m_Speed),
|
Math::lerp(static_cast<float>(GetVelocity().z), static_cast<float>(direction.z * m_Speed),
|
||||||
static_cast<float>(delta * AIR_MOVEMENT))});
|
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) {
|
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;
|
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));
|
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
|
} // namespace blitz
|
||||||
@@ -20,63 +20,92 @@ using namespace godot;
|
|||||||
|
|
||||||
void Player::_bind_methods() {}
|
void Player::_bind_methods() {}
|
||||||
|
|
||||||
Player::Player() : m_PeerId(0) {
|
Player::Player() : m_PeerId(0) {}
|
||||||
// we set the player to an invalid position
|
|
||||||
set_position({-99999, -999999, -999999});
|
|
||||||
}
|
|
||||||
|
|
||||||
Player::~Player() {}
|
Player::~Player() {}
|
||||||
|
|
||||||
void Player::_ready() {
|
void Player::_ready() {
|
||||||
godot::InputMap::get_singleton()->load_from_project_settings();
|
if (Engine::get_singleton()->is_editor_hint()) {
|
||||||
m_Mesh = Object::cast_to<godot::Node3D>(find_child("Mesh"));
|
return;
|
||||||
m_AnimationTree = Object::cast_to<godot::AnimationTree>(find_child("AnimationTree"));
|
}
|
||||||
DEV_ASSERT(m_Mesh);
|
|
||||||
DEV_ASSERT(m_AnimationTree);
|
|
||||||
|
|
||||||
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) {
|
void Player::_physics_process(float delta) {
|
||||||
if (godot::Engine::get_singleton()->is_editor_hint())
|
if (godot::Engine::get_singleton()->is_editor_hint())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
move_and_slide();
|
m_Player->move_and_slide();
|
||||||
animate(delta);
|
UpdateAnimation(delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::animate(float delta) {
|
Vector3 Player::GetPosition() const {
|
||||||
if (is_on_floor()) {
|
return m_Player->get_position();
|
||||||
m_AnimationTree->set("parameters/ground_air_transition/transition_request", "grounded");
|
}
|
||||||
|
|
||||||
float speed = get_velocity().length();
|
void Player::SetPosition(const Vector3& a_Position) {
|
||||||
|
m_Player->set_position(a_Position);
|
||||||
|
}
|
||||||
|
|
||||||
if (speed > 0.2f) {
|
Vector3 Player::GetVelocity() const {
|
||||||
if (speed >= RunSpeed) {
|
return m_Player->get_velocity();
|
||||||
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
|
}
|
||||||
godot::UtilityFunctions::lerp(
|
|
||||||
m_AnimationTree->get("parameters/iwr_blend/blend_amount"), 1.0, delta * AnimationBlend));
|
void Player::SetVelocity(const Vector3& a_Velocity) {
|
||||||
} else {
|
m_Player->set_velocity(a_Velocity);
|
||||||
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
|
}
|
||||||
godot::UtilityFunctions::lerp(
|
|
||||||
m_AnimationTree->get("parameters/iwr_blend/blend_amount"), 0.0, delta * AnimationBlend));
|
void Player::UpdateAnimation(float a_Delta) {
|
||||||
}
|
Vector3 velocity = m_Player->get_velocity();
|
||||||
} else {
|
float angle = m_Player->get_rotation().y;
|
||||||
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
|
|
||||||
godot::UtilityFunctions::lerp(
|
Vector3 direction = velocity.rotated({0, 1, 0}, -angle);
|
||||||
m_AnimationTree->get("parameters/iwr_blend/blend_amount"), -1.0, delta * AnimationBlend));
|
if (direction.length() < 1.0f) {
|
||||||
}
|
direction.zero();
|
||||||
} else {
|
} 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 {
|
Vector3 Player::GetCameraRotation() const {
|
||||||
return m_Mesh->get_rotation();
|
return m_Player->get_rotation();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::SetCameraRotation(const Vector3& a_Rotation) {
|
void Player::SetCameraRotation(const Vector3& a_Rotation) {
|
||||||
m_Mesh->set_rotation(a_Rotation);
|
m_Player->set_rotation(a_Rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace blitz
|
} // namespace blitz
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ void ServerWorld::SyncPlayersPos() {
|
|||||||
Player* player = Object::cast_to<Player>(m_Players->get_child(i));
|
Player* player = Object::cast_to<Player>(m_Players->get_child(i));
|
||||||
DEV_ASSERT(player);
|
DEV_ASSERT(player);
|
||||||
m_NetworkInterface->BroadcastPacket(protocol::packets::PlayerPositionAndRotation(
|
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)
|
if (!player)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if ((data.m_Position - player->get_position()).length() > 10) {
|
if ((data.m_Position - player->GetPosition()).length() > 10) {
|
||||||
UtilityFunctions::print(
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ void ServerWorld::HandlePacket(const protocol::packets::PlayerPositionAndRotatio
|
|||||||
void ServerWorld::AddPlayer(PlayerID a_PlayerId, godot::String a_PlayerName) {
|
void ServerWorld::AddPlayer(PlayerID a_PlayerId, godot::String a_PlayerName) {
|
||||||
World::AddPlayer(a_PlayerId, a_PlayerName);
|
World::AddPlayer(a_PlayerId, a_PlayerName);
|
||||||
Player* player = GetPlayerById(a_PlayerId);
|
Player* player = GetPlayerById(a_PlayerId);
|
||||||
player->set_position({0, 0, 0});
|
player->SetPosition({0, 0, 0});
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace blitz
|
} // namespace blitz
|
||||||
Reference in New Issue
Block a user