EstartgameS

Particle Engine

Nov 26th, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 15.46 KB | None | 0 0
  1. =begin
  2.  
  3.  Particle Engine
  4.  by arevulopapo
  5.  port by PK8
  6.  Created: 11/15/2007
  7.  Modified: 5/9/2012 - 5/12/2012
  8.  Ported: 5/10/2012 (VX), 5/12/2012 (Ace)
  9.  ──────────────────────────────────────────────────────────────────────────────
  10.  ■ Introduction
  11.    This script lets you create particle effects in your game. Particles are
  12.    integrated into "Spriteset_Map", so the can be displayed over/under an event.
  13.  ──────────────────────────────────────────────────────────────────────────────
  14.  ■ Methods Aliased
  15.    Spriteset_Map.initialize
  16.    Spriteset_Map.update
  17.    Spriteset_Map.dispose
  18.  ──────────────────────────────────────────────────────────────────────────────
  19.  ■ Thanks
  20.    mobychan for helping out and pointing me to Archeia's RPG Maker VX's Ace
  21.    tips and tricks page, which talked about how events set to parallel process
  22.    need to be put in a loop in order for them to keep animating while events
  23.    are being interacted with.
  24.  ──────────────────────────────────────────────────────────────────────────────
  25.  ■ Usage
  26.    Effects are called from "Script" command like this:
  27.      particle_effect(EVENT_ID, EFFECT, LOCK, X, Y)
  28.        EVENT_ID  - ID of an event the particles will flow from.
  29.                     -7/"follower3" for Follower 3
  30.                     -6/"follower2" for Follower 2
  31.                     -5/"follower1" for Follower 1
  32.                     -4/"airship" for Airship.
  33.                     -3/"ship" for Ship.
  34.                     -2/"boat" for Boat.
  35.                     -1/"player" for Player.
  36.                      1 and above for Events.
  37.                      * Use @event_id for "This Event"
  38.                      * Use an array to specify multiple IDs.
  39.        EFFECT    - name of the effect to call. Names are defined in the
  40.                    Spriteset_Map. (Find: case effect)
  41.                      * Use an array to specify multiple effects.
  42.        LOCK      - alignment of the particles. 'event' to align particles
  43.                    with event's map position, 'screen' to align with event's
  44.                    screen position.
  45.                    For static events, like fireplaces, teleports, etc.
  46.                     'event' alignment is recommended.
  47.                    For moving events use 'screen' mode.
  48.        X, Y      - number of pixels that will be added to the event's position
  49.                    to determine the starting point of particles.
  50.                    That's your most powerful weapon. See the demo for examples.
  51.                     * Use an array to specify multiple X/Y coordinates.
  52.  ──────────────────────────────────────────────────────────────────────────────
  53.  ■ Port Modifications
  54.    o Added shortcuts within the script calls. You can now use arrays in
  55.      EVENT_ID, EFFECT, X, and Y to set more events, effects, and coordinates.
  56.    o Users can now set particles onto vehicles by setting EVENT_ID to "boat",
  57.      "ship", and "airship".
  58.                    
  59. =end
  60.  
  61. #==============================================================================
  62. # ** Spriteset_Map
  63. #------------------------------------------------------------------------------
  64. #  This class brings together map screen sprites, tilemaps, etc. It's used
  65. # within the Scene_Map class.
  66. #==============================================================================
  67.  
  68. class Spriteset_Map
  69.   #--------------------------------------------------------------------------
  70.   # * Add Effect
  71.   #--------------------------------------------------------------------------
  72.   def add_effect(event=1, effect='', lock='event', x=0, y=0)
  73.     # Case: Event
  74.     event = event.downcase.gsub(/\s+/, "") if event.is_a?(String)
  75.     case event
  76.     when -20, "follower16"; object = $game_player.followers[15]
  77.     when -19, "follower15"; object = $game_player.followers[14]
  78.     when -18, "follower14"; object = $game_player.followers[13]
  79.     when -17, "follower13"; object = $game_player.followers[12]
  80.     when -16, "follower12"; object = $game_player.followers[11]
  81.     when -15, "follower11"; object = $game_player.followers[10]
  82.     when -14, "follower10"; object = $game_player.followers[9]
  83.     when -13, "follower9";  object = $game_player.followers[8]
  84.     when -12, "follower8";  object = $game_player.followers[7]
  85.     when -11, "follower7";  object = $game_player.followers[6]
  86.     when -10, "follower6";  object = $game_player.followers[5]
  87.     when -9,  "follower5";  object = $game_player.followers[4]
  88.     when -8,  "follower4";  object = $game_player.followers[3]
  89.     when -7,  "follower3";  object = $game_player.followers[2]
  90.     when -6,  "follower2";  object = $game_player.followers[1]
  91.     when -5,  "follower1";  object = $game_player.followers[0]
  92.     when -4,  "airship";    object = $game_map.airship
  93.     when -3,  "ship";       object = $game_map.ship
  94.     when -2,  "boat";       object = $game_map.boat
  95.     when -1,  "player";     object = $game_player
  96.     else
  97.       if event.is_a?(Integer); object = $game_map.events[event]
  98.       else; object = event
  99.       end
  100.     end
  101.     object = nil if object.is_a?(Game_Follower) and $game_player.vehicle != nil
  102.    
  103.     #------------------------------------------------------------------------
  104.     # Configuration
  105.     #------------------------------------------------------------------------
  106.     case effect
  107.     # (sprite, acceleration[x,y], gravity[x,y], opacity[base,loss], blending)
  108.     when 'blue'
  109.       sprite='star_blue'
  110.       add_particles(object, x, y, sprite, [1.00*(-15+rand(30))/10, 1.00*(-15+rand(30))/10], [0,0], [160,5+rand(15)], lock, 1)
  111.     when 'red'
  112.       sprite='star_red'
  113.       add_particles(object, x, y, sprite, [1.00*(-15+rand(30))/10, 1.00*(-15+rand(30))/10], [0,0], [160,5+rand(15)], lock, 1)
  114.     when 'green'
  115.       sprite='star_green'
  116.       add_particles(object, x, y, sprite, [1.00*(-15+rand(30))/10, 1.00*(-15+rand(30))/10], [0,0], [160,5+rand(15)], lock, 1)
  117.     when 'yellow'
  118.       sprite='star_yellow'
  119.       add_particles(object, x, y, sprite, [1.00*(-15+rand(30))/10, 1.00*(-15+rand(30))/10], [0,0], [160,5+rand(15)], lock, 1)
  120.     when 'smash'
  121.       sprite='smash'
  122.       add_particles(object, x, y, sprite, [1.00*(-15+rand(30))/10, 1.00*(-15+rand(30))/10], [0,0], [160,5+rand(15)], lock, 1)
  123.     when 'fire'
  124.       sprite='particle_yellow'
  125.       add_particles(object, x, y, sprite, [(rand(7)-3)*0.2, 0], [0,0.15], [255,8+rand(5)], lock, 1)
  126.     when 'fire2'
  127.       sprite='particle_orange'
  128.       add_particles(object, x, y, sprite, [(rand(7)-3)*0.2, 0], [0,0.15], [255,8+rand(5)], lock, 1)
  129.     when 'sparks'
  130.       sprite='particle_red'
  131.       add_particles(object, x, y, sprite, [0.5*(-25+rand(50))/10, -4], [0,-0.5], [255,20], lock, 1)
  132.     when 'smoke'
  133.       sprite='smoke'
  134.       add_particles(object, x, y, sprite, [0.1*(-25+rand(50))/10, 0], [0,0.13], [128,3], lock, 1)
  135.     when 'cells'
  136.       sprite='particle_red'
  137.       dx = 1.00*(-100 + rand(200))/100
  138.       dy = 1.00*(-100 + rand(200))/100
  139.       add_particles(object, x, y, sprite, [5*dx, 5*dy], [0.3*dx,0.3*dy],
  140.       [255,10], lock, 1)
  141.       #sines
  142.     when 'black'
  143.       sprite='star_sine'
  144.       add_particles(object, x, y, sprite, [(rand(7)-3)*0.2, 0], [0,0.15],
  145.       [255,8+rand(5)], lock, 1)
  146.       #sines
  147.     when 'portal'
  148.       sprite='portal'
  149.       add_particles(object, x, y, sprite, [0,0.2], [0,0.1], [255,1+rand(10)], lock, 1)
  150.     when 'blackportal'
  151.       sprite='blackportal'
  152.       add_particles(object, x, y, sprite, [0,0.2], [0,0.1], [255,1+rand(10)], lock, 1)
  153.     end
  154.   end
  155.   #--------------------------------------------------------------------------
  156.   # * Public Instance Variables
  157.   #--------------------------------------------------------------------------
  158.   attr_reader :particles
  159.   #--------------------------------------------------------------------------
  160.   # * Alias Listings
  161.   #--------------------------------------------------------------------------
  162.   alias :particle_ssm_init :initialize
  163.   alias :particle_ssm_update :update
  164.   alias :particle_ssm_dispose :dispose
  165.   #--------------------------------------------------------------------------
  166.   # * Object Initialization
  167.   #--------------------------------------------------------------------------
  168.   def initialize
  169.     @particles = []
  170.     particle_ssm_init
  171.   end
  172.   #--------------------------------------------------------------------------
  173.   # * Dispose
  174.   #--------------------------------------------------------------------------
  175.   def dispose
  176.     @particles.each{ |d| d.dispose }
  177.     particle_ssm_dispose
  178.   end
  179.   #--------------------------------------------------------------------------
  180.   # * Frame Update
  181.   #--------------------------------------------------------------------------
  182.   def update
  183.     @particles.each_with_index{ |p,i|
  184.       @particles[i].update
  185.       if p.opacity == 0
  186.         p.dispose
  187.         @particles.delete_at(i)
  188.       end
  189.       }
  190.     particle_ssm_update
  191.   end
  192.   #--------------------------------------------------------------------------
  193.   # * Add Particles
  194.   #--------------------------------------------------------------------------
  195.   def add_particles(object = $game_player, x = 0, y = 0, sprite = '',
  196.     acc = [0,0], grav = [0,0], opacity = [255,0], lock = 'event', blend = 0)
  197.     if !object.is_a?(NilClass)
  198.       if lock == 'event'
  199.         @particles << Particle_Event.new(@viewport1, object, x, y, sprite, acc,
  200.           grav, opacity, blend)
  201.       elsif lock == 'screen'
  202.         @particles << Particle_Screen.new(@viewport1, object, x, y, sprite, acc,
  203.           grav, opacity, blend)
  204.       end
  205.     end
  206.   end
  207. end
  208.  
  209. #===============================================================================​
  210. # ** Particle_Screen
  211. #===============================================================================​
  212.  
  213. class Particle_Screen < Sprite
  214.   #--------------------------------------------------------------------------
  215.   # * Object Initialization
  216.   #--------------------------------------------------------------------------
  217.   def initialize(viewport = Viewport.new(0,0,800,600), object = $game_player,
  218.     x = 0, y = 0, sprite = '', acc = [0,0], grav = [0,0], opacity = [255,3],
  219.     blend = 0)
  220.     if !object.is_a?(NilClass)
  221.       super(viewport)
  222.       self.bitmap = Cache.picture('Particles/' + sprite)
  223.       self.x = object.screen_x + x
  224.       self.y = object.screen_y - 16 + y
  225.       self.ox = self.oy = self.bitmap.width/2
  226.       self.blend_type = blend
  227.       self.opacity = opacity[0]
  228.       @object = object
  229.       @origin = [self.x, self.y]
  230.       @acceleration = acc
  231.       @gravity = grav
  232.       @coords = [0.00, 0.00]
  233.       @opacity = opacity[1]
  234.       update
  235.     end
  236.   end
  237.   #--------------------------------------------------------------------------
  238.   # * Frame Update
  239.   #--------------------------------------------------------------------------
  240.   def update
  241.     if !@object.is_a?(NilClass)
  242.       @acceleration[0] -= @gravity[0] if @gravity[0] != 0
  243.       @acceleration[1] -= @gravity[1] if @gravity[1] != 0
  244.       @coords[0] += @acceleration[0]
  245.       @coords[1] += @acceleration[1]
  246.       self.opacity -= @opacity
  247.       self.x = @origin[0] + @coords[0]
  248.       self.y = @origin[1] + @coords[1]
  249.       if self.y > (@object.screen_y - 16)
  250.         self.z = @object.screen_z + 32
  251.       else
  252.         if @object == $game_map.airship; self.z = @object.screen_z + 120
  253.         elsif @object.is_a?(Game_Follower); self.z = @object.screen_z + 1
  254.         else
  255.           self.z = @object.screen_z - 32
  256.         end
  257.       end
  258.     end
  259.   end
  260. end
  261.  
  262. #===============================================================================​
  263. # ** Particle_Event
  264. #===============================================================================​
  265.  
  266. class Particle_Event < Sprite
  267.   #--------------------------------------------------------------------------
  268.   # * Object Initialization
  269.   #--------------------------------------------------------------------------
  270.   def initialize(viewport = Viewport.new(0,0,800,600), object = $game_player,
  271.     x = 0, y = 0, sprite = '', acc = [0,0], grav = [0,0], opacity = [255,3],
  272.     blend = 0)
  273.     if !object.is_a?(NilClass)
  274.       super(viewport)
  275.       self.bitmap = Cache.picture('Particles/' + sprite)
  276.       self.x = object.x*32 + 16 - $game_map.display_x * 32 + x
  277.       self.y = object.y*32 + 32 - $game_map.display_y * 32 + y
  278.       self.ox = self.oy = self.bitmap.width/2
  279.       self.blend_type = blend
  280.       self.opacity = opacity[0]
  281.       @object = object
  282.       @origin = [object.x*32 + x + 16, object.y*32 + y + 32]
  283.       @acceleration = acc
  284.       @gravity = grav
  285.       @coords = [0.00, 0.00]
  286.       @opacity = opacity[1]
  287.       update
  288.     end
  289.   end
  290.   #--------------------------------------------------------------------------
  291.   # * Frame Update
  292.   #--------------------------------------------------------------------------
  293.   def update
  294.     if !@object.is_a?(NilClass)
  295.       @acceleration[0] -= @gravity[0] if @gravity[0] != 0
  296.       @acceleration[1] -= @gravity[1] if @gravity[1] != 0
  297.       @coords[0] += @acceleration[0]
  298.       @coords[1] += @acceleration[1]
  299.       self.opacity -= @opacity
  300.       self.x = @origin[0] + @coords[0] - $game_map.display_x * 32
  301.       self.y = @origin[1] + @coords[1] - $game_map.display_y * 32 - 16
  302.       if self.y > (@object.screen_y - 16)
  303.         self.z = @object.screen_z + 40
  304.       else
  305.         self.z = @object.screen_z - 40
  306.       end
  307.     end
  308.   end
  309. end
  310.  
  311. #==============================================================================
  312. # ** Scene_Map
  313. #------------------------------------------------------------------------------
  314. #  This class performs the map screen processing.
  315. #==============================================================================
  316.  
  317. class Scene_Map < Scene_Base
  318.   #--------------------------------------------------------------------------
  319.   # * Public Instance Variables
  320.   #--------------------------------------------------------------------------
  321.   attr_reader :spriteset
  322. end
  323.  
  324. #==============================================================================
  325. # ** Game_Interpreter
  326. #------------------------------------------------------------------------------
  327. #  An interpreter for executing event commands. This class is used within the
  328. # Game_Map, Game_Troop, and Game_Event classes.
  329. #==============================================================================
  330.  
  331. class Game_Interpreter
  332.   #--------------------------------------------------------------------------
  333.   # * Add effect
  334.   #--------------------------------------------------------------------------
  335.   def particle_effect(event=@event_id, effect='', lock='event', x=0, y=0)
  336.     event, effect, x, y = Array(event), Array(effect), Array(x), Array(y)
  337.     event.each{ |event_i| effect.each{ |effect_i| x.each{ |x_i| y.each{ |y_i|
  338.       SceneManager.scene.spriteset.add_effect(event_i, effect_i, lock, x_i, y_i)
  339.     } } } }
  340.   end
  341. end
Add Comment
Please, Sign In to add comment