diff options
| author | Amir Rajan <[email protected]> | 2020-09-11 02:02:01 -0500 |
|---|---|---|
| committer | Amir Rajan <[email protected]> | 2020-09-11 02:02:57 -0500 |
| commit | 33ec37b141e896b47ed642923fd33b0c658ae9fb (patch) | |
| tree | a40d3e5d41beeb06508200078f6f26b0ee57d6a4 /samples/09_performance/02_sprites_as_entities/app/main.rb | |
| parent | 958cf43779d2bf528869e80511c4c4f2a433b2db (diff) | |
| download | dragonruby-game-toolkit-contrib-33ec37b141e896b47ed642923fd33b0c658ae9fb.tar.gz dragonruby-game-toolkit-contrib-33ec37b141e896b47ed642923fd33b0c658ae9fb.zip | |
synced samples
Diffstat (limited to 'samples/09_performance/02_sprites_as_entities/app/main.rb')
| -rw-r--r-- | samples/09_performance/02_sprites_as_entities/app/main.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/samples/09_performance/02_sprites_as_entities/app/main.rb b/samples/09_performance/02_sprites_as_entities/app/main.rb new file mode 100644 index 0000000..21babda --- /dev/null +++ b/samples/09_performance/02_sprites_as_entities/app/main.rb @@ -0,0 +1,64 @@ +# Sprites represented as Entities using the queue ~args.outputs.sprites~ +# yields nicer access apis over Hashes, but require a bit more code upfront. +# The hash sample has to use star[:s] to get the speed of the star, but +# an entity can use .s instead. +def random_x args + (args.grid.w.randomize :ratio) * -1 +end + +def random_y args + (args.grid.h.randomize :ratio) * -1 +end + +def random_speed + 1 + (4.randomize :ratio) +end + +def new_star args + args.state.new_entity :star, { + x: (random_x args), + y: (random_y args), + w: 4, h: 4, + path: 'sprites/tiny-star.png', + s: random_speed + } +end + +def move_star args, star + star.x += star.s + star.y += star.s + if star.x > args.grid.w || star.y > args.grid.h + star.x = (random_x args) + star.y = (random_y args) + star.s = random_speed + end +end + +def tick args + args.state.star_count ||= 0 + + # sets console command when sample app initially opens + if Kernel.global_tick_count == 0 + 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| new_star args } + end + + # update + args.state.stars.each { |s| move_star args, s } + + # render + args.outputs.sprites << args.state.stars + 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 |
