95 lines
3.0 KiB
GDScript
95 lines
3.0 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 = 20.0
|
|
|
|
# respawn after falling
|
|
const FALL_TIME_TO_RESPAWN : float = 3.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]
|
|
character.health -= health_lost_on_fall
|
|
|
|
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():
|
|
# Get KB Input
|
|
var current_movement = movement_input()
|
|
if current_movement != null:
|
|
character.velocity.x = lerp(character.velocity.x, current_movement.x * move_speed, delta * move_transition_speed)
|
|
character.velocity.z = lerp(character.velocity.z, current_movement.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)
|
|
|
|
func movement_input():
|
|
# Get KB Input
|
|
var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
|
|
# Add Controller Input
|
|
if abs(Input.get_joy_axis(0,JOY_AXIS_LEFT_X)) > ControlMapping.L_JOYSTICK_SENSITIVITY or abs(Input.get_joy_axis(0,JOY_AXIS_LEFT_Y)) > ControlMapping.L_JOYSTICK_SENSITIVITY:
|
|
input_dir += joypad_walk()
|
|
return input_dir
|
|
|
|
func joypad_walk():
|
|
# Joypad right stick look control
|
|
var dir_out = Vector2(0,0)
|
|
var xAxis = Input.get_joy_axis(0,JOY_AXIS_LEFT_X)
|
|
var yAxis = Input.get_joy_axis(0,JOY_AXIS_LEFT_Y)
|
|
|
|
if abs(xAxis) > ControlMapping.L_JOYSTICK_DEADZONE:
|
|
dir_out.x = xAxis
|
|
if abs(yAxis) > ControlMapping.L_JOYSTICK_DEADZONE:
|
|
dir_out.y = yAxis
|
|
|
|
return dir_out
|
|
|
|
func dodge_roll():
|
|
if character.is_on_floor():
|
|
if Input.is_action_just_pressed("dodge"):
|
|
if character.stamina > character.DODGE_STAMINA_COST:
|
|
var dodge_direction = movement_input()
|
|
if dodge_direction:
|
|
character.stamina -= character.DODGE_STAMINA_COST
|
|
character.dodge_direction = dodge_direction
|
|
Transitioned.emit(self,"dodge roll")
|
|
|
|
func attack():
|
|
if Input.is_action_just_pressed("attack"):
|
|
if character.stamina > character.ATTACK_STAMINA_COST:
|
|
character.stamina -= character.ATTACK_STAMINA_COST
|
|
Transitioned.emit(self,"attack")
|