Add player and player controls
This commit is contained in:
parent
71ae05823c
commit
d9a29f3070
38
src/scenes/levels/level.tscn
Normal file
38
src/scenes/levels/level.tscn
Normal file
@ -0,0 +1,38 @@
|
||||
[gd_scene load_steps=5 format=3 uid="uid://dw0lbkh31rofd"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://nsv4lbw7j8mi" path="res://src/scenes/player/player.tscn" id="1_crbv0"]
|
||||
|
||||
[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_amufq"]
|
||||
sky_horizon_color = Color(0.662243, 0.671743, 0.686743, 1)
|
||||
ground_horizon_color = Color(0.662243, 0.671743, 0.686743, 1)
|
||||
|
||||
[sub_resource type="Sky" id="Sky_crbv0"]
|
||||
sky_material = SubResource("ProceduralSkyMaterial_amufq")
|
||||
|
||||
[sub_resource type="Environment" id="Environment_d2tjv"]
|
||||
background_mode = 2
|
||||
sky = SubResource("Sky_crbv0")
|
||||
tonemap_mode = 2
|
||||
glow_enabled = true
|
||||
|
||||
[node name="Level" type="Node3D"]
|
||||
|
||||
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
|
||||
environment = SubResource("Environment_d2tjv")
|
||||
|
||||
[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="CSGBox3D" type="CSGBox3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.5, 0)
|
||||
use_collision = true
|
||||
size = Vector3(50, 1, 50)
|
||||
|
||||
[node name="Player" parent="." instance=ExtResource("1_crbv0")]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
|
||||
|
||||
[node name="CSGBox3D2" type="CSGBox3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -3.04248, 1.91993, 5.20496)
|
||||
use_collision = true
|
||||
size = Vector3(7.08496, 4.78943, 7.72217)
|
||||
32
src/scenes/player/player.tscn
Normal file
32
src/scenes/player/player.tscn
Normal file
@ -0,0 +1,32 @@
|
||||
[gd_scene load_steps=6 format=3 uid="uid://nsv4lbw7j8mi"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://csgybabwb8cbg" path="res://src/scripts/player/player.gd" id="1_swhsd"]
|
||||
[ext_resource type="Script" uid="uid://cxnvqh8ijf0ox" path="res://src/scripts/player/boom.gd" id="2_qopur"]
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_yx56f"]
|
||||
|
||||
[sub_resource type="CapsuleMesh" id="CapsuleMesh_swhsd"]
|
||||
|
||||
[sub_resource type="SeparationRayShape3D" id="SeparationRayShape3D_swhsd"]
|
||||
|
||||
[node name="Player" type="CharacterBody3D"]
|
||||
script = ExtResource("1_swhsd")
|
||||
|
||||
[node name="CollisionShape" type="CollisionShape3D" parent="."]
|
||||
shape = SubResource("CapsuleShape3D_yx56f")
|
||||
|
||||
[node name="Mesh" type="MeshInstance3D" parent="."]
|
||||
mesh = SubResource("CapsuleMesh_swhsd")
|
||||
|
||||
[node name="Boom" type="SpringArm3D" parent="." node_paths=PackedStringArray("target")]
|
||||
transform = Transform3D(1, 0, 0, 0, 0.866025, 0.5, 0, -0.5, 0.866025, 0, 1, 0)
|
||||
shape = SubResource("SeparationRayShape3D_swhsd")
|
||||
spring_length = 4.0
|
||||
script = ExtResource("2_qopur")
|
||||
target = NodePath("../HorizontalPivot/VerticalPivot")
|
||||
|
||||
[node name="Camera" type="Camera3D" parent="Boom"]
|
||||
|
||||
[node name="HorizontalPivot" type="Node3D" parent="."]
|
||||
|
||||
[node name="VerticalPivot" type="Node3D" parent="HorizontalPivot"]
|
||||
32
src/scenes/player/player.tscn29126206583.tmp
Normal file
32
src/scenes/player/player.tscn29126206583.tmp
Normal file
@ -0,0 +1,32 @@
|
||||
[gd_scene load_steps=6 format=3 uid="uid://nsv4lbw7j8mi"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://csgybabwb8cbg" path="res://src/scripts/player/player.gd" id="1_swhsd"]
|
||||
[ext_resource type="Script" uid="uid://cxnvqh8ijf0ox" path="res://src/scripts/player/boom.gd" id="2_qopur"]
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_yx56f"]
|
||||
|
||||
[sub_resource type="CapsuleMesh" id="CapsuleMesh_swhsd"]
|
||||
|
||||
[sub_resource type="SeparationRayShape3D" id="SeparationRayShape3D_swhsd"]
|
||||
|
||||
[node name="Player" type="CharacterBody3D"]
|
||||
script = ExtResource("1_swhsd")
|
||||
|
||||
[node name="CollisionShape" type="CollisionShape3D" parent="."]
|
||||
shape = SubResource("CapsuleShape3D_yx56f")
|
||||
|
||||
[node name="Mesh" type="MeshInstance3D" parent="."]
|
||||
mesh = SubResource("CapsuleMesh_swhsd")
|
||||
|
||||
[node name="Boom" type="SpringArm3D" parent="." node_paths=PackedStringArray("target")]
|
||||
transform = Transform3D(1, 0, 0, 0, 0.866025, 0.5, 0, -0.5, 0.866025, 0, 1, 0)
|
||||
shape = SubResource("SeparationRayShape3D_swhsd")
|
||||
spring_length = 4.0
|
||||
script = ExtResource("2_qopur")
|
||||
target = NodePath("../HorizontalPivot/VerticalPivot")
|
||||
|
||||
[node name="Camera" type="Camera3D" parent="Boom"]
|
||||
|
||||
[node name="HorizontalPivot" type="Node3D" parent="."]
|
||||
|
||||
[node name="VerticalPivot" type="Node3D" parent="HorizontalPivot"]
|
||||
32
src/scenes/player/player.tscn29133398622.tmp
Normal file
32
src/scenes/player/player.tscn29133398622.tmp
Normal file
@ -0,0 +1,32 @@
|
||||
[gd_scene load_steps=6 format=3 uid="uid://nsv4lbw7j8mi"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://csgybabwb8cbg" path="res://src/scripts/player/player.gd" id="1_swhsd"]
|
||||
[ext_resource type="Script" uid="uid://cxnvqh8ijf0ox" path="res://src/scripts/player/boom.gd" id="2_qopur"]
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_yx56f"]
|
||||
|
||||
[sub_resource type="CapsuleMesh" id="CapsuleMesh_swhsd"]
|
||||
|
||||
[sub_resource type="SeparationRayShape3D" id="SeparationRayShape3D_swhsd"]
|
||||
|
||||
[node name="Player" type="CharacterBody3D"]
|
||||
script = ExtResource("1_swhsd")
|
||||
|
||||
[node name="CollisionShape" type="CollisionShape3D" parent="."]
|
||||
shape = SubResource("CapsuleShape3D_yx56f")
|
||||
|
||||
[node name="Mesh" type="MeshInstance3D" parent="."]
|
||||
mesh = SubResource("CapsuleMesh_swhsd")
|
||||
|
||||
[node name="Boom" type="SpringArm3D" parent="." node_paths=PackedStringArray("target")]
|
||||
transform = Transform3D(1, 0, 0, 0, 0.866025, 0.5, 0, -0.5, 0.866025, 0, 1, 0)
|
||||
shape = SubResource("SeparationRayShape3D_swhsd")
|
||||
spring_length = 4.0
|
||||
script = ExtResource("2_qopur")
|
||||
target = NodePath("../HorizontalPivot/VerticalPivot")
|
||||
|
||||
[node name="Camera" type="Camera3D" parent="Boom"]
|
||||
|
||||
[node name="HorizontalPivot" type="Node3D" parent="."]
|
||||
|
||||
[node name="VerticalPivot" type="Node3D" parent="HorizontalPivot"]
|
||||
7
src/scripts/player/boom.gd
Normal file
7
src/scripts/player/boom.gd
Normal file
@ -0,0 +1,7 @@
|
||||
extends SpringArm3D
|
||||
|
||||
@export var target: Node3D
|
||||
@export var decay: float = 25.0
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
global_transform = global_transform.interpolate_with(target.global_transform, 1.0 - exp(-decay * delta))
|
||||
1
src/scripts/player/boom.gd.uid
Normal file
1
src/scripts/player/boom.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://cxnvqh8ijf0ox
|
||||
58
src/scripts/player/player.gd
Normal file
58
src/scripts/player/player.gd
Normal file
@ -0,0 +1,58 @@
|
||||
extends CharacterBody3D
|
||||
|
||||
|
||||
const SPEED = 5.0
|
||||
const JUMP_VELOCITY = 4.5
|
||||
|
||||
# Stores the x/y direction of the camera
|
||||
var _look := Vector2.ZERO
|
||||
|
||||
@export var mouse_sensitivity: float = 0.0075
|
||||
@export var camera_max_pitch: float = 25
|
||||
@export var camera_min_pitch: float = -75
|
||||
|
||||
@onready var horizontal_pivot: Node3D = $HorizontalPivot
|
||||
@onready var vertical_pivot: Node3D = $HorizontalPivot/VerticalPivot
|
||||
|
||||
func _ready() -> void:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
frame_camera_rotation()
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("move_jump") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
var direction := get_movement_direction()
|
||||
if direction:
|
||||
velocity.x = direction.x * SPEED
|
||||
velocity.z = direction.z * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
velocity.z = move_toward(velocity.z, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event.is_action_pressed("ui_cancel"):
|
||||
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
||||
|
||||
if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
|
||||
if event is InputEventMouseMotion:
|
||||
_look = -event.relative * mouse_sensitivity
|
||||
|
||||
func frame_camera_rotation() -> void:
|
||||
horizontal_pivot.rotate_y(_look.x)
|
||||
vertical_pivot.rotate_x(_look.y)
|
||||
vertical_pivot.rotation.x = clampf(vertical_pivot.rotation.x, deg_to_rad(camera_min_pitch), deg_to_rad(camera_max_pitch))
|
||||
_look = Vector2.ZERO
|
||||
|
||||
func get_movement_direction() -> Vector3:
|
||||
var input_dir := Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
|
||||
var input_vector := Vector3(input_dir.x, 0, input_dir.y).normalized()
|
||||
var direction := horizontal_pivot.global_transform.basis * input_vector
|
||||
return direction
|
||||
1
src/scripts/player/player.gd.uid
Normal file
1
src/scripts/player/player.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://csgybabwb8cbg
|
||||
Loading…
x
Reference in New Issue
Block a user