Advertisement
TechSkylander1518

Big Events in v18

Dec 8th, 2021
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 17.68 KB | None | 0 0
  1. #===============================================================================
  2. # ** Game_Player
  3. #-------------------------------------------------------------------------------
  4. #  This class handles the player. Its functions include event starting
  5. #  determinants and map scrolling. Refer to "$game_player" for the one
  6. #  instance of this class.
  7. #===============================================================================
  8. class Game_Player < Game_Character
  9.   attr_accessor :bump_se
  10.   attr_accessor :charsetData
  11.   attr_accessor :encounter_count
  12.  
  13.   def initialize(*arg)
  14.     super(*arg)
  15.     @lastdir=0
  16.     @lastdirframe=0
  17.     @bump_se=0
  18.   end
  19.  
  20.   def map
  21.     @map = nil
  22.     return $game_map
  23.   end
  24.  
  25.   def bush_depth
  26.     return 0 if @tile_id > 0 || @always_on_top
  27.     xbehind = (@direction==4) ? @x+1 : (@direction==6) ? @x-1 : @x
  28.     ybehind = (@direction==8) ? @y+1 : (@direction==2) ? @y-1 : @y
  29.     # Both current tile and previous tile are on the same map; just return super
  30.     return super if $game_map.valid?(@x,@y) && $game_map.valid?(xbehind,ybehind)
  31.     # The current or the previous tile is on a different map; consult MapFactory
  32.     return 0 if !$MapFactory
  33.     # Get map and coordinates of the current tile
  34.     if $game_map.valid?(@x,@y)
  35.       heremap = self.map; herex = @x; herey = @y
  36.     else
  37.       newhere = $MapFactory.getNewMap(@x,@y)
  38.       return 0 unless newhere && newhere[0]   # Map not found
  39.       heremap = newhere[0]; herex = newhere[1]; herey = newhere[2]
  40.     end
  41.     # Get map and coordinates of the previous tile
  42.     newbehind = $MapFactory.getNewMap(xbehind,ybehind)
  43.     if $game_map.valid?(xbehind,ybehind)
  44.       behindmap = self.map; behindx = xbehind; behindy = ybehind
  45.     else
  46.       return 0 unless newbehind && newbehind[0]   # Map not found
  47.       behindmap = newbehind[0]; behindx = newbehind[1]; behindy = newbehind[2]
  48.     end
  49.     # Return bush depth
  50.     if !jumping?
  51.       return 32 if heremap.deepBush?(herex, herey) && behindmap.deepBush?(behindx, behindy)
  52.       return 12 if heremap.bush?(herex, herey) && !moving?
  53.     end
  54.     return 0
  55.   end
  56.  
  57.   def pbHasDependentEvents?
  58.     return $PokemonGlobal.dependentEvents.length>0
  59.   end
  60.  
  61.   def bump_into_object
  62.     return if @bump_se && @bump_se>0
  63.     pbSEPlay("Player bump")
  64.     @bump_se = Graphics.frame_rate/4
  65.   end
  66.  
  67.   def move_down(turn_enabled = true)
  68.     turn_down if turn_enabled
  69.     if passable?(@x, @y, 2)
  70.       return if pbLedge(0,1)
  71.       return if pbEndSurf(0,1)
  72.       turn_down
  73.       @y += 1
  74.       $PokemonTemp.dependentEvents.pbMoveDependentEvents
  75.       increase_steps
  76.     else
  77.       if !check_event_trigger_touch(@direction)
  78.         bump_into_object
  79.       end
  80.     end
  81.   end
  82.  
  83.   def move_left(turn_enabled = true)
  84.     turn_left if turn_enabled
  85.     if passable?(@x, @y, 4)
  86.       return if pbLedge(-1,0)
  87.       return if pbEndSurf(-1,0)
  88.       turn_left
  89.       @x -= 1
  90.       $PokemonTemp.dependentEvents.pbMoveDependentEvents
  91.       increase_steps
  92.     else
  93.       if !check_event_trigger_touch(@direction)
  94.         bump_into_object
  95.       end
  96.     end
  97.   end
  98.  
  99.   def move_right(turn_enabled = true)
  100.     turn_right if turn_enabled
  101.     if passable?(@x, @y, 6)
  102.       return if pbLedge(1,0)
  103.       return if pbEndSurf(1,0)
  104.       turn_right
  105.       @x += 1
  106.       $PokemonTemp.dependentEvents.pbMoveDependentEvents
  107.       increase_steps
  108.     else
  109.       if !check_event_trigger_touch(@direction)
  110.         bump_into_object
  111.       end
  112.     end
  113.   end
  114.  
  115.   def move_up(turn_enabled = true)
  116.     turn_up if turn_enabled
  117.     if passable?(@x, @y, 8)
  118.       return if pbLedge(0,-1)
  119.       return if pbEndSurf(0,-1)
  120.       turn_up
  121.       @y -= 1
  122.       $PokemonTemp.dependentEvents.pbMoveDependentEvents
  123.       increase_steps
  124.     else
  125.       if !check_event_trigger_touch(@direction)
  126.         bump_into_object
  127.       end
  128.     end
  129.   end
  130.  
  131.   def turnGeneric(dir)
  132.     old_direction = @direction
  133.     super
  134.     if @direction != old_direction && !@move_route_forcing && !pbMapInterpreterRunning?
  135.       Events.onChangeDirection.trigger(self, self)
  136.     end
  137.   end
  138.  
  139.   def pbTriggeredTrainerEvents(triggers,checkIfRunning=true)
  140.     result = []
  141.     # If event is running
  142.     return result if checkIfRunning && $game_system.map_interpreter.running?
  143.     # All event loops
  144.     for event in $game_map.events.values
  145.       next if !event.name[/trainer\((\d+)\)/i]
  146.       distance = $~[1].to_i
  147.       # If event coordinates and triggers are consistent
  148.       if pbEventCanReachPlayer?(event,self,distance) and triggers.include?(event.trigger)
  149.         # If starting determinant is front event (other than jumping)
  150.         result.push(event) if not event.jumping? and not event.over_trigger?
  151.       end
  152.     end
  153.     return result
  154.   end
  155.  
  156.   def pbTriggeredCounterEvents(triggers,checkIfRunning=true)
  157.     result = []
  158.     # If event is running
  159.     return result if checkIfRunning && $game_system.map_interpreter.running?
  160.     # All event loops
  161.     for event in $game_map.events.values
  162.       next if !event.name[/counter\((\d+)\)/i]
  163.       distance = $~[1].to_i
  164.       # If event coordinates and triggers are consistent
  165.       if pbEventFacesPlayer?(event,self,distance) and triggers.include?(event.trigger)
  166.         # If starting determinant is front event (other than jumping)
  167.         result.push(event) if not event.jumping? and not event.over_trigger?
  168.       end
  169.     end
  170.     return result
  171.   end
  172.  
  173.   def pbCheckEventTriggerAfterTurning
  174.   end
  175.  
  176.   def pbCheckEventTriggerFromDistance(triggers)
  177.     ret = pbTriggeredTrainerEvents(triggers)
  178.     ret.concat(pbTriggeredCounterEvents(triggers))
  179.     return false if ret.length==0
  180.     for event in ret
  181.       event.start
  182.     end
  183.     return true
  184.   end
  185.  
  186.   def pbFacingEvent(ignoreInterpreter=false)
  187.     return nil if $game_system.map_interpreter.running? && !ignoreInterpreter
  188.     new_x = @x + (@direction == 6 ? 1 : @direction == 4 ? -1 : 0)
  189.     new_y = @y + (@direction == 2 ? 1 : @direction == 8 ? -1 : 0)
  190.     return nil if !$game_map.valid?(new_x, new_y)
  191.     for event in $game_map.events.values
  192.       next if event.x != new_x || event.y != new_y
  193.       next if event.jumping? || event.over_trigger?
  194.       return event
  195.     end
  196.     if $game_map.counter?(new_x, new_y)
  197.       new_x += (@direction == 6 ? 1 : @direction == 4 ? -1 : 0)
  198.       new_y += (@direction == 2 ? 1 : @direction == 8 ? -1 : 0)
  199.       for event in $game_map.events.values
  200.         next if event.x != new_x || event.y != new_y
  201.         next if event.jumping? || event.over_trigger?
  202.         return event
  203.       end
  204.     end
  205.     return nil
  206.   end
  207.  
  208.   #-----------------------------------------------------------------------------
  209.   # * Passable Determinants
  210.   #     x : x-coordinate
  211.   #     y : y-coordinate
  212.   #     d : direction (0,2,4,6,8)
  213.   #         * 0 = Determines if all directions are impassable (for jumping)
  214.   #-----------------------------------------------------------------------------
  215.   def passable?(x, y, d)
  216.     # Get new coordinates
  217.     new_x = x + (d == 6 ? 1 : d == 4 ? -1 : 0)
  218.     new_y = y + (d == 2 ? 1 : d == 8 ? -1 : 0)
  219.     # If coordinates are outside of map
  220.     return false if !$game_map.validLax?(new_x, new_y)
  221.     if !$game_map.valid?(new_x, new_y)
  222.       return false if !$MapFactory
  223.       return $MapFactory.isPassableFromEdge?(new_x, new_y)
  224.     end
  225.     # If debug mode is ON and Ctrl key was pressed
  226.     return true if $DEBUG and Input.press?(Input::CTRL)
  227.     return super
  228.   end
  229.  
  230.   #-----------------------------------------------------------------------------
  231.   # * Set Map Display Position to Center of Screen
  232.   #-----------------------------------------------------------------------------
  233.   def center(x, y)
  234.     center_x = (Graphics.width/2 - Game_Map::TILE_WIDTH/2) * Game_Map::X_SUBPIXELS
  235.     center_y = (Graphics.height/2 - Game_Map::TILE_HEIGHT/2) * Game_Map::Y_SUBPIXELS
  236.     dispx = x * Game_Map::REAL_RES_X - center_x
  237.     dispy = y * Game_Map::REAL_RES_Y - center_y
  238.     self.map.display_x = dispx
  239.     self.map.display_y = dispy
  240.   end
  241.  
  242.   #-----------------------------------------------------------------------------
  243.   # * Move to Designated Position
  244.   #     x : x-coordinate
  245.   #     y : y-coordinate
  246.   #-----------------------------------------------------------------------------
  247.   def moveto(x, y)
  248.     super
  249.     # Centering
  250.     center(x, y)
  251.     # Make encounter count
  252.     make_encounter_count
  253.   end
  254.  
  255.   #-----------------------------------------------------------------------------
  256.   # * Make Encounter Count
  257.   #-----------------------------------------------------------------------------
  258.   def make_encounter_count
  259.     # Image of two dice rolling
  260.     if $game_map.map_id != 0
  261.       n = $game_map.encounter_step
  262.       @encounter_count = rand(n) + rand(n) + 1
  263.     end
  264.   end
  265.  
  266.   #-----------------------------------------------------------------------------
  267.   # * Refresh
  268.   #-----------------------------------------------------------------------------
  269.   def refresh
  270.     @opacity    = 255
  271.     @blend_type = 0
  272.   end
  273.  
  274.   #-----------------------------------------------------------------------------
  275.   # * Trigger event(s) at the same coordinates as self with the appropriate
  276.   #   trigger(s) that can be triggered
  277.   #-----------------------------------------------------------------------------
  278.   def check_event_trigger_here(triggers)
  279.     result = false
  280.     # If event is running
  281.     return result if $game_system.map_interpreter.running?
  282.     # All event loops
  283.     for event in $game_map.events.values
  284.       # If event coordinates and triggers are consistent
  285.       next if !event.at_coordinate?(@x, @y)
  286.       next if !triggers.include?(event.trigger)
  287.       # If starting determinant is same position event (other than jumping)
  288.       next if event.jumping? || !event.over_trigger?
  289.       event.start
  290.       result = true
  291.     end
  292.     return result
  293.   end
  294.  
  295.   #-----------------------------------------------------------------------------
  296.   # * Front Event Starting Determinant
  297.   #-----------------------------------------------------------------------------
  298.   def check_event_trigger_there(triggers)
  299.     result = false
  300.     # If event is running
  301.     return result if $game_system.map_interpreter.running?
  302.     # Calculate front event coordinates
  303.     new_x = @x + (@direction == 6 ? 1 : @direction == 4 ? -1 : 0)
  304.     new_y = @y + (@direction == 2 ? 1 : @direction == 8 ? -1 : 0)
  305.     return false if !$game_map.valid?(new_x, new_y)
  306.     # All event loops
  307.     for event in $game_map.events.values
  308.       # If event coordinates and triggers are consistent
  309.       next if !event.at_coordinate?(new_x, new_y)
  310.       next if !triggers.include?(event.trigger)
  311.       # If starting determinant is front event (other than jumping)
  312.       next if event.jumping? || event.over_trigger?
  313.       event.start
  314.       result = true
  315.     end
  316.     # If fitting event is not found
  317.     if result == false
  318.       # If front tile is a counter
  319.       if $game_map.counter?(new_x, new_y)
  320.         # Calculate coordinates of 1 tile further away
  321.         new_x += (@direction == 6 ? 1 : @direction == 4 ? -1 : 0)
  322.         new_y += (@direction == 2 ? 1 : @direction == 8 ? -1 : 0)
  323.         return false if !$game_map.valid?(new_x, new_y)
  324.         # All event loops
  325.         for event in $game_map.events.values
  326.           # If event coordinates and triggers are consistent
  327.           next if !event.at_coordinate?(new_x, new_y)
  328.           next if !triggers.include?(event.trigger)
  329.           # If starting determinant is front event (other than jumping)
  330.           next if event.jumping? || event.over_trigger?
  331.           event.start
  332.           result = true
  333.         end
  334.       end
  335.     end
  336.     return result
  337.   end
  338.  
  339.   #-----------------------------------------------------------------------------
  340.   # * Touch Event Starting Determinant
  341.   #-----------------------------------------------------------------------------
  342.   def check_event_trigger_touch(dir)
  343.     result = false
  344.     return result if $game_system.map_interpreter.running?
  345.     # All event loops
  346.     x_offset = (dir == 4) ? -1 : (dir == 6) ? 1 : 0
  347.     y_offset = (dir == 8) ? -1 : (dir == 2) ? 1 : 0
  348.     for event in $game_map.events.values
  349.       next if ![1, 2].include?(event.trigger)   # Player touch, event touch
  350.       # If event coordinates and triggers are consistent
  351.       next if !event.at_coordinate?(@x + x_offset, @y + y_offset)
  352.       if event.name[/trainer\((\d+)\)/i]
  353.         distance = $~[1].to_i
  354.         next if !pbEventCanReachPlayer?(event,self,distance)
  355.       elsif event.name[/counter\((\d+)\)/i]
  356.         distance = $~[1].to_i
  357.         next if !pbEventFacesPlayer?(event,self,distance)
  358.       end
  359.       # If starting determinant is front event (other than jumping)
  360.       next if event.jumping? || event.over_trigger?
  361.       event.start
  362.       result = true
  363.     end
  364.     return result
  365.   end
  366.  
  367.   #-----------------------------------------------------------------------------
  368.   # * Frame Update
  369.   #-----------------------------------------------------------------------------
  370.   def update
  371.     last_real_x = @real_x
  372.     last_real_y = @real_y
  373.     super
  374.     update_screen_position(last_real_x, last_real_y)
  375.     # Update dependent events
  376.     $PokemonTemp.dependentEvents.updateDependentEvents
  377.     # Count down the time between allowed bump sounds
  378.     @bump_se -= 1 if @bump_se && @bump_se>0
  379.     # Finish up dismounting from surfing
  380.     if $PokemonTemp.endSurf && !moving?
  381.       pbCancelVehicles
  382.       $PokemonTemp.surfJump = nil
  383.       $PokemonTemp.endSurf  = false
  384.     end
  385.     update_event_triggering
  386.   end
  387.  
  388.   def update_command_new
  389.     dir = Input.dir4
  390.     unless pbMapInterpreterRunning? or $game_temp.message_window_showing or
  391.            $PokemonTemp.miniupdate or $game_temp.in_menu
  392.       # Move player in the direction the directional button is being pressed
  393.       if dir==@lastdir && Graphics.frame_count-@lastdirframe>Graphics.frame_rate/20
  394.         case dir
  395.         when 2; move_down
  396.         when 4; move_left
  397.         when 6; move_right
  398.         when 8; move_up
  399.         end
  400.       elsif dir!=@lastdir
  401.         case dir
  402.         when 2; turn_down
  403.         when 4; turn_left
  404.         when 6; turn_right
  405.         when 8; turn_up
  406.         end
  407.       end
  408.     end
  409.     # Record last direction input
  410.     @lastdirframe = Graphics.frame_count if dir!=@lastdir
  411.     @lastdir      = dir
  412.   end
  413.  
  414.   # Center player on-screen
  415.   def update_screen_position(last_real_x, last_real_y)
  416.     return if !@moved_this_frame
  417.     center_x = (Graphics.width/2 - Game_Map::TILE_WIDTH/2) * Game_Map::X_SUBPIXELS
  418.     center_y = (Graphics.height/2 - Game_Map::TILE_HEIGHT/2) * Game_Map::Y_SUBPIXELS
  419.     if @real_y < last_real_y and @real_y - $game_map.display_y < center_y
  420.       $game_map.scroll_up(last_real_y - @real_y)
  421.     end
  422.     if @real_y > last_real_y and @real_y - $game_map.display_y > center_y
  423.       $game_map.scroll_down(@real_y - last_real_y)
  424.     end
  425.     if @real_x < last_real_x and @real_x - $game_map.display_x < center_x
  426.       $game_map.scroll_left(last_real_x - @real_x)
  427.     end
  428.     if @real_x > last_real_x and @real_x - $game_map.display_x > center_x
  429.       $game_map.scroll_right(@real_x - last_real_x)
  430.     end
  431.   end
  432.  
  433.   def update_event_triggering
  434.     return if moving?
  435.     # Try triggering events upon walking into them/in front of them
  436.     if @moved_this_frame
  437.       $PokemonTemp.dependentEvents.pbTurnDependentEvents
  438.       result = pbCheckEventTriggerFromDistance([2])
  439.       # Event determinant is via touch of same position event
  440.       result |= check_event_trigger_here([1,2])
  441.       # No events triggered, try other event triggers upon finishing a step
  442.       pbOnStepTaken(result)
  443.     end
  444.     # If C button was pressed, try to manually interact with events
  445.     if Input.trigger?(Input::C) && !$PokemonTemp.miniupdate
  446.       # Same position and front event determinant
  447.       check_event_trigger_here([0])
  448.       check_event_trigger_there([0,2])
  449.     end
  450.   end
  451. end
  452.  
  453.  
  454.  
  455. def pbGetPlayerCharset(meta,charset,trainer=nil,force=false)
  456.   trainer = $Trainer if !trainer
  457.   outfit = (trainer) ? trainer.outfit : 0
  458.   if $game_player && $game_player.charsetData && !force
  459.     return nil if $game_player.charsetData[0]==$PokemonGlobal.playerID &&
  460.                   $game_player.charsetData[1]==charset &&
  461.                   $game_player.charsetData[2]==outfit
  462.   end
  463.   $game_player.charsetData = [$PokemonGlobal.playerID,charset,outfit] if $game_player
  464.   ret = meta[charset]
  465.   ret = meta[1] if !ret || ret==""
  466.   if pbResolveBitmap("Graphics/Characters/"+ret+"_"+outfit.to_s)
  467.     ret = ret+"_"+outfit.to_s
  468.   end
  469.   return ret
  470. end
  471.  
  472. def pbUpdateVehicle
  473.   meta = pbGetMetadata(0,MetadataPlayerA+$PokemonGlobal.playerID)
  474.   if meta
  475.     charset = 1                                 # Regular graphic
  476.     if $PokemonGlobal.diving;     charset = 5   # Diving graphic
  477.     elsif $PokemonGlobal.surfing; charset = 3   # Surfing graphic
  478.     elsif $PokemonGlobal.bicycle; charset = 2   # Bicycle graphic
  479.     end
  480.     newCharName = pbGetPlayerCharset(meta,charset)
  481.     $game_player.character_name = newCharName if newCharName
  482.   end
  483. end
  484.  
  485. def pbCancelVehicles(destination=nil)
  486.   $PokemonGlobal.surfing = false
  487.   $PokemonGlobal.diving  = false
  488.   $PokemonGlobal.bicycle = false if !destination || !pbCanUseBike?(destination)
  489.   pbUpdateVehicle
  490. end
  491.  
  492. def pbCanUseBike?(mapid)
  493.   return true if pbGetMetadata(mapid,MetadataBicycleAlways)
  494.   val = pbGetMetadata(mapid,MetadataBicycle)
  495.   val = pbGetMetadata(mapid,MetadataOutdoor) if val==nil
  496.   return (val) ? true : false
  497. end
  498.  
  499. def pbMountBike
  500.   return if $PokemonGlobal.bicycle
  501.   $PokemonGlobal.bicycle = true
  502.   pbUpdateVehicle
  503.   bikebgm = pbGetMetadata(0,MetadataBicycleBGM)
  504.   pbCueBGM(bikebgm,0.5) if bikebgm
  505. end
  506.  
  507. def pbDismountBike
  508.   return if !$PokemonGlobal.bicycle
  509.   $PokemonGlobal.bicycle = false
  510.   pbUpdateVehicle
  511.   $game_map.autoplayAsCue
  512. end
  513.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement