Home > RGSS Script Reference > Game_Player
Game_Player
Inherits from: Game_Character
Description: This class handles centering the map relative to the player, event triggering decisions relative to the player, and has methods for dealing with increasing the step count and encounter steps.
class Game_Player < Game_Character
# ------------------------------------
CENTER_X = (320 - 16) * 4
CENTER_Y = (240 - 16) * 4
# ------------------------------------
def passable?(x, y, d)
new_x = x + (d == 6 ? 1 : d == 4 ? -1 : 0)
new_y = y + (d == 2 ? 1 : d == 8 ? -1 : 0)
unless $game_map.valid?(new_x, new_y)
return false
end
if $DEBUG and Input.press?(Input::CTRL)
return true
end
super
end
# ------------------------------------
def center(x, y)
max_x = ($game_map.width - 20) * 128
max_y = ($game_map.height - 15) * 128
$game_map.display_x = [0, [x * 128 - CENTER_X, max_x].min].max
$game_map.display_y = [0, [y * 128 - CENTER_Y, max_y].min].max
end
# ------------------------------------
def moveto(x, y)
super
center(x, y)
make_encounter_count
end
# ------------------------------------
def increase_steps
super
unless @move_route_forcing
$game_party.increase_steps
if $game_party.steps % 2 == 0
$game_party.check_map_slip_damage
end
end
end
# ------------------------------------
def encounter_count
return @encounter_count
end
# ------------------------------------
def make_encounter_count
n = $game_map.encounter_step
@encounter_count = rand(n) + rand(n) + 1
end
# ------------------------------------
def refresh
if $game_party.actors.size == 0
@character_name = ""
@character_hue = 0
return
end
actor = $game_party.actors[0]
@character_name = actor.character_name
@character_hue = actor.character_hue
@opacity = 255
@blend_type = 0
end
# ------------------------------------
def check_event_trigger_here(triggers)
result = false
if $game_system.map_interpreter.running?
return result
end
for event in $game_map.events.values
if event.x == @x and event.y == @y and triggers.include?(event.trigger)
if event.over_trigger?
event.start
result = true
end
end
end
return result
end
# ------------------------------------
def check_event_trigger_there(triggers)
result = false
if $game_system.map_interpreter.running?
return result
end
new_x = @x + (@direction == 6 ? 1 : @direction == 4 ? -1 : 0)
new_y = @y + (@direction == 2 ? 1 : @direction == 8 ? -1 : 0)
for event in $game_map.events.values
if event.x == new_x and event.y == new_y and
triggers.include?(event.trigger)
unless event.over_trigger?
event.start
result = true
end
end
end
if result == false
if $game_map.counter?(new_x, new_y)
new_x += (@direction == 6 ? 1 : @direction == 4 ? -1 : 0)
new_y += (@direction == 2 ? 1 : @direction == 8 ? -1 : 0)
for event in $game_map.events.values
if event.x == new_x and event.y == new_y and
triggers.include?(event.trigger)
unless event.over_trigger?
event.start
result = true
end
end
end
end
end
return result
end
# ------------------------------------
def check_event_trigger_touch(x, y)
result = false
if $game_system.map_interpreter.running?
return result
end
for event in $game_map.events.values
if event.x == x and event.y == y and [1,2].include?(event.trigger)
unless event.over_trigger?
event.start
result = true
end
end
end
return result
end
# ------------------------------------
def update
last_moving = moving?
unless moving? or $game_system.map_interpreter.running? or
@move_route_forcing or $game_temp.message_window_showing
case Input.dir4
when 2
move_down
when 4
move_left
when 6
move_right
when 8
move_up
end
end
last_real_x = @real_x
last_real_y = @real_y
super
if @real_y > last_real_y and @real_y - $game_map.display_y > CENTER_Y
$game_map.scroll_down(@real_y - last_real_y)
end
if @real_x < last_real_x and @real_x - $game_map.display_x < CENTER_X
$game_map.scroll_left(last_real_x - @real_x)
end
if @real_x > last_real_x and @real_x - $game_map.display_x > CENTER_X
$game_map.scroll_right(@real_x - last_real_x)
end
if @real_y < last_real_y and @real_y - $game_map.display_y < CENTER_Y
$game_map.scroll_up(last_real_y - @real_y)
end
unless moving?
if last_moving
result = check_event_trigger_here([1,2])
if result == false
unless $DEBUG and Input.press?(Input::CTRL)
if @encounter_count > 0
@encounter_count -= 1
end
end
end
end
if Input.trigger?(Input::C)
check_event_trigger_here([0])
check_event_trigger_there([0,1,2])
end
end
end
end
|
Encounter_Count: This counter is decremented each step. When it reaches 0, a random encounter is triggered.
Passable?
Arguments:
X: The X coordinate of the tile to check.
Y: The Y coordinate of the tile to check.
D: The direction to check for passability (0 = Multidirectional, 2 = Down, 4 = Left, 6 = Right, 8 = Up).
Local Variables:
New_X: The X coordinate of the destination tile.
New_Y: The Y coordinate of the destination tile.
How it Works: This method checks whether the hero sprite can pass from one tile to another. The first thing it does is computes the value of new_X and new_Y based on the current X and Y coordinates and the direction the hero sprite is facing. The unless $game_map.valid?(new_x, new_y) makes the method return false if the destination tile isn't within the bounds of the map. The final part of the method checks to see if the $debug flag is set and the user is holding the CTRL key (The debug flag is set if the user is test-playing as opposed to playing using a standalone executable). If both of these conditions are true, then any tile within the bounds of the map can be passed, so the method returns true. Otherwise, the call to Game_Character#Passable? performs the checks for passability that are common to both NPC sprites and the hero sprite. See the Game_Character page for a description of this method.
Center
Arguments:
X: The hero sprite's X coordinate in tiles.
Y: The hero sprite's Y coordinate in tiles.
Local Variables:
Max_X: The largest horizontal scroll value of the map, in real unts (4 real units = 1 pixel)
Max_Y: The largest vertical scroll value of the map, in real unts (4 real units = 1 pixel)
How it Works: Centers the map display relative to the hero. The first two statements set the values of Max_X and Max_Y to the maximum possible scroll values in the respective directions. The next two statements computer how far the map should be scrolled in each direction given the hero sprite's position. Note that that constants CENTER_X and CENTER_Y are subtracted from the hero's real coordinates because the map scrolling "lags behind" the hero sprite's movement by ~9.5 tiles in the X direction, and ~8 tiles in the Y direction. This adjustment ensures that lag enters into the equation. The values are then bounds-checked to ensure they are not less than 0 or more than the maximum scroll value (this happens when the hero sprite is near the edge of the map).
Moveto
Arguments:
X: The X coordinate to which to move the hero sprite.
Y: The Y coordinate to which to move the hero sprite.
Local Variables: None
How it Works: Moves the hero sprite to directly to the X and Y coordinates specified. First, a call to Game_Character#Moveto is made to execute the part of the method that is common to both NPC sprites and the hero sprite. Then, the screen is centered on the hero sprite, and finally, a check is made to see whether a random encounter occurs.
Increase_Steps
Arguments: None
Local Variables: None
How it Works: Deals with the effects of taking a step. The call to Game_Character#Increase_Steps resets the sprite's stop counter (for walk graphic processing), and then proceeds to handle effects of taking a step unique to the hero sprite. Unless the hero sprite's movement is being forced by a "Move Event" command, the party's number of steps is incremented. If the party's number of steps is an even number, the method makes a call to Game_Party#Check_Slip_Damage to see if any characters in the party need to be damaged as a result of status ailments that cause progressive damage on the map.
Encounter_Count
Arguments: None
Local Variables: None
How it Works: Returns the current value of @encounter_count.
Make_Encounter_Count
Arguments: None
Local Variables:
n: The average steps per encounter set for the current map.
How it Works: Sets @encounter_count to a value between 1 and 2n-1, where n is the average number of steps per encounter set for the current map.
Refresh
Arguments: None
Local Variables:
Actor: The first character in the party.
How it Works: Refreshes the graphic shown for the hero sprite. If the party size is zero, then a transparent graphic is shown. Otherwise, the method finds the ID of the first actor in the party. It then sets the character's graphic and hue to the ones defined for that actor. Then it sets the hero's opacity to 255 and its blend type to "Normal".
Check_Event_Trigger_Here
Arguments:
Triggers: An array of the triggers to check (0 = Action Key, 1 = Touched by Hero, 2 = Collision with Hero)
Local Variables:
Result: A boolean value returned to the caller.
How it Works: Checks to see if an event on the current tile should be triggered. First, the value of result is set to
false. Next, it checks to see if an event is in the process of executing. If it is, then the method returns false, since a new non-parallel event can't be triggered if one is already running. The for event in $game_map.events.values loop checks each event to see if the event's X and Y coordinates match the player's X and Y coordinates. If they do, and the event has one of the triggers passed as arguments, the method checks to see if Game_Event#over_trigger? is true for that event (that is, the event is transparent or has the Phasing property). If it is, then the event is started and the method returns. Otherwise, the method returns false.
Check_Event_Trigger_There
Arguments:
Triggers: An array of the triggers to check (0 = Action Key, 1 = Touched by Hero, 2 = Collision with Hero)
Local Variables:
Result: A boolean value returned to the caller.
New_X: The X coordinate of the tile to check for triggered events.
New_Y: The Y coordinate of the tile to check for triggered events.
How it Works: Checks to see if an event on the current tile should be triggered. First, the value of result is set to
false. Next, it checks to see if an event is in the process of executing. If it is, then the method returns false, since a new non-parallel event can't be triggered if one is already running. The next part of the method calculates the X/Y coordinates of the tile check by checking the hero sprite's @direction and adding or subtracting 1 from the hero sprite's X and/or Y coordinates, as appropriate. Then, every event on the map is checked to see if it is occupying the tile specified by new_x and new_y. If it is, and has one of the triggers passed to the method as arguments, the event is started unless it is intended to be triggered from the same space (such as a Phasing event or a transparent event). If there is no event to be triggered one tile in front of the hero sprite, the method next checks to see if an event should be triggered over a counter. If the tile directly in front of the hero is a counter, then the same checks as above are performed on the tile two tiles away in the direction the hero is facing. If that check also fails, then the method returns false.
Check_Event_Trigger_Touch
Arguments:
X: The X coordinate to check for on-touch events.
Y: The Y coordinate to check for on-touch events.
Local Variables:
Result: A boolean value returned to the caller.
How it Works: Checks to see if an event on the current tile should be triggered. First, the value of result is set to
false. Next, it checks to see if an event is in the process of executing. If it is, then the method returns false, since a new non-parallel event can't be triggered if one is already running. The loop checks every event on the map to see if its X and Y coordinates match the X and Y coordinates passed to the method. If they do and the event's trigger is 1 (Touched by Hero) or 2 (Collision with Hero), then the event is started unless it is intended to be triggered from the same tile. If the check fails for every event on the map, the method returns false.
Update
Arguments: None
Local Variables:
Last_Moving: Saves the hero sprite's movement flag.
Last_Real_X: Saves the hero sprite's last X coordinate in real unts (4 real units = 1 pixel)
Last_Real_Y: Saves the hero sprite's last Y coordinate in real unts (4 real units = 1 pixel)
How it Works: Updates the hero sprite's state each frame. The method starts off by remembering whether or not the character is moving prior to updating its state. It then checks to see if the player has one of the arrow keys pushed. If the character isn't already moving, a non-parallel event isn't running, isn't being forced by a "Move Event" operation, and a message window isn't being displayed onscreen, the method calls the movement method corresponding to the arrow key the user has pressed. Then, the method memorizes the hero sprite's X and Y coordinates in real units. After doing all that, the call to Game_Character#Update handles the effects of the hero sprite's current behavior on the sprite graphic itself. The rest of the present method deals with centering the hero sprite onscreen, checking whether the hero has triggered an event, and decrementing the random encounter counter. The next four if statements check to see if the map needs to be scrolled as a result of the hero sprite having moved. The unless moving? clause processes what happens when this frame's update caused the hero sprite to arrive at a new tile. "Touched by Hero" and "Collision with Hero" events are checked to see if the arrival has triggered them and the @encounter_count value is decremented unless the player is test-playing and holding CTRL. Finally, the method checks to see if the player is pressing the "C" button. If he or she is, then events on the same tile with the "Action Key" trigger and events on the tile directly in front of the hero with the "Action Key", "Touched by Hero" and "Collision with Hero" triggers are checked to see if they have been triggered by the hero.
|
|