Advertisement
mikb89

[VX] Some Popup v2.9

Jun 17th, 2012
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 16.40 KB | None | 0 0
  1. # Some Popup v 2.9
  2. # VX version
  3. # by mikb89
  4.  
  5. # Details:
  6. #  Show some popup text when facing an event.
  7. #  Text can be placed in center of screen, over the player or over the event.
  8. #  Write [pop] in a comment to assign the popup to an event and write in the
  9. #   next comment the text that will be shown.
  10. # NOTE: [pop] is by default, but you can change it in the settings.
  11. #  Text can be also grayed, to indicate a non-available something. To do so,
  12. #   write [npop] instead of [pop].
  13. #  You can also show a picture instead of text. In order to do this, the first
  14. #   comment must be [ppop] and the second will contain the name of the Picture.
  15. #  It is possible to play a sound/music effect for each event. Just write in a
  16. #   third comment these lines:
  17. #    SE (or ME or BGM or BGS)
  18. #    Audio filename
  19. #    Volume (1-100)
  20. #    Pitch (50-150)
  21. #    You can omit the two last lines, and they will be set by default:
  22. #    Volume 80 for BGS and SE, 100 for BGM and ME
  23. #    Pitch 100
  24. # ATTENTION: comments have to be the first two (or three) commands of the event.
  25. #
  26. #  You can also call a script with:
  27. #    $game_player.remove_town_sprite
  28. #   in it to remove the sprite. For example if you put the sprite on an event
  29. #   which you'll speak, with this code you can remove the popup.
  30.  
  31. # Configurations:
  32. module SPOP
  33.   ID = "pop" # set "loc" for old version compatibility.
  34.    # What you have to write on a event to be identified as popup one.
  35.    # If the value here is for example "pop" you'll have to write:
  36.    #  - [pop] for the common text popup;
  37.    #  - [npop] for the grayed out popup;
  38.    #  - [ppop] for the picture popup.
  39.   AUTO_REMOVE_AT_TRANSFER = true
  40.    # Test to understand what I mean.
  41.    #  true - gives the same effect as the one in Chrono Trigger towns.
  42.    #  false - let the popup be visible after the teleport. Will fade out at the
  43.    #          first player movement.
  44.   GRAYED_COLOR = Color.new(255,245,255,175)
  45.    # Value of grey color. Red, green, blue, alpha. From 0 to 255.
  46.   WALK_8_DIR = true
  47.    # You don't have to include the 8dir script. Just set true this.
  48.   POPUP_TRANSITION = 9
  49.    # The effect of the popup appearing/disappearing.
  50.    #  0: no effect
  51.    #  1: fading in/out
  52.    #  2: movement up/down
  53.    #  3: movement & fading
  54.    #  4: reduced movement
  55.    #  5: reduced movement & fading
  56.    #  6: zoom in/out
  57.    #  7: zoom & fading
  58.    #  8: zoom & movement
  59.    #  9: zoom, movement, fading
  60.    # 10: zoom & reduced movement
  61.    # 11: zoom, reduced movement, fading
  62.   POPUP_SOUND = ["SE", "Book", 80, 100]
  63.    # Play something on popup.
  64.    # 4 parameters:
  65.    #  1. Sound kind ("SE", "ME", "BGS", "BGM");
  66.    #  2. Name of the file;
  67.    #  3. Volume (0-100);
  68.    #  4. Pitch (50-150 (or 15-453 if you want MAXIMUM values)).
  69.    # To deactivate sound just set "" the 2. or set 0 to 3. Examples:
  70.    #  POPUP_SOUND = ["SE", "", 80, 100]
  71.    #  POPUP_SOUND = ["SE", "Book", 0, 100]
  72.    # Won't be played.
  73.    # Eventual BGM or BGS playing will fade as the graphic fade/zoom/move and
  74.    #  will start after the popup close. Obviously not valid if using SE/ME.
  75.  
  76.    # Examples with ME, BGM, BGS:
  77.    #  POPUP_SOUND = ["ME", "Item", 100, 100]
  78.    #  POPUP_SOUND = ["BGM", "Town1", 100, 100]
  79.    #  POPUP_SOUND = ["BGS", "Clock", 100, 100]
  80.   POPUP_BINDING = 2
  81.    # Where the popup should be binded.
  82.    #  0: center of the screen
  83.    #  1: over the player
  84.    #  2: over the event
  85.   DISTANCE_MIN = 1
  86.    # The minimum distance at which popup will be visible.
  87.    # With 0, you will get popups only by touch.
  88.    # 1 is default, like it was in older versions.
  89.    # For larger number, only the closer event will popup, even if there are
  90.    #  others in the same line.
  91. end
  92.  
  93. # Others:
  94. #  You'll see 'town' everywhere in the script. This is because of the SECOND
  95. #   name given to this script: "Popup town name".
  96. #  The FIRST original name was "Location system", from this the [loc] to add in
  97. #   event comments. By the way I never publicated the version with this name, so
  98. #   you won't find anything.
  99.  
  100. #Codename: spop
  101.  
  102. ($imported ||= {})[:mikb89_spop] = true
  103.  
  104. # License:
  105. # - You can ask me to include support for other scripts as long as these scripts
  106. #   use the $imported[script] = true;
  107. # - You can modify and even repost my scripts, after having received a response
  108. #   by me. For reposting it, anyway, you must have done heavy edit or porting,
  109. #   you can't do a post with the script as is;
  110. # - You can use my scripts for whatever you want, from free to open to
  111. #   commercial games. I'd appreciate by the way if you let me know about what
  112. #   you're doing;
  113. # - You must credit me, if you use this script or part of it.
  114.  
  115. class Game_Player < Game_Character
  116.   #--------------------------------------------------------------------------
  117.   # * Object Initialization
  118.   #--------------------------------------------------------------------------
  119.   alias_method(:initGP_b4_spop, :initialize) unless method_defined?(:initGP_b4_spop)
  120. #class Game_Player#def initialize() <- aliased
  121.   def initialize
  122.     initGP_b4_spop
  123.     @town_sprite = nil
  124.     @town_text = ""
  125.     reset_audio
  126.     @town_ex_audio = nil
  127.     @sync_event = nil
  128.   end
  129.   #--------------------------------------------------------------------------
  130.   # * Frame Update
  131.   #--------------------------------------------------------------------------
  132.   alias_method(:updateGP_b4_spop, :update) unless method_defined?(:updateGP_b4_spop)
  133. #class Game_Player#def update() <- aliased
  134.   def update
  135.     updateGP_b4_spop
  136.     if @town_sprite != nil
  137.       case SPOP::POPUP_BINDING
  138.       when 1
  139.         @town_sprite.x = screen_x
  140.         if @town_sprite.y != screen_y && screen_y != @sync_y
  141.           @town_sprite.y = screen_y - (@town_sprite.y - @sync_y).abs
  142.           @sync_y = screen_y
  143.         end
  144.       when 2
  145.         if @sync_event != nil
  146.           @town_sprite.x = @sync_event.screen_x
  147.           if @town_sprite.y != @sync_event.screen_y && @sync_event.screen_y != @sync_y
  148.             @town_sprite.y = @sync_event.screen_y - (@town_sprite.y - @sync_y).abs
  149.             @sync_y = @sync_event.screen_y
  150.           end
  151.           remove_town_sprite if Math.hypot(@sync_event.distance_x_from_player, @sync_event.distance_y_from_player) > 2 + SPOP::DISTANCE_MIN
  152.         end
  153.       end
  154.       rem = ["gameover", "title", "battle"].include?($game_temp.next_scene)
  155.       @town_sprite.update
  156.       if [1,3,5,7,9,11].include?(SPOP::POPUP_TRANSITION)
  157.         @town_sprite.opacity -= 15 if @town_sprite.z == 5 && @town_sprite.opacity > 0
  158.         @town_sprite.opacity += 15 if @town_sprite.z == 10 && @town_sprite.opacity < 255
  159.         rem = true if @town_sprite.opacity <= 0
  160.       end
  161.       if [2,3,4,5,8,9,10,11].include?(SPOP::POPUP_TRANSITION)
  162.         mov = [4,5,10,11].include?(SPOP::POPUP_TRANSITION) ? 32 : 64
  163.         val = mov/16
  164.         t = @town_sprite.y
  165.         @town_sprite.y += val if @town_sprite.z == 5 && @toadd > -mov
  166.         @town_sprite.y -= val if @town_sprite.z == 10 && @toadd > 0
  167.         @toadd -= val if t != @town_sprite.y
  168.         rem = true if @toadd <= -mov
  169.       end
  170.       if [6,7,8,9,10,11].include?(SPOP::POPUP_TRANSITION)
  171.         if @town_sprite.z == 5 && @town_sprite.zoom_x > 0
  172.           @town_sprite.zoom_x -= 0.25
  173.           @town_sprite.zoom_y -= 0.25
  174.         end
  175.         if @town_sprite.z == 10 && @town_sprite.zoom_x < 1
  176.           @town_sprite.zoom_x += 0.25
  177.           @town_sprite.zoom_y += 0.25
  178.         end
  179.         rem = true if @town_sprite.zoom_x <= 0
  180.       end
  181.       if @town_ex_audio != nil
  182.         if @audiowait > 0
  183.           @audiowait -= 1
  184.         elsif @audiowait == 0
  185.           if @town_audio != nil
  186.             @town_audio.play
  187.             if @town_ex_audio.class != @town_audio.class
  188.               @town_ex_audio.play
  189.               @town_ex_audio = nil
  190.             end
  191.             reset_audio if @town_audio.name != SPOP::POPUP_SOUND[1]
  192.           end
  193.           @audiowait = -1
  194.         end
  195.       end
  196.       remove_town_sprite(true) if rem
  197.     end
  198.   end
  199.   #--------------------------------------------------------------------------
  200.   # * Removing of town sprite when changing map
  201.   #--------------------------------------------------------------------------
  202.   alias_method(:perform_transfer_b4_spop, :perform_transfer) unless method_defined?(:perform_transfer_b4_spop)
  203. #class Game_Player#def perform_transfer() <- aliased
  204.   def perform_transfer
  205.     remove_town_sprite(true, false) if SPOP::AUTO_REMOVE_AT_TRANSFER
  206.     perform_transfer_b4_spop
  207.   end
  208.   #--------------------------------------------------------------------------
  209.   # * Processing of Movement via input from the Directional Buttons
  210.   #--------------------------------------------------------------------------
  211. #class Game_Player#def move_by_input() <- rewritten
  212.   def move_by_input
  213.     return unless movable?
  214.     return if $game_map.interpreter.running?
  215.     x, y = self.x, self.y
  216.     case SPOP::WALK_8_DIR ? Input.dir8 : Input.dir4
  217.     when 1
  218.       move_lower_left
  219.       if @move_failed
  220.         check_town(x-1, y+1)
  221.       else
  222.         SPOP::DISTANCE_MIN.times {|t| break if check_town(x-2-t, y+2+t)}
  223.       end
  224.     when 2
  225.       move_down
  226.       if @move_failed
  227.         check_town(x, y+1)
  228.       else
  229.         SPOP::DISTANCE_MIN.times {|t| break if check_town(x, y+2+t)}
  230.       end
  231.     when 3
  232.       move_lower_right
  233.       if @move_failed
  234.         check_town(x+1, y+1)
  235.       else
  236.         SPOP::DISTANCE_MIN.times {|t| break if check_town(x+2+t, y+2+t)}
  237.       end
  238.     when 4
  239.       move_left
  240.       if @move_failed
  241.         check_town(x-1, y)
  242.       else
  243.         SPOP::DISTANCE_MIN.times {|t| break if check_town(x-2-t, y)}
  244.       end
  245.     when 6
  246.       move_right
  247.       if @move_failed
  248.         check_town(x+1, y)
  249.       else
  250.         SPOP::DISTANCE_MIN.times {|t| break if check_town(x+2+t, y)}
  251.       end
  252.     when 7
  253.       move_upper_left
  254.       if @move_failed
  255.         check_town(x-1, y-1)
  256.       else
  257.         SPOP::DISTANCE_MIN.times {|t| break if check_town(x-2-t, y-2-t)}
  258.       end
  259.     when 8
  260.       move_up
  261.       if @move_failed
  262.         check_town(x, y-1)
  263.       else
  264.         SPOP::DISTANCE_MIN.times {|t| break if check_town(x, y-2-t)}
  265.       end
  266.     when 9
  267.       move_upper_right
  268.       if @move_failed
  269.         check_town(x+1, y-1)
  270.       else
  271.         SPOP::DISTANCE_MIN.times {|t| break if check_town(x+2+t, y-2-t)}
  272.       end
  273.     end
  274.   end
  275.   #--------------------------------------------------------------------------
  276.   # * Operations for sprite removal and audio stopping
  277.   #--------------------------------------------------------------------------
  278. #class Game_Player#def remove_town_sprite(instant, audio)
  279.   def remove_town_sprite(instant=false, audio=true)
  280.     if @town_sprite != nil
  281.       if instant || SPOP::POPUP_TRANSITION == 0
  282.         if audio
  283.           @town_audio.class.stop if @town_audio != nil
  284.           @town_ex_audio.play if @town_ex_audio != nil
  285.         end
  286.         @town_ex_audio = nil
  287.         @town_sprite.dispose
  288.         @town_sprite = nil
  289.         @sync_event = nil
  290.       else
  291.         @town_sprite.z = 5
  292.         unless @town_audio.is_a?(RPG::SE)
  293.           @town_audio.class.fade(4) if @town_audio != nil
  294.         end
  295.       end
  296.     end
  297.   end
  298.   #--------------------------------------------------------------------------
  299.   # * Set the audio as the one specified in SPOP or passed
  300.   #--------------------------------------------------------------------------
  301. #class Game_Player#def reset_audio(spn)
  302.   def reset_audio(spn = SPOP::POPUP_SOUND)
  303.     @town_audio = (spn[1] == "" ||
  304.                   spn[2] <= 0) ? nil :
  305.                     case spn[0]
  306.                     when "BGM"; RPG::BGM.new(spn[1], spn[2], spn[3])
  307.                     when "BGS"; RPG::BGS.new(spn[1], spn[2], spn[3])
  308.                     when "ME"; RPG::ME.new(spn[1], spn[2], spn[3])
  309.                     when "SE"; RPG::SE.new(spn[1], spn[2], spn[3])
  310.                     end
  311.   end
  312.   #--------------------------------------------------------------------------
  313.   # * Check if there is a town event in front of the player
  314.   #--------------------------------------------------------------------------
  315. #class Game_Player#def check_town(x, y)
  316.   def check_town(x, y)
  317.     return false if $game_map.interpreter.running?
  318.     result = false
  319.     for event in $game_map.events_xy(x, y)
  320.       unless [1,2].include?(event.trigger) and event.priority_type == 1
  321.         if event.list != nil
  322.           if event.list[0].code == 108 and
  323.             ["[#{SPOP::ID}]", "[n#{SPOP::ID}]", "[p#{SPOP::ID}]"].include?(event.list[0].parameters[0])
  324.             result = true
  325.             next if @town_sprite != nil && @town_sprite.z == 10 && @town_text == event.list[1].parameters[0]
  326.             remove_town_sprite(true)
  327.             @town_sprite = Sprite.new
  328.             @town_sprite.z = 10
  329.             if [6,7,8,9,10,11].include?(SPOP::POPUP_TRANSITION)
  330.               @town_sprite.zoom_x = @town_sprite.zoom_y = 0.0
  331.             end
  332.             @town_sprite.opacity = 15 if [1,3,5,7,9,11].include?(SPOP::POPUP_TRANSITION)
  333.             if event.list[0].parameters[0] != "[p#{SPOP::ID}]"
  334.               @town_sprite.bitmap ||= Bitmap.new(1,1)
  335.               siz = @town_sprite.bitmap.text_size(event.list[1].parameters[0])
  336.               h = siz.height
  337.               s = siz.width
  338.               @town_sprite.bitmap.dispose
  339.               @town_sprite.bitmap = Bitmap.new(s, 24)
  340.               if event.list[0].parameters[0] == "[n#{SPOP::ID}]"
  341.                 ex = @town_sprite.bitmap.font.color
  342.                 @town_sprite.bitmap.font.color = SPOP::GRAYED_COLOR
  343.               end
  344.               @town_sprite.bitmap.draw_text(0,2,s,22,event.list[1].parameters[0],1)
  345.               @town_sprite.bitmap.font.color = ex if event.list[0].parameters[0] == "[n#{SPOP::ID}]"
  346.             else
  347.               @town_sprite.bitmap = Cache.picture(event.list[1].parameters[0])
  348.               s = @town_sprite.bitmap.width
  349.               h = @town_sprite.bitmap.height
  350.             end
  351.             @town_text = event.list[1].parameters[0]
  352.             @town_sprite.ox = s/2
  353.             @town_sprite.oy = h/2
  354.             case SPOP::POPUP_BINDING
  355.             when 1
  356.               @town_sprite.x = screen_x#*32+16
  357.               @town_sprite.y = @sync_y = screen_y#*32+16
  358.             when 2
  359.               @town_sprite.x = event.screen_x#*32+16
  360.               @town_sprite.y = @sync_y = event.screen_y#*32+16
  361.               @sync_event = event
  362.             else
  363.               @town_sprite.x = 544/2# - s/2
  364.               @town_sprite.y = 416/2# - h/2
  365.             end
  366.             @town_sprite.y -= 64 if [0,1,6,7].include?(SPOP::POPUP_TRANSITION)
  367.             @town_sprite.y -= 32 if [4,5,10,11].include?(SPOP::POPUP_TRANSITION)
  368.             @toadd = [2,3,4,5,8,9,10,11].include?(SPOP::POPUP_TRANSITION) ? 64 : 0
  369.             @toadd -= 32 if [4,5,10,11].include?(SPOP::POPUP_TRANSITION)
  370.             if @town_audio != nil || event.list[2].code == 108
  371.               if ["BGM", "ME", "BGS", "SE"].include?(event.list[2].parameters[0]) &&
  372.                 event.list[3].code == 408
  373.                 arr = []
  374.                 arr.push(event.list[2].parameters[0])
  375.                 arr.push(event.list[3].parameters[0])
  376.                 if event.list[4].code == 408
  377.                   arr.push(event.list[4].parameters[0].to_i)
  378.                   arr.push(event.list[5].parameters[0].to_i) if event.list[5].code == 408
  379.                 else
  380.                   arr.push(["BGS", "SE"].include?(event.list[2].parameters[0]) ? 80 : 100)
  381.                 end
  382.                 arr.push(100) if arr.size < 4
  383.                 reset_audio(arr)
  384.               end
  385.               @town_ex_audio = @town_audio.class.last if [RPG::BGM, RPG::BGS].include?(@town_audio.class)
  386.               if @town_ex_audio != nil
  387.                 @town_ex_audio.class.fade(4)
  388.                 @audiowait = 4
  389.               else
  390.                 @town_audio.play
  391.                 reset_audio if arr != nil
  392.               end
  393.             end
  394.           end
  395.         end
  396.       end
  397.     end
  398.     remove_town_sprite unless result
  399.     return result
  400.   end
  401. end
  402.  
  403. #--Compatibility for Auto Save & Quick Save systems.---#
  404. #  -Remove the sprite because a Sprite can't be saved. #
  405. #------------------------------------------------------#
  406. class Scene_File
  407.   alias_method(:write_save_data_b4_spop, :write_save_data) unless method_defined?(:write_save_data_b4_spop)
  408. #class Scene_File#def write_save_data(file) <- aliased
  409.   def write_save_data(file)
  410.     $game_player.remove_town_sprite(true)
  411.     write_save_data_b4_spop(file)
  412.   end
  413. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement