This commit is contained in:
2024-08-02 04:48:34 +03:00
5 changed files with 108 additions and 6 deletions

View File

@ -3,9 +3,9 @@ extends Node3D
func _ready():
add_child($Dungeon.generate_geometry())
$Camera.position = $Dungeon.get_spawn_point()
$Camera.position.y += 0.5
$Player.position = $Dungeon.get_spawn_point()
$Player.position.y += 0.5
func _process(delta):
$Camera.rotate_y(0.01)
pass

64
Scripts/Player.gd Normal file
View File

@ -0,0 +1,64 @@
extends CharacterBody3D
@export var camera: Camera3D
const VIEW_BOB_FREQUENCY = 8
const VIEW_BOB_AMPLITUDE = 0.03
var speed := 2
var turn_speed_sec := 0.1
var turn_degrees := 90
var view_bob_time := 0.0 # increased while velocity is not zero
func _get_input() -> Vector3:
var input_dir := Vector3()
if Input.is_action_pressed("move_forward"):
input_dir -= global_transform.basis.z
if Input.is_action_pressed("move_backward"):
input_dir += global_transform.basis.z
return input_dir.normalized()
func _process_view_bobbing(delta: float) -> void:
view_bob_time += delta * velocity.length()
camera.position.y = sin(view_bob_time * VIEW_BOB_FREQUENCY) * VIEW_BOB_AMPLITUDE
camera.position.x = cos(view_bob_time * (VIEW_BOB_FREQUENCY / 2)) * VIEW_BOB_AMPLITUDE
func _ready() -> void:
pass
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("turn_left"):
create_tween().tween_property(
self,
"rotation_degrees:y",
rotation_degrees.y + turn_degrees,
turn_speed_sec
)
if event.is_action_pressed("turn_right"):
create_tween().tween_property(
self,
"rotation_degrees:y",
rotation_degrees.y - turn_degrees,
turn_speed_sec
)
func _process(delta: float) -> void:
_process_view_bobbing(delta)
func _physics_process(delta: float) -> void:
# TODO
#velocity.y += gravity * delta
var final_velocity = _get_input() * speed
velocity.x = final_velocity.x
velocity.z = final_velocity.z
move_and_slide()