feat: added player body class

This commit is contained in:
Sara Gerretsen 2026-04-25 19:04:34 +02:00
parent bdcf5f81a1
commit 07d69bf55e
7 changed files with 159 additions and 1 deletions

View file

@ -0,0 +1,51 @@
#include "player_body.h"
#include "core/input/input.h"
#include "scene/main/viewport.h"
void PlayerBody::update_movement() {
Vector3 velocity{ get_velocity() };
Vector3 world_input{ world_move_input().normalized() };
velocity.x = world_input.x;
velocity.z = world_input.z;
velocity += get_gravity() * get_physics_process_delta_time();
set_velocity(velocity);
move_and_slide();
}
void PlayerBody::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_ENTER_TREE:
set_physics_process(true);
set_process_unhandled_input(true);
break;
case NOTIFICATION_PHYSICS_PROCESS:
update_movement();
return;
}
}
void PlayerBody::unhandled_input(Ref<InputEvent> const &event) {
if (event->is_action("move_left") || event->is_action("move_right")) {
this->move_input.x = Input::get_singleton()->get_axis("move_left", "move_right");
}
if (event->is_action("move_forward") || event->is_action("move_backward")) {
this->move_input.y = Input::get_singleton()->get_axis("move_backward", "move_forward");
}
}
Vector3 PlayerBody::world_move_input() const {
Basis const basis{ get_viewport()->get_camera_3d()->get_global_basis() };
Vector3 left{ basis.get_column(0) };
left.y = 0.f;
Vector3 forward{ -basis.get_column(2) };
forward.y = 0.f;
return {
left.normalized() * this->move_input.x +
forward.normalized() * this->move_input.y
};
}

View file

@ -0,0 +1,19 @@
#pragma once
#include "scene/3d/physics/character_body_3d.h"
class PlayerBody : CharacterBody3D {
GDCLASS(PlayerBody, CharacterBody3D);
static void _bind_methods() {}
void update_movement();
protected:
void _notification(int what);
void unhandled_input(Ref<InputEvent> const &event) override;
Vector3 world_move_input() const;
private:
float run_speed{ 5.f };
float run_acceleration{ 5.f };
Vector2 move_input{ 0, 0 };
};

View file

@ -1,11 +1,13 @@
#include "register_types.h"
#include "core/object/class_db.h"
#include "player_body.h"
void initialize_viscosity_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
ClassDB::register_class<PlayerBody>();
}
void uninitialize_viscosity_module(ModuleInitializationLevel p_level) {

View file

@ -18,6 +18,8 @@ dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.cte
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/uastc_level=0
compress/rdo_quality_loss=0.0
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
@ -25,6 +27,10 @@ mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/channel_remap/red=0
process/channel_remap/green=1
process/channel_remap/blue=2
process/channel_remap/alpha=3
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false

View file

@ -0,0 +1,35 @@
[gd_scene format=3 uid="uid://d38nedywn8cfc"]
[ext_resource type="PackedScene" uid="uid://8t87iy0t0igo" path="res://objects/player.tscn" id="1_ctiq7"]
[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_ctiq7"]
sky_horizon_color = Color(0.66224277, 0.6717428, 0.6867428, 1)
ground_horizon_color = Color(0.66224277, 0.6717428, 0.6867428, 1)
[sub_resource type="Sky" id="Sky_nlfnm"]
sky_material = SubResource("ProceduralSkyMaterial_ctiq7")
[sub_resource type="Environment" id="Environment_x4mbn"]
background_mode = 2
sky = SubResource("Sky_nlfnm")
tonemap_mode = 2
glow_enabled = true
[node name="TestRoom" type="Node3D" unique_id=112638301]
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="." unique_id=1908495879]
transform = Transform3D(-0.8660254, -0.43301278, 0.25, 0, 0.49999997, 0.86602545, -0.50000006, 0.75, -0.43301266, 0, 0, 0)
shadow_enabled = true
[node name="WorldEnvironment" type="WorldEnvironment" parent="." unique_id=1908998327]
environment = SubResource("Environment_x4mbn")
[node name="CSGCombiner3D" type="CSGCombiner3D" parent="." unique_id=664068670]
use_collision = true
[node name="CSGBox3D" type="CSGBox3D" parent="CSGCombiner3D" unique_id=1095456809]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.25, 0)
size = Vector3(10, 0.5, 10)
[node name="Player" parent="." unique_id=591139793 instance=ExtResource("1_ctiq7")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.029971719, 1, 2.8870416)

View file

@ -0,0 +1,17 @@
[gd_scene format=3 uid="uid://8t87iy0t0igo"]
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_bxedw"]
radius = 0.6972656
[sub_resource type="CapsuleMesh" id="CapsuleMesh_eqqp1"]
[node name="Player" type="PlayerBody" unique_id=591139793]
[node name="CollisionShape3D" type="CollisionShape3D" parent="." unique_id=1143027860]
shape = SubResource("CapsuleShape3D_bxedw")
[node name="MeshInstance3D" type="MeshInstance3D" parent="." unique_id=1058685291]
mesh = SubResource("CapsuleMesh_eqqp1")
[node name="Camera3D" type="Camera3D" parent="." unique_id=1911777954]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.3841858e-07, 0.95835394, 3.1808815)

View file

@ -8,8 +8,36 @@
config_version=5
[animation]
compatibility/default_parent_skeleton_in_mesh_instance_3d=true
[application]
config/name="viscosity"
config/features=PackedStringArray("4.4", "Forward Plus")
run/main_scene="uid://d38nedywn8cfc"
config/features=PackedStringArray("4.6", "Forward Plus")
config/icon="res://icon.svg"
[input]
move_left={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null)
]
}
move_right={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null)
]
}
move_forward={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"location":0,"echo":false,"script":null)
]
}
move_backward={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null)
]
}