- 320
- Posts
- 15
- Years
- Seen Dec 27, 2021
I've made a modified version of pbMoveTowardPlayer, which allows an event to move towards another event instead of the player ( handy for cutscenes etc where positions are dynamic ).
Here is the script, place it somewhere above main:
The script can be used in an event like this:
Where :Distraction and :Redwood are the names of the respective events ( case insensitive ).
Or to move to a specified position instead:
Where :Distraction is the event's name, 24 is the X, and 15 is the Y.
Hope this is useful for some of you :)
If you find any bugs or have a suggestion, please let me know.
Here is the script, place it somewhere above main:
Spoiler:
Code:
class Game_Character
def move_toward_event(event)
sx = @x - event.x
sy = @y - event.y
if sx == 0 and sy == 0
return
end
abs_sx = sx.abs
abs_sy = sy.abs
if abs_sx == abs_sy
(rand(2) == 0) ? abs_sx += 1 : abs_sy += 1
end
if abs_sx > abs_sy
sx > 0 ? move_left : move_right
if not moving? and sy != 0
sy > 0 ? move_up : move_down
end
else
sy > 0 ? move_up : move_down
if not moving? and sx != 0
sx > 0 ? move_left : move_right
end
end
end
def move_toward_position(x, y)
sx = @x - x
sy = @y - y
if sx == 0 and sy == 0
return
end
abs_sx = sx.abs
abs_sy = sy.abs
if abs_sx == abs_sy
(rand(2) == 0) ? abs_sx += 1 : abs_sy += 1
end
if abs_sx > abs_sy
sx > 0 ? move_left : move_right
if not moving? and sy != 0
sy > 0 ? move_up : move_down
end
else
sy > 0 ? move_up : move_down
if not moving? and sx != 0
sx > 0 ? move_left : move_right
end
end
end
end
def getEventByName(name)
if name.is_a?(Symbol)
name = name.to_s
end
$game_map.events.values.each do |event|
return event if event.name.downcase == name.downcase
end
return nil
end
def _resolveEvent(event)
if event.is_a?(Game_Event)
return event
end
if event.is_a?(Numeric)
return get_character(event)
end
return getEventByName(event) # Symbol or string
end
def pbMoveTowardEvent(event, otherEvent)
event = _resolveEvent(event)
otherEvent = _resolveEvent(otherEvent)
maxsize=[$game_map.width,$game_map.height].max
#return if !pbEventCanReachPlayer?(event,otherEvent,maxsize)
loop do
x=event.x
y=event.y
event.move_toward_event(otherEvent)
break if event.x==x && event.y==y
while event.moving?
Graphics.update
Input.update
pbUpdateSceneMap
end
end
$PokemonMap.addMovedEvent(event.id) if $PokemonMap
end
def pbMoveTowardPosition(event, x, y)
event = _resolveEvent(event)
maxsize=[$game_map.width,$game_map.height].max
#return if !pbEventCanReachPlayer?(event,otherEvent,maxsize)
loop do
event.move_toward_position(x, y)
break if event.x==x && event.y==y
while event.moving?
Graphics.update
Input.update
pbUpdateSceneMap
end
end
$PokemonMap.addMovedEvent(event.id) if $PokemonMap
end
The script can be used in an event like this:
Code:
pbMoveTowardEvent(:Distraction, :Oak)
Where :Distraction and :Redwood are the names of the respective events ( case insensitive ).
Or to move to a specified position instead:
Code:
pbMoveTowardPosition(:Distraction, 24, 15)
Where :Distraction is the event's name, 24 is the X, and 15 is the Y.
Hope this is useful for some of you :)
If you find any bugs or have a suggestion, please let me know.
Last edited: