Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Created August 10, 2024 23:27
Show Gist options
  • Save amirrajan/20fc5562f7d3f86f76d4234b9e4e3a76 to your computer and use it in GitHub Desktop.
Save amirrajan/20fc5562f7d3f86f76d4234b9e4e3a76 to your computer and use it in GitHub Desktop.
DragonRuby Game Toolkit - Animation State Machine
class Player
def initialize
@action_lookup = {
idle: {
frame_count: 7,
dir: "sprites/player/idle",
repeat: true
},
attack: {
frame_count: 6,
dir: "sprites/player/attack",
next_action: :idle
},
}
@action_lookup.each do |k, v|
v.hold_for ||= 6
v.default_index ||= 0
v.duration = v.frame_count * v.hold_for
end
@action_lookup
end
def action_finished?
@action_at.elapsed_time > @action_lookup[@action].duration
end
def current_action
@action_lookup[@action]
end
def tick
@next_action = nil
if args.inputs.keyboard.key_down.space
@next_action = :attack
elsif ...
...
end
if action_finished?
@next_action = current_action.next_action
end
...
if @next_action && @next_action != @action
@action = @next_action
@action_at = Kernel.tick_count
end
end
def prefab
frame_index = Numeric.frame_index(start_at: @action_at, **current_action) || current_action.default_index
path = "#{current_action.dir}/#{frame_index.png}"
{ x: ..., y: ..., path: path }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment