third persson prototype

This commit is contained in:
2024-08-12 11:45:42 +02:00
parent ba66b7b3b4
commit d9aba1924f
25 changed files with 24091 additions and 39 deletions

111
src/Player.cpp Normal file
View File

@@ -0,0 +1,111 @@
#include "Player.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/input.hpp>
#include <godot_cpp/classes/input_map.hpp>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
static const float WalkSpeed = 2.0;
static const float RunSpeed = 5.0;
static const float JumpStrength = 15.0;
static const float Gravity = 50.0;
static const float LerpValue = 0.15;
static const float AnimationBlend = 7.0;
namespace blitz {
void Player::_bind_methods() {}
Player::Player() {}
Player::~Player() {}
void Player::_ready() {
godot::InputMap::get_singleton()->load_from_project_settings();
m_PlayerMesh = Object::cast_to<godot::Node3D>(get_child(0));
m_SpringArmPivot = Object::cast_to<godot::Node3D>(get_child(2));
m_AnimationTree = Object::cast_to<godot::AnimationTree>(get_child(4));
DEV_ASSERT(m_PlayerMesh);
DEV_ASSERT(m_SpringArmPivot);
DEV_ASSERT(m_AnimationTree);
apply_floor_snap();
animate(0);
}
void Player::_physics_process(float delta) {
if (godot::Engine::get_singleton()->is_editor_hint())
return;
auto* Input = godot::Input::get_singleton();
godot::Vector3 move_direction{0, 0, 0};
move_direction.x = Input->get_action_strength("move_right") - Input->get_action_strength("move_left");
move_direction.z = Input->get_action_strength("move_backwards") - Input->get_action_strength("move_forwards");
move_direction = move_direction.rotated({0, 1, 0}, m_SpringArmPivot->get_rotation().y);
godot::Vector3 newVelocity = get_velocity();
newVelocity.y -= Gravity * delta;
set_velocity(newVelocity);
if (Input->is_action_pressed("run"))
m_Speed = RunSpeed;
else
m_Speed = WalkSpeed;
newVelocity = get_velocity();
newVelocity.x = move_direction.x * m_Speed;
newVelocity.z = move_direction.z * m_Speed;
set_velocity(newVelocity);
if (move_direction != godot::Vector3{0, 0, 0}) {
godot::Vector3 newRotation = m_PlayerMesh->get_rotation();
newRotation.y = godot::UtilityFunctions::lerp_angle(
newRotation.y, godot::UtilityFunctions::atan2(get_velocity().x, get_velocity().z), LerpValue);
m_PlayerMesh->set_rotation(newRotation);
}
bool justLanded = is_on_floor() && m_SnapVector == godot::Vector3{0, 0, 0};
bool isJumping = is_on_floor() && Input->is_action_just_pressed("jump");
if (isJumping) {
newVelocity = get_velocity();
newVelocity.y = JumpStrength;
set_velocity(newVelocity);
m_SnapVector.zero();
} else if (justLanded) {
m_SnapVector = {0, -1, 0};
}
apply_floor_snap();
move_and_slide();
animate(delta);
}
void Player::animate(float delta) {
if (is_on_floor()) {
m_AnimationTree->set("parameters/ground_air_transition/transition_request", "grounded");
if (get_velocity().length() > 0) {
if (m_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));
}
} else {
m_AnimationTree->set("parameters/ground_air_transition/transition_request", "air");
}
}
} // namespace blitz

37
src/Player.h Normal file
View File

@@ -0,0 +1,37 @@
#pragma once
#include <godot_cpp/classes/animation_tree.hpp>
#include <godot_cpp/classes/character_body3d.hpp>
#include <godot_cpp/classes/node3d.hpp>
namespace blitz {
class Player : public godot::CharacterBody3D {
GDCLASS(Player, godot::CharacterBody3D);
protected:
static void _bind_methods();
public:
Player();
~Player();
void _ready();
void _physics_process(float delta);
void animate(float delta);
private:
godot::Node3D* m_PlayerMesh;
godot::Node3D* m_SpringArmPivot;
godot::AnimationTree* m_AnimationTree;
godot::Vector3 m_SnapVector;
float m_Speed;
/*
@onready var player_mesh : Node3D = $Mesh
@onready var spring_arm_pivot : Node3D = $SpringArmPivot
@onready var animator : AnimationTree = $AnimationTree
*/
};
} // namespace blitz

65
src/SpringArmPivot.cpp Normal file
View File

