57 lines
1.7 KiB
GDScript
57 lines
1.7 KiB
GDScript
extends State
|
|
class_name PlayerState
|
|
|
|
@export var move_speed = 10.0
|
|
@export var move_transition_speed = 10.0
|
|
@export var health_lost_on_fall = 2.0
|
|
|
|
# respawn after falling
|
|
const FALL_TIME_TO_RESPAWN : float = 4.0
|
|
const TIME_TO_CACHE_POSITION : float = 2.0
|
|
var ground_pos_cached = []
|
|
var fall_timer = FALL_TIME_TO_RESPAWN
|
|
var ground_pos_timer = 0
|
|
|
|
@onready var character = get_parent().character
|
|
|
|
func Update(delta):
|
|
pass
|
|
|
|
func respawn_on_fall(delta):
|
|
if character.is_on_floor():
|
|
if ground_pos_timer > 0:
|
|
ground_pos_timer -= delta
|
|
else:
|
|
ground_pos_timer = TIME_TO_CACHE_POSITION
|
|
ground_pos_cached.append(character.global_position)
|
|
if ground_pos_cached.size() > 2:
|
|
ground_pos_cached.pop_front()
|
|
else:
|
|
if fall_timer > 0:
|
|
fall_timer -= delta
|
|
else:
|
|
fall_timer = FALL_TIME_TO_RESPAWN
|
|
character.global_position = ground_pos_cached[0]
|
|
|
|
func apply_gravity(delta):
|
|
if !character.is_on_floor():
|
|
character.velocity.y -= 9.8 * delta
|
|
|
|
func body_look_at_mouse():
|
|
var mouse_raycast = MousePos.get_mouse_world_position()
|
|
if mouse_raycast != null:
|
|
character.body.look_at(Vector3(mouse_raycast.x,character.body.global_position.y,mouse_raycast.y),Vector3.UP)
|
|
|
|
func standard_movement(delta):
|
|
if character.is_on_floor():
|
|
var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
|
|
|
|
if input_dir != null:
|
|
character.velocity.x = lerp(character.velocity.x, input_dir.x * move_speed, delta * move_transition_speed)
|
|
character.velocity.z = lerp(character.velocity.z, input_dir.y * move_speed,delta * move_transition_speed)
|
|
else:
|
|
if character.velocity:
|
|
const MOMENTUM = 2.0
|
|
character.velocity.x = lerp(character.velocity.x, 0.0,delta * MOMENTUM)
|
|
character.velocity.z = lerp(character.velocity.z, 0.0,delta * MOMENTUM)
|