Advertisement
LiTTleDRAgo

[RGSS/2/3] Event Interaction

Jun 28th, 2012
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.89 KB | None | 0 0
  1. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  2. # [Xp/Vx-VXA] Event Interaction
  3. # Version: 2.40
  4. # Author : LiTTleDRAgo
  5. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  6. module LiTTleDRAgo
  7.   module Event_Interaction
  8.    
  9.     BUTTON_ACTION_PREV = Input::L
  10.     BUTTON_ACTION_NEXT = Input::R
  11.     BUTTON_ACTION_CONFIRM = Input::C
  12.     EVENT_INTERACT_SWITCH = false
  13.    
  14.     VX  = defined?(Window_ActorCommand)
  15.     VXA = defined?(Window_BattleActor)
  16.    
  17.   end
  18. end
  19.  
  20. EVENT_INTERACTION =<<_SCRIPT_
  21. #==============================================================================
  22. # ** Scene_Map
  23. #------------------------------------------------------------------------------
  24. #  This class performs map screen processing.
  25. #==============================================================================
  26. class Scene_Map
  27.  
  28.   alias_method :drg136_main, :main unless method_defined?(:drg136_main)
  29.   alias_method :drg136_upd, :update unless method_defined?(:drg136_upd)
  30.  
  31.   attr_accessor :event_interaction
  32.   def main
  33.     @event_interaction = [Window_EventInteraction.new]
  34.     @event_interaction[6] = LiTTleDRAgo::Event_Interaction::VX
  35.     drg136_main
  36.     @event_interaction[0].dispose
  37.   end
  38.  
  39.   def update
  40.     drg136_upd
  41.     event_interaction_update
  42.   end
  43.  
  44.   def event_interaction_update
  45.     cond = @event_interaction[6] ? !$game_player.movable? :
  46.             ($game_system.map_interpreter.running? or
  47.             $game_player.move_route_forcing or
  48.             $game_temp.message_window_showing)
  49.       @event_interaction[0].update_position
  50.     unless cond
  51.       @event_interaction[0].update  
  52.       event = facing_object([0,1,2],0) || facing_object([0,1,2],1)
  53.       event_interaction_refresh(event)
  54.     end
  55.   end
  56.  
  57.   def event_interaction_refresh(e)
  58.     @event_interaction[5] = $game_player.direction
  59.     set_command_object(e,[],'',0)
  60.     event = e ? e.list.select {|list| [108,408].include?(list.code)} : []
  61.     notes = event.collect {|list| list.parameters[0]}.join
  62.     var_eventint_process(notes)
  63.     return
  64.     event.list.each {|list| next unless [108,408].include?(list.code)
  65.     var_eventint_process(list.parameters[0])} if event
  66.   end
  67.    
  68.   def var_eventint_process(note)
  69.     return if (note || '') == ''
  70.     @event_interaction[2] = $1.split(/,/).uniq if note =~ /<Action = (.+?)>/i
  71.     @event_interaction[3] = $1.to_s            if note =~ /<Object = (.+?)>/i
  72.   end
  73.  
  74.   def set_command_object(event=nil,command=[],object='',post=0)
  75.     @event_interaction[1] = event
  76.     @event_interaction[2] = command
  77.     @event_interaction[3] = object
  78.     @event_interaction[4] = post
  79.   end
  80.      
  81.   def facing_object(triggers,type)
  82.     coor,dir = [$game_player.x,$game_player.y],$game_player.direction
  83.     new_x = coor[0] + (dir == 6 ? 1 : dir == 4 ? -1 : 0)
  84.     new_y = coor[1] + (dir == 2 ? 1 : dir == 8 ? -1 : 0)
  85.     type = type+2 if @event_interaction[6]
  86.     case type
  87.     when 0
  88.       $game_map.events.values.each {|event|
  89.         if [event.x.round,event.y.round] == [new_x.round,new_y.round] &&
  90.             triggers.include?(event.trigger) &&
  91.             !event.jumping? && !event.over_trigger?
  92.           return event
  93.         end }
  94.     when 1  
  95.       $game_map.events.values.each {|event|
  96.         if event.x == coor[0] && event.y == coor[1] &&
  97.             triggers.include?(event.trigger) &&
  98.             !event.jumping? && event.over_trigger?
  99.           return event
  100.         end }
  101.     when 2
  102.       front_x = $game_map.x_with_direction(coor[0], dir)
  103.       front_y = $game_map.y_with_direction(coor[1], dir)
  104.       $game_map.events_xy(front_x, front_y).each {|event|
  105.         if triggers.include?(event.trigger) && event.priority_type == 1
  106.           return event
  107.         end}
  108.       if $game_map.counter?(front_x, front_y)
  109.         front_x = $game_map.x_with_direction(front_x, dir)
  110.         front_y = $game_map.y_with_direction(front_y, dir)
  111.         $game_map.events_xy(front_x, front_y).each {|event|
  112.           if triggers.include?(event.trigger) && event.priority_type == 1
  113.             return event
  114.           end}
  115.       end
  116.     when 3      
  117.       $game_map.events_xy(coor[0], coor[1]).each {|event|
  118.         if [1,2].include?(event.trigger) and event.priority_type == 1
  119.           return event
  120.         end }
  121.     end
  122.     return nil
  123.   end
  124. end
  125.  
  126. #==============================================================================
  127. # ** Window_EventInteraction
  128. #------------------------------------------------------------------------------
  129. #  This window is used to select the choice when interacting with event
  130. #==============================================================================
  131. class Window_EventInteraction < Window_Base
  132.  
  133.   MODULE = LiTTleDRAgo::Event_Interaction
  134.   BUTTON_ACTION_PREV    = MODULE::BUTTON_ACTION_PREV
  135.   BUTTON_ACTION_NEXT    = MODULE::BUTTON_ACTION_NEXT
  136.   BUTTON_ACTION_CONFIRM = MODULE::BUTTON_ACTION_CONFIRM
  137.   ENABLE_SWITCH         = MODULE::EVENT_INTERACT_SWITCH
  138.  
  139.   def initialize
  140.     super( (640 - init_width) / 2, 0, init_width, init_height)
  141.     self.contents = Bitmap.new(width - 32, height - 32)
  142.     self.visible = false
  143.     self.opacity = 128
  144.     @index = 0
  145.   end
  146.  
  147.   def init_width() 240  end
  148.   def init_height() 64 end
  149.   def event() scene.event_interaction[1] || 0  end
  150.   def command_text(index=0)  object(@command[index]) end
  151.  
  152.   def enabled?
  153.     return true if !ENABLE_SWITCH
  154.     return $game_switches[ENABLE_SWITCH]
  155.   end
  156.    
  157.   def object(obj=scene.event_interaction[3])
  158.     text = obj.to_s
  159.     text.gsub!(/\\[Vv]\[([0-9]+)\]/) { $game_variables[$1.to_i] }
  160.     text.gsub!(/\\[Nn]\[([0-9]+)\]/) {
  161.         $game_actors[$1.to_i] != nil ? $game_actors[$1.to_i].name : '' }
  162.     text = convert_escape_characters(text.dup) if MODULE::VXA
  163.     return text
  164.   end
  165.  
  166.   def scene
  167.     MODULE::VXA ? SceneManager.scene : $scene
  168.   end
  169.  
  170.   def update
  171.     return unless scene.is_a?(Scene_Map)
  172.     @command = scene.event_interaction[2]
  173.     interpreter = MODULE::VX ? $game_map.interpreter.running? :
  174.                   $game_system.map_interpreter.running?
  175.     return self.visible = false  if interpreter || (@command || []).empty? ||
  176.             @index == nil || event == 0 || object == ''
  177.     self.visible = enabled?
  178.     @index = @command[@index].nil? ? 0 : @index
  179.     if self.visible
  180.       update_position
  181.       if Input.repeat?(BUTTON_ACTION_PREV)
  182.         sound_play('cursor')
  183.         @index = (@index - 1) % @command.size
  184.       elsif Input.repeat?(BUTTON_ACTION_NEXT)
  185.         sound_play('cursor')
  186.         @index = (@index + 1) % @command.size
  187.       elsif Input.repeat?(BUTTON_ACTION_CONFIRM)
  188.         refresh
  189.         if self.contents.font.color == normal_color
  190.           sound_play('decision')
  191.           $interaction = @command[@index].downcase
  192.           $object_event = object.downcase
  193.           self.visible = false
  194.           event.start if event != 0
  195.           scene.event_interaction[2] = []
  196.         else
  197.           $interaction = nil
  198.           sound_play('buzzer')
  199.         end
  200.         return
  201.       end
  202.       refresh
  203.     end
  204.   end
  205.  
  206.   def update_position
  207.     return unless self.visible
  208.     return self.visible = false if event == 0
  209.     if scene.event_interaction[4] == 0
  210.       self.x = [[event.screen_x - self.width/2, 0].max, 640 - self.width].min
  211.       self.y = [[event.screen_y-32-self.height, 0].max, 480 - self.height].min
  212.     else
  213.       x = scene.event_interaction[4][0].to_f.round
  214.       y = scene.event_interaction[4][1].to_f.round  
  215.       screen_x=(x * 128 - $game_map.display_x + 3) / 4 + 16
  216.       screen_y=(y * 128 - $game_map.display_y + 3) / 4 + 32
  217.       self.x = [[screen_x - self.width/2, 0].max, 640 - self.width].min
  218.       self.y = [[screen_y-32-self.height, 0].max, 480 - self.height].min
  219.     end
  220.   end    
  221.  
  222.   def sound_play(se)
  223.     case se
  224.     when 'cursor'
  225.       MODULE::VX ? Sound.play_cursor :
  226.       $game_system.se_play($data_system.cursor_se)
  227.     when 'decision'
  228.       MODULE::VX ? MODULE::VXA ? Sound.play_ok :
  229.       Sound.play_decision : $game_system.se_play($data_system.decision_se)
  230.     when 'buzzer'
  231.       MODULE::VX ? Sound.play_buzzer :
  232.       $game_system.se_play($data_system.buzzer_se)
  233.     end
  234.   end
  235.  
  236.   def refresh
  237.     return if @index.nil? || @command.nil? || !scene.is_a?(Scene_Map)
  238.     return self.visible = false if event == 0
  239.     text = command_text(@index)+' '+object
  240.     if text.length >= 20 && !@changed
  241.       self.contents.dispose
  242.       self.contents = Bitmap.new(width+100 - 32, height - 32)
  243.       self.width += 100
  244.       @changed = true
  245.     elsif text.length < 20 && @changed
  246.       self.contents.dispose
  247.       self.contents = Bitmap.new(width-100 - 32, height - 32)
  248.       self.width -= 100
  249.       @changed = false
  250.     end
  251.     cw,ch  = self.width - 32, self.height - 32
  252.     rect   = Rect.new(0, 0, cw, ch)
  253.     font = self.contents.font.size
  254.     self.contents.font.size = font
  255.     self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  256.     self.contents.font.color = object == '' ? disabled_color : normal_color
  257.     self.contents.draw_text(0, 0, cw, 32, text,1)
  258.     self.contents.font.size -= 6
  259.     self.contents.draw_text(4, 0, cw, 32, '<Q', 0)  if BUTTON_ACTION_PREV
  260.     self.contents.draw_text(-3, 0, cw, 32, 'W>', 2) if BUTTON_ACTION_NEXT
  261.     self.contents.font.size = font
  262.   end
  263. end
  264. #==============================================================================
  265. # ** Game_Player
  266. #------------------------------------------------------------------------------
  267. #  This class handles maps. It includes event starting determinants and map
  268. # scrolling functions. The instance of this class is referenced by $game_map.
  269. #==============================================================================
  270.  
  271. class Game_Player
  272.   MODULE = LiTTleDRAgo::Event_Interaction
  273.   if MODULE::VX
  274.     alias drg_136_chk check_action_event unless method_defined?(:drg_136_chk)
  275.     def check_action_event
  276.       scene = MODULE::VXA ? SceneManager.scene : $scene
  277.       return if scene.is_a?(Scene_Map) && !scene.event_interaction[2].empty?
  278.       drg_136_chk
  279.     end
  280.   end
  281. end
  282. #==============================================================================
  283. # ** Game_Interpreter
  284. #------------------------------------------------------------------------------
  285. #  An interpreter for executing event commands. This class is used within the
  286. # Game_Map, Game_Troop, and Game_Event classes.
  287. #==============================================================================
  288. Klass = LiTTleDRAgo::Event_Interaction::VX ? Game_Interpreter : Interpreter
  289. class Klass
  290.   VXA = LiTTleDRAgo::Event_Interaction::VXA
  291.   alias drg138_com_end command_end unless VXA || method_defined?(:drg138_com_end)
  292.   def command_end
  293.     drg138_com_end
  294.     s = VXA ? SceneManager.scene : $scene
  295.     event = s.event_interaction[1]
  296.     $interaction, $object_event = nil, '' if event && event.id == @event_id
  297.   end
  298. end
  299. _SCRIPT_
  300.  
  301. eval(EVENT_INTERACTION)
  302.  
  303. $drago_event_interaction = true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement