diff options
| author | Amir Rajan <[email protected]> | 2020-09-22 06:27:46 -0500 |
|---|---|---|
| committer | Amir Rajan <[email protected]> | 2020-09-22 06:27:46 -0500 |
| commit | 20d5b4057b44ffcf92478b2a8e9476ace2fdc0f5 (patch) | |
| tree | b4742e4f9acfd5400a04f314164812606a71df9f /samples/03_rendering_sprites/01_animation_using_separate_pngs | |
| parent | 5b2311900072cfff9582bb0296140cfb354cb911 (diff) | |
| download | dragonruby-game-toolkit-contrib-20d5b4057b44ffcf92478b2a8e9476ace2fdc0f5.tar.gz dragonruby-game-toolkit-contrib-20d5b4057b44ffcf92478b2a8e9476ace2fdc0f5.zip | |
synced with 1.22
Diffstat (limited to 'samples/03_rendering_sprites/01_animation_using_separate_pngs')
| -rw-r--r-- | samples/03_rendering_sprites/01_animation_using_separate_pngs/app/main.rb | 131 | ||||
| -rw-r--r-- | samples/03_rendering_sprites/01_animation_using_separate_pngs/license-for-sample.txt | 9 | ||||
| -rw-r--r-- | samples/03_rendering_sprites/01_animation_using_separate_pngs/replay.txt | 162 | ||||
| -rw-r--r-- | samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_0.png | bin | 0 -> 12896 bytes | |||
| -rw-r--r-- | samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_1.png | bin | 0 -> 2964 bytes | |||
| -rw-r--r-- | samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_2.png | bin | 0 -> 3047 bytes | |||
| -rw-r--r-- | samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_3.png | bin | 0 -> 2655 bytes | |||
| -rw-r--r-- | samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_4.png | bin | 0 -> 2725 bytes | |||
| -rw-r--r-- | samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_5.png | bin | 0 -> 2655 bytes |
9 files changed, 302 insertions, 0 deletions
diff --git a/samples/03_rendering_sprites/01_animation_using_separate_pngs/app/main.rb b/samples/03_rendering_sprites/01_animation_using_separate_pngs/app/main.rb new file mode 100644 index 0000000..80c40f2 --- /dev/null +++ b/samples/03_rendering_sprites/01_animation_using_separate_pngs/app/main.rb @@ -0,0 +1,131 @@ +=begin + + Reminders: + + - String interpolation: Uses #{} syntax; everything between the #{ and the } is evaluated + as Ruby code, and the placeholder is replaced with its corresponding value or result. + + In this sample app, we're using string interpolation to iterate through images in the + sprites folder using their image path names. + + - args.outputs.sprites: An array. Values in this array generate sprites on the screen. + The parameters are [X, Y, WIDTH, HEIGHT, IMAGE PATH] + For more information about sprites, go to mygame/documentation/05-sprites.md. + + - args.outputs.labels: An array. Values in the array generate labels on the screen. + 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. + + - args.inputs.keyboard.key_down.KEY: Determines if a key is in the down state, or pressed. + Stores the frame that key was pressed on. + For more information about the keyboard, go to mygame/documentation/06-keyboard.md. + +=end + +# This sample app demonstrates how sprite animations work. +# There are two sprites that animate forever and one sprite +# that *only* animates when you press the "f" key on the keyboard. + +# This is the entry point to your game. The `tick` method +# executes at 60 frames per second. There are two methods +# in this tick "entry point": `looping_animation`, and the +# second method is `one_time_animation`. +def tick args + looping_animation args + one_time_animation args +end + +# This function shows how to animate a sprite that loops forever. +def looping_animation args + # Here we define a few local variables that will be sent + # into the magic function that gives us the correct sprite image + # over time. There are four things we need in order to figure + # out which sprite to show. + + # 1. When to start the animation. + start_looping_at = 0 + + # 2. The number of pngs that represent the full animation. + number_of_sprites = 6 + + # 3. How long to show each png. + number_of_frames_to_show_each_sprite = 4 + + # 4. Whether the animation should loop once, or forever. + does_sprite_loop = true + + # With the variables defined above, we can get a number + # which represents the sprite to show by calling the `frame_index` function. + # In this case the number will be between 0, and 5 (you can see the sprites + # in the ./sprites directory). + sprite_index = start_looping_at.frame_index number_of_sprites, + number_of_frames_to_show_each_sprite, + does_sprite_loop + + # Now that we have `sprite_index, we can present the correct file. + args.outputs.sprites << [100, 100, 100, 100, "sprites/dragon_fly_#{sprite_index}.png"] + + # Try changing the numbers below to see how the animation changes: + args.outputs.sprites << [100, 200, 100, 100, "sprites/dragon_fly_#{0.frame_index 6, 4, true}.png"] +end + +# This function shows how to animate a sprite that executes +# only once when the "f" key is pressed. +def one_time_animation args + # This is just a label the shows instructions within the game. + args.outputs.labels << [220, 350, "(press f to animate)"] + + # If "f" is pressed on the keyboard... + if args.inputs.keyboard.key_down.f + # Print the frame that "f" was pressed on. + puts "Hello from main.rb! The \"f\" key was in the down state on frame: #{args.inputs.keyboard.key_down.f}" + + # And MOST IMPORTANTLY set the point it time to start the animation, + # equal to "now" which is represented as args.state.tick_count. + + # Also IMPORTANT, you'll notice that the value of when to start looping + # is stored in `args.state`. This construct's values are retained across + # executions of the `tick` method. + args.state.start_looping_at = args.state.tick_count + end + + # These are the same local variables that were defined + # for the `looping_animation` function. + number_of_sprites = 6 + number_of_frames_to_show_each_sprite = 4 + + # Except this sprite does not loop again. If the animation time has passed, + # then the frame_index function returns nil. + does_sprite_loop = false + + sprite_index = args.state + .start_looping_at + .frame_index number_of_sprites, + number_of_frames_to_show_each_sprite, + does_sprite_loop + + # This line sets the frame index to zero, if + # the animation duration has passed (frame_index returned nil). + + # Remeber: we are not looping forever here. + sprite_index ||= 0 + + # Present the sprite. + args.outputs.sprites << [100, 300, 100, 100, "sprites/dragon_fly_#{sprite_index}.png"] + + tick_instructions args, "Sample app shows how to use Numeric#frame_index and string interpolation to animate a sprite over time." +end + +def tick_instructions args, text, y = 715 + return if args.state.key_event_occurred + if args.inputs.mouse.click || + args.inputs.keyboard.directional_vector || + args.inputs.keyboard.key_down.enter || + args.inputs.keyboard.key_down.escape + args.state.key_event_occurred = true + end + + args.outputs.debug << [0, y - 50, 1280, 60].solid + args.outputs.debug << [640, y, text, 1, 1, 255, 255, 255].label + args.outputs.debug << [640, y - 25, "(click to dismiss instructions)" , -2, 1, 255, 255, 255].label +end diff --git a/samples/03_rendering_sprites/01_animation_using_separate_pngs/license-for-sample.txt b/samples/03_rendering_sprites/01_animation_using_separate_pngs/license-for-sample.txt new file mode 100644 index 0000000..8fa4d42 --- /dev/null +++ b/samples/03_rendering_sprites/01_animation_using_separate_pngs/license-for-sample.txt @@ -0,0 +1,9 @@ +Copyright 2019 DragonRuby LLC (code), Nick Culbertson @mobypixel (art) + +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/03_rendering_sprites/01_animation_using_separate_pngs/replay.txt b/samples/03_rendering_sprites/01_animation_using_separate_pngs/replay.txt new file mode 100644 index 0000000..33c47c3 --- /dev/null +++ b/samples/03_rendering_sprites/01_animation_using_separate_pngs/replay.txt @@ -0,0 +1,162 @@ +replay_version 2.0 +stopped_at 780 +seed 100 +recorded_at Sun Sep 29 21:41:16 2019 +[:mouse_move, 1081, 144, 2, 1, 54] +[:mouse_move, 1076, 144, 2, 2, 55] +[:mouse_move, 1061, 145, 2, 3, 56] +[:mouse_move, 1049, 146, 2, 4, 57] +[:mouse_move, 964, 148, 2, 5, 58] +[:mouse_move, 899, 148, 2, 6, 59] +[:mouse_move, 795, 153, 2, 7, 60] +[:mouse_move, 728, 160, 2, 8, 61] +[:mouse_move, 644, 175, 2, 9, 62] +[:mouse_move, 597, 185, 2, 10, 63] +[:mouse_move, 519, 206, 2, 11, 64] +[:mouse_move, 488, 217, 2, 12, 65] +[:mouse_move, 451, 229, 2, 13, 66] +[:mouse_move, 443, 231, 2, 14, 67] +[:mouse_move, 438, 233, 2, 15, 68] +[:mouse_move, 423, 238, 2, 16, 69] +[:mouse_move, 420, 239, 2, 17, 70] +[:mouse_move, 416, 240, 2, 18, 71] +[:mouse_move, 415, 240, 2, 19, 84] +[:mouse_move, 394, 253, 2, 20, 85] +[:mouse_move, 376, 264, 2, 21, 86] +[:mouse_move, 329, 296, 2, 22, 87] +[:mouse_move, 303, 315, 2, 23, 88] +[:mouse_move, 268, 342, 2, 24, 89] +[:mouse_move, 249, 356, 2, 25, 90] +[:mouse_move, 234, 372, 2, 26, 91] +[:mouse_move, 228, 380, 2, 27, 92] +[:mouse_move, 223, 388, 2, 28, 93] +[:mouse_move, 222, 391, 2, 29, 94] +[:mouse_move, 221, 394, 2, 30, 95] +[:mouse_move, 221, 396, 2, 31, 96] +[:mouse_move, 223, 397, 2, 32, 97] +[:mouse_move, 229, 397, 2, 33, 98] +[:mouse_move, 231, 397, 2, 34, 99] +[:mouse_move, 235, 397, 2, 35, 100] +[:mouse_move, 237, 397, 2, 36, 101] +[:mouse_move, 238, 397, 2, 37, 102] +[:mouse_move, 238, 398, 2, 38, 105] +[:mouse_move, 237, 398, 2, 39, 108] +[:mouse_move, 236, 397, 2, 40, 110] +[:mouse_move, 235, 397, 2, 41, 111] +[:mouse_move, 237, 397, 2, 42, 139] +[:mouse_move, 240, 397, 2, 43, 140] +[:mouse_move, 245, 398, 2, 44, 141] +[:mouse_move, 248, 398, 2, 45, 142] +[:mouse_move, 255, 399, 2, 46, 143] +[:mouse_move, 258, 399, 2, 47, 144] +[:mouse_move, 266, 400, 2, 48, 145] +[:mouse_move, 268, 400, 2, 49, 146] +[:mouse_move, 273, 400, 2, 50, 147] +[:mouse_move, 275, 400, 2, 51, 148] +[:mouse_move, 277, 400, 2, 52, 149] +[:mouse_move, 280, 400, 2, 53, 150] +[:mouse_move, 282, 400, 2, 54, 151] +[:mouse_move, 285, 400, 2, 55, 152] +[:mouse_move, 286, 399, 2, 56, 153] +[:mouse_move, 288, 399, 2, 57, 154] +[:mouse_move, 289, 399, 2, 58, 155] +[:mouse_move, 290, 399, 2, 59, 156] +[:mouse_move, 291, 399, 2, 60, 157] +[:mouse_move, 294, 399, 2, 61, 158] +[:mouse_move, 295, 399, 2, 62, 159] +[:mouse_move, 297, 399, 2, 63, 160] +[:mouse_move, 298, 399, 2, 64, 161] +[:mouse_move, 300, 398, 2, 65, 162] +[:mouse_move, 302, 398, 2, 66, 163] +[:mouse_move, 305, 398, 2, 67, 164] +[:mouse_move, 307, 398, 2, 68, 165] +[:mouse_move, 310, 398, 2, 69, 166] +[:mouse_move, 312, 398, 2, 70, 167] +[:mouse_move, 315, 397, 2, 71, 168] +[:mouse_move, 317, 397, 2, 72, 169] +[:mouse_move, 320, 397, 2, 73, 170] +[:mouse_move, 321, 397, 2, 74, 171] +[:mouse_move, 323, 397, 2, 75, 172] +[:mouse_move, 325, 397, 2, 76, 173] +[:mouse_move, 327, 397, 2, 77, 174] +[:mouse_move, 328, 397, 2, 78, 175] +[:mouse_move, 329, 397, 2, 79, 176] +[:mouse_move, 332, 397, 2, 80, 177] +[:mouse_move, 333, 397, 2, 81, 178] +[:mouse_move, 336, 397, 2, 82, 179] +[:mouse_move, 338, 397, 2, 83, 180] +[:mouse_move, 341, 397, 2, 84, 181] +[:mouse_move, 342, 397, 2, 85, 182] +[:mouse_move, 345, 397, 2, 86, 183] +[:mouse_move, 346, 397, 2, 87, 184] +[:mouse_move, 350, 397, 2, 88, 185] +[:mouse_move, 352, 397, 2, 89, 186] +[:mouse_move, 355, 397, 2, 90, 187] +[:mouse_move, 356, 397, 2, 91, 188] +[:mouse_move, 359, 397, 2, 92, 189] +[:mouse_move, 360, 397, 2, 93, 190] +[:mouse_move, 363, 397, 2, 94, 191] +[:mouse_move, 365, 397, 2, 95, 192] +[:mouse_move, 367, 397, 2, 96, 193] +[:mouse_move, 369, 397, 2, 97, 194] +[:mouse_move, 372, 397, 2, 98, 195] +[:mouse_move, 373, 397, 2, 99, 196] +[:mouse_move, 376, 397, 2, 100, 197] +[:mouse_move, 377, 397, 2, 101, 198] +[:mouse_move, 379, 397, 2, 102, 199] +[:mouse_move, 381, 397, 2, 103, 200] +[:mouse_move, 383, 397, 2, 104, 201] +[:mouse_move, 385, 397, 2, 105, 202] +[:mouse_move, 386, 397, 2, 106, 203] +[:mouse_move, 389, 397, 2, 107, 204] +[:mouse_move, 391, 397, 2, 108, 205] +[:mouse_move, 397, 397, 2, 109, 206] +[:mouse_move, 398, 397, 2, 110, 207] +[:mouse_move, 403, 397, 2, 111, 208] +[:mouse_move, 405, 397, 2, 112, 209] +[:mouse_move, 407, 397, 2, 113, 210] +[:mouse_move, 409, 397, 2, 114, 211] +[:mouse_move, 410, 397, 2, 115, 212] +[:mouse_move, 411, 397, 2, 116, 218] +[:mouse_move, 411, 395, 2, 117, 328] +[:mouse_move, 411, 393, 2, 118, 329] +[:mouse_move, 412, 387, 2, 119, 330] +[:mouse_move, 413, 382, 2, 120, 331] +[:mouse_move, 415, 371, 2, 121, 332] +[:mouse_move, 416, 364, 2, 122, 333] +[:mouse_move, 417, 349, 2, 123, 334] +[:mouse_move, 418, 343, 2, 124, 335] +[:mouse_move, 419, 335, 2, 125, 336] +[:mouse_move, 419, 332, 2, 126, 337] +[:mouse_move, 420, 328, 2, 127, 338] +[:mouse_move, 420, 327, 2, 128, 339] +[:mouse_move, 420, 329, 2, 129, 345] +[:mouse_move, 421, 331, 2, 130, 346] +[:mouse_move, 421, 332, 2, 131, 347] +[:mouse_move, 421, 333, 2, 132, 348] +[:mouse_move, 422, 334, 2, 133, 349] +[:key_down_raw, 102, 0, 2, 134, 411] +[:key_up_raw, 102, 0, 2, 135, 417] +[:key_down_raw, 102, 0, 2, 136, 470] +[:key_up_raw, 102, 0, 2, 137, 476] +[:key_down_raw, 102, 0, 2, 138, 526] +[:key_up_raw, 102, 0, 2, 139, 531] +[:key_down_raw, 102, 0, 2, 140, 537] +[:key_up_raw, 102, 0, 2, 141, 541] +[:key_down_raw, 102, 0, 2, 142, 546] +[:key_up_raw, 102, 0, 2, 143, 550] +[:key_down_raw, 102, 0, 2, 144, 555] +[:key_up_raw, 102, 0, 2, 145, 558] +[:key_down_raw, 102, 0, 2, 146, 563] +[:key_up_raw, 102, 0, 2, 147, 567] +[:key_down_raw, 102, 0, 2, 148, 572] +[:key_up_raw, 102, 0, 2, 149, 575] +[:key_down_raw, 102, 0, 2, 150, 579] +[:key_up_raw, 102, 0, 2, 151, 582] +[:key_down_raw, 102, 0, 2, 152, 632] +[:key_up_raw, 102, 0, 2, 153, 657] +[:key_down_raw, 102, 0, 2, 154, 687] +[:key_up_raw, 102, 0, 2, 155, 692] +[:key_down_raw, 1073742051, 1024, 2, 156, 779] +[:key_down_raw, 113, 1024, 2, 157, 779] +[:key_up_raw, 113, 1024, 2, 158, 779] diff --git a/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_0.png b/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_0.png Binary files differnew file mode 100644 index 0000000..fb179af --- /dev/null +++ b/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_0.png diff --git a/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_1.png b/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_1.png Binary files differnew file mode 100644 index 0000000..8cfe531 --- /dev/null +++ b/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_1.png diff --git a/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_2.png b/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_2.png Binary files differnew file mode 100644 index 0000000..cb462e1 --- /dev/null +++ b/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_2.png diff --git a/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_3.png b/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_3.png Binary files differnew file mode 100644 index 0000000..04c4977 --- /dev/null +++ b/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_3.png diff --git a/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_4.png b/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_4.png Binary files differnew file mode 100644 index 0000000..b29fa3d --- /dev/null +++ b/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_4.png diff --git a/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_5.png b/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_5.png Binary files differnew file mode 100644 index 0000000..99f4e74 --- /dev/null +++ b/samples/03_rendering_sprites/01_animation_using_separate_pngs/sprites/dragon_fly_5.png |
