refactor to state machine
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
extends State
|
||||
|
||||
func process_physics(delta: float) -> State:
|
||||
if not multiplayer.is_server():
|
||||
return
|
||||
|
||||
var target_velocity = Vector3(0, subject.velocity.y, 0)
|
||||
subject.velocity = subject.velocity.move_toward(target_velocity, subject.air_deceleration * delta)
|
||||
subject.velocity += Main.gravity_velocity
|
||||
subject.move_and_slide()
|
||||
|
||||
if subject.is_on_floor():
|
||||
var input_direction = %Input.direction
|
||||
if input_direction.length() < 0.05:
|
||||
return %Idle
|
||||
|
||||
if %Input.walking:
|
||||
return %Walk
|
||||
|
||||
return %Run
|
||||
|
||||
return
|
||||
+16
-1
@@ -1,4 +1,19 @@
|
||||
extends State
|
||||
|
||||
func process_physics(delta: float) -> State:
|
||||
return
|
||||
if not multiplayer.is_server():
|
||||
return
|
||||
|
||||
var target_velocity = Vector3(0, subject.velocity.y, 0)
|
||||
subject.velocity = subject.velocity.move_toward(target_velocity, subject.acceleration * delta)
|
||||
subject.velocity += Main.gravity_velocity
|
||||
subject.move_and_slide()
|
||||
|
||||
if not subject.is_on_floor():
|
||||
return %Fall
|
||||
|
||||
var input_direction = %Input.direction
|
||||
if input_direction.length() < 0.05:
|
||||
return
|
||||
|
||||
return %Run
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
extends State
|
||||
|
||||
func process_physics(delta: float) -> State:
|
||||
if not multiplayer.is_server():
|
||||
return
|
||||
|
||||
var input_direction = %Input.direction
|
||||
if input_direction.length() < 0.05:
|
||||
return %Idle
|
||||
|
||||
var move_direction = Vector3(input_direction.x, 0, input_direction.y)
|
||||
var target_velocity = Vector3(move_direction.x * subject.run_speed, subject.velocity.y, move_direction.z * subject.run_speed)
|
||||
subject.velocity = subject.velocity.move_toward(target_velocity, subject.acceleration * delta)
|
||||
subject.velocity += Main.gravity_velocity
|
||||
subject.move_and_slide()
|
||||
|
||||
if move_direction.length() >= 0.1:
|
||||
subject.last_direction = move_direction
|
||||
var target_angle := Vector3.FORWARD.signed_angle_to(subject.last_direction, Vector3.UP)
|
||||
subject.skin.global_rotation.y = lerp_angle(subject.skin.rotation.y, target_angle, subject.rotation_speed * delta)
|
||||
|
||||
if %Input.walking:
|
||||
return %Walk
|
||||
|
||||
if not subject.is_on_floor():
|
||||
return %Fall
|
||||
|
||||
return
|
||||
@@ -0,0 +1,19 @@
|
||||
class_name State
|
||||
extends Node
|
||||
|
||||
var subject
|
||||
|
||||
func enter() -> void:
|
||||
pass
|
||||
|
||||
func exit() -> void:
|
||||
pass
|
||||
|
||||
func process_input(_event: InputEvent) -> State:
|
||||
return null
|
||||
|
||||
func process_frame(_delta: float) -> State:
|
||||
return null
|
||||
|
||||
func process_physics(_delta: float) -> State:
|
||||
return null
|
||||
@@ -0,0 +1,51 @@
|
||||
extends Node
|
||||
|
||||
@export var current_state: State
|
||||
var subject: Node
|
||||
|
||||
func set_subject(new_subject: Node) -> void:
|
||||
subject = new_subject
|
||||
for state in get_children():
|
||||
state.subject = subject
|
||||
|
||||
func _change_state(new_state: State) -> void:
|
||||
if current_state:
|
||||
#print("Exiting: %s" % current_state.name)
|
||||
current_state.exit()
|
||||
|
||||
current_state = new_state
|
||||
print("Entering: %s" % current_state.name)
|
||||
current_state.enter()
|
||||
|
||||
func process_physics(delta: float) -> void:
|
||||
if not current_state:
|
||||
return
|
||||
|
||||
if not current_state.subject:
|
||||
return
|
||||
|
||||
var new_state = current_state.process_physics(delta)
|
||||
if new_state:
|
||||
_change_state(new_state)
|
||||
|
||||
func process_input(event: InputEvent) -> void:
|
||||
if not current_state:
|
||||
return
|
||||
|
||||
if not current_state.subject:
|
||||
return
|
||||
|
||||
var new_state = current_state.process_input(event)
|
||||
if new_state:
|
||||
_change_state(new_state)
|
||||
|
||||
func process_frame(delta: float) -> void:
|
||||
if not current_state:
|
||||
return
|
||||
|
||||
if not current_state.subject:
|
||||
return
|
||||
|
||||
var new_state = current_state.process_frame(delta)
|
||||
if new_state:
|
||||
_change_state(new_state)
|
||||
@@ -0,0 +1,28 @@
|
||||
extends State
|
||||
|
||||
func process_physics(delta: float) -> State:
|
||||
if not multiplayer.is_server():
|
||||
return
|
||||
|
||||
var input_direction = %Input.direction
|
||||
if input_direction.length() < 0.05:
|
||||
return %Idle
|
||||
|
||||
var move_direction = Vector3(input_direction.x, 0, input_direction.y)
|
||||
var target_velocity = Vector3(move_direction.x * subject.walk_speed, subject.velocity.y, move_direction.z * subject.walk_speed)
|
||||
subject.velocity = subject.velocity.move_toward(target_velocity, subject.acceleration * delta)
|
||||
subject.velocity += Main.gravity_velocity
|
||||
subject.move_and_slide()
|
||||
|
||||
if move_direction.length() >= 0.1:
|
||||
subject.last_direction = move_direction
|
||||
var target_angle := Vector3.FORWARD.signed_angle_to(subject.last_direction, Vector3.UP)
|
||||
subject.skin.global_rotation.y = lerp_angle(subject.skin.rotation.y, target_angle, subject.rotation_speed * delta)
|
||||
|
||||
if not %Input.walking:
|
||||
return %Run
|
||||
|
||||
if not subject.is_on_floor():
|
||||
return %Fall
|
||||
|
||||
return
|
||||
Reference in New Issue
Block a user