State machine debug stats

This commit is contained in:
Rakarake 2023-03-22 22:30:06 +01:00
parent c6db46fb8a
commit 036aac2977
5 changed files with 20 additions and 22 deletions

View file

@ -20,6 +20,12 @@ func stat_set(stat: Dictionary):
func set_text(node, stat): func set_text(node, stat):
node.text = stat["key"] + ": " + stat["value"] 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): func _process(_delta):
# Add fps stat # Add fps stat
stat_set({ "key": "fps", "value": str(Engine.get_frames_per_second()) }) stat_set({ "key": "fps", "value": str(Engine.get_frames_per_second()) })

View file

@ -1,18 +1,20 @@
# Place available states under this node.
# Provides the mechanism for switching states.
@icon("res://state_machine/think.png") @icon("res://state_machine/think.png")
extends Node extends Node
class_name StateMachine class_name StateMachine
# Place available states under this node.
# Provides the mechanism for switching states.
@export var initial_state: NodePath @export var initial_state: NodePath
var state: Node # Current state var state: Node # Current state
# Set by owner
var state_debug_stat = null
func _ready(): func _ready():
state = get_node(initial_state) state = get_node(initial_state)
func transition(new_state, args = {}): 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 state = new_state
new_state.enter(args) new_state.enter(args)

View file

@ -13,14 +13,9 @@ func _init(p_rise_curve = null, p_initial_velocity = 0.0, p_released_curve = nul
# Calculate new y-velocity # Calculate new y-velocity
func jump(yvel: float, time: float, released_jump: bool, delta: float, stat_key = null): func jump(yvel: float, time: float, released_jump: bool, delta: float, stat_key = null):
if released_jump: 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 return yvel - released_curve.sample(time) * delta
else: else:
# Rising when holding the button # 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 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)

View file

@ -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: func run(xvel: float, dir: float, delta: float, stat_key = null) -> float:
# Stopping speed # Stopping speed
if sign(dir) == 0: 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) return move_toward(xvel, 0, stop_curve.sample(abs(xvel)/time) * delta)
# Forward speed # Forward speed
elif sign(dir) == sign(xvel) or sign(xvel) == 0: 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 return xvel + sign(dir) * accel_curve.sample(abs(xvel)/time) * delta
# Turning speed # Turning speed
else: 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 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)

View file

@ -1,11 +1,10 @@
extends CharacterBody2D
# An example of a script that could be on an enemy/player (in this case enemy). # 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 # Controls state changes and state input such as direction, could use
# area detectors then change the state for example. # area detectors then change the state for example.
extends CharacterBody2D
func current_state(): func current_state():
return $StateMachine.state return stm.state
@onready var stm = $StateMachine @onready var stm = $StateMachine
@onready var air = $StateMachine/Air @onready var air = $StateMachine/Air
@ -14,6 +13,7 @@ func current_state():
func _ready(): func _ready():
air.jump_debug_stat = { "key": "player jump curve" } air.jump_debug_stat = { "key": "player jump curve" }
run.run_debug_stat = { "key": "player run curve" } run.run_debug_stat = { "key": "player run curve" }
stm.state_debug_stat = { "key": "player state" }
func _physics_process(delta): func _physics_process(delta):
# Start falling # Start falling