Advertisement
quack_rgss

Character Afterimages

Jun 15th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.59 KB | None | 0 0
  1. =begin
  2. #==============================================================================
  3.  * Character Afterimages - v1.2
  4.   Author: Quack
  5. #==============================================================================
  6. What it does:
  7. Allows you to add afterimage effects to player and events.
  8.  
  9. Call Script command set_afterimage(id, has_image, speed = 0) allows you to add
  10. or remove afterimage from a character.
  11. Explanation of the commands parameters:
  12. id: id of a character. -1=player, 0=this event, above zero=event with this id
  13. has_image: if true, adds afterimage to character, else removes it
  14. speed: the lowest movement speed at which the characters afterimage becomes visible.
  15.  
  16. Event notetags:
  17. <afterimage> - Adds afterimage to this event
  18. <afterimage_speed: X> . The lowest movement speed at which the afterimage becomes
  19. visible (0 by default, ergo always)
  20.  
  21. If you want the players afterimage to only be visible while dashing, just use
  22. this in a Call Script command:
  23. $game_player.image_only_on_dash(true)
  24.                      
  25. Instructions/Install notes:
  26. Just paste under Materials like you would most scripts.
  27.  
  28. Terms of Use:
  29. Use it however you wish, but do give credit if you do.
  30.  
  31. Credits:
  32. Victor for the Game_Event.note from his basic module for saving me time.
  33.  
  34. Bugs:
  35. None I've found so far.
  36.  
  37. History:
  38. 29/10/2016 - More features.
  39. 20/11/2012 - Added features, changed name of script to Character Afterimages
  40. 18/11/2012 - First release
  41. =end
  42.  
  43. #==============================================================================
  44. # * Settings Start
  45. #==============================================================================
  46. module QUACK
  47.   module AFTERIMAGE
  48.     DISABLE_SWITCH = 20 #if this switch is set to true no afterimages will be visible.
  49.     # Afterimage visual settings:
  50.     START_OPACITY = 240 #the amount of opacity an image starts with
  51.     OPACITY_STEP = 10 #the amount of opacity removed from an image each frame
  52.     IMAGE_SPAWN_INTERVAL = 4 #how often in frames an afterimage is spawn
  53.     PLAYER_IMAGE_ONLY_ON_DASH = true
  54.     PLAYER_HAS_IMAGE = true
  55.   end
  56. end
  57. #==============================================================================
  58. # * Settings End
  59. #==============================================================================
  60. class Spriteset_Map
  61.   alias afterimg_init initialize
  62.   def initialize
  63.     @image_sprites = []
  64.     @unused_image_sprites = []
  65.     @count = 0
  66.     afterimg_init
  67.   end
  68.  
  69.   alias afterimg_update update
  70.   def update
  71.     afterimg_update
  72.     update_character_images
  73.   end
  74.  
  75.   def update_character_images
  76.     if !@image_sprites.nil?
  77.       @image_sprites.each {|s| s.update if s.in_use? }
  78.     end
  79.    
  80.     #if !@image_sprites.nil?
  81.     #  if @image_sprites.size > 0
  82.     #    i = @image_sprites.size
  83.     #    @image_sprites.size.times {
  84.     #      i -= 1
  85.     #      if @image_sprites[i].opacity <= 0
  86.     #          @image_sprites[i].dispose
  87.     #          @image_sprites.delete_at(i)
  88.     #      end
  89.     #    }
  90.     #  end
  91.     #end
  92.    
  93.     if !$game_switches[QUACK::AFTERIMAGE::DISABLE_SWITCH]
  94.       if TMP[7001] != 0
  95.         TMP[7001].each {|c| create_image(c) if c.new_afterimg? }
  96.       end
  97.     #  @count += 1
  98.     #  if @count >= QUACK::AFTERIMAGE::IMAGE_SPAWN_INTERVAL
  99.     #    @count = 0
  100.     #    for i in $game_map.events.keys
  101.     #      create_image($game_map.events[i]) if $game_map.events[i].has_afterimage?
  102.     #    end
  103.       if $game_player.has_afterimage? && $game_player.new_afterimg?
  104.         if $game_player.afterimage_only_on_dash?
  105.           create_image($game_player) if $game_player.full_dash?#dash?
  106.         else
  107.           create_image($game_player)
  108.         end
  109.       end
  110.     #  end
  111.     end
  112.   end
  113.  
  114.   def create_image(char)
  115.     if !@image_sprites.nil?
  116.       img = @image_sprites.find {|s| !s.in_use? }
  117.     end
  118.     if !img.nil?
  119.       img.init(char)
  120.       return
  121.     end
  122.     new = Sprite_Character_Afterimage.new(@viewport1, char)
  123.     new.init(char)
  124.     @image_sprites << new
  125.   end
  126.  
  127.   alias afterimg_dispose dispose
  128.   def dispose
  129.     afterimg_dispose
  130.     dispose_image_sprites
  131.   end
  132.  
  133.   def dispose_image_sprites
  134.     @unused_image_sprites.clear
  135.     @image_sprites.each {|s| s.dispose }
  136.     @image_sprites.clear
  137.   end
  138.  
  139.  
  140. end
  141.  
  142. class Sprite_Character_Afterimage < Sprite_Character
  143.   def initialize(viewport, character = nil)
  144.     @opacityx = character.afterimage_opac
  145.     @jump_height = character.jump_height
  146.     @xx = character.real_x
  147.     @yy = character.real_y
  148.     if character.direction == 8
  149.       @y_offset = 3
  150.     else
  151.       @y_offset = 5
  152.     end
  153.     @character = character
  154.     super(viewport, character)
  155.   end
  156.  
  157.   def init(character)
  158.     @jump_height = character.jump_height
  159.     @xx = character.real_x
  160.     @yy = character.real_y
  161.     if character.afterimage_type == 1
  162.       @xx += (rand(10) - 5) * 0.02
  163.       @yy += (rand(10) - 5) * 0.02
  164.     end
  165.     if character.direction == 8
  166.       @y_offset = 3
  167.     else
  168.       @y_offset = 5
  169.     end
  170.     #super(viewport, character)
  171.     @character = character
  172.     @opacityx = character.afterimage_opac
  173.     self.opacity = @opacityx
  174.     self.tone = character.afterimage_color
  175.     self.x = $game_map.adjust_x(@xx) * 32 + 16
  176.     self.y = $game_map.adjust_y(@yy) * 32 + 32 - @y_offset - @jump_height
  177.     self.z = @character.screen_z# - 1
  178.     self.blend_type = 0
  179.     #if @blurred.nil?
  180.     #  @blurred = true
  181.     #  self.bitmap = self.bitmap.dup
  182.     #  self.bitmap.blur
  183.     #end
  184.     @visible = true
  185.     update
  186.     @u = true
  187.   end
  188.  
  189.   def no_opacity?
  190.     return @opacityx < @character.afterimage_opac_step
  191.   end
  192.   def in_use?
  193.     @visible
  194.   end
  195.  
  196.   def update
  197.     super
  198.     if @opacityx >= @character.afterimage_opac_step
  199.       @opacityx -= @character.afterimage_opac_step
  200.       self.opacity = @opacityx
  201.       @visible = false if @opacityx < 1
  202.       self.x = $game_map.adjust_x(@xx) * 32 + 16
  203.       self.y = $game_map.adjust_y(@yy) * 32 + 32 - @y_offset - @jump_height
  204.     end
  205.   end
  206.  
  207.   def update_balloon; end
  208.   def setup_new_effect; end
  209.   def update_position; end
  210.   def update_other; end
  211.    
  212.   def update_bitmap
  213.     if graphic_changed? && @u
  214.       @tile_id = @character.tile_id
  215.       @character_name = @character.character_name
  216.       @character_index = @character.character_index
  217.       if @tile_id > 0
  218.         set_tile_bitmap
  219.       else
  220.         set_character_bitmap
  221.       end
  222.     end
  223.   end
  224.    
  225.   def update_src_rect
  226.     if @tile_id == 0 && @u
  227.       index = @character.character_index
  228.       pattern = @character.pattern < 3 ? @character.pattern : 1
  229.       sx = (index % 4 * 3 + pattern) * @cw
  230.       sy = (index / 4 * 4 + (@character.direction - 2) / 2) * @ch
  231.       self.src_rect.set(sx, sy, @cw, @ch)
  232.       @u = false
  233.     end
  234.   end
  235. end
  236.  
  237. class Game_Character < Game_CharacterBase
  238.  
  239.   attr_accessor :afterimage_type
  240.   attr_accessor :afterimage_freq
  241.   attr_accessor :afterimage_opac
  242.   attr_accessor :afterimage_color
  243.   attr_accessor :afterimage_opac_step
  244.  
  245.   alias afterimg_game_character_init initialize
  246.   def initialize
  247.     afterimg_game_character_init
  248.     @afterimage = false
  249.     @afterimage_speed = 0
  250.     @afterimage_offset_x = 0
  251.     @afterimage_offset_y = -4
  252.     @afterimage_type = 0
  253.     @afterimage_freq = QUACK::AFTERIMAGE::IMAGE_SPAWN_INTERVAL
  254.     @afterimage_opac = QUACK::AFTERIMAGE::START_OPACITY
  255.     @afterimage_opac_step = QUACK::AFTERIMAGE::OPACITY_STEP
  256.     @afterimage_color = Tone.new
  257.     @afterimage_frame_count = 0
  258.     @afterimage_last_x = @real_x
  259.     @afterimage_last_y = @real_y
  260.     @afterimage_has_moved = false
  261.   end
  262.  
  263.   alias afterimg_update update
  264.   def update
  265.     afterimg_update
  266.     if @afterimage
  267.       @afterimage_has_moved = false
  268.       if @afterimage_last_x != @real_x || @afterimage_last_y != @real_y
  269.         @afterimage_has_moved = true
  270.         @afterimage_last_x = @real_x
  271.         @afterimage_last_y = @real_y
  272.       end
  273.     end
  274.   end
  275.  
  276.   def new_afterimg?
  277.     return false if !@afterimage_has_moved || @afterimage_speed > @move_speed
  278.     @afterimage_frame_count += 1
  279.     @afterimage_frame_count = 0 if @afterimage_frame_count == @afterimage_freq
  280.     return @afterimage_frame_count == 0
  281.   end
  282.  
  283.   def has_afterimage?; @afterimage && @afterimage_speed <= real_move_speed; end
  284.   def set_afterimage(val)
  285.     @afterimage = val
  286.     if @afterimage
  287.       TMP[7001] = [] if TMP[7001] == 0
  288.       TMP[7001].push(self)
  289.     else
  290.       if TMP[7001] != 0 && TMP[7001].include?(self)
  291.         TMP[7001].delete(self)
  292.       end
  293.     end
  294.     SceneManager.scene.spriteset.refresh_characters
  295.   end
  296.   def set_afterimage_speed(val); @afterimage_speed = val; end
  297.   def set_afterimage_settings(freq=nil,opac=nil,opac_step=nil,color=nil)
  298.     @afterimage_freq = freq unless freq.nil?
  299.     @afterimage_opac = opac unless opac.nil?
  300.     @afterimage_opac_step = opac_step unless opac_step.nil?
  301.     @afterimage_color = color unless color.nil?
  302.   end
  303. end
  304.  
  305. class Game_Event < Game_Character
  306. if !$imported[:ve_basic_module]
  307.   def note
  308.     return ""     if !@page || !@page.list || @page.list.size <= 0
  309.     return @notes if @notes && @page.list == @note_page
  310.     @note_page = @page.list.dup
  311.     comment_list = []
  312.     @page.list.each do |item|
  313.       next unless item && (item.code == 108 || item.code == 408)
  314.       comment_list.push(item.parameters[0])
  315.     end
  316.     @notes = comment_list.join("\r\n")
  317.     @notes
  318.   end
  319. end # if !$imported[:ve_basic_module]
  320.  
  321.   alias afterimg_setup_page_settings setup_page_settings
  322.   def setup_page_settings
  323.     afterimg_setup_page_settings
  324.     @afterimage = note =~ /<AFTERIMAGE>/i ? true : @afterimage
  325.     @afterimage_speed = note =~ /<AFTERIMAGE_SPEED: (.*)>/i ? $1.to_i : @afterimage_speed
  326.     if @afterimage
  327.       TMP[7001] = [] if TMP[7001] == 0
  328.         TMP[7001].push(self)
  329.     end
  330.   end
  331. end
  332.  
  333. class Game_Player
  334.   alias afterimg_game_player_init initialize
  335.   def initialize
  336.     afterimg_game_player_init
  337.     @afterimage_on_dash = QUACK::AFTERIMAGE::PLAYER_IMAGE_ONLY_ON_DASH
  338.     @afterimage = QUACK::AFTERIMAGE::PLAYER_HAS_IMAGE
  339.     @afterimage_color = Tone.new(-40,-20,20,50)
  340.   end
  341.  
  342.   def afterimage_only_on_dash?
  343.     @afterimage_on_dash
  344.   end
  345.  
  346.   def image_only_on_dash(val)
  347.     @afterimage_on_dash = val
  348.   end
  349. end
  350.  
  351. class Game_Interpreter
  352.   def set_afterimage(id, has_image, speed = 0)
  353.     c = get_character(id)
  354.     c.set_afterimage(has_image)
  355.     c.set_afterimage_speed(speed)
  356.   end
  357. end
  358.  
  359. class Scene_Map < Scene_Base
  360.  
  361.   #--------------------------------------------------------------------------
  362.   # public instance variables
  363.   #--------------------------------------------------------------------------
  364.   attr_accessor :spriteset
  365.  
  366. end # Scene_Map
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement