Last active
July 24, 2024 11:46
-
-
Save amirrajan/dc2af5b0937740f6d01202104f28773e to your computer and use it in GitHub Desktop.
DragonRuby Game Toolkit - Many to Many Collision Performance - https://youtu.be/cc3Sx2j85mM
This file contains 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 | |
# initialize state | |
args.state.rects ||= [] | |
args.state.rect_size ||= 8 | |
args.state.move_squares ||= :no | |
if args.inputs.keyboard.key_down.tab | |
args.state.move_squares = args.state.move_squares == :yes ? :no : :yes | |
end | |
args.outputs.watch "FPS: #{GTK.current_framerate.to_sf}" | |
args.outputs.watch "rect count: #{args.state.rects.length.to_i}" | |
args.outputs.watch "move squares? (press tab to toggle) #{args.state.move_squares}" | |
# if the mouse is clicked, add 100 random rects | |
if args.inputs.mouse.click | |
args.state.rects << { x: args.inputs.mouse.x, | |
y: args.inputs.mouse.y, | |
w: args.state.rect_size, | |
h: args.state.rect_size, | |
path: :solid, | |
a: 128, | |
speed: rand(5) + 5 } | |
99.times do | |
angle = rand(360) | |
distance = rand(160) + 32 | |
args.state.rects << { x: args.inputs.mouse.x + angle.vector_x * distance, | |
y: args.inputs.mouse.y + angle.vector_y * distance, | |
w: args.state.rect_size, | |
h: args.state.rect_size, | |
path: :solid, | |
a: 128, | |
speed: (rand(5) + 5) * 1.randomize(:sign) } | |
end | |
end | |
# set rect colors back to green (no collision) | |
args.state.rects.each do |rect| | |
rect.r = 0 | |
rect.g = 128 | |
rect.b = 0 | |
end | |
# move rects if the state is set to move_squares | |
if args.state.move_squares == :yes | |
args.state.rects.each do |rect| | |
rect.x += rect.speed | |
if rect.x > 1280 | |
rect.x = 1280 | |
rect.speed = -rect.speed | |
elsif rect.x < 0 | |
rect.x = 0 | |
rect.speed = -rect.speed | |
end | |
end | |
end | |
# check for collisions many to many | |
Geometry.each_intersect_rect(args.state.rects, args.state.rects) do |rect_one, rect_two| | |
rect_one.r = 255 | |
rect_one.g = 0 | |
rect_one.b = 0 | |
rect_two.r = 255 | |
rect_two.g = 0 | |
rect_two.b = 0 | |
end | |
# render mouse cursor | |
args.outputs.primitives << { x: args.inputs.mouse.x, | |
y: args.inputs.mouse.y, | |
w: 192, | |
h: 192, | |
anchor_x: 0.5, | |
anchor_y: 0.5, | |
path: :solid, | |
r: 0, | |
g: 0, | |
b: 0, | |
a: 32 } | |
# render rects | |
args.outputs.primitives << args.state.rects | |
end | |
$gtk.reset |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment