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/06_static_sprites_as_classes/app/main.rb | |
| 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/06_static_sprites_as_classes/app/main.rb')
| -rw-r--r-- | samples/09_performance/06_static_sprites_as_classes/app/main.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/samples/09_performance/06_static_sprites_as_classes/app/main.rb b/samples/09_performance/06_static_sprites_as_classes/app/main.rb new file mode 100644 index 0000000..db5bf8e --- /dev/null +++ b/samples/09_performance/06_static_sprites_as_classes/app/main.rb @@ -0,0 +1,56 @@ +# Sprites represented as Classes using the queue ~args.outputs.static_sprites~. +# bypasses the queue behavior of ~args.outputs.sprites~. All instances are held +# by reference. You get better performance, but you are mutating state of held objects +# which is less functional/data oriented. +class Star + attr_sprite + + def initialize grid + @grid = grid + @x = (rand @grid.w) * -1 + @y = (rand @grid.h) * -1 + @w = 4 + @h = 4 + @s = 1 + (4.randomize :ratio) + @path = 'sprites/tiny-star.png' + end + + def move + @x += @s + @y += @s + @x = (rand @grid.w) * -1 if @x > @grid.right + @y = (rand @grid.h) * -1 if @y > @grid.top + end +end + +# calls methods needed for game to run properly +def tick args + # sets console command when sample app initially opens + if Kernel.global_tick_count == 0 + puts "" + puts "" + puts "=========================================================" + puts "* INFO: Static Sprites, Classes" + puts "* INFO: Please specify the number of sprites to render." + args.gtk.console.set_command "reset_with count: 100" + end + + # init + if args.state.tick_count == 0 + args.state.stars = args.state.star_count.map { |i| Star.new args.grid } + args.outputs.static_sprites << args.state.stars + end + + # update + args.state.stars.each(&:move) + + # render + args.outputs.background_color = [0, 0, 0] + args.outputs.primitives << args.gtk.current_framerate_primitives +end + +# resets game, and assigns star count given by user +def reset_with count: count + $gtk.reset + $gtk.args.state.star_count = count +end |
