Last active
June 13, 2025 21:38
-
-
Save amirrajan/3190b194906672ade49ce72d6f593768 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def tick args | |
if Kernel.tick_count > 518 | |
puts "#{args.state.dvd}" | |
GTK.slowmo! 30 | |
end | |
args.state.dvd ||= { x: 640, | |
y: 360, | |
w: 64, | |
h: 64, | |
path: "sprites/square/blue.png", | |
dx: 5, | |
dy: 5, | |
collision_x: false, | |
collision_y: false, } | |
args.state.boundaries ||= [ | |
{ target_w: 5, growth_direction: :x, apply_offset: false, x: 0, y: 0, w: 5, h: 720, path: :solid, r: 255, g: 0, b: 0 }, | |
{ target_w: 5, growth_direction: :x, apply_offset: true, x: 1275, y: 0, w: 5, h: 720, path: :solid, r: 255, g: 0, b: 0 }, | |
{ target_h: 5, growth_direction: :y, apply_offset: false, x: 0, y: 0, w: 1280, h: 5, path: :solid, r: 255, g: 0, b: 0 }, | |
{ target_h: 5, growth_direction: :y, apply_offset: true, x: 0, y: 715, w: 1280, h: 5, path: :solid, r: 255, g: 0, b: 0 }, | |
] | |
args.state.dvd.collision_x = false | |
args.state.dvd.collision_y = false | |
args.state.dvd.y += args.state.dvd.dy | |
collision = args.state.boundaries.find do |boundary| | |
Geometry.intersect_rect?(args.state.dvd, boundary) | |
end | |
if collision | |
if args.state.dvd.dy > 0 | |
args.state.dvd.y = collision.y - args.state.dvd.h | |
args.state.dvd.dy *= -1 | |
else args.state.dvd.dy < 0 | |
args.state.dvd.y = collision.y + collision.h | |
args.state.dvd.dy *= -1 | |
end | |
args.state.dvd.collision_y = true | |
end | |
args.state.dvd.x += args.state.dvd.dx | |
collision = args.state.boundaries.find do |boundary| | |
Geometry.intersect_rect?(args.state.dvd, boundary) | |
end | |
boundries = args.state.boundaries.find_all { |boundary| Geometry.intersect_rect?(args.state.dvd, boundary) } | |
if boundries.length > 0 | |
puts "Collision detected with boundaries: #{boundries.inspect}" | |
end | |
if collision | |
if args.state.dvd.dx > 0 | |
args.state.dvd.x = collision.x - args.state.dvd.w | |
args.state.dvd.dx *= -1 | |
else args.state.dvd.dx < 0 | |
args.state.dvd.x = collision.x + collision.w | |
args.state.dvd.dx *= -1 | |
end | |
args.state.dvd.collision_x = true | |
end | |
args.outputs.watch "#{args.state.dvd}" | |
args.outputs.sprites << args.state.boundaries | |
args.outputs.sprites << args.state.dvd | |
if args.inputs.keyboard.key_down.space | |
puts Kernel.tick_count | |
args.state.boundaries.each do |boundary| | |
if boundary.growth_direction == :x | |
boundary.target_w += 5 | |
elsif boundary.growth_direction == :y | |
boundary.target_h += 5 | |
end | |
end | |
collision = args.state.boundaries.find do |boundary| | |
Geometry.intersect_rect?(args.state.dvd, boundary) | |
end | |
if collision && args.state.dvd.dx > 0 | |
args.state.dvd.x = collision.x - args.state.dvd.w | |
elsif collision | |
args.state.dvd.x = collision.x + collision.w | |
end | |
if collision && !args.state.dvd.collision_x | |
args.state.dvd.dx *= -1 | |
end | |
end | |
args.state.boundaries.each do |boundary| | |
if boundary.growth_direction == :x | |
boundary.w = boundary.w.lerp(boundary.target_w, 0.1) | |
end | |
if boundary.growth_direction == :y | |
boundary.h = boundary.h.lerp(boundary.target_h, 0.1) | |
end | |
end | |
end | |
# GTK.reset_and_replay "replay.txt", speed: 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment