Advertisement
nio_kasgami

XP game_Player

Dec 30th, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.09 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.  
  9. class Game_Player < Game_Character
  10.   #--------------------------------------------------------------------------
  11.   # * Invariables
  12.   #--------------------------------------------------------------------------
  13.   CENTER_X = (320 - 16) * 4   # Center screen x-coordinate * 4
  14.   CENTER_Y = (240 - 16) * 4   # Center screen y-coordinate * 4
  15.   #--------------------------------------------------------------------------
  16.   # * Passable Determinants
  17.   #     x : x-coordinate
  18.   #     y : y-coordinate
  19.   #     d : direction (0,2,4,6,8)
  20.   #         * 0 = Determines if all directions are impassable (for jumping)
  21.   #--------------------------------------------------------------------------
  22.   def passable?(x, y, d)
  23.     # Get new coordinates
  24.     new_x = x + (d == 6 ? 1 : d == 4 ? -1 : 0)
  25.     new_y = y + (d == 2 ? 1 : d == 8 ? -1 : 0)
  26.     # If coordinates are outside of map
  27.     unless $game_map.valid?(new_x, new_y)
  28.       # Impassable
  29.       return false
  30.     end
  31.     # If debug mode is ON and ctrl key was pressed
  32.     if $DEBUG and Input.press?(Input::CTRL)
  33.       # Passable
  34.       return true
  35.     end
  36.     super
  37.   end
  38.   #--------------------------------------------------------------------------
  39.   # * Set Map Display Position to Center of Screen
  40.   #--------------------------------------------------------------------------
  41.   def center(x, y)
  42.     max_x = ($game_map.width - 20) * 128
  43.     max_y = ($game_map.height - 15) * 128
  44.     $game_map.display_x = [0, [x * 128 - CENTER_X, max_x].min].max
  45.     $game_map.display_y = [0, [y * 128 - CENTER_Y, max_y].min].max
  46.   end
  47.   #--------------------------------------------------------------------------
  48.   # * Move to Designated Position
  49.   #     x : x-coordinate
  50.   #     y : y-coordinate
  51.   #--------------------------------------------------------------------------
  52.   def moveto(x, y)
  53.     super
  54.     # Centering
  55.     center(x, y)
  56.     # Make encounter count
  57.     make_encounter_count
  58.   end
  59.   #--------------------------------------------------------------------------
  60.   # * Increaase Steps
  61.   #--------------------------------------------------------------------------
  62.   def increase_steps
  63.     super
  64.     # If move route is not forcing
  65.     unless @move_route_forcing
  66.       # Increase steps
  67.       $game_party.increase_steps
  68.       # Number of steps are an even number
  69.       if $game_party.steps % 2 == 0
  70.         # Slip damage check
  71.         $game_party.check_map_slip_damage
  72.       end
  73.     end
  74.   end
  75.   #--------------------------------------------------------------------------
  76.   # * Get Encounter Count
  77.   #--------------------------------------------------------------------------
  78.   def encounter_count
  79.     return @encounter_count
  80.   end
  81.   #--------------------------------------------------------------------------
  82.   # * Make Encounter Count
  83.   #--------------------------------------------------------------------------
  84.   def make_encounter_count
  85.     # Image of two dice rolling
  86.     if $game_map.map_id != 0
  87.       n = $game_map.encounter_step
  88.       @encounter_count = rand(n) + rand(n) + 1
  89.     end
  90.   end
  91.   #--------------------------------------------------------------------------
  92.   # * Refresh
  93.   #--------------------------------------------------------------------------
  94.   def refresh
  95.     # If party members = 0
  96.     if $game_party.actors.size == 0
  97.       # Clear character file name and hue
  98.       @character_name = ""
  99.       @character_hue = 0
  100.       # End method
  101.       return
  102.     end
  103.     # Get lead actor
  104.     actor = $game_party.actors[0]
  105.     # Set character file name and hue
  106.     @character_name = actor.character_name
  107.     @character_hue = actor.character_hue
  108.     # Initialize opacity level and blending method
  109.     @opacity = 255
  110.     @blend_type = 0
  111.   end
  112.   #--------------------------------------------------------------------------
  113.   # * Same Position Starting Determinant
  114.   #--------------------------------------------------------------------------
  115.   def check_event_trigger_here(triggers)
  116.     result = false
  117.     # If event is running
  118.     if $game_system.map_interpreter.running?
  119.       return result
  120.     end
  121.     # All event loops
  122.     for event in $game_map.events.values
  123.       # If event coordinates and triggers are consistent
  124.       if event.x == @x and event.y == @y and triggers.include?(event.trigger)
  125.         # If starting determinant is same position event (other than jumping)
  126.         if not event.jumping? and event.over_trigger?
  127.           event.start
  128.           result = true
  129.         end
  130.       end
  131.     end
  132.     return result
  133.   end
  134.   #--------------------------------------------------------------------------
  135.   # * Front Envent Starting Determinant
  136.   #--------------------------------------------------------------------------
  137.   def check_event_trigger_there(triggers)
  138.     result = false
  139.     # If event is running
  140.     if $game_system.map_interpreter.running?
  141.       return result
  142.     end
  143.     # Calculate front event coordinates
  144.     new_x = @x + (@direction == 6 ? 1 : @direction == 4 ? -1 : 0)
  145.     new_y = @y + (@direction == 2 ? 1 : @direction == 8 ? -1 : 0)
  146.     # All event loops
  147.     for event in $game_map.events.values
  148.       # If event coordinates and triggers are consistent
  149.       if event.x == new_x and event.y == new_y and
  150.          triggers.include?(event.trigger)
  151.         # If starting determinant is front event (other than jumping)
  152.         if not event.jumping? and not event.over_trigger?
  153.           event.start
  154.           result = true
  155.         end
  156.       end
  157.     end
  158.     # If fitting event is not found
  159.     if result == false
  160.       # If front tile is a counter
  161.       if $game_map.counter?(new_x, new_y)
  162.         # Calculate 1 tile inside coordinates
  163.         new_x += (@direction == 6 ? 1 : @direction == 4 ? -1 : 0)
  164.         new_y += (@direction == 2 ? 1 : @direction == 8 ? -1 : 0)
  165.         # All event loops
  166.         for event in $game_map.events.values
  167.           # If event coordinates and triggers are consistent
  168.           if event.x == new_x and event.y == new_y and
  169.              triggers.include?(event.trigger)
  170.             # If starting determinant is front event (other than jumping)
  171.             if not event.jumping? and not event.over_trigger?
  172.               event.start
  173.               result = true
  174.             end
  175.           end
  176.         end
  177.       end
  178.     end
  179.     return result
  180.   end
  181.   #--------------------------------------------------------------------------
  182.   # * Touch Event Starting Determinant
  183.   #--------------------------------------------------------------------------
  184.   def check_event_trigger_touch(x, y)
  185.     result = false
  186.     # If event is running
  187.     if $game_system.map_interpreter.running?
  188.       return result
  189.     end
  190.     # All event loops
  191.     for event in $game_map.events.values
  192.       # If event coordinates and triggers are consistent
  193.       if event.x == x and event.y == y and [1,2].include?(event.trigger)
  194.         # If starting determinant is front event (other than jumping)
  195.         if not event.jumping? and not event.over_trigger?
  196.           event.start
  197.           result = true
  198.         end
  199.       end
  200.     end
  201.     return result
  202.   end
  203.   #--------------------------------------------------------------------------
  204.   # * Frame Update
  205.   #--------------------------------------------------------------------------
  206.   def update
  207.     # Remember whether or not moving in local variables
  208.     last_moving = moving?
  209.     # If moving, event running, move route forcing, and message window
  210.     # display are all not occurring
  211.     unless moving? or $game_system.map_interpreter.running? or
  212.            @move_route_forcing or $game_temp.message_window_showing
  213.       # Move player in the direction the directional button is being pressed
  214.       case Input.dir4
  215.       when 2
  216.         move_down
  217.       when 4
  218.         move_left
  219.       when 6
  220.         move_right
  221.       when 8
  222.         move_up
  223.       end
  224.     end
  225.     # Remember coordinates in local variables
  226.     last_real_x = @real_x
  227.     last_real_y = @real_y
  228.     super
  229.     # If character moves down and is positioned lower than the center
  230.     # of the screen
  231.     if @real_y > last_real_y and @real_y - $game_map.display_y > CENTER_Y
  232.       # Scroll map down
  233.       $game_map.scroll_down(@real_y - last_real_y)
  234.     end
  235.     # If character moves left and is positioned more let on-screen than
  236.     # center
  237.     if @real_x < last_real_x and @real_x - $game_map.display_x < CENTER_X
  238.       # Scroll map left
  239.       $game_map.scroll_left(last_real_x - @real_x)
  240.     end
  241.     # If character moves right and is positioned more right on-screen than
  242.     # center
  243.     if @real_x > last_real_x and @real_x - $game_map.display_x > CENTER_X
  244.       # Scroll map right
  245.       $game_map.scroll_right(@real_x - last_real_x)
  246.     end
  247.     # If character moves up and is positioned higher than the center
  248.     # of the screen
  249.     if @real_y < last_real_y and @real_y - $game_map.display_y < CENTER_Y
  250.       # Scroll map up
  251.       $game_map.scroll_up(last_real_y - @real_y)
  252.     end
  253.     # If not moving
  254.     unless moving?
  255.       # If player was moving last time
  256.       if last_moving
  257.         # Event determinant is via touch of same position event
  258.         result = check_event_trigger_here([1,2])
  259.         # If event which started does not exist
  260.         if result == false
  261.           # Disregard if debug mode is ON and ctrl key was pressed
  262.           unless $DEBUG and Input.press?(Input::CTRL)
  263.             # Encounter countdown
  264.             if @encounter_count > 0
  265.               @encounter_count -= 1
  266.             end
  267.           end
  268.         end
  269.       end
  270.       # If C button was pressed
  271.       if Input.trigger?(Input::C)
  272.         # Same position and front event determinant
  273.         check_event_trigger_here([0])
  274.         check_event_trigger_there([0,1,2])
  275.       end
  276.     end
  277.   end
  278. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement