Advertisement
Rolpege

Follow.as

Mar 15th, 2011
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.aberustudios.cloneinfiltration.effects
  2. {
  3.     import flash.geom.Point;
  4.     import flash.geom.Rectangle;
  5.    
  6.     import net.flashpunk.Entity;
  7.     import net.flashpunk.FP;
  8.  
  9.     public class Follow
  10.     {
  11.         public var x:Number;
  12.         public var y:Number;
  13.        
  14.         public var target:Entity;
  15.         public var within:Rectangle;
  16.         public var speed:Number;
  17.        
  18.         public function Follow(target:Entity, within:Rectangle, speed:Number = 10)
  19.         {
  20.             setView(target, within, speed);
  21.         }
  22.        
  23.         public function setView(target:Entity, within:Rectangle, speed:Number = 10, teleport:Boolean = true):void
  24.         {
  25.             this.target = target;
  26.             this.within = within;
  27.             this.speed = speed;
  28.            
  29.             if(teleport) this.teleport();
  30.         }
  31.        
  32.         public function teleport():void
  33.         {
  34.             x = target.x - FP.halfWidth;
  35.             y = target.y - FP.halfHeight;
  36.            
  37.             stayWithin();
  38.         }
  39.        
  40.         public function update():void
  41.         {
  42.             var distance:Number = FP.distance(target.x - FP.halfWidth, target.y - FP.halfHeight, x, y);
  43.             var movementSpeed:Number = distance / speed;
  44.            
  45.             FP.stepTowards(this, target.x - FP.halfWidth, target.y - FP.halfHeight, movementSpeed);
  46.            
  47.             stayWithin();
  48.            
  49.             FP.camera.x = int(x);
  50.             FP.camera.y = int(y);
  51.         }
  52.        
  53.         protected function stayWithin():void
  54.         {
  55.             if(x < within.x) x = within.x;
  56.             if(x + FP.width > within.x + within.width) x = within.x + within.width - FP.width;
  57.            
  58.             if(y < within.y) y = within.y;
  59.             if(y + FP.height > within.y + within.height) y = within.y + within.height - FP.height;
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement