#=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# Automatic Event Movement
# Author: ForeverZer0
# Version: 1.0
# Date: 10.22.10
#=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
#
# Introduction:
# This script will allow you to easily set up events to move across the game
# map in random manner. The event will move from a point on the map's edge in
# a line until it reaches another map edge, at which point it will transfer
# randomly to another point and take a new direction and repeat. The user
# also has the option to define possible directions that the event can switch
# to and the number of tiles that event can move from its position. All of
# the setup is a few simple comments in the event's page.
#
# Instructions:
# Create an event, setting the graphic, move speed, and move frequency as
# desired. Don't forget to check the "Always on Top" and "Through" boxes if
# using this for a bird. Set the event's direction if you do not want it to be
# chosen randomly by changing the graphic to the appropriate direction. If the
# "Direction Fix" box is checked, it will disable the event from changing
# directions randomly when it begins a new route across the screen.
#
# Optional Featues:
#
# To set the possible directions that the event can choose from randomly
# after it exits the screen, create another comment under the first that
# reads:
#
# Directions = DIRECTION1 DIRECTION2 DIRECTION3 etc etc
#
# Do not seperate the directions with commas, simply use single spaces.
# The values are numbers that correspond to each directions. The directions
# are releative to a calculator keypad setup:
#
# 7 8 9
# 4 6
# 1 2 3
#
# To set the variance, which is the number of tiles give or take that the
# event can possibly shift from its original position when re-entering the
# screen, create a comment that reads:
#
# Variance = NUMBER_OF_TILES
#
# Here is an example of what the comment should look like:
#
# Comment: Auto Move
# Directions = 4 6
# Variance = 1
#
# In this example, the event will move randomly either left or right across
# the screen, and stay within one tile either way from it original coords.
#
#=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
class Game_Event
# Private constant. DO NOT edit.
MOVES = [nil, :move_lower_left, :move_down, :move_lower_right, :move_left,
nil, :move_right, :move_upper_left, :move_up, :move_upper_right]
alias zer0_automove_event_init initialize
def initialize(map_id, event)
# Set event's original coordinates and map size in instance variables.
@coords = [event.x, event.y]
@map_w, @map_h = $game_map.width, $game_map.height
zer0_automove_event_init(map_id, event)
end
alias zer0_automove_event_refresh refresh
def refresh
zer0_automove_event_refresh
if @page != nil
# Set flag if comment "Auto Move" is found on top line.
if @page.list[0].code == 108 && @page.list[0].parameters[0] == 'Auto Move'
@bird_event, @dir = true, @direction
# Set directions and variance if defined, else it will be chosen randomly.
@bird_variance = comment_value('Variance = ')
directions = comment_value('Directions = ', true)
@directions = directions == nil ? [1, 2, 3, 4, 6, 7, 8, 9] : directions
end
end
end
def comment_value(string, to_a = false)
# Returns the defined integer following the string in an event comment.
@page.list.each {|command| if [108, 408].include?(command.code) &&
command.parameters[0] =~ string
string = command.parameters[0].gsub(string) { '' }
return to_a ? string.split(' ').collect {|num| num.to_i } : string.to_i
end }
return
end
alias zer0_automove_event_upd update
def update
# Check if this event is a auto-move and not moving.
if @bird_event && !moving?
# Check if event is on the screen.
unless at_map_edge?
self.send(MOVES[@dir])
else # If event has reached edge of screen.
# Reset the event's direction unless the Direction Fix box checked.
@dir = @directions[rand(@directions.size)] unless @page.direction_fix
x, y = @dir == 4 ? @map_w-1 : 0, [1, 2, 3].include?(@dir) ? 0 : @map_h-1
if @bird_variance == nil
# Set coordinates randomly if no variance is defined.
[4, 6].include?(@dir) ? moveto(x, rand(@map_h)) : moveto(rand(@map_w), y)
else
value = rand(@bird_variance+1)
# If variance is defined, keep within defined range, randomly.
if [4, 6].include?(@dir)
moveto(x, rand(2) == 0 ? (@coords[1]+value) : (@coords[1]-value))
else
moveto(rand(2) == 0 ? (@coords[0]+value) : (@coords[0]-value), y)
end
end
end
end
zer0_automove_event_upd
end
def at_map_edge?
# Returns true if event's next move is off the map's visible edge.
return case @dir
when 1 then self.x == 0 || self.y == @map_h-1
when 2 then self.y == @map_h-1
when 3 then self.x == @map_w-1 || self.y == @map_h-1
when 4 then self.x == 0
when 6 then self.x == @map_w-1
when 7 then [self.x, self.y].include?(0)
when 8 then self.y == 0
when 9 then self.x == @map_w-1 || self.y == 0
end
end
end