Skip to content

Instantly share code, notes, and snippets.

@mroberti
Last active March 29, 2020 17:30
Show Gist options
  • Save mroberti/5053b431bdd980543fcd92f7d80ad24b to your computer and use it in GitHub Desktop.
Save mroberti/5053b431bdd980543fcd92f7d80ad24b to your computer and use it in GitHub Desktop.
$screenW = 1280
$screenH = 720
$centerX = $screenW/2
$centerY = $screenH/2
def tick args
defaults args
render args
input args
calc args
end
def defaults args
args.state.easing_inprogress ||= []
args.state.player ||= args.state.new_entity(:player, x: $centerX, y: $centerY, angle: 0)
end
def render args
# TODO
# OK, not sure if this is the preferred method, but
# it works for now. I could use some pointers!
args.outputs.sprites << {
x: $centerX,
y: $centerY,
w: 397/2,
h: 149/2,
path: "sprites/ship1.png",
angle: args.state.player.angle,
angle_anchor_x: 0.5,
angle_anchor_y: 0.5
}
end
def input args
if args.inputs.mouse.click
args.state.easing_inprogress << { start_at: args.state.tick_count + 60,
duration: 180,
start_value: args.state.player.angle-90,
end_value: args.state.player.angle,
target_property: :angle,
target_entity: args.state.player,
transition: [:flip, :quart, :flip] }
end
end
def calc args
# process queue
args.state.easing_inprogress.each do |h|
next_percentage = ease_percentage current_tick: args.state.tick_count,
start_tick: h[:start_at],
duration: h[:duration]
#Checking for nil
if (next_percentage)
for theTransition in h[:transition] do
distance = h[:end_value]-h[:start_value]
theValue = (next_percentage*distance)+h[:start_value]
next_percentage = send(theTransition, next_percentage)
h[:target_entity].as_hash[h[:target_property]] = theValue
end
puts "args.state.player.angle: " + args.state.player.angle.to_s
end
end
# delete completed
args.state.easing_inprogress.reject! { |h| h[:start_at] + h[:duration] < args.state.tick_count }
end
def quart perc
puts "In quart perc: " + perc.to_s
puts "perc * perc * perc * perc: " + (perc * perc * perc * perc).to_s
perc * perc * perc * perc
end
def flip perc
puts "in flip perc: " + perc.to_s
1 - perc
end
def ease_percentage opts
return opts[:start_value] if opts[:current_tick] < opts[:start_tick]
return opts[:end_value] if opts[:current_tick] > (opts[:start_tick] + opts[:duration])
return (opts[:current_tick]- opts[:start_tick])/(opts[:duration])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment