Chaos Project

RPG Maker => RPG Maker Scripts => Topic started by: maximusmaxy on August 20, 2011, 11:53:33 am

Title: Field of view script help
Post by: maximusmaxy on August 20, 2011, 11:53:33 am
Hey, this is the first legit script I've ever created. It's a field of view script that i use for stealth type parts of my game. Basically whenever you walk into the field of view of an event it sets its self switch A to on, which than could be used to do other things(I have guards send you back to the beginning).

I was wondering if the field of view could be highlighted a transparent colour to show there view. Probably by wearing magic glasses or something. I know i'm probably ment to alias something but i don't know how or where to. Also my $game_player.x/y don't update for some reason so i'm using game variables instead which works. Any help would be much appreciated. Here is what i got so far.

=begin 
To use this script place a script call eg. FoV.new(7,1,5) in a parallel process
to have the event constantly look for you. If you enter the events field of
view it will activate its self switch 'A' which could than trigger a chase
event system or an autorun event, however you want that event to react.
Script call = FoV.new(ID,DISTANCE,TYPE)

ID is the ID of the event.

DISTANCE is how many tiles they look in that direction. Default 5.
Only use ODD distances for diamond as it has been poorly scripted.

TYPE is the field of view type. Default Diamond.

Type 0: Diamond      Type 1: Pyramid      Type 2: Linear

                              N             
      3                      3N               
     234                    23N               
   E1234N                 E123N               E1234N
     234                    23N               
      3                      3N               
                              N                 
                                       
where Distance = 5  where Distance = 4    where distance = 5

You can omit putting in the distance/type and use the defaults
eg. FoV.new(7,2) or FoV.new(4)
=end

class FoV
 
  def initialize(id,dist = 5,type = 0)
    @x = $game_map.events[id].x
    @y = $game_map.events[id].y
    @d = $game_map.events[id].direction
    @key = [$game_map.map_id,id,'A']
    case type
      when 0 #Diamond(Top left origin x, y, distance)
      case @d
        when 2 #down
        diamond(@x - (dist-1)/2, @y + 1, dist)
        when 4 #left
        diamond(@x - dist,@y - (dist-1)/2, dist)
        when 6 #right
        diamond(@x + 1,@y - (dist-1)/2, dist)
        when 8 #up
        diamond(@x - (dist-1)/2,@y - dist, dist)
      end
      when 1 #Pyramid(x, y, left right?, LR inc, up down?, UD inc, distance)
      case @d
        when 2 #down
        pyramid(@x, @y + 1, -1, 1, 1, 0, dist)
        when 4 #left
        pyramid(@x - 1, @y, -1, 0, -1, 1, dist)
        when 6 #right
        pyramid(@x + 1, @y, 1, 0, 1, -1, dist)
        when 8 #up
        pyramid(@x, @y - 1, 1, -1, -1, 0, dist)
      end
      when 2 #Linear(x, y, x increment, y increment, distance)
      case @d
        when 2 #down
        linear(@x, @y+1, 0, 1, dist)
        when 4 #left
        linear(@x-1, @y, -1, 0, dist)
        when 6 #right
        linear(@x+1, @y, 1, 0, dist)
        when 8 #up
        linear(@x, @y-1, 0, -1, dist)
      end
    end
  end
 
  def diamond(x,y,n)
    for i in 0..(n-1)
      for j in 0..((n-1) - (2 * ((i-(n-1)/2).abs)))
        a = x + i
        b = y + ((n-1)/2-i).abs + j
        check(a,b)
      end
    end
  end
 
  def pyramid(x,y,lr,lri,ud,udi,n)
    for i in 0..(n-1)
      for j in 0..(2*(i+1)-2)
        a = x + (i*lr) + (j*lri)
        b = y + (i*ud) + (j*udi)
        check(a,b)
      end
    end
  end
 
  def linear(x,y,ix,iy,n)
    for i in 0..(n-1)
      a = x + (i*ix)
      b = y + (i*iy)
      check(a,b)
    end
  end
 
  def check(x,y)
    #if x == $game_player.x and y == $game_player.y
    if x == $game_variables[0002] and y == $game_variables[0003]
      $game_self_switches[@key] = true
    end
  end
 
end