@@ -0,0 +1,65 @@
#include "SpringArmPivot.h"
#include "Player.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/input.hpp>
#include <godot_cpp/classes/input_event_mouse_motion.hpp>
#include <godot_cpp/classes/spring_arm3d.hpp>
#include <godot_cpp/core/error_macros.hpp>
#include <godot_cpp/core/math.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
static const float NormalFov = 75.0;
static const float RunFov = 90.0;
static const float CameraBlend = 0.05;
namespace blitz {
void SpringArmPivot::_bind_methods() {
godot::ClassDB::bind_method(godot::D_METHOD("get_dynfov"), &SpringArmPivot::IsFovDynamic);
godot::ClassDB::bind_method(godot::D_METHOD("set_dynfov", "dynamic_fov"), &SpringArmPivot::SetDynamicFov);
ADD_PROPERTY(godot::PropertyInfo(godot::Variant::BOOL, "dynamic_fov"), "set_dynfov", "get_dynfov");
}
SpringArmPivot::SpringArmPivot() {}
SpringArmPivot::~SpringArmPivot() {}
void SpringArmPivot::_ready() {
m_SpringArm = Object::cast_to<godot::SpringArm3D>(get_child(0));
m_Camera = Object::cast_to<godot::Camera3D>(m_SpringArm->get_child(0));
DEV_ASSERT(m_SpringArm);
DEV_ASSERT(m_Camera);
if (!godot::Engine::get_singleton()->is_editor_hint()) {
godot::Input::get_singleton()->set_mouse_mode(godot::Input::MOUSE_MODE_CAPTURED);
}
}
void SpringArmPivot::_unhandled_input(const godot::Ref<godot::InputEvent>& p_event) {
auto* event = Object::cast_to<godot::InputEventMouseMotion>(p_event.ptr());
if (event) {
rotate_y(-event->get_relative().x * 0.005);
m_SpringArm->rotate_x(-event->get_relative().y * 0.005);
godot::Vector3 rotationClamped = m_SpringArm->get_rotation();
rotationClamped.x = godot::UtilityFunctions::clamp(rotationClamped.x, -Math_PI / 4, Math_PI / 4);
m_SpringArm->set_rotation(rotationClamped);
}
}
void SpringArmPivot::_physics_process(float delta) {
if (m_DynamicFOV) {
auto* parent = Object::cast_to<Player>(get_owner());
if (parent->is_on_floor()) {
if (godot::Input::get_singleton()->is_action_pressed("run")) {
m_Camera->set_fov(godot::UtilityFunctions::lerp(m_Camera->get_fov(), RunFov, CameraBlend));
} else {
m_Camera->set_fov(godot::UtilityFunctions::lerp(m_Camera->get_fov(), NormalFov, CameraBlend));
}
} else {
m_Camera->set_fov(godot::UtilityFunctions::lerp(m_Camera->get_fov(), NormalFov, CameraBlend));
}
}
}
} // namespace blitz

38
src/SpringArmPivot.h Normal file
View File

@@ -0,0 +1,38 @@
#pragma once
#include <godot_cpp/classes/input_event.hpp>
#include <godot_cpp/classes/node3d.hpp>
#include <godot_cpp/classes/spring_arm3d.hpp>
#include <godot_cpp/classes/camera3d.hpp>
namespace blitz {
class SpringArmPivot : public godot::Node3D {
GDCLASS(SpringArmPivot, godot::Node3D);
protected:
static void _bind_methods();
public:
SpringArmPivot();
~SpringArmPivot();
void _ready();
void _unhandled_input(const godot::Ref<godot::InputEvent>& p_event);
void _physics_process(float delta);
void SetDynamicFov(bool fov) {
m_DynamicFOV = fov;
}
bool IsFovDynamic() const {
return m_DynamicFOV;
}
private:
godot::SpringArm3D* m_SpringArm;
godot::Camera3D* m_Camera;
bool m_DynamicFOV = false;
};
} // namespace blitz

View File

@@ -1,6 +1,7 @@
#include "register_types.h"
#include "gdexample.h"
#include "Player.h"
#include "SpringArmPivot.h"
#include <gdextension_interface.h>
#include <godot_cpp/core/defs.hpp>
@@ -8,37 +9,29 @@
using namespace godot;
void initialize_example_module(ModuleInitializationLevel p_level)
{
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE)
{
return;
}
void initialize_example_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
ClassDB::register_class<GDExample>();
ClassDB::register_class<blitz::Player>();
ClassDB::register_class<blitz::SpringArmPivot>();
}
void uninitialize_example_module(ModuleInitializationLevel p_level)
{
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE)
{
return;
}
void uninitialize_example_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
}
extern "C"
{
GDExtensionBool GDE_EXPORT library_init(
GDExtensionInterfaceGetProcAddress p_get_proc,
const GDExtensionClassLibraryPtr p_library,
GDExtensionInitialization *r_initialization)
{
godot::GDExtensionBinding::InitObject init_obj(p_get_proc, p_library, r_initialization);
GDExtensionBool GDE_EXPORT library_init(GDExtensionInterfaceGetProcAddress p_get_proc, const GDExtensionClassLibraryPtr p_library,
GDExtensionInitialization* r_initialization) {
godot::GDExtensionBinding::InitObject init_obj(p_get_proc, p_library, r_initialization);
init_obj.register_initializer(initialize_example_module);
init_obj.register_terminator(uninitialize_example_module);
init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE);
init_obj.register_initializer(initialize_example_module);
init_obj.register_terminator(uninitialize_example_module);
init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE);
return init_obj.init();
}
}
return init_obj.init();
}