#==============================================================================
# ** Encounter Detection Chaser
#------------------------------------------------------------------------------
# SephirothSpawn
# Version 1.01
# 2007-08-29
# SDK : Version 2.0+, Part I, II
#------------------------------------------------------------------------------
# * Version History :
#
# Version 1 ---------------------------------------------------- (2007-08-16)
# Version 1.01 ------------------------------------------------ (2007-08-29)
# - Update : Moved move_toward_target method to MACL
#------------------------------------------------------------------------------
# * Requirements :
#
# Method & Class Library 2.11+
#------------------------------------------------------------------------------
# * Description :
#
# This script was designed to allow you to make quick events chase your
# player (or another event) with a simple comment line. You selete a target,
# a chase speed and a start and stop range (when the target gets x distance).
#------------------------------------------------------------------------------
# * Instructions :
#
# Place The Script Below the SDK and Above Main.
#
# Comment : ENCOUNTER DETECTION a b c d e f
#
# a : target (0 - player, event_id - event)
# b : speed (movement speed)
# c : start range (range when event starts chasing player)
# d : stop range (range when event stops chasing player) (should be > c)
# e : Animation id to display when in sight (optional)
# f : animation id to display when giving up (optional, but requires e)
#------------------------------------------------------------------------------
# * Syntax :
#
# You may change the chaser values, but when an event is refreshed, the
# instance @encdet_is_chaser is reset to false and if any comment lines
# are read, it will reset the instances as well.
#
# Instance List : encdet_is_chaser, encdet_is_chasing, encdet_chase_target
# encdet_chase_speed, encdet_chase_start_range,
# encdet_chase_stop_range, encdet_chase_start_aid,
# encdet_chase_stop_aid
#
# All instances of Game_Event
#------------------------------------------------------------------------------
# * Special Thanks :
#
# Thanks to Khatharr for helping me debug the chaser bug.
#==============================================================================
#------------------------------------------------------------------------------
# * SDK Log Script
#------------------------------------------------------------------------------
SDK.log('Encounter Detection Chaser', 'SephirothSpawn', 1.01, '2007-08-29')
SDK.check_requirements(2.0, [2], {'Method & Class Library' => 2.11})
#------------------------------------------------------------------------------
# * Begin SDK Enable Test
#------------------------------------------------------------------------------
if SDK.enabled?('Encounter Detection Chaser')
#==============================================================================
# ** Game_Event
#==============================================================================
class Game_Event
#--------------------------------------------------------------------------
# * Public Instance Variables
#--------------------------------------------------------------------------
attr_accessor :encdet_is_chaser
attr_accessor :encdet_is_chasing
attr_accessor :encdet_chase_target
attr_accessor :encdet_chase_speed
attr_accessor :encdet_chase_start_range
attr_accessor :encdet_chase_stop_range
attr_accessor :encdet_chase_start_aid
attr_accessor :encdet_chase_stop_aid
#--------------------------------------------------------------------------
# * Alias Listings
#--------------------------------------------------------------------------
alias_method :seph_encdet_gmevt_init, :initialize
alias_method :seph_encdet_gmevt_rsp, :refresh_set_page
alias_method :seph_encdet_gmevt_um, :update_movement
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize(*args)
# Original Initialization
seph_encdet_gmevt_init(*args)
# Sets Enc Chaser Off if not on
@encdet_is_chaser = false unless @encdet_is_chaser
end
#--------------------------------------------------------------------------
# * Refresh Set Page
#--------------------------------------------------------------------------
def refresh_set_page
# Original Set Page
seph_encdet_gmevt_rsp
# Sets Enc Chaser Off
@encdet_is_chaser = false
@encdet_chase_start_aid = nil
@encdet_chase_stop_aid = nil
# Pass Through Event Commands
for ec in @list
# Skip if Not Comment Code
next unless [108, 408].include?(ec.code)
# Skip if Comment Doesn't Include Encounter Detection
next unless ec.parameters[0].upcase.include?('ENCOUNTER DETECTION')
# Gets Parameters
pm = ec.parameters[0].split
# Turn Chaser Flag On
@encdet_is_chaser = true
# Set Chasing Paramters
t = pm[2].to_i
@encdet_chase_target = t == 0 ? $game_player : $game_map.events[t]
@encdet_chase_speed = pm[3].to_i
@encdet_chase_start_range = pm[4].to_i
@encdet_chase_stop_range = pm[5].to_i
@encdet_chase_start_aid = pm[6].to_i if pm.size > 6
@encdet_chase_stop_aid = pm[7].to_i if pm.size > 7
# Gets Range to Target
range = VR.range(self, @encdet_chase_target)
# Gets Chasing Status
b = @encdet_is_chasing
# Fix Chasing Status - If Chasing, Turn off if past stop range
# If not chasing, turn on if within start range
@encdet_is_chasing = b ? range > @encdet_chase_stop_range :
range <= @encdet_chase_start_range
# If was chasing, but isn't anymore
if b && @encdet_is_chasing == false && @encdet_chase_stop_aid != nil
# Set to Non-chasing animation id
@animation_id = @encdet_chase_stop_aid
end
# If wasn't chasing, but is now
if b == false && @encdet_is_chasing && @encdet_chase_start_aid != nil
# Set to chasing animation id
@animation_id = @encdet_chase_start_aid
end
# Update Encounter Detection Movespeed
update_encdet_movespeed
end
end
#--------------------------------------------------------------------------
# * Frame Update : Update Movement
#--------------------------------------------------------------------------
def update_movement
# If Chaser
if @encdet_is_chaser && moving? == false
# Gets Range to Target
range = VR.range(self, @encdet_chase_target)
# If Chasing - Turn off if Range is greater than stop range
if @encdet_is_chasing && range > @encdet_chase_stop_range
@encdet_is_chasing = false
# Set to Non-chasing animation id
unless @encdet_chase_stop_aid.nil?
@animation_id = @encdet_chase_stop_aid
end
# If Not Chasing But Range is Less that start range
elsif !@encdet_is_chasing && range <= @encdet_chase_start_range
# Turn Chasing On
@encdet_is_chasing = true
# Set to chasing animation id
unless @encdet_chase_start_aid.nil?
@animation_id = @encdet_chase_start_aid
end
end
# If On, Move Toward Player
if @encdet_is_chasing
move_toward_target(@encdet_chase_target.x, @encdet_chase_target.y)
end
end
# Update Enc Det Movespeed
update_encdet_movespeed
# Original Update Movement
seph_encdet_gmevt_um
end
#--------------------------------------------------------------------------
# * Frame Update : Enc Det Movespeed
#--------------------------------------------------------------------------
def update_encdet_movespeed
# If Chasing and Chase Speed isn't nil
if @encdet_is_chasing && @encdet_chase_speed != nil &&
@encdet_chase_old_speed.nil?
# Save Old Speed
@encdet_chase_old_speed = @move_speed
# Set Move Speed to Chase Speed
@move_speed = @encdet_chase_speed
# If Not Chasing and Old Speed has been set
elsif @encdet_is_chasing == false && @encdet_chase_old_speed != nil
# Restore Movespeed
@move_speed = @encdet_chase_old_speed
# Set Old Speed to nil
@encdet_chase_old_speed = nil
end
end
#--------------------------------------------------------------------------
# * Move object toward target
#--------------------------------------------------------------------------
def move_toward_target(x, y)
# Get difference in player coordinates
sx = @x - x
sy = @y - y
# If coordinates are equal
return if sx == 0 and sy == 0
# Get absolute value of difference
abs_sx = sx.abs
abs_sy = sy.abs
# If horizontal and vertical distances are equal
if abs_sx == abs_sy
# Increase one of them randomly by 1
rand(2) == 0 ? abs_sx += 1 : abs_sy += 1
end
# If horizontal distance is longer
if abs_sx > abs_sy
# Move towards player, prioritize left and right directions
sx > 0 ? move_left : move_right
if not moving? and sy != 0
sy > 0 ? move_up : move_down
end
# If vertical distance is longer
else
# Move towards player, prioritize up and down directions
sy > 0 ? move_up : move_down
if not moving? and sx != 0
sx > 0 ? move_left : move_down
end
end
end
end
#------------------------------------------------------------------------------
# * End SDK Enable Test
#------------------------------------------------------------------------------
end