Advertisement
mikb89

[XP] Some Popup v2.8

Jun 17th, 2012
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 19.35 KB | None | 0 0
  1. # Some Popup v 2.8
  2. # XP 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", "046-Book01", 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", "046-Book01", 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", "010-Item01", 100, 100]
  78.    #  POPUP_SOUND = ["BGM", "023-Town01", 100, 100]
  79.    #  POPUP_SOUND = ["BGS", "016-Drips01", 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. end
  86.  
  87. # Others:
  88. #  You'll see 'town' everywhere in the script. This is because of the SECOND
  89. #   name given to this script: "Popup town name".
  90. #  The FIRST original name was "Location system", from this the [loc] to add in
  91. #   event comments. By the way I never publicated the version with this name, so
  92. #   you won't find anything.
  93.  
  94. #Codename: spop
  95.  
  96. ($imported ||= {})[:mikb89_spop] = true
  97.  
  98. # License:
  99. # - You can ask me to include support for other scripts as long as these scripts
  100. #   use the $imported[script] = true;
  101. # - You can modify and even repost my scripts, after having received a response
  102. #   by me. For reposting it, anyway, you must have done heavy edit or porting,
  103. #   you can't do a post with the script as is;
  104. # - You can use my scripts for whatever you want, from free to open to
  105. #   commercial games. I'd appreciate by the way if you let me know about what
  106. #   you're doing;
  107. # - You must credit me, if you use this script or part of it.
  108.  
  109. class Game_Player < Game_Character
  110.   #--------------------------------------------------------------------------
  111.   # * Object Initialization
  112.   #--------------------------------------------------------------------------
  113. #class Game_Player#def initialize()
  114.   def initialize
  115.     super
  116.     @town_sprite = nil
  117.     @town_text = ""
  118.     reset_audio
  119.     @town_ex_audio = [nil, ""]
  120.     @sync_event = nil
  121.   end
  122.   #--------------------------------------------------------------------------
  123.   # * Frame Update
  124.   #--------------------------------------------------------------------------
  125. #class Game_Player#def update() <- rewritten
  126.   def update
  127.     # Remember whether or not moving in local variables
  128.     last_moving = moving?
  129.     # If moving, event running, move route forcing, and message window
  130.     # display are all not occurring
  131.     unless moving? or $game_system.map_interpreter.running? or
  132.            @move_route_forcing or $game_temp.message_window_showing
  133.       # Move player in the direction the directional button is being pressed
  134.       move_by_input # Edited from the original
  135.     end
  136.     # Remember coordinates in local variables
  137.     last_real_x = @real_x
  138.     last_real_y = @real_y
  139.     super
  140.     # Some Popup addition START
  141.     update_town_sprite
  142.     # Some Popup addition END
  143.     # If character moves down and is positioned lower than the center
  144.     # of the screen
  145.     if @real_y > last_real_y and @real_y - $game_map.display_y > CENTER_Y
  146.       # Scroll map down
  147.       $game_map.scroll_down(@real_y - last_real_y)
  148.     end
  149.     # If character moves left and is positioned more let on-screen than
  150.     # center
  151.     if @real_x < last_real_x and @real_x - $game_map.display_x < CENTER_X
  152.       # Scroll map left
  153.       $game_map.scroll_left(last_real_x - @real_x)
  154.     end
  155.     # If character moves right and is positioned more right on-screen than
  156.     # center
  157.     if @real_x > last_real_x and @real_x - $game_map.display_x > CENTER_X
  158.       # Scroll map right
  159.       $game_map.scroll_right(@real_x - last_real_x)
  160.     end
  161.     # If character moves up and is positioned higher than the center
  162.     # of the screen
  163.     if @real_y < last_real_y and @real_y - $game_map.display_y < CENTER_Y
  164.       # Scroll map up
  165.       $game_map.scroll_up(last_real_y - @real_y)
  166.     end
  167.     # If not moving
  168.     unless moving?
  169.       # If player was moving last time
  170.       if last_moving
  171.         # Event determinant is via touch of same position event
  172.         result = check_event_trigger_here([1,2])
  173.         # If event which started does not exist
  174.         if result == false
  175.           # Disregard if debug mode is ON and ctrl key was pressed
  176.           unless $DEBUG and Input.press?(Input::CTRL)
  177.             # Encounter countdown
  178.             if @encounter_count > 0
  179.               @encounter_count -= 1
  180.             end
  181.           end
  182.         end
  183.       end
  184.       # If C button was pressed
  185.       if Input.trigger?(Input::C)
  186.         # Same position and front event determinant
  187.         check_event_trigger_here([0])
  188.         check_event_trigger_there([0,1,2])
  189.       end
  190.     end
  191.   end
  192.   #--------------------------------------------------------------------------
  193.   # * Part of the update
  194.   #--------------------------------------------------------------------------
  195. #class Game_Player#def update_town_sprite()
  196.   def update_town_sprite
  197.     if @town_sprite != nil
  198.       case SPOP::POPUP_BINDING
  199.       when 1
  200.         @town_sprite.x = screen_x
  201.         if @town_sprite.y != screen_y && screen_y != @sync_y
  202.           @town_sprite.y = screen_y - (@town_sprite.y - @sync_y).abs
  203.           @sync_y = screen_y
  204.         end
  205.       when 2
  206.         if @sync_event != nil
  207.           @town_sprite.x = @sync_event.screen_x
  208.           if @town_sprite.y != @sync_event.screen_y && @sync_event.screen_y != @sync_y
  209.             @town_sprite.y = @sync_event.screen_y - (@town_sprite.y - @sync_y).abs
  210.             @sync_y = @sync_event.screen_y
  211.           end
  212.           sx = @sync_event.x - @x
  213.           sy = @sync_event.y - @y
  214.           remove_town_sprite if Math.hypot(sx, sy) > 2
  215.         end
  216.       end
  217.       rem = false
  218.       @town_sprite.update
  219.       if [1,3,5,7,9,11].include?(SPOP::POPUP_TRANSITION)
  220.         @town_sprite.opacity -= 15 if @town_sprite.z == 5 && @town_sprite.opacity > 0
  221.         @town_sprite.opacity += 15 if @town_sprite.z == 10 && @town_sprite.opacity < 255
  222.         rem = true if @town_sprite.opacity <= 0
  223.       end
  224.       if [2,3,4,5,8,9,10,11].include?(SPOP::POPUP_TRANSITION)
  225.         mov = [4,5,10,11].include?(SPOP::POPUP_TRANSITION) ? 32 : 64
  226.         val = mov/16
  227.         t = @town_sprite.y
  228.         @town_sprite.y += val if @town_sprite.z == 5 && @toadd > -mov
  229.         @town_sprite.y -= val if @town_sprite.z == 10 && @toadd > 0
  230.         @toadd -= val if t != @town_sprite.y
  231.         rem = true if @toadd <= -mov
  232.       end
  233.       if [6,7,8,9,10,11].include?(SPOP::POPUP_TRANSITION)
  234.         if @town_sprite.z == 5 && @town_sprite.zoom_x > 0
  235.           @town_sprite.zoom_x -= 0.25
  236.           @town_sprite.zoom_y -= 0.25
  237.         end
  238.         if @town_sprite.z == 10 && @town_sprite.zoom_x < 1
  239.           @town_sprite.zoom_x += 0.25
  240.           @town_sprite.zoom_y += 0.25
  241.         end
  242.         rem = true if @town_sprite.zoom_x <= 0
  243.       end
  244.       if @town_ex_audio[0] != nil
  245.         if @audiowait > 0
  246.           @audiowait -= 1
  247.         elsif @audiowait == 0
  248.           if @town_audio[0] != nil
  249.             $game_system.method("#{@town_audio[1]}_play").call(@town_audio[0])
  250.             if @town_ex_audio[1] != @town_audio[1]
  251.               $game_system.method("#{@town_ex_audio[1]}_play").call(@town_ex_audio[0])
  252.               @town_ex_audio = [nil, ""]
  253.             end
  254.             reset_audio if @town_audio[0].name != SPOP::POPUP_SOUND[1]
  255.           end
  256.           @audiowait = -1
  257.         end
  258.       end
  259.       remove_town_sprite(true) if rem
  260.     end
  261.   end
  262.   #--------------------------------------------------------------------------
  263.   # * Popup removal at straighten (scene change)
  264.   #--------------------------------------------------------------------------
  265. if method_defined?(:straighten)
  266.   alias_method(:straighten_b4_spop, :straighten) unless method_defined?(:straighten_b4_spop)
  267. end
  268. #class Game_Player#def straighten() <- aliased/added
  269.   def straighten
  270.     remove_town_sprite(true)
  271.     if respond_to?(:straighten_b4_spop)
  272.       straighten_b4_spop
  273.     else
  274.       super
  275.     end
  276.   end
  277.   #--------------------------------------------------------------------------
  278.   # * Player has moved?
  279.   #--------------------------------------------------------------------------
  280.   alias_method(:increase_steps_b4_spop, :increase_steps) unless method_defined?(:increase_steps_b4_spop)
  281. #class Game_Player#def increase_steps() <- aliased
  282.   def increase_steps
  283.     increase_steps_b4_spop
  284.     @move_failed = false
  285.   end
  286.   #--------------------------------------------------------------------------
  287.   # * Processing of Movement via input from the Directional Buttons
  288.   #--------------------------------------------------------------------------
  289. #class Game_Player#def move_by_input()
  290.   def move_by_input
  291.     x, y = self.x, self.y
  292.     @move_failed = true
  293.     case SPOP::WALK_8_DIR ? Input.dir8 : Input.dir4
  294.     when 1
  295.       move_lower_left
  296.       if @move_failed
  297.         check_town(x-1, y+1)
  298.       else
  299.         check_town(x-2, y+2)
  300.       end
  301.     when 2
  302.       move_down
  303.       if @move_failed
  304.         check_town(x, y+1)
  305.       else
  306.         check_town(x, y+2)
  307.       end
  308.     when 3
  309.       move_lower_right
  310.       if @move_failed
  311.         check_town(x+1, y+1)
  312.       else
  313.         check_town(x+2, y+2)
  314.       end
  315.     when 4
  316.       move_left
  317.       if @move_failed
  318.         check_town(x-1, y)
  319.       else
  320.         check_town(x-2, y)
  321.       end
  322.     when 6
  323.       move_right
  324.       if @move_failed
  325.         check_town(x+1, y)
  326.       else
  327.         check_town(x+2, y)
  328.       end
  329.     when 7
  330.       move_upper_left
  331.       if @move_failed
  332.         check_town(x-1, y-1)
  333.       else
  334.         check_town(x-2, y-2)
  335.       end
  336.     when 8
  337.       move_up
  338.       if @move_failed
  339.         check_town(x, y-1)
  340.       else
  341.         check_town(x, y-2)
  342.       end
  343.     when 9
  344.       move_upper_right
  345.       if @move_failed
  346.         check_town(x+1, y-1)
  347.       else
  348.         check_town(x+2, y-2)
  349.       end
  350.     end
  351.   end
  352.   #--------------------------------------------------------------------------
  353.   # * Operations for sprite removal and audio stopping
  354.   #--------------------------------------------------------------------------
  355. #class Game_Player#def remove_town_sprite(instant, audio)
  356.   def remove_town_sprite(instant=false, audio=true)
  357.     if @town_sprite != nil
  358.       if instant || SPOP::POPUP_TRANSITION == 0
  359.         if audio
  360.           $game_system.method("#{@town_audio[1]}_stop").call if @town_audio[1] != ""
  361.           $game_system.method("#{@town_ex_audio[1]}_play").call(@town_ex_audio[0]) if @town_ex_audio[1] != ""
  362.         end
  363.         @town_ex_audio = [nil, ""]
  364.         @town_sprite.dispose
  365.         @town_sprite = nil
  366.         @sync_event = nil
  367.       else
  368.         @town_sprite.z = 5
  369.         unless ["se", "me", ""].include?(@town_audio[1])
  370.           $game_system.method("#{@town_audio[1]}_fade").call(4)
  371.         end
  372.       end
  373.     end
  374.   end
  375.   #--------------------------------------------------------------------------
  376.   # * Set the audio as the one specified in SPOP or passed
  377.   #--------------------------------------------------------------------------
  378. #class Game_Player#def reset_audio(spn)
  379.   def reset_audio(spn = SPOP::POPUP_SOUND)
  380.     @town_audio = [(spn[1] == "" || spn[2] <= 0) ? nil :
  381.                     RPG::AudioFile.new(spn[1], spn[2], spn[3]),
  382.                     spn[0].downcase]
  383.   end
  384.   #--------------------------------------------------------------------------
  385.   # * Check if there is a town event in front of the player
  386.   #--------------------------------------------------------------------------
  387. #class Game_Player#def check_town(x, y)
  388.   def check_town(x, y)
  389.     return false if $game_system.map_interpreter.running?
  390.     result = false
  391.     for event in $game_map.events_xy(x, y)
  392.       unless [1,2].include?(event.trigger) and event.priority_type == 1
  393.         if event.list != nil
  394.           if event.list[0].code == 108 and
  395.             ["[#{SPOP::ID}]", "[n#{SPOP::ID}]", "[p#{SPOP::ID}]"].include?(event.list[0].parameters[0])
  396.             result = true
  397.             next if @town_sprite != nil && @town_sprite.z == 10 && @town_text == event.list[1].parameters[0]
  398.             remove_town_sprite(true)
  399.             @town_sprite = Sprite.new
  400.             @town_sprite.z = 10
  401.             if [6,7,8,9,10,11].include?(SPOP::POPUP_TRANSITION)
  402.               @town_sprite.zoom_x = @town_sprite.zoom_y = 0.0
  403.             end
  404.             @town_sprite.opacity = 15 if [1,3,5,7,9,11].include?(SPOP::POPUP_TRANSITION)
  405.             if event.list[0].parameters[0] != "[p#{SPOP::ID}]"
  406.               @town_sprite.bitmap ||= Bitmap.new(1,1)
  407.               siz = @town_sprite.bitmap.text_size(event.list[1].parameters[0])
  408.               h = siz.height
  409.               s = siz.width
  410.               @town_sprite.bitmap.dispose
  411.               @town_sprite.bitmap = Bitmap.new(s, 24)
  412.               if event.list[0].parameters[0] == "[n#{SPOP::ID}]"
  413.                 ex = @town_sprite.bitmap.font.color
  414.                 @town_sprite.bitmap.font.color = SPOP::GRAYED_COLOR
  415.               end
  416.               @town_sprite.bitmap.draw_text(0,2,s,22,event.list[1].parameters[0],1)
  417.               @town_sprite.bitmap.font.color = ex if event.list[0].parameters[0] == "[n#{SPOP::ID}]"
  418.             else
  419.               @town_sprite.bitmap = Cache.picture(event.list[1].parameters[0])
  420.               s = @town_sprite.bitmap.width
  421.               h = @town_sprite.bitmap.height
  422.             end
  423.             @town_text = event.list[1].parameters[0]
  424.             @town_sprite.ox = s/2
  425.             @town_sprite.oy = h/2
  426.             case SPOP::POPUP_BINDING
  427.             when 1
  428.               @town_sprite.x = screen_x#*32+16
  429.               @town_sprite.y = @sync_y = screen_y#*32+16
  430.             when 2
  431.               @town_sprite.x = event.screen_x#*32+16
  432.               @town_sprite.y = @sync_y = event.screen_y#*32+16
  433.               @sync_event = event
  434.             else
  435.               @town_sprite.x = 544/2# - s/2
  436.               @town_sprite.y = 416/2# - h/2
  437.             end
  438.             @town_sprite.y -= 64 if [0,1,6,7].include?(SPOP::POPUP_TRANSITION)
  439.             @town_sprite.y -= 32 if [4,5,10,11].include?(SPOP::POPUP_TRANSITION)
  440.             @toadd = [2,3,4,5,8,9,10,11].include?(SPOP::POPUP_TRANSITION) ? 64 : 0
  441.             @toadd -= 32 if [4,5,10,11].include?(SPOP::POPUP_TRANSITION)
  442.             if @town_audio[0] != nil || event.list[2].code == 108
  443.               if ["BGM", "ME", "BGS", "SE"].include?(event.list[2].parameters[0]) &&
  444.                 event.list[3].code == 408
  445.                 arr = []
  446.                 arr.push(event.list[2].parameters[0])
  447.                 arr.push(event.list[3].parameters[0])
  448.                 if event.list[4].code == 408
  449.                   arr.push(event.list[4].parameters[0].to_i)
  450.                   arr.push(event.list[5].parameters[0].to_i) if event.list[5].code == 408
  451.                 else
  452.                   arr.push(["BGS", "SE"].include?(event.list[2].parameters[0]) ? 80 : 100)
  453.                 end
  454.                 arr.push(100) if arr.size < 4
  455.                 reset_audio(arr)
  456.               end
  457.               if ["bgm", "bgs"].include?(@town_audio[1])
  458.                 @town_ex_audio = [$game_system.method("playing_#{@town_audio[1]}").call,
  459.                                   @town_audio[1]]
  460.               else
  461.                 @town_ex_audio[0] = nil
  462.               end
  463.               if @town_ex_audio[0] != nil
  464.                 $game_system.method("#{@town_ex_audio[1]}_fade").call(4)
  465.                 @audiowait = 4
  466.               else
  467.                 $game_system.method("#{@town_audio[1]}_play").call(@town_audio[0])
  468.                 reset_audio if arr != nil
  469.               end
  470.             end
  471.           end
  472.         end
  473.       end
  474.     end
  475.     remove_town_sprite unless result
  476.     return result
  477.   end
  478. end
  479.  
  480. if SPOP::AUTO_REMOVE_AT_TRANSFER
  481. class Scene_Map
  482.   #--------------------------------------------------------------------------
  483.   # * Removing of town sprite when changing map
  484.   #--------------------------------------------------------------------------
  485.   alias_method(:transfer_player_b4_spop, :transfer_player) unless method_defined?(:transfer_player_b4_spop)
  486. #class Scene_Map#def transfer_player() <- aliased
  487.   def transfer_player
  488.     $game_player.remove_town_sprite(true, false)
  489.     transfer_player_b4_spop
  490.   end
  491. end
  492. end
  493.  
  494. class Game_Map
  495.   #--------------------------------------------------------------------------
  496.   # * Get event list in x, y
  497.   #--------------------------------------------------------------------------
  498. #class Game_Map#def events_xy(x, y)
  499.   unless method_defined?(:events_xy)
  500.   def events_xy(x, y)
  501.     result = []
  502.     for event in $game_map.events.values
  503.       result.push(event) if event.x == x and event.y == y
  504.     end
  505.     return result
  506.   end
  507.   end
  508. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement