diff options
| author | _Tradam <[email protected]> | 2021-12-16 19:22:26 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-12-16 19:22:26 -0500 |
| commit | 5954b9beb4d4a3b4f248d72d1851195f030558a8 (patch) | |
| tree | fecd8aa840a25afdb502915b0fdb4d03b7ed339a /samples/09_performance/08_collision_limits | |
| parent | 2f845281f133849256b57bb08fd3e9ae57600784 (diff) | |
| parent | eaa29e72939f5edf61735ccbb73c36ee89369f65 (diff) | |
| download | dragonruby-game-toolkit-contrib-master.tar.gz dragonruby-game-toolkit-contrib-master.zip | |
Diffstat (limited to 'samples/09_performance/08_collision_limits')
| -rw-r--r-- | samples/09_performance/08_collision_limits/app/main.rb | 55 | ||||
| -rw-r--r-- | samples/09_performance/08_collision_limits/license-for-sample.txt | 9 |
2 files changed, 64 insertions, 0 deletions
diff --git a/samples/09_performance/08_collision_limits/app/main.rb b/samples/09_performance/08_collision_limits/app/main.rb new file mode 100644 index 0000000..01ad308 --- /dev/null +++ b/samples/09_performance/08_collision_limits/app/main.rb @@ -0,0 +1,55 @@ +=begin + + Reminders: + - find_all: Finds all elements of a collection that meet certain requirements. + In this sample app, we're finding all bodies that intersect with the center body. + + - args.outputs.solids: An array. The values generate a solid. + The parameters are [X, Y, WIDTH, HEIGHT, RED, GREEN, BLUE] + For more information about solids, go to mygame/documentation/03-solids-and-borders.md. + + - args.outputs.labels: An array. The values generate a label. + The parameters are [X, Y, TEXT, SIZE, ALIGNMENT, RED, GREEN, BLUE, ALPHA, FONT STYLE] + For more information about labels, go to mygame/documentation/02-labels.md. + + - ARRAY#intersect_rect?: Returns true or false depending on if two rectangles intersect. + +=end + +# This code demonstrates moving objects that loop around once they exceed the scope of the screen, +# which has dimensions of 1280 by 720, and also detects collisions between objects called "bodies". + +def body_count num + $gtk.args.state.other_bodies = num.map { [1280 * rand, 720 * rand, 10, 10] } # other_bodies set using num collection +end + +def tick args + + # Center body's values are set using an array + # Map is used to set values of 2000 other bodies + # All bodies that intersect with center body are stored in collisions collection + args.state.center_body ||= [640 - 100, 360 - 100, 200, 200] # calculations done to place body in center + args.state.other_bodies ||= 2000.map { [1280 * rand, 720 * rand, 10, 10] } # 2000 bodies given random position on screen + + # finds all bodies that intersect with center body, stores them in collisions + collisions = args.state.other_bodies.find_all { |b| b.intersect_rect? args.state.center_body } + + args.borders << args.state.center_body # outputs center body as a black border + + # transparency changes based on number of collisions; the more collisions, the redder (more transparent) the box becomes + args.solids << [args.state.center_body, 255, 0, 0, collisions.length * 5] # center body is red solid + args.solids << args.state.other_bodies # other bodies are output as (black) solids, as well + + args.labels << [10, 30, args.gtk.current_framerate] # outputs frame rate in bottom left corner + + # Bodies are returned to bottom left corner if positions exceed scope of screen + args.state.other_bodies.each do |b| # for each body in the other_bodies collection + b.x += 5 # x and y are both incremented by 5 + b.y += 5 + b.x = 0 if b.x > 1280 # x becomes 0 if star exceeds scope of screen (goes too far right) + b.y = 0 if b.y > 720 # y becomes 0 if star exceeds scope of screen (goes too far up) + end +end + +# Resets the game. +$gtk.reset diff --git a/samples/09_performance/08_collision_limits/license-for-sample.txt b/samples/09_performance/08_collision_limits/license-for-sample.txt new file mode 100644 index 0000000..100dcec --- /dev/null +++ b/samples/09_performance/08_collision_limits/license-for-sample.txt @@ -0,0 +1,9 @@ +Copyright 2019 DragonRuby LLC + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
