summaryrefslogtreecommitdiffhomepage
path: root/samples/08_tweening_lerping_easing_functions/04_parametric_enemy_movement
diff options
context:
space:
mode:
authorAmir Rajan <[email protected]>2020-09-22 06:27:46 -0500
committerAmir Rajan <[email protected]>2020-09-22 06:27:46 -0500
commit20d5b4057b44ffcf92478b2a8e9476ace2fdc0f5 (patch)
treeb4742e4f9acfd5400a04f314164812606a71df9f /samples/08_tweening_lerping_easing_functions/04_parametric_enemy_movement
parent5b2311900072cfff9582bb0296140cfb354cb911 (diff)
downloaddragonruby-game-toolkit-contrib-20d5b4057b44ffcf92478b2a8e9476ace2fdc0f5.tar.gz
dragonruby-game-toolkit-contrib-20d5b4057b44ffcf92478b2a8e9476ace2fdc0f5.zip
synced with 1.22
Diffstat (limited to 'samples/08_tweening_lerping_easing_functions/04_parametric_enemy_movement')
-rw-r--r--samples/08_tweening_lerping_easing_functions/04_parametric_enemy_movement/app/main.rb213
-rw-r--r--samples/08_tweening_lerping_easing_functions/04_parametric_enemy_movement/license-for-sample.txt9
-rw-r--r--samples/08_tweening_lerping_easing_functions/04_parametric_enemy_movement/replay.txt94
3 files changed, 316 insertions, 0 deletions
diff --git a/samples/08_tweening_lerping_easing_functions/04_parametric_enemy_movement/app/main.rb b/samples/08_tweening_lerping_easing_functions/04_parametric_enemy_movement/app/main.rb
new file mode 100644
index 0000000..8e73fb0
--- /dev/null
+++ b/samples/08_tweening_lerping_easing_functions/04_parametric_enemy_movement/app/main.rb
@@ -0,0 +1,213 @@
+def new_star args
+ { x: 1280.randomize(:ratio),
+ starting_y: 800,
+ distance_to_travel: 900 + 100.randomize(:ratio),
+ duration: 100.randomize(:ratio) + 60,
+ created_at: args.state.tick_count,
+ max_alpha: 128.randomize(:ratio) + 128,
+ b: 255.randomize(:ratio),
+ g: 200.randomize(:ratio),
+ w: 1.randomize(:ratio) + 1,
+ h: 1.randomize(:ratio) + 1 }
+end
+
+def new_enemy args
+ { x: 1280.randomize(:ratio),
+ starting_y: 800,
+ distance_to_travel: -900,
+ duration: 60.randomize(:ratio) + 180,
+ created_at: args.state.tick_count,
+ w: 32,
+ h: 32,
+ fire_rate: (30.randomize(:ratio) + (60 - args.state.score)).to_i }
+end
+
+def new_bullet args, starting_x, starting_y, enemy_speed
+ { x: starting_x,
+ starting_y: starting_y,
+ distance_to_travel: -900,
+ created_at: args.state.tick_count,
+ duration: 900 / (enemy_speed.abs + 2.0 + (5.0 * args.state.score.fdiv(100))).abs,
+ w: 5,
+ h: 5 }
+end
+
+def new_player_bullet args, starting_x, starting_y, player_speed
+ { x: starting_x,
+ starting_y: starting_y,
+ distance_to_travel: 900,
+ created_at: args.state.tick_count,
+ duration: 900 / (player_speed + 2.0),
+ w: 5,
+ h: 5 }
+end
+
+def defaults args
+ args.outputs.background_color = [0, 0, 0]
+ args.state.score ||= 0
+ args.state.stars ||= []
+ args.state.enemies ||= []
+ args.state.bullets ||= []
+ args.state.player_bullets ||= []
+ args.state.max_stars = 50
+ args.state.max_enemies = 10
+ args.state.player.x ||= 640
+ args.state.player.y ||= 100
+ args.state.player.w ||= 32
+ args.state.player.h ||= 32
+
+ if args.state.tick_count == 0
+ args.state.stars.clear
+ args.state.max_stars.times do
+ s = new_star args
+ s[:created_at] += s[:duration].randomize(:ratio)
+ args.state.stars << s
+ end
+ end
+
+ if args.state.tick_count == 0
+ args.state.enemies.clear
+ args.state.max_enemies.times do
+ s = new_enemy args
+ s[:created_at] += s[:duration].randomize(:ratio)
+ args.state.enemies << s
+ end
+ end
+end
+
+def input args
+ if args.inputs.keyboard.left
+ args.state.player.x -= 5
+ elsif args.inputs.keyboard.right
+ args.state.player.x += 5
+ end
+
+ if args.inputs.keyboard.up
+ args.state.player.y += 5
+ elsif args.inputs.keyboard.down
+ args.state.player.y -= 5
+ end
+
+ if args.inputs.keyboard.key_down.space
+ args.state.player_bullets << new_player_bullet(args,
+ args.state.player.x + args.state.player.w.half,
+ args.state.player.y + args.state.player.h, 5)
+ end
+
+ args.state.player.y = args.state.player.y.greater(0).lesser(720 - args.state.player.w)
+ args.state.player.x = args.state.player.x.greater(0).lesser(1280 - args.state.player.h)
+end
+
+def completed? entity
+ (entity[:created_at] + entity[:duration]).elapsed_time > 0
+end
+
+def calc_stars args
+ if (stars_to_add = args.state.max_stars - args.state.stars.length) > 0
+ stars_to_add.times { args.state.stars << new_star(args) }
+ end
+ args.state.stars = args.state.stars.reject { |s| completed? s }
+end
+
+def move_enemies args
+ if (enemies_to_add = args.state.max_enemies - args.state.enemies.length) > 0
+ enemies_to_add.times { args.state.enemies << new_enemy(args) }
+ end
+
+ args.state.enemies = args.state.enemies.reject { |s| completed? s }
+end
+
+def move_bullets args
+ args.state.enemies.each do |e|
+ if args.state.tick_count.mod_zero?(e[:fire_rate])
+ args.state.bullets << new_bullet(args, e[:x] + e[:w].half, current_y(e), e[:distance_to_travel] / e[:duration])
+ end
+ end
+
+ args.state.bullets = args.state.bullets.reject { |s| completed? s }
+ args.state.player_bullets = args.state.player_bullets.reject { |s| completed? s }
+end
+
+def intersect? entity_one, entity_two
+ entity_one.merge(y: current_y(entity_one))
+ .intersect_rect? entity_two.merge(y: current_y(entity_two))
+end
+
+def kill args
+ bullets_hitting_enemies = []
+ dead_bullets = []
+ dead_enemies = []
+
+ args.state.player_bullets.each do |b|
+ args.state.enemies.each do |e|
+ if intersect? b, e
+ dead_bullets << b
+ dead_enemies << e
+ end
+ end
+ end
+
+ args.state.score += dead_enemies.length
+
+ args.state.player_bullets.reject! { |b| dead_bullets.include? b }
+ args.state.enemies.reject! { |e| dead_enemies.include? e }
+
+ dead = args.state.bullets.any? do |b|
+ [args.state.player.x,
+ args.state.player.y,
+ args.state.player.w,
+ args.state.player.h].intersect_rect? b.merge(y: current_y(b))
+ end
+ return unless dead
+ args.gtk.reset
+ defaults args
+end
+
+def calc args
+ calc_stars args
+ move_enemies args
+ move_bullets args
+ kill args
+end
+
+def current_y entity
+ entity[:starting_y] + (entity[:distance_to_travel] * entity[:created_at].ease(entity[:duration], :identity))
+end
+
+def render args
+ args.outputs.solids << args.state.stars.map do |s|
+ [s[:x],
+ current_y(s),
+ s[:w], s[:h], 0, s[:g], s[:b], s[:max_alpha] * s[:created_at].ease(20, :identity)]
+ end
+
+ args.outputs.borders << args.state.enemies.map do |s|
+ [s[:x],
+ current_y(s),
+ s[:w], s[:h], 255, 0, 0]
+ end
+
+ args.outputs.borders << args.state.bullets.map do |b|
+ [b[:x],
+ current_y(b),
+ b[:w], b[:h], 255, 0, 0]
+ end
+
+ args.outputs.borders << args.state.player_bullets.map do |b|
+ [b[:x],
+ current_y(b),
+ b[:w], b[:h], 255, 255, 255]
+ end
+
+ args.borders << [args.state.player.x,
+ args.state.player.y,
+ args.state.player.w,
+ args.state.player.h, 255, 255, 255]
+end
+
+def tick args
+ defaults args
+ input args
+ calc args
+ render args
+end
diff --git a/samples/08_tweening_lerping_easing_functions/04_parametric_enemy_movement/license-for-sample.txt b/samples/08_tweening_lerping_easing_functions/04_parametric_enemy_movement/license-for-sample.txt
new file mode 100644
index 0000000..100dcec
--- /dev/null
+++ b/samples/08_tweening_lerping_easing_functions/04_parametric_enemy_movement/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/08_tweening_lerping_easing_functions/04_parametric_enemy_movement/replay.txt b/samples/08_tweening_lerping_easing_functions/04_parametric_enemy_movement/replay.txt
new file mode 100644
index 0000000..6580c5b
--- /dev/null
+++ b/samples/08_tweening_lerping_easing_functions/04_parametric_enemy_movement/replay.txt
@@ -0,0 +1,94 @@
+replay_version 2.0
+stopped_at 673
+seed 100
+recorded_at Sun Sep 29 22:32:05 2019
+[:key_down_raw, 1073741904, 0, 2, 1, 63]
+[:key_up_raw, 1073741904, 0, 2, 2, 85]
+[:key_down_raw, 1073741903, 0, 2, 3, 93]
+[:key_up_raw, 1073741903, 0, 2, 4, 111]
+[:key_down_raw, 1073741904, 0, 2, 5, 114]
+[:key_up_raw, 1073741904, 0, 2, 6, 136]
+[:key_down_raw, 1073741903, 0, 2, 7, 162]
+[:key_up_raw, 1073741903, 0, 2, 8, 183]
+[:key_down_raw, 32, 0, 2, 9, 184]
+[:key_up_raw, 32, 0, 2, 10, 189]
+[:key_down_raw, 32, 0, 2, 11, 194]
+[:key_up_raw, 32, 0, 2, 12, 198]
+[:key_down_raw, 32, 0, 2, 13, 202]
+[:key_down_raw, 1073741904, 0, 2, 14, 205]
+[:key_up_raw, 32, 0, 2, 15, 207]
+[:key_down_raw, 1073741904, 0, 2, 16, 229]
+[:key_down_raw, 1073741904, 0, 2, 17, 231]
+[:key_down_raw, 32, 0, 2, 18, 232]
+[:key_up_raw, 1073741904, 0, 2, 19, 235]
+[:key_up_raw, 32, 0, 2, 20, 237]
+[:key_down_raw, 1073741903, 0, 2, 21, 238]
+[:key_down_raw, 32, 0, 2, 22, 241]
+[:key_up_raw, 32, 0, 2, 23, 245]
+[:key_down_raw, 32, 0, 2, 24, 249]
+[:key_up_raw, 32, 0, 2, 25, 253]
+[:key_down_raw, 32, 0, 2, 26, 258]
+[:key_up_raw, 32, 0, 2, 27, 262]
+[:key_down_raw, 32, 0, 2, 28, 266]
+[:key_up_raw, 1073741903, 0, 2, 29, 267]
+[:key_down_raw, 1073741904, 0, 2, 30, 269]
+[:key_up_raw, 32, 0, 2, 31, 271]
+[:key_up_raw, 1073741904, 0, 2, 32, 291]
+[:key_down_raw, 1073741903, 0, 2, 33, 293]
+[:key_down_raw, 32, 0, 2, 34, 301]
+[:key_up_raw, 32, 0, 2, 35, 307]
+[:key_down_raw, 32, 0, 2, 36, 346]
+[:key_up_raw, 1073741903, 0, 2, 37, 347]
+[:key_down_raw, 1073741904, 0, 2, 38, 350]
+[:key_up_raw, 32, 0, 2, 39, 351]
+[:key_down_raw, 1073741904, 0, 2, 40, 373]
+[:key_down_raw, 1073741904, 0, 2, 41, 375]
+[:key_down_raw, 1073741904, 0, 2, 42, 377]
+[:key_down_raw, 1073741904, 0, 2, 43, 378]
+[:key_down_raw, 1073741904, 0, 2, 44, 380]
+[:key_down_raw, 1073741904, 0, 2, 45, 382]
+[:key_down_raw, 1073741904, 0, 2, 46, 384]
+[:key_down_raw, 1073741904, 0, 2, 47, 386]
+[:key_up_raw, 1073741904, 0, 2, 48, 386]
+[:key_down_raw, 32, 0, 2, 49, 387]
+[:key_up_raw, 32, 0, 2, 50, 394]
+[:key_down_raw, 1073741903, 0, 2, 51, 398]
+[:key_down_raw, 32, 0, 2, 52, 399]
+[:key_up_raw, 32, 0, 2, 53, 403]
+[:key_down_raw, 32, 0, 2, 54, 408]
+[:key_up_raw, 32, 0, 2, 55, 413]
+[:key_down_raw, 32, 0, 2, 56, 418]
+[:key_up_raw, 32, 0, 2, 57, 422]
+[:key_down_raw, 32, 0, 2, 58, 427]
+[:key_up_raw, 32, 0, 2, 59, 431]
+[:key_down_raw, 32, 0, 2, 60, 435]
+[:key_up_raw, 32, 0, 2, 61, 439]
+[:key_up_raw, 1073741903, 0, 2, 62, 440]
+[:key_down_raw, 1073741904, 0, 2, 63, 441]
+[:key_down_raw, 1073741904, 0, 2, 64, 461]
+[:key_down_raw, 1073741904, 0, 2, 65, 463]
+[:key_down_raw, 1073741904, 0, 2, 66, 465]
+[:key_down_raw, 1073741904, 0, 2, 67, 466]
+[:key_down_raw, 1073741904, 0, 2, 68, 468]
+[:key_up_raw, 1073741904, 0, 2, 69, 468]
+[:key_down_raw, 1073741904, 0, 2, 70, 483]
+[:key_down_raw, 1073741904, 0, 2, 71, 503]
+[:key_down_raw, 1073741904, 0, 2, 72, 505]
+[:key_down_raw, 1073741904, 0, 2, 73, 507]
+[:key_down_raw, 32, 0, 2, 74, 508]
+[:key_up_raw, 1073741904, 0, 2, 75, 509]
+[:key_up_raw, 32, 0, 2, 76, 512]
+[:key_down_raw, 1073741903, 0, 2, 77, 515]
+[:key_down_raw, 32, 0, 2, 78, 516]
+[:key_up_raw, 32, 0, 2, 79, 520]
+[:key_up_raw, 1073741903, 0, 2, 80, 521]
+[:key_down_raw, 32, 0, 2, 81, 524]
+[:key_up_raw, 32, 0, 2, 82, 528]
+[:key_down_raw, 32, 0, 2, 83, 532]
+[:key_up_raw, 32, 0, 2, 84, 535]
+[:key_down_raw, 32, 0, 2, 85, 539]
+[:key_up_raw, 32, 0, 2, 86, 543]
+[:key_down_raw, 32, 0, 2, 87, 551]
+[:key_up_raw, 32, 0, 2, 88, 555]
+[:key_down_raw, 1073742051, 1024, 2, 89, 656]
+[:key_down_raw, 113, 1024, 2, 90, 672]