01-02-2007, 01:00 PM
Cone of Vision + improved "follow hero"
Lobosque
Version: 0.5
Jan 2 2007
Introduction
This script make an event follow if the hero is inside his cone of vision.
This cone has a sight (range) of 4. So if you are inside it, the event will follow you.
And this is a cone with an obstacle (the sand show what the event is seeing)
I think that I saw a script like this once, but I decided to write this one from scratch.
Script
You can just paste this above main:
Instructions
To make an event use the cone to follow you, all you gotta do is set this in an "call script".
The default sight is 7, if you want to use this, just use "add_cone".
to make him stop, use
You can activate a switch too, set the SWITCH constant in the top of the code to the switch ID that you want to be activated
Changelog
Bugfixes
Compatibility
It just rewrite the "follow hero" function, so it'll probably be compatible with any script
Author's Notes
Please leave your opinion and any bug you find or suggestion you have. And don't forget the credits.
Lobosque
Version: 0.5
Jan 2 2007
This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.
No support is given. If you are the owner of the thread, please contact administration.
Introduction
This script make an event follow if the hero is inside his cone of vision.
This cone has a sight (range) of 4. So if you are inside it, the event will follow you.
And this is a cone with an obstacle (the sand show what the event is seeing)
I think that I saw a script like this once, but I decided to write this one from scratch.
Script
You can just paste this above main:
The code
Code:
#the switch to be activated when the event see the hero. false if none
SWITCH = false
class Game_Character
attr_reader :cone
attr_reader :prev_x
attr_reader :prev_y
attr_reader :prev_dir
attr_accessor :sight
alias conesys_gamecharacter_initialize initialize
def initialize
@cone = []
@sight = 0
conesys_gamecharacter_initialize
end
def get_cone(sight = 7)
cone = []
@sight = sight
#This algorithim makes the cone using a 2d array
case self.direction
when 2 #down
#adds the first square
line = 1
cone.push([self.x,self.y + 1])
factor = 1
#now comes the routine to make the cone
while line < sight
line += 1
cone.push([self.x,self.y + line])
1.upto(factor) do |a|
cone.push([self.x - a,self.y + line])
cone.push([self.x + a,self.y + line])
end
factor += 1
end
when 4 #left
line = 1
cone.push([self.x - 1,self.y])
factor = 1
#now comes the routine to make the cone
while line < sight
line += 1
cone.push([self.x - line,self.y])
1.upto(factor) do |a|
cone.push([self.x - line,self.y - a])
cone.push([self.x - line,self.y + a])
end
factor += 1
end
when 6 #right
line = 1
cone.push([self.x + 1,self.y])
factor = 1
#now comes the routine to make the cone
while line < sight
line += 1
cone.push([self.x + line,self.y])
1.upto(factor) do |a|
cone.push([self.x + line,self.y - a])
cone.push([self.x + line,self.y + a])
end
factor += 1
end
when 8 #up
#adds the first square
line = 1
cone.push([self.x,self.y + 1])
factor = 1
#now comes the routine to make the cone
while line < sight
line += 1
cone.push([self.x,self.y - line])
1.upto(factor) do |a|
cone.push([self.x - a,self.y - line])
cone.push([self.x + a,self.y - line])
end
factor += 1
end
end
cone_obstacles(cone)
end
#here any tile that is covered by an obstacle is removed
#from the cone
def cone_obstacles(cone)
for i in 0..cone.length
if cone[i] != nil
if !$game_map.passable?(cone[i][0], cone[i][1], 0)
case self.direction
when 2 #down
#the diference between the sight and the obstacle position
limit = self.sight - (cone[i][1] - self.y)
position = 1
#to make the read easier
index = cone.index([cone[i][0],cone[i][1] + 1])
cone[index] = nil if index != nil
factor = 1
#now comes the routine to remove the bloked tiles
while position < limit
position += 1
index = cone.index([cone[i][0],cone[i][1] + position])
cone[index] = nil if index != nil
1.upto(factor) do |a|
index = cone.index([cone[i][0] - a,cone[i][1] + position])
cone[index] = nil if index != nil
index = cone.index([cone[i][0] + a,cone[i][1] + position])
cone[index] = nil if index != nil
end
factor += 1
end
when 4 #left
#the diference between the sight and the obstacle position
limit = self.sight - (self.x - cone[i][0])
position = 1
#to make the read easier
index = cone.index([cone[i][0] - 1,cone[i][1]])
cone[index] = nil if index != nil
factor = 1
#now comes the routine to remove the bloked tiles
while position < limit
position += 1
index = cone.index([cone[i][0] - position,cone[i][1]])
cone[index] = nil if index != nil
1.upto(factor) do |a|
index = cone.index([cone[i][0] - position,cone[i][1] - a])
cone[index] = nil if index != nil
index = cone.index([cone[i][0] - position,cone[i][1] + a])
cone[index] = nil if index != nil
end
factor += 1
end
when 6 #right
#the diference between the sight and the obstacle position
limit = self.sight - (cone[i][0] - self.x)
position = 1
#to make the read easier
index = cone.index([cone[i][0] + 1,cone[i][1]])
cone[index] = nil if index != nil
factor = 1
#now comes the routine to remove the bloked tiles
while position < limit
position += 1
index = cone.index([cone[i][0] + position,cone[i][1]])
cone[index] = nil if index != nil
1.upto(factor) do |a|
index = cone.index([cone[i][0] + position,cone[i][1] - a])
cone[index] = nil if index != nil
index = cone.index([cone[i][0] + position,cone[i][1] + a])
cone[index] = nil if index != nil
end
factor += 1
end
when 8 #up
#the diference between the sight and the obstacle position
limit = self.sight - (self.y - cone[i][1])
position = 1
#to make the read easier
index = cone.index([cone[i][0],cone[i][1] - 1])
cone[index] = nil if index != nil
factor = 1
#now comes the routine to remove the bloked tiles
while position < limit
position += 1
index = cone.index([cone[i][0],cone[i][1] - position])
cone[index] = nil if index != nil
1.upto(factor) do |a|
index = cone.index([cone[i][0] - a,cone[i][1] - position])
cone[index] = nil if index != nil
index = cone.index([cone[i][0] + a,cone[i][1] - position])
cone[index] = nil if index != nil
end
factor += 1
end
end
end
end
end
#update the variables used to check the need of a refresh
@prev_x = self.x
@prev_y = self.y
@prev_dir = self.direction
@cone = cone
end
def in_cone
#return false if the event do not have a cone
if cone != []
#now it checks if the actual position of the hero is inside the cone
for i in 0...cone.length
if cone[i] != nil
if $game_player.x == cone[i][0] && $game_player.y == cone[i][1]
$game_switches[SWITCH] = true if SWITCH
$game_map.need_refresh = true
move_toward_player
end
end
end
end
end
def move_toward_player
# Get difference in player coordinates
sx = @x - $game_player.x
sy = @y - $game_player.y
# If coordinates are equal
if sx == 0 and sy == 0
return
end
#Now the 8-direction following
#diagonal movements
if sx > 0 && sy > 0; move_upper_left
elsif sx > 0 && sy < 0; move_lower_left
elsif sx < 0 && sy > 0; move_upper_right
elsif sx < 0 && sy < 0; move_lower_right
#normal movement
elsif sx < 0 && sy == 0; move_right
elsif sx > 0 && sy == 0; move_left
elsif sx == 0 && sy < 0; move_down
elsif sx == 0 && sy > 0; move_up
end
end
#these modifications make the "hero touch" work better
#thanks Linkin_T for the help
def check_event_trigger_touch(x, y)
return if not @stop_count > (40 - @move_frequency * 2) * (6 - @move_frequency)
old_touch(x, y)
end
def check_event_trigger_auto
return if not @stop_count > (40 - @move_frequency * 2) * (6 - @move_frequency)
old_auto
end
end
class Game_Event < Game_Character
attr_reader :has_cone
alias conesys_gameevent_initialize initialize
alias old_touch check_event_trigger_touch
alias old_auto check_event_trigger_auto
def initialize(map_id, event)
#a flag that tells if the event is cone-following-enabled or not
@has_cone = false
conesys_gameevent_initialize(map_id, event)
end
#start the cone routine.
def add_cone(sight = 7)
@has_cone = true
get_cone(sight)
end
#stop the "cone following"
def stop_cone
@has_cone = false
end
end
class Game_Player < Game_Character
alias conesys_gameplayer_update update
def update
check_cone
conesys_gameplayer_update
end
def check_cone
#check if the event has a cone
for i in $game_map.events.keys
event = $game_map.events[i]
if event.has_cone && $scene.is_a?(Scene_Map)
#check if the player is inside the cone of vision and make the
#event follow him
event.in_cone
#update the cone if the event moved or the direction is different
if event.x != event.prev_x || event.y != event.prev_y || event.direction != event.prev_dir
event.get_cone(event.sight)
end
end
end
end
end
Instructions
To make an event use the cone to follow you, all you gotta do is set this in an "call script".
Code:
$game_map.events[event id].add_cone(sight)
The default sight is 7, if you want to use this, just use "add_cone".
to make him stop, use
Code:
$game_map.events[event id].stop_cone
You can activate a switch too, set the SWITCH constant in the top of the code to the switch ID that you want to be activated
Changelog
Version 0.5
Version 0.5
Bugfixes
- The cone when the event look to left or right.
- Lag reduced.
- Wrong status addition fixed (intelligence to be exact).
- A lot of code modification and cleanup.
- A "hero touch" bug. (Thanks Linkin_T for the help).
- A switch is activated when the event see the hero.
- Obstacles block events vision.
Compatibility
It just rewrite the "follow hero" function, so it'll probably be compatible with any script
Author's Notes
Please leave your opinion and any bug you find or suggestion you have. And don't forget the credits.