From 57710b7b7e26d8c031189b1d1de5667ac130cba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Wed, 24 May 2023 00:44:16 +0300 Subject: [PATCH] add player controller --- .../shaders/outline_shader.tres | 2 +- player.gd | 50 +++++++++++++++++++ player.tscn | 21 ++++++++ project.godot | 41 +++++++++++++++ test_world.tscn | 43 ++++++++++++++++ 5 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 player.gd create mode 100644 player.tscn create mode 100644 test_world.tscn diff --git a/addons/cyclops_level_builder/shaders/outline_shader.tres b/addons/cyclops_level_builder/shaders/outline_shader.tres index 7e78ea6..c35c7fb 100644 --- a/addons/cyclops_level_builder/shaders/outline_shader.tres +++ b/addons/cyclops_level_builder/shaders/outline_shader.tres @@ -33,7 +33,7 @@ void fragment() { // LinearSceneDepth:4 { float __log_depth = textureLod(depth_tex_frg_4, SCREEN_UV, 0.0).x; - vec4 __depth_view = INV_PROJECTION_MATRIX * vec4(vec3(SCREEN_UV, __log_depth) * 2.0 - 1.0, 1.0); + vec4 __depth_view = INV_PROJECTION_MATRIX * vec4(SCREEN_UV * 2.0 - 1.0, __log_depth, 1.0); __depth_view.xyz /= __depth_view.w; n_out4p0 = -__depth_view.z; } diff --git a/player.gd b/player.gd new file mode 100644 index 0000000..62555dd --- /dev/null +++ b/player.gd @@ -0,0 +1,50 @@ +extends CharacterBody3D + +@export var speed: float = 5.0 +@export var jump_velocity = 4.5 +@export var mouse_sensitivity: float = 6.0 +@export var acceleration: float = 5.0 + +@onready var head: Node3D = %Head +@onready var camera: Camera3D = %Camera +# Get the gravity from the project settings to be synced with RigidBody nodes. +var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity") + + +func _ready() -> void: + Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) + + +func _physics_process(delta: float) -> void: + if !is_on_floor(): + velocity.y -= gravity * delta + + if Input.is_action_just_pressed("jump") and is_on_floor(): + velocity.y = jump_velocity + + var input_dir := Input.get_vector("move_left", "move_right", "move_forward", "move_back") + var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() + var temp_vel := velocity + temp_vel.y = 0 + + var target := direction + target *= speed + + temp_vel = temp_vel.lerp(target, acceleration * delta) + velocity.x = temp_vel.x + velocity.z = temp_vel.z + + move_and_slide() + + +func _input(event: InputEvent) -> void: + if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED: + head.rotation.x -= event.relative.y * mouse_sensitivity * 0.001 + head.rotation.x = clamp(head.rotation.x, -PI/2.0 + 0.05, PI/2.0 - 0.05) + + rotation.y -= event.relative.x * mouse_sensitivity * 0.001 + rotation.y = wrapf(rotation.y, 0.0, TAU) + + if event.is_action_pressed("ui_cancel"): + var cm = Input.get_mouse_mode() + Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE if cm == Input.MOUSE_MODE_CAPTURED else Input.MOUSE_MODE_CAPTURED) diff --git a/player.tscn b/player.tscn new file mode 100644 index 0000000..41b00b2 --- /dev/null +++ b/player.tscn @@ -0,0 +1,21 @@ +[gd_scene load_steps=3 format=3 uid="uid://da54yhilto0si"] + +[ext_resource type="Script" path="res://player.gd" id="1_pvx6e"] + +[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_suvdr"] + +[node name="Player" type="CharacterBody3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0) +script = ExtResource("1_pvx6e") +acceleration = 15.0 + +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] +shape = SubResource("CapsuleShape3D_suvdr") + +[node name="Head" type="Node3D" parent="."] +unique_name_in_owner = true +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0) + +[node name="Camera" type="Camera3D" parent="Head"] +unique_name_in_owner = true +current = true diff --git a/project.godot b/project.godot index 2cb3cfe..3c9ef53 100644 --- a/project.godot +++ b/project.godot @@ -13,3 +13,44 @@ config_version=5 config/name="quack" config/features=PackedStringArray("4.0", "Forward Plus") config/icon="res://icon.svg" + +[autoload] + +CyclopsAutoload="*res://addons/cyclops_level_builder/cyclops_global_scene.tscn" + +[editor_plugins] + +enabled=PackedStringArray("res://addons/cyclops_level_builder/plugin.cfg") + +[input] + +move_forward={ +"deadzone": 0.5, +"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,"echo":false,"script":null) +] +} +move_back={ +"deadzone": 0.5, +"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,"echo":false,"script":null) +] +} +move_left={ +"deadzone": 0.5, +"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,"echo":false,"script":null) +] +} +move_right={ +"deadzone": 0.5, +"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,"echo":false,"script":null) +] +} +jump={ +"deadzone": 0.5, +"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":32,"key_label":0,"unicode":32,"echo":false,"script":null) +] +} +throw_bread={ +"deadzone": 0.5, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(175, 18),"global_position":Vector2(864, 364),"factor":1.0,"button_index":1,"pressed":true,"double_click":false,"script":null) +] +} diff --git a/test_world.tscn b/test_world.tscn new file mode 100644 index 0000000..8f79a8d --- /dev/null +++ b/test_world.tscn @@ -0,0 +1,43 @@ +[gd_scene load_steps=7 format=3 uid="uid://cr3xqj20qcpaj"] + +[ext_resource type="PackedScene" uid="uid://da54yhilto0si" path="res://player.tscn" id="1_qifre"] + +[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_s4rhg"] +sky_horizon_color = Color(0.64625, 0.65575, 0.67075, 1) +ground_horizon_color = Color(0.64625, 0.65575, 0.67075, 1) + +[sub_resource type="Sky" id="Sky_4d0mh"] +sky_material = SubResource("ProceduralSkyMaterial_s4rhg") + +[sub_resource type="Environment" id="Environment_plruy"] +background_mode = 2 +sky = SubResource("Sky_4d0mh") +tonemap_mode = 2 +glow_enabled = true + +[sub_resource type="BoxMesh" id="BoxMesh_58wgr"] +size = Vector3(30, 1, 30) + +[sub_resource type="BoxShape3D" id="BoxShape3D_rt6rr"] +size = Vector3(30, 1, 30) + +[node name="TestWorld" type="Node3D"] + +[node name="WorldEnvironment" type="WorldEnvironment" parent="."] +environment = SubResource("Environment_plruy") + +[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] +transform = Transform3D(-0.866025, -0.433013, 0.25, 0, 0.5, 0.866025, -0.5, 0.75, -0.433013, 0, 0, 0) +shadow_enabled = true + +[node name="MeshInstance3D" type="MeshInstance3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.5, 0) +mesh = SubResource("BoxMesh_58wgr") + +[node name="StaticBody3D" type="StaticBody3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.5, 0) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D"] +shape = SubResource("BoxShape3D_rt6rr") + +[node name="Player" parent="." instance=ExtResource("1_qifre")]