player character has move and dodge

This commit is contained in:
2025-06-23 22:58:24 -05:00
parent 4d27d00b73
commit a2df5e276a
18 changed files with 292 additions and 48 deletions

16
Scripts/State.gd Normal file
View File

@@ -0,0 +1,16 @@
extends Node
class_name State
signal Transitioned
func Enter():
pass
func Exit():
pass
func Update(_delta : float):
pass
func Physics_Update(_delta : float):
pass

1
Scripts/State.gd.uid Normal file
View File

@@ -0,0 +1 @@
uid://55lxwlg155dq

View File

@@ -1,34 +1,23 @@
extends CharacterBody3D
class_name Player
var input_dir
var dodge_direction
@onready var body: MeshInstance3D = $Body
@onready var anim_player: AnimationPlayer = $AnimationPlayer
const MAX_STAMINA : float = 100
var stamina = MAX_STAMINA
const STAMINA_REGEN_RATE = 10.0
const MOVE_SPEED = 10.0
const MOVE_TRANSITION_SPEED = 7.0
## Gameplay Settings
const DODGE_STAMINA_COST = 30.0
func _physics_process(delta: float) -> void:
if is_on_floor():
standard_movement(delta)
body_look_at_mouse()
apply_gravity(delta)
stamina_regen(delta)
move_and_slide()
func body_look_at_mouse():
var mouse_raycast = MousePos.get_mouse_world_position()
if mouse_raycast != null:
body.look_at(Vector3(mouse_raycast.x,body.global_position.y,mouse_raycast.y),Vector3.UP)
func apply_gravity(delta):
if !is_on_floor():
velocity.y -= 9.8 * delta
func standard_movement(delta):
input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
if input_dir != null:
velocity.x = lerp(velocity.x, input_dir.x * MOVE_SPEED,delta * MOVE_TRANSITION_SPEED)
velocity.z = lerp(velocity.z, input_dir.y * MOVE_SPEED,delta * MOVE_TRANSITION_SPEED)
func stamina_regen(delta):
if stamina < MAX_STAMINA:
print("STAMINA : ", stamina)
stamina = clamp(stamina + delta * STAMINA_REGEN_RATE,0,MAX_STAMINA)

2
Scripts/player_attack.gd Normal file
View File

@@ -0,0 +1,2 @@
extends State
class_name PlayerAttack

View File

@@ -0,0 +1 @@
uid://c2xwiexy5b3b3

View File

@@ -1,5 +1,4 @@
extends Camera3D
class_name PlayerCamera
extends Node3D
@export var player : Player

View File

@@ -0,0 +1,21 @@
extends PlayerState
class_name PlayerDodgeRoll
const DODGE_SPEED : float = 20.0
const DODGE_TIME : float = .1
var dodge_timer = DODGE_TIME
func Enter():
character.anim_player.play("dodge")
func Physics_Update(delta):
if dodge_timer > 0:
dodge_timer -= delta
else:
dodge_timer = DODGE_TIME
Transitioned.emit(self,"on floor")
character.velocity.x = character.dodge_direction.x * DODGE_SPEED
character.velocity.z = character.dodge_direction.y * DODGE_SPEED

View File

@@ -0,0 +1 @@
uid://dhnl0penaqfkx

View File

@@ -0,0 +1,19 @@
extends PlayerState
class_name PlayerOnFloor
func Physics_Update(delta):
dodge_roll()
standard_movement(delta)
body_look_at_mouse()
apply_gravity(delta)
respawn_on_fall(delta)
func dodge_roll():
if character.is_on_floor():
if character.stamina > character.DODGE_STAMINA_COST:
if Input.is_action_just_pressed("dodge"):
var dodge_direction = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
if dodge_direction:
character.stamina -= character.DODGE_STAMINA_COST
character.dodge_direction = dodge_direction
Transitioned.emit(self,"dodge roll")

View File

@@ -0,0 +1 @@
uid://wnisqyoyai2h

View File

@@ -0,0 +1,42 @@
extends Node
class_name PlayerStateMachine
@export var character : Player
@export var initial_state : State
var current_state : State
var states : Dictionary = {}
func _ready() -> void:
for child in get_children():
if child is State:
states[child.name.to_lower()] = child
child.Transitioned.connect(on_child_transition)
if initial_state:
initial_state.Enter()
current_state = initial_state
func _process(delta: float) -> void:
if current_state:
current_state.Update(delta)
func _physics_process(delta: float) -> void:
if current_state:
current_state.Physics_Update(delta)
func on_child_transition(state,new_state_name):
if state != current_state:
return
var new_state = states.get(new_state_name.to_lower())
if !new_state:
return
if current_state:
current_state.Exit()
new_state.Enter()
current_state = new_state
print("PLAYER STATE CHANGED TO : ",current_state)

View File

@@ -0,0 +1 @@
uid://bk6205bvyl0to

56
Scripts/player_states.gd Normal file
View File

@@ -0,0 +1,56 @@
extends State
class_name PlayerState
@export var move_speed = 10.0
@export var move_transition_speed = 7.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)

View File

@@ -0,0 +1 @@
uid://mmr40sx0iij5