You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.3 KiB
GDScript

extends AnimatedSprite3D
class_name View
@onready var status = (%Status as Status)
class Trans:
var condition:StringName
var compareType:String
var conditionValue
var to:StringName
class Trigger:
var condition:StringName
var to:StringName
var run_map = {}
var trans_map = {}
var trigger_map = {}
var move_sprite_frames:SpriteFrames
func _ready():
# 地面行走
trans("idle_loop","run1","speed_xz",">",1)
run("run1","run2_loop")
trans("run1","run3","speed_xz","<",1)
trans("run2_loop","run3","speed_xz","<",1)
run("run3","idle_loop")
trans("run3","run1","speed_xz",">",1)
# 跳跃
trigger("any","jump1","trigger_jump")
run("jump1","jump2_loop")
trans("jump2_loop","jump3","speed_y","<",0)
run("jump3","jump4_loop")
trans("jump4_loop","jump5","is_on_floor","=",true)
run("jump5","idle_loop")
trans("jump5","run1","speed_xz",">",1)
func _process(delta):
update_flip()
if status.is_free_control:
update_trigger()
update_trans()
func init(default:SpriteFrames):
sprite_frames = default
move_sprite_frames = default
play("idle_loop")
func reset():
sprite_frames = move_sprite_frames
play("idle_loop")
func run(from:StringName,to:StringName):
run_map[from] = to
func trans(from:StringName,to:StringName,condition,compareType,conditionValue):
var newTrans = Trans.new()
newTrans.condition = condition
newTrans.compareType = compareType
newTrans.conditionValue = conditionValue
newTrans.to = to
trans_map[from] = newTrans
func trigger(from:StringName,to:StringName,condition):
var newTrigger = Trans.new()
newTrigger.condition = condition
newTrigger.to = to
trigger_map[from] = newTrigger
func update_trigger():
for animation in trigger_map:
var trigger = trigger_map[animation]
var triggerValue = status.get(trigger.condition)
if triggerValue:
status.set(trigger.condition,false)
play(trigger.to)
func update_trans():
if animation in trans_map:
var trans = trans_map[animation]
var conditionValue = status.get(trans.condition)
match trans.compareType:
">":if conditionValue <= trans.conditionValue:return
"<":if conditionValue >= trans.conditionValue:return
"=":if conditionValue != trans.conditionValue:return
play(trans.to)
func update_flip():
scale.x = 1 if status.is_right else -1
func _on_animation_finished():
if animation in run_map:
var run = run_map[animation]
play(run)