|
| 1 | +# ****************************************************************************************** |
| 2 | +# |
| 3 | +# raylib [textures] example - sprite explosion |
| 4 | +# |
| 5 | +# Example originally created with raylib 2.5, last time updated with raylib 3.5 |
| 6 | +# |
| 7 | +# Example ported to Ruby by Wilson Silva (@wilsonsilva). Works with Raylib 4.5 |
| 8 | +# |
| 9 | +# Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, |
| 10 | +# BSD-like license that allows static linking with closed source software |
| 11 | +# |
| 12 | +# Copyright (c) 2019-2023 Anata and Ramon Santamaria (@raysan5) |
| 13 | +# |
| 14 | +# ****************************************************************************************** |
| 15 | + |
| 16 | +require 'bundler/setup' |
| 17 | +require 'raylib' |
| 18 | + |
| 19 | +# Initialization |
| 20 | +# -------------------------------------------------------------------------------------- |
| 21 | +SCREEN_WIDTH = 800 |
| 22 | +SCREEN_HEIGHT = 450 |
| 23 | + |
| 24 | +Raylib.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'raylib [textures] example - sprite explosion') |
| 25 | + |
| 26 | +NUM_FRAMES_PER_LINE = 5 |
| 27 | +NUM_LINES = 5 |
| 28 | + |
| 29 | +Raylib.init_audio_device |
| 30 | + |
| 31 | +# Load explosion sound |
| 32 | +fx_boom = Raylib.load_sound(File.join(__dir__, 'resources/boom.wav')) |
| 33 | + |
| 34 | +# Load explosion texture |
| 35 | +explosion = Raylib.load_texture(File.join(__dir__, 'resources/explosion.png')) |
| 36 | + |
| 37 | +# Init variables for animation |
| 38 | +frame_width = explosion.width / NUM_FRAMES_PER_LINE.to_f |
| 39 | +frame_height = explosion.height / NUM_LINES.to_f |
| 40 | +current_frame = 0 |
| 41 | +current_line = 0 |
| 42 | + |
| 43 | +frame_rec = Raylib::Rectangle.create(0, 0, frame_width, frame_height) # Sprite one frame rectangle width |
| 44 | +position = Raylib::Vector2.create(0.0, 0.0) # Sprite one frame rectangle height |
| 45 | + |
| 46 | +active = false |
| 47 | +frames_counter = 0 |
| 48 | + |
| 49 | +Raylib.set_target_fps(120) |
| 50 | +# -------------------------------------------------------------------------------------- |
| 51 | + |
| 52 | +# Main game loop |
| 53 | +until Raylib.window_should_close # Detect window close button or ESC key |
| 54 | + # Update |
| 55 | + # ---------------------------------------------------------------------------------- |
| 56 | + |
| 57 | + # Check for mouse button pressed and activate explosion (if not active) |
| 58 | + if Raylib.is_mouse_button_pressed(Raylib::MOUSE_BUTTON_LEFT) && !active |
| 59 | + position = Raylib.get_mouse_position |
| 60 | + active = true |
| 61 | + |
| 62 | + position.x -= frame_width / 2.0 |
| 63 | + position.y -= frame_height / 2.0 |
| 64 | + |
| 65 | + Raylib.play_sound(fx_boom) |
| 66 | + end |
| 67 | + |
| 68 | + # Compute explosion animation frames |
| 69 | + if active |
| 70 | + frames_counter += 1 |
| 71 | + |
| 72 | + if frames_counter > 2 |
| 73 | + current_frame += 1 |
| 74 | + |
| 75 | + if current_frame >= NUM_FRAMES_PER_LINE |
| 76 | + current_frame = 0 |
| 77 | + current_line += 1 |
| 78 | + |
| 79 | + if current_line >= NUM_LINES |
| 80 | + current_line = 0 |
| 81 | + active = false |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + frames_counter = 0 |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + frame_rec.x = frame_width * current_frame |
| 90 | + frame_rec.y = frame_height * current_line |
| 91 | + # ---------------------------------------------------------------------------------- |
| 92 | + |
| 93 | + # Draw |
| 94 | + # ---------------------------------------------------------------------------------- |
| 95 | + Raylib.begin_drawing |
| 96 | + Raylib.clear_background(Raylib::RAYWHITE) |
| 97 | + |
| 98 | + # Draw explosion required frame rectangle |
| 99 | + Raylib.draw_texture_rec(explosion, frame_rec, position, Raylib::WHITE) if active |
| 100 | + |
| 101 | + Raylib.end_drawing |
| 102 | + # ---------------------------------------------------------------------------------- |
| 103 | +end |
| 104 | + |
| 105 | +# De-Initialization |
| 106 | +# -------------------------------------------------------------------------------------- |
| 107 | +Raylib.unload_texture(explosion) # Unload texture |
| 108 | +Raylib.unload_sound(fx_boom) # Unload sound |
| 109 | + |
| 110 | +Raylib.close_audio_device |
| 111 | +Raylib.close_window |
| 112 | +# -------------------------------------------------------------------------------------- |
0 commit comments