Advertisement
Guest User

MS - Vehicles+

a guest
Jun 28th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 19.46 KB | None | 0 0
  1. #==============================================================================
  2. # MS - Vehicles+
  3. #------------------------------------------------------------------------------
  4. # por Masked
  5. #==============================================================================
  6. #==============================================================================
  7. # Instruções
  8. #------------------------------------------------------------------------------
  9. # Para criar um evento-veículo, deixe o nome dele dessa forma:
  10. # vehicle[tipo]
  11. #
  12. # Sendo o tipo o nome (sem espaços) do veículo que você configurar, por
  13. # exemplo:
  14. # vehicle[horse]
  15. # ou:
  16. # vehicle[airship]
  17. #
  18. # Para um veículo customizado (que seja usado só uma vez, por exemplo) use
  19. # o tipo 'custom':
  20. # vehicle[custom]
  21. #
  22. # Dessa forma todas as configurações devem ser feitas por comentários do
  23. # evento, por exemplo:
  24. #
  25. # speed: 4,
  26. # p_flag: 0x0,
  27. # max: 0,
  28. # bgm: 'Ship',
  29. # flying: false,
  30. #
  31. #------------------------------------------------------------------------------
  32. # Flags de passabilidade
  33. #------------------------------------------------------------------------------
  34. # 0x000: Normal
  35. # 0x001: Passável por baixo
  36. # 0x002: Passável pela esquerda
  37. # 0x004: Passável pela direita
  38. # 0x008: Passável por cima
  39. # 0x020: Escada
  40. # 0x040: Arbusto
  41. # 0x080: Balcão
  42. # 0x100: Dano
  43. # 0x200: Bote
  44. # 0x400: Navio
  45. # 0x800: Dirigível pode estacionar
  46. #
  47. # P.S: Somar as flags é o mesmo que juntar elas, ou seja, se as flag for
  48. # 0x0800 + 0x0400 o veículo passará tanto por áreas onde dirigíveis podem
  49. # estacionar quanto em áreas onde o navio pode passar
  50. #==============================================================================
  51. ($imported ||= {})[:mbs_vehicles_plus] = true
  52. #==============================================================================
  53. # >> MBS
  54. #==============================================================================
  55. module MBS
  56.   #============================================================================
  57.   # >> VehiclesPlus
  58.   #============================================================================
  59.   module VehiclesPlus
  60.     #==========================================================================
  61.     # Configurações
  62.     #==========================================================================
  63.    
  64.     # Definição dos veículos
  65.     @@_vehicles = {
  66.    
  67.       # Bote
  68.       boat: {
  69.         # Velocidade (4 por padrão)
  70.         speed: 4,
  71.        
  72.         # Flag de passabilidade (vide lista nas instruções)
  73.         p_flag: 0x0200,
  74.        
  75.         # Máximo de veículos desse tipo num mapa (0 para ilimitado)
  76.         max: 0,
  77.        
  78.         # Música de fundo do veículo na pasta Audio/BGM
  79.         bgm: 'Ship',
  80.        
  81.         # Caso o veículo seja voador, mude para true, se não, false
  82.         flying: false,
  83.       },
  84.      
  85.       # Navio
  86.       ship: {
  87.         speed: 4,
  88.         p_flag: 0x400,
  89.         max: 0,
  90.         bgm: 'Ship',
  91.         flying: false,
  92.       },
  93.      
  94.       # Dirigível
  95.       airship: {
  96.         speed: 4,
  97.         p_flag: 0x0,
  98.         max: 0,
  99.         bgm: 'Airship',
  100.         flying: true,
  101.       },
  102.      
  103.       # Cavalo
  104.       horse: {
  105.         speed: 5,
  106.         p_flag: 0x1 + 0x2 + 0x4 + 0x8,
  107.         max: 0,
  108.         bgm: 'Field1',
  109.         flying: false,
  110.       },
  111.      
  112.       # Balão
  113.       baloon: {
  114.         speed: 3,
  115.         p_flag: 0x0,
  116.         max: 0,
  117.         bgm: 'Airship',
  118.         flying: true,
  119.       },
  120.     }
  121.    
  122.     #==========================================================================
  123.     # Fim das configurações
  124.     #==========================================================================
  125.    
  126.     # Estrutura dos veículos
  127.     Vehicle = Struct.new(:speed, :p_flag, :max, :show_player, :bgm, :flying)
  128.    
  129.     #--------------------------------------------------------------------------
  130.     # * Aquisição de um veículo
  131.     #--------------------------------------------------------------------------
  132.     def self.vehicle(type)
  133.       v = @@_vehicles[type]
  134.       Vehicle.new(v[:speed], v[:p_flag], v[:max], v[:show_player], v[:bgm], v[:flying])
  135.     end
  136.     #--------------------------------------------------------------------------
  137.     # * Aquisição da configuração do sistema para o veículo
  138.     #--------------------------------------------------------------------------
  139.     def self.system_vehicle(game_vehicle)
  140.       return unless game_vehicle.event
  141.       v = RPG::System::Vehicle.new
  142.       v.character_name = game_vehicle.event.character_name
  143.       v.character_index = game_vehicle.event.character_index
  144.       v.bgm = RPG::BGM.new(vehicle(game_vehicle.type).bgm)
  145.       v.start_map_id = game_vehicle.event.map_id
  146.       v.start_x = game_vehicle.event.x
  147.       v.start_y = game_vehicle.event.y
  148.       v
  149.     end
  150.     #--------------------------------------------------------------------------
  151.     # * Verificação da existência de um veículo
  152.     #--------------------------------------------------------------------------
  153.     def self.has_vehicle?(type)
  154.       @@_vehicles.has_key?(type) || type == CUSTOM
  155.     end
  156.     #--------------------------------------------------------------------------
  157.     # * Definição de um veículo customizado
  158.     #--------------------------------------------------------------------------
  159.     def self.register_custom(event)
  160.       type = "custom_#{@@_vehicles.size}#{rand(999)}".to_sym
  161.       comments = []
  162.       event.list.each do |cmd|
  163.         if cmd.code == 108 || cmd.code == 408
  164.           comments << cmd.parameters[0]
  165.         end
  166.       end
  167.       @@_vehicles[type] = eval("{#{comments.inject(""){|r, v| r + v}}}")
  168.       type
  169.     end
  170.    
  171.     # Padrão para o nome dos eventos de veículos
  172.     REGEX = /^\s*vehicle[ ]*\[[ ]*(\S+)[ ]*\][ ]*$/i
  173.    
  174.     # Símbolo para veículos customizados
  175.     CUSTOM = :custom
  176.    
  177.   end
  178. end
  179.  
  180. #==============================================================================
  181. # ** Kernel
  182. #------------------------------------------------------------------------------
  183. # Atalhos para o alias
  184. #==============================================================================
  185. module Kernel
  186.   #--------------------------------------------------------------------------
  187.   # * Cria o alias
  188.   #--------------------------------------------------------------------------
  189.   def mbsalias(method)
  190.     alias_method("mbsvehiclesplus_#{method}".to_sym, method.to_sym)
  191.   end
  192.  
  193.   #--------------------------------------------------------------------------
  194.   # * Chama o alias
  195.   #--------------------------------------------------------------------------
  196.   def mbscall(method, *a, &b)
  197.     __send__("mbsvehiclesplus_#{method}".to_sym, *a, &b)
  198.   end
  199. end
  200. #==============================================================================
  201. # ** Game_CharacterBase
  202. #------------------------------------------------------------------------------
  203. # Aqui são feitas as alterações para colidir com os veículos
  204. #==============================================================================
  205. class Game_CharacterBase
  206.   #--------------------------------------------------------------------------
  207.   # * Detectar colisão com veículos
  208.   #     x : coordenada X
  209.   #     y : coordenada Y
  210.   #--------------------------------------------------------------------------
  211.   def collide_with_vehicles?(x, y)
  212.     $game_map.vehicles.any? {|v| v.pos_nt?(x, y)}
  213.   end
  214. end
  215. #==============================================================================
  216. # ** Game_Player
  217. #------------------------------------------------------------------------------
  218. # Aqui são feitas as alterações para o personagem entrar nos veículos
  219. #==============================================================================
  220. class Game_Player < Game_Character
  221.   mbsalias :update_vehicle_get_off
  222.   #--------------------------------------------------------------------------
  223.   # * Entrada em veículos
  224.   #    Necessário estar fora de um veículo para processar
  225.   #--------------------------------------------------------------------------
  226.   def get_on_vehicle
  227.     front_x = $game_map.round_x_with_direction(@x, @direction)
  228.     front_y = $game_map.round_y_with_direction(@y, @direction)
  229.     $game_map.vehicles.each do |v|
  230.       if v.pos?(front_x, front_y)
  231.         @vehicle_type = v.type
  232.         @vehicle = v
  233.       end
  234.     end
  235.     if vehicle
  236.       @vehicle_getting_on = true
  237.       force_move_forward unless in_airship?
  238.       @followers.gather
  239.     end
  240.     @vehicle_getting_on
  241.   end
  242.   #--------------------------------------------------------------------------
  243.   # * Verificação da passabilidade
  244.   #--------------------------------------------------------------------------
  245.   def map_passable?(x, y, d)
  246.     if !vehicle.nil?
  247.       return vehicle_passable?(x, y, d)
  248.     else
  249.       super
  250.     end
  251.   end
  252.   #--------------------------------------------------------------------------
  253.   # * Verificação da passabilidade para o veículo
  254.   #--------------------------------------------------------------------------
  255.   def vehicle_passable?(x, y, d)
  256.     $game_map.check_passage(x, y, vehicle.p_flag)
  257.   end
  258.   #--------------------------------------------------------------------------
  259.   # * Atualização da saida do veículo
  260.   #--------------------------------------------------------------------------
  261.   def update_vehicle_get_off(*a, &b)
  262.     mbscall(:update_vehicle_get_off, *a, &b)
  263.     if !@followers.gathering? && vehicle.altitude == 0
  264.       @vehicle = nil
  265.     end
  266.   end
  267.   #--------------------------------------------------------------------------
  268.   # * Aquisição do veículo em uso atualmente
  269.   #--------------------------------------------------------------------------
  270.   def vehicle
  271.     @vehicle
  272.   end
  273. end
  274. #==============================================================================
  275. # ** Game_Event
  276. #------------------------------------------------------------------------------
  277. # Aqui são feitas as alterações necessárias para se ter acesso às informações
  278. # requeridas sobre o evento
  279. #==============================================================================
  280. class Game_Event < Game_Character
  281.   #--------------------------------------------------------------------------
  282.   # * Definição dos atributos
  283.   #--------------------------------------------------------------------------
  284.   attr_reader :map_id
  285.   #--------------------------------------------------------------------------
  286.   # * Aquisição do nome do evento
  287.   #--------------------------------------------------------------------------
  288.   def name
  289.     @event.name
  290.   end
  291. end
  292. #==============================================================================
  293. # ** Game_Vehicle
  294. #------------------------------------------------------------------------------
  295. # Aqui são feitas as modificações para se criar veículos adicionais
  296. #==============================================================================
  297. class Game_Vehicle < Game_Character
  298.   #--------------------------------------------------------------------------
  299.   # * Alias
  300.   #--------------------------------------------------------------------------
  301.   mbsalias :initialize
  302.   mbsalias :system_vehicle
  303.   #--------------------------------------------------------------------------
  304.   # * Definição dos atributos
  305.   #--------------------------------------------------------------------------
  306.   attr_accessor :p_flag, :show_player
  307.   attr_reader :event, :type
  308.   #--------------------------------------------------------------------------
  309.   # * Carregamento das configuração do sistema
  310.   #--------------------------------------------------------------------------
  311.   def initialize(type, event, *a, &b)
  312.     @event = event
  313.     mbscall(:initialize, type, *a, &b)
  314.   end
  315.   #--------------------------------------------------------------------------
  316.   # * Inicialização da velocidade de movimento
  317.   #--------------------------------------------------------------------------
  318.   def init_move_speed(*a, &b)
  319.     @move_speed = MBS::VehiclesPlus.vehicle(@type).speed
  320.   end
  321.   #--------------------------------------------------------------------------
  322.   # * Carregamento das configuração do sistema
  323.   #--------------------------------------------------------------------------
  324.   def load_system_settings(*a, &b)
  325.     v = system_vehicle
  326.     @map_id = v.start_map_id
  327.     @real_x = @x = v.start_x
  328.     @real_y = @y = v.start_y
  329.     @character_name = v.character_name
  330.     @character_index = v.character_index
  331.     @p_flag = mbs_vehicle.p_flag
  332.     @show_player = mbs_vehicle.show_player
  333.   end
  334.   #--------------------------------------------------------------------------
  335.   # * Aquisição das configurações do veículo do script
  336.   #--------------------------------------------------------------------------
  337.   def mbs_vehicle
  338.     MBS::VehiclesPlus.vehicle(@type)
  339.   end
  340.   #--------------------------------------------------------------------------
  341.   # * Aquisição das configurações do sistema do veículo
  342.   #--------------------------------------------------------------------------
  343.   def system_vehicle(*a, &b)
  344.     MBS::VehiclesPlus.system_vehicle(self) || mbscall(:system_vehicle, *a, &b)
  345.   end
  346.   #--------------------------------------------------------------------------
  347.   # * Atualização da tela
  348.   #--------------------------------------------------------------------------
  349.   def update(*a, &b)
  350.     super
  351.     update_airship_altitude if mbs_vehicle.flying
  352.   end
  353.   #--------------------------------------------------------------------------
  354.   # * Renovação
  355.   #--------------------------------------------------------------------------
  356.   def refresh(*a, &b)
  357.     if mbs_vehicle.flying
  358.       @priority_type = @driving ? 2 : 0
  359.     else
  360.       @priority_type = 1
  361.     end
  362.   end
  363.   #--------------------------------------------------------------------------
  364.   # * Definição de mobilidade
  365.   #--------------------------------------------------------------------------
  366.   def movable?
  367.     !moving? && !(mbs_vehicle.flying && @altitude < max_altitude)
  368.   end
  369.   #--------------------------------------------------------------------------
  370.   # * Definição de permissão de desembarque
  371.   #--------------------------------------------------------------------------
  372.   def land_ok?(x, y, d)
  373.     if mbs_vehicle.flying
  374.       return false unless $game_map.airship_land_ok?(x, y)
  375.       return false unless $game_map.events_xy(x, y).empty?
  376.     end
  377.     x2 = $game_map.round_x_with_direction(x, d)
  378.     y2 = $game_map.round_y_with_direction(y, d)
  379.     return false unless $game_map.valid?(x2, y2)
  380.     return false unless $game_map.passable?(x2, y2, reverse_dir(d))
  381.     return false if collide_with_characters?(x2, y2)
  382.     return true
  383.   end
  384.   #--------------------------------------------------------------------------
  385.   # * Definição da posição do veículo
  386.   #--------------------------------------------------------------------------
  387.   def moveto(*a, &b)
  388.     set_location(@map_id, *a, &b)
  389.     @real_x = x
  390.     @real_y = y
  391.   end
  392. end
  393. #==============================================================================
  394. # ** Game_Map
  395. #------------------------------------------------------------------------------
  396. # Aqui são feitas as modificações para transformar os eventos com nome de
  397. # veículos em veículos
  398. #==============================================================================
  399. class Game_Map
  400.   #--------------------------------------------------------------------------
  401.   # * Alias
  402.   #--------------------------------------------------------------------------
  403.   mbsalias :setup
  404.   mbsalias :events
  405.   mbsalias :referesh_vehicles
  406.   #--------------------------------------------------------------------------
  407.   # * Verificação de se um evento é um veículo
  408.   #--------------------------------------------------------------------------
  409.   def __vehicle?(event)
  410.     return false unless event.name =~ MBS::VehiclesPlus::REGEX
  411.     return MBS::VehiclesPlus.has_vehicle?($1.to_sym)
  412.   end
  413.   private :__vehicle?
  414.   #--------------------------------------------------------------------------
  415.   # * Aquisição dos eventos de veículos
  416.   #--------------------------------------------------------------------------
  417.   def vehicle_events
  418.     r = {}
  419.     @events.each do |id, event|
  420.       if __vehicle?(event)
  421.         r[id] = event
  422.         @events.delete(id)
  423.       end
  424.     end
  425.     r
  426.   end
  427.   #--------------------------------------------------------------------------
  428.   # * Aquisição do hash dos veículos
  429.   #--------------------------------------------------------------------------
  430.   def vehicles_hash
  431.     vehicles.compact.inject({}) do |hash, vehicle|
  432.       hash[vehicle.event.id] = vehicle if vehicle.event
  433.       hash
  434.     end
  435.   end
  436.   #--------------------------------------------------------------------------
  437.   # * Aquisição de todos os eventos
  438.   #--------------------------------------------------------------------------
  439.   def all_events
  440.     events.merge vehicles_hash
  441.   end
  442.   #--------------------------------------------------------------------------
  443.   # * Configuração do mapa
  444.   #--------------------------------------------------------------------------
  445.   def setup(*a, &b)
  446.     mbscall(:setup, *a, &b)
  447.     referesh_vehicles
  448.   end
  449.   #--------------------------------------------------------------------------
  450.   # * Criação dos veículos
  451.   #--------------------------------------------------------------------------
  452.   def create_vehicles
  453.     @vehicles = []
  454.     @vehicles[0] = Game_Vehicle.new(:boat, nil)
  455.     @vehicles[1] = Game_Vehicle.new(:ship, nil)
  456.     @vehicles[2] = Game_Vehicle.new(:airship, nil)
  457.   end
  458.   #--------------------------------------------------------------------------
  459.   # * Atualização dos veículos
  460.   #--------------------------------------------------------------------------
  461.   def referesh_vehicles(*a, &b)
  462.     @vehicles.clear
  463.     @vehicles[0] = Game_Vehicle.new(:boat, nil)
  464.     @vehicles[1] = Game_Vehicle.new(:ship, nil)
  465.     @vehicles[2] = Game_Vehicle.new(:airship, nil)
  466.     count = {}
  467.     vehicle_events.each do |id, e|
  468.       e.name =~ MBS::VehiclesPlus::REGEX
  469.       type = $1.to_sym
  470.      
  471.       if type == :custom
  472.         type = MBS::VehiclesPlus.register_custom(e)
  473.       end
  474.      
  475.       v = Game_Vehicle.new(type, e)
  476.       mbs = MBS::VehiclesPlus.vehicle(type)
  477.       if ((count[type] ||= 0) < mbs.max || mbs.max == 0)
  478.         @vehicles << v
  479.         count[type] += 1
  480.       end
  481.     end
  482.     mbscall(:referesh_vehicles, *a, &b)
  483.   end
  484.   #--------------------------------------------------------------------------
  485.   # * Aquisição do veículo
  486.   #--------------------------------------------------------------------------
  487.   def vehicle(type)
  488.     return @vehicles.find {|v| v.type == type}
  489.   end
  490. end
  491. #==============================================================================
  492. # ** Game_Interpreter
  493. #------------------------------------------------------------------------------
  494. # Aqui são feitas as modificações para que os comandos de evento que envolvem
  495. # eventos funcionem com os eventos-veículos
  496. #==============================================================================
  497. class Game_Interpreter
  498.   #--------------------------------------------------------------------------
  499.   # * Aquisição do personagem
  500.   #--------------------------------------------------------------------------
  501.   def get_character(param)
  502.     if $game_party.in_battle
  503.       nil
  504.     elsif param < 0
  505.       $game_player
  506.     else
  507.       events = same_map? ? $game_map.all_events : {}
  508.       events[param > 0 ? param : @event_id]
  509.     end
  510.   end
  511. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement