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/04_sprites_as_strict_entities | |
| 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/04_sprites_as_strict_entities')
3 files changed, 81 insertions, 0 deletions
diff --git a/samples/09_performance/04_sprites_as_strict_entities/app/main.rb b/samples/09_performance/04_sprites_as_strict_entities/app/main.rb new file mode 100644 index 0000000..a2688c2 --- /dev/null +++ b/samples/09_performance/04_sprites_as_strict_entities/app/main.rb @@ -0,0 +1,72 @@ +# Sprites represented as StrictEntities using the queue ~args.outputs.sprites~ +# yields apis access similar to Entities, but all properties that can be set on the +# entity must be predefined with a default value. Strict entities do not support the +# addition of new properties after the fact. They are more performant than OpenEntities +# because of this constraint. +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_strict(:star, + x: (random_x args), + y: (random_y args), + w: 4, h: 4, + path: 'sprites/tiny-star.png', + s: random_speed) do |entity| + # invoke attr_sprite so that it responds to + # all properties that are required to render a sprite + entity.attr_sprite + end +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 "" + puts "" + puts "=========================================================" + puts "* INFO: Sprites, Strict Entities" + 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 diff --git a/samples/09_performance/04_sprites_as_strict_entities/license-for-sample.txt b/samples/09_performance/04_sprites_as_strict_entities/license-for-sample.txt new file mode 100644 index 0000000..100dcec --- /dev/null +++ b/samples/09_performance/04_sprites_as_strict_entities/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. diff --git a/samples/09_performance/04_sprites_as_strict_entities/sprites/tiny-star.png b/samples/09_performance/04_sprites_as_strict_entities/sprites/tiny-star.png Binary files differnew file mode 100644 index 0000000..e04786a --- /dev/null +++ b/samples/09_performance/04_sprites_as_strict_entities/sprites/tiny-star.png |
