Advertisement
marlosgama

Untitled

Jul 5th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.28 KB | None | 0 0
  1. #==============================================================================
  2. # ** Game_Player
  3. #------------------------------------------------------------------------------
  4. #  Esta classe lida com o jogador. Ela contém dados, como
  5. # direção, coordenadas x e y.
  6. #------------------------------------------------------------------------------
  7. #  Autor: Valentine
  8. #==============================================================================
  9.  
  10. class Game_Player < Game_Character
  11.  
  12.   attr_reader   :target
  13.   attr_reader   :blocked
  14.   attr_accessor :path
  15.   attr_accessor :item_attack_time
  16.  
  17.   def init_basic
  18.     @target = nil
  19.     @path = nil
  20.     @lost_path = nil
  21.     @got_drop = false
  22.     @weapon_attack_time = 0
  23.     @item_attack_time = 0
  24.     @try_path = 0
  25.     @blocked = []
  26.   end
  27.  
  28.   def clear
  29.     @path = nil
  30.     clear_target
  31.   end
  32.  
  33.   def clear_target
  34.     return unless @target
  35.     $network.send_target(-1, Constants::TARGET_NONE)
  36.   end
  37.  
  38.   def target=(target)
  39.     @target = target
  40.     #@path = @target if @target && $game_actors[1].weapons[0] && !range_weapon?
  41.   end
  42.  
  43.   def range_weapon?
  44.     Configs::RANGE_WEAPONS.has_key?($game_actors[1].weapons[0].id)
  45.   end
  46.  
  47.   def collide_with_characters?(x, y)
  48.     super || collide_with_players?(x, y) && $game_map.pvp
  49.   end
  50.  
  51.   def quest_in_progress?(quest_id)
  52.         # Reduz o índice que foi configurado com um inteiro
  53.         #a mais nos comandos de evento
  54.     actor.quests.has_key?(quest_id - 1) && actor.quests[quest_id - 1].in_progress?
  55.   end
  56.  
  57.   def quest_finished?(quest_id)
  58.     actor.quests.has_key?(quest_id - 1) && actor.quests[quest_id - 1].finished?
  59.   end
  60.  
  61.   def protection_level?(target)
  62.     actor.level < Configs::MIN_LEVEL_PVP || target.actor.level < Configs::MIN_LEVEL_PVP
  63.   end
  64.  
  65.     def vip?
  66.         $network.vip?
  67.     end
  68.  
  69.   def in_range?(target, range)
  70.     distance_x_from(target.x).abs <= range && distance_y_from(target.y).abs <= range
  71.   end
  72.  
  73.   def usable?(item)
  74.     mp_enough?(item) && actor.usable?(item) && (item.for_friend? || valid_target?(item.range))
  75.   end
  76.  
  77.   def mp_enough?(item)
  78.     if item.is_a?(RPG::Skill) && item.mp_cost > actor.mp
  79.       $windows[:chat].write_message(Vocab::InsufficientMP, Configs::ERROR_COLOR)
  80.       return false
  81.     end
  82.     return true
  83.   end
  84.  
  85.   def can_attack?
  86.     @weapon_attack_time = Configs::ATTACK_TIME
  87.     return can_attack_range? if $game_actors[1].weapons[0] && range_weapon?
  88.     return can_attack_normal?
  89.   end
  90. =begin
  91.   def can_attack_normal?
  92.     if !in_range?(@target, 1)
  93.       return false
  94.     elsif @target.admin?
  95.       $windows[:chat].write_message(Vocab::AttackAdmin, Configs::ERROR_COLOR)
  96.       return false
  97.     elsif @target.is_a?(Game_NetPlayer) && !$game_map.pvp
  98.       $windows[:chat].write_message(Vocab::NonPvP, Configs::ERROR_COLOR)
  99.       return false
  100.     elsif @target.is_a?(Game_NetPlayer) && protection_level?(@target)
  101.       $windows[:chat].write_message(Vocab::ProtectionLevel, Configs::ERROR_COLOR)
  102.       return false
  103.     end
  104.     return true
  105.   end
  106. =end
  107.   def can_attack_normal?
  108.     x = $game_map.round_x_with_direction(@x, @direction)
  109.     y = $game_map.round_y_with_direction(@y, @direction)
  110.     player = collide_with_players?(x, y)
  111.     # Se o evento está em frente ou abaixo do jogador
  112.     if collide_with_events?(x, y) || event_trigger_here?([0])
  113.       return true
  114.     elsif !player
  115.       return false
  116.     elsif player.admin?
  117.       $windows[:chat].write_message(Vocab::AttackAdmin, Configs::ERROR_COLOR)
  118.       return false
  119.     elsif !$game_map.pvp
  120.       $windows[:chat].write_message(Vocab::NonPvP, Configs::ERROR_COLOR)
  121.       return false
  122.     elsif protection_level?(player)
  123.       $windows[:chat].write_message(Vocab::ProtectionLevel, Configs::ERROR_COLOR)
  124.       return false
  125.     end
  126.     return true
  127.   end
  128.  
  129.   def event_trigger_here?(triggers)
  130.     $game_map.events_xy(@x, @y).any? { |event| event.trigger_in?(triggers) && !event.normal_priority? }
  131.   end
  132.  
  133.   def can_attack_range?
  134.     range, item_id, mp_cost = Configs::RANGE_WEAPONS[$game_actors[1].weapons[0].id].drop(1)
  135.     x = $game_map.round_x_with_direction(@x, @direction)
  136.     y = $game_map.round_y_with_direction(@y, @direction)
  137.     if collide_with_events?(x, y)
  138.       return true
  139.     elsif item_id > 0 && !$game_party.has_item?($data_items[item_id])
  140.       $windows[:chat].write_message(Vocab::NotAmmunition, Configs::ERROR_COLOR)
  141.       return false
  142.     elsif mp_cost && $game_actors[1].mp < mp_cost
  143.       $windows[:chat].write_message(Vocab::InsufficientMP, Configs::ERROR_COLOR)
  144.       return false
  145.     elsif !valid_target?(range)
  146.       return false
  147.     end
  148.     return true
  149.   end
  150.  
  151.   def valid_target?(range)
  152.     if !@target
  153.       $windows[:chat].write_message(Vocab::NotTarget, Configs::ERROR_COLOR)
  154.       return false
  155.     elsif !in_range?(@target, range)
  156.       $windows[:chat].write_message(Vocab::TargetNotInRange, Configs::ERROR_COLOR)
  157.       return false
  158.     elsif blocked_passage?
  159.       $windows[:chat].write_message(Vocab::NotSeeTarget, Configs::ERROR_COLOR)
  160.       return false
  161.     elsif @target.admin?
  162.       $windows[:chat].write_message(Vocab::AttackAdmin, Configs::ERROR_COLOR)
  163.       return false
  164.     elsif @target.is_a?(Game_NetPlayer) && !$game_map.pvp
  165.       $windows[:chat].write_message(Vocab::NonPvP, Configs::ERROR_COLOR)
  166.       return false
  167.     elsif @target.is_a?(Game_NetPlayer) && protection_level?(@target)
  168.       $windows[:chat].write_message(Vocab::ProtectionLevel, Configs::ERROR_COLOR)
  169.       return false
  170.     end
  171.     return true
  172.   end
  173.  
  174.   def blocked_passage?
  175.     # Retorna falso se o jogador e o alvo estiverem nas
  176.     #mesmas coordenadas x e y, pois, nesta versão do Ruby,
  177.     #o atan2 retorna um erro
  178.     return false if pos?(@target.x, @target.y)
  179.     radians = Math.atan2(@target.x - @x, @target.y - @y)
  180.     speed_x = Math.sin(radians)
  181.     speed_y = Math.cos(radians)
  182.     range_x = (@target.x - @x).abs
  183.     range_y = (@target.y - @y).abs
  184.     range_x -= 1 if range_x > 0
  185.     range_y -= 1 if range_y > 0
  186.     result = false
  187.     x = @x
  188.     y = @y
  189.     while true
  190.       # Soma valores decimais
  191.       x += speed_x
  192.       y += speed_y
  193.       x2 = x.to_i
  194.       y2 = y.to_i
  195.       if !$game_map.valid?(x2, y2) || !map_passable?(x2, y2, @direction)
  196.         result = true
  197.         break
  198.       elsif distance_x_from(x2).abs > range_x || distance_y_from(y2).abs > range_y
  199.         break
  200.       end
  201.     end
  202.     result
  203.   end
  204.  
  205.   def real_move_speed
  206.     @move_speed
  207.   end
  208.  
  209.   def move_by_input
  210.     return if !movable? || $game_map.interpreter.running?
  211.     send_movement(Input.dir4) if Input.dir4 > 0 && !$typing
  212.   end
  213.  
  214.   def send_movement(d)
  215.     return if !passable?(@x, @y, d) && @direction == d
  216.     $network.send_player_movement(d)
  217.     move_straight(d)
  218.   end
  219.  
  220.   def turn_toward_target
  221.     sx = distance_x_from(@@target.x)
  222.     sy = distance_y_from(@@target.y)
  223.     if sx.abs > sy.abs
  224.       d = sx > 0 ? 4 : 6
  225.       set_direction(d)
  226.       $network.send_player_movement(d)
  227.     elsif sy != 0
  228.       d = sy > 0 ? 8 : 2
  229.       set_direction(d)
  230.       $network.send_player_movement(d)
  231.     end
  232.   end
  233.  
  234.   def update_attack
  235.     if @weapon_attack_time > 0
  236.       @weapon_attack_time -= 1
  237.       return
  238.     end
  239.     # Só começa a contar o tempo do ataque se
  240.     #não tiver ocupado com mensagem
  241.     #$network.send_player_attack if !$game_message.busy? && !$typing && (@@target && in_range?(@@target, 1) || @target && can_attack?)#$game_map.interpreter.running?
  242.     $network.send_player_attack if Input.press?(Configs::ATTACK_KEY) && !$game_message.busy? && can_attack? && !$typing
  243.   end
  244.  
  245.   def update_nonmoving(last_moving)
  246.     return if $game_map.interpreter.running?
  247.     if last_moving
  248.       $game_party.on_player_walk
  249.       return if check_touch_event
  250.     end
  251.     # Se a tecla é uma letra e a caixa de texto estiver ativa
  252.     if movable? && Input.trigger?(Configs::ATTACK_KEY) && @weapon_attack_time == 0 && !$typing
  253.     #if movable? && @@target && in_range?(@@target, 1) && !$typing
  254.       return if get_on_off_vehicle
  255.       return if check_action_event
  256.     end
  257.     #update_path if @path
  258.     update_encounter if last_moving
  259.   end
  260.  
  261.   def update_trigger
  262.     clear if Input.trigger?(:B)
  263.     remove_drop if Input.repeat?(Configs::GET_DROP_KEY)#Mouse.click?(:L)
  264.     select_nearest_enemy if Input.trigger?(Configs::SELECT_ENEMY_KEY)
  265.     #find_path if Mouse.click?(:L)
  266.   end
  267.  
  268.   def remove_drop
  269.     return if $typing
  270.     #@got_drop = false
  271.     $game_map.drops.each_with_index do |drop, i|
  272.       #next unless Mouse.in_tile?(drop)
  273.       #next unless in_range?(drop, 1)
  274.       next unless pos?(drop.x, drop.y)
  275.       $network.send_remove_drop(i)
  276.       #@got_drop = true
  277.       break
  278.     end
  279.   end
  280.  
  281.   def update_hotbar
  282.     if @item_attack_time > 0
  283.       @item_attack_time -= 1
  284.       return
  285.     end
  286.     use_hotbar
  287.   end
  288.  
  289.   def use_hotbar
  290.     return if $typing
  291.     actor.hotbar.each_with_index do |item, i|
  292.       next unless Input.press?(Configs::HOTKEYS[i])
  293.       @item_attack_time = Configs::COOLDOWN_SKILL_TIME
  294.       $network.send_use_hotbar(i) if item && usable?(item)
  295.       break
  296.     end
  297.   end
  298.  
  299.   def select_nearest_enemy
  300.     # Se a tecla é uma letra e a caixa de texto estiver ativa
  301.     return if $typing
  302.     event_id = 0
  303.     sx = 10
  304.     sy = 10
  305.     $game_map.events.each_value do |event|
  306.       # Se é um inimigo que não morreu e está no alcance
  307.       next unless event.actor? && in_range?(event, 10)
  308.       real_distance_x = distance_x_from(event.x).abs
  309.       real_distance_y = distance_y_from(event.y).abs
  310.       if real_distance_x + real_distance_y < sx + sy
  311.         event_id = event.id
  312.         sx = real_distance_x
  313.         sy = real_distance_y
  314.       end
  315.     end
  316.     $network.send_target(event_id, Constants::TARGET_ENEMY) if event_id > 0
  317.   end
  318.  
  319.   def find_path
  320.     return unless map_passable?(Mouse.tile_x, Mouse.tile_y, @direction)
  321.     return if $game_map.interpreter.running?
  322.     return if over_window?
  323.     return if @got_drop
  324.     return if @@target
  325.     @path = Path.new(Mouse.tile_x, Mouse.tile_y)
  326.   end
  327.  
  328.   def over_window?
  329.     $windows.each_value do |window|
  330.       if window.is_a?(Window_Chat) && window.opacity > 0 && window.in_area?(0, 0, window.width, window.height + 22)
  331.         return true
  332.       elsif window.is_a?(Window) && window.visible && window.in_area?
  333.         return true
  334.       elsif window.is_a?(Sprite2) && window.in_area?
  335.         return true
  336.       end
  337.     end
  338.     SceneManager.scene.icons.each { |icon| return true if icon.in_area? }
  339.     return false
  340.   end
  341. =begin
  342.   def update_path
  343.     #if @lost_path
  344.       #update_lost_path
  345.     #else
  346.       update_normal_path
  347.     #end
  348.   end
  349.  
  350.   def update_normal_path
  351.     sx = distance_x_from(@path.x)
  352.     sy = distance_y_from(@path.y)
  353.     if sx.abs > sy.abs
  354.       d = sx > 0 ? 4 : 6
  355.       send_path_movement(d)
  356.       send_path_movement(sy > 0 ? 8 : 2) if !passable?(@x, @y, d) && sy != 0
  357.     elsif sy != 0
  358.       d = sy > 0 ? 8 : 2
  359.       send_path_movement(d)
  360.       send_path_movement(sx > 0 ? 4 : 6) if !passable?(@x, @y, d) && sx != 0
  361.     end
  362.     #@try_path += pos?(@real_x, @real_y) ? 1 : 0
  363.     @lost_path = pos?(@real_x, @real_y)
  364.     # Se o @path não foi limpado
  365.     @path = nil if @path && pos?(@path.x, @path.y) && !@target
  366.     #@path = nil if (@path && pos?(@path.x, @path.y) || @try_path == 4) && !@target
  367.   end
  368.  
  369.   def update_lost_path
  370.     if @direction == 4 || @direction == 6
  371.       if passable?(@x, @y, 2)
  372.         send_movement(2)
  373.         @lost_path = false
  374.       elsif passable?(@x, @y, 8)
  375.         send_movement(8)
  376.         @lost_path = false
  377.       else
  378.         send_movement(reverse_dir(@direction))
  379.       end
  380.     elsif @direction == 2 || @direction == 8
  381.       if passable?(@x, @y, 4)
  382.         send_movement(4)
  383.         @lost_path = false
  384.       elsif passable?(@x, @y, 6)
  385.         send_movement(6)
  386.         @lost_path = false
  387.       else
  388.         send_movement(reverse_dir(@direction))
  389.       end
  390.     end
  391.     @try_path += pos?(@real_x, @real_y) ? 1 : 0
  392.     if @try_path == 4
  393.       @path = nil
  394.       @lost_path = false
  395.     end
  396.   end
  397. =end
  398.   def send_path_movement(d)
  399.     if passable?(@x, @y, d) && !@path.is_a?(Path) && @path.pos?($game_map.round_x_with_direction(@x, @direction), $game_map.round_y_with_direction(@y, @direction))
  400.       @path = nil
  401.       return
  402.     end
  403.     send_movement(d)
  404.   end
  405.  
  406. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement