State machine debug stats
This commit is contained in:
parent
c6db46fb8a
commit
036aac2977
|
@ -20,6 +20,12 @@ func stat_set(stat: Dictionary):
|
|||
func set_text(node, stat):
|
||||
node.text = stat["key"] + ": " + stat["value"]
|
||||
|
||||
# Utility function for your leasure
|
||||
func maybe_put_stat(stat_key, value: String):
|
||||
if stat_key != null:
|
||||
stat_key["value"] = value
|
||||
stat_set(stat_key)
|
||||
|
||||
func _process(_delta):
|
||||
# Add fps stat
|
||||
stat_set({ "key": "fps", "value": str(Engine.get_frames_per_second()) })
|
||||
|
|
|
@ -1,18 +1,20 @@
|
|||
# Place available states under this node.
|
||||
# Provides the mechanism for switching states.
|
||||
@icon("res://state_machine/think.png")
|
||||
extends Node
|
||||
class_name StateMachine
|
||||
|
||||
# Place available states under this node.
|
||||
# Provides the mechanism for switching states.
|
||||
|
||||
@export var initial_state: NodePath
|
||||
var state: Node # Current state
|
||||
|
||||
# Set by owner
|
||||
var state_debug_stat = null
|
||||
|
||||
func _ready():
|
||||
state = get_node(initial_state)
|
||||
|
||||
func transition(new_state, args = {}):
|
||||
print("Node: " + get_parent().name + " transitioning to state: " + new_state.name)
|
||||
StatHub.maybe_put_stat(state_debug_stat, new_state.name)
|
||||
state = new_state
|
||||
new_state.enter(args)
|
||||
|
||||
|
|
|
@ -13,14 +13,9 @@ func _init(p_rise_curve = null, p_initial_velocity = 0.0, p_released_curve = nul
|
|||
# Calculate new y-velocity
|
||||
func jump(yvel: float, time: float, released_jump: bool, delta: float, stat_key = null):
|
||||
if released_jump:
|
||||
maybe_put_stat(stat_key, "released jump")
|
||||
StatHub.maybe_put_stat(stat_key, "released jump")
|
||||
return yvel - released_curve.sample(time) * delta
|
||||
else:
|
||||
# Rising when holding the button
|
||||
maybe_put_stat(stat_key, "holding jump")
|
||||
StatHub.maybe_put_stat(stat_key, "holding jump")
|
||||
return yvel - rise_curve.sample(time) * delta
|
||||
|
||||
func maybe_put_stat(stat_key, value):
|
||||
if stat_key != null:
|
||||
stat_key["value"] = value
|
||||
StatHub.stat_set(stat_key)
|
||||
|
|
|
@ -16,18 +16,13 @@ func _init(p_accel_curve = null, p_turn_curve = null, p_stop_curve = null):
|
|||
func run(xvel: float, dir: float, delta: float, stat_key = null) -> float:
|
||||
# Stopping speed
|
||||
if sign(dir) == 0:
|
||||
maybe_put_stat(stat_key, "stopping")
|
||||
StatHub.maybe_put_stat(stat_key, "stopping")
|
||||
return move_toward(xvel, 0, stop_curve.sample(abs(xvel)/time) * delta)
|
||||
# Forward speed
|
||||
elif sign(dir) == sign(xvel) or sign(xvel) == 0:
|
||||
maybe_put_stat(stat_key, "forward")
|
||||
StatHub.maybe_put_stat(stat_key, "forward")
|
||||
return xvel + sign(dir) * accel_curve.sample(abs(xvel)/time) * delta
|
||||
# Turning speed
|
||||
else:
|
||||
maybe_put_stat(stat_key, "turning")
|
||||
StatHub.maybe_put_stat(stat_key, "turning")
|
||||
return xvel + sign(dir) * turn_curve.sample(abs(xvel)/time) * delta
|
||||
|
||||
func maybe_put_stat(stat_key, value):
|
||||
if stat_key != null:
|
||||
stat_key["value"] = value
|
||||
StatHub.stat_set(stat_key)
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
extends CharacterBody2D
|
||||
|
||||
# An example of a script that could be on an enemy/player (in this case enemy).
|
||||
# Controls state changes and state input such as direction, could use
|
||||
# area detectors then change the state for example.
|
||||
extends CharacterBody2D
|
||||
|
||||
func current_state():
|
||||
return $StateMachine.state
|
||||
return stm.state
|
||||
|
||||
@onready var stm = $StateMachine
|
||||
@onready var air = $StateMachine/Air
|
||||
|
@ -14,6 +13,7 @@ func current_state():
|
|||
func _ready():
|
||||
air.jump_debug_stat = { "key": "player jump curve" }
|
||||
run.run_debug_stat = { "key": "player run curve" }
|
||||
stm.state_debug_stat = { "key": "player state" }
|
||||
|
||||
func _physics_process(delta):
|
||||
# Start falling
|
||||
|
|
Loading…
Reference in a new issue