Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.aberustudios.cloneinfiltration.effects
- {
- import flash.geom.Point;
- import flash.geom.Rectangle;
- import net.flashpunk.Entity;
- import net.flashpunk.FP;
- public class Follow
- {
- public var x:Number;
- public var y:Number;
- public var target:Entity;
- public var within:Rectangle;
- public var speed:Number;
- public function Follow(target:Entity, within:Rectangle, speed:Number = 10)
- {
- setView(target, within, speed);
- }
- public function setView(target:Entity, within:Rectangle, speed:Number = 10, teleport:Boolean = true):void
- {
- this.target = target;
- this.within = within;
- this.speed = speed;
- if(teleport) this.teleport();
- }
- public function teleport():void
- {
- x = target.x - FP.halfWidth;
- y = target.y - FP.halfHeight;
- stayWithin();
- }
- public function update():void
- {
- var distance:Number = FP.distance(target.x - FP.halfWidth, target.y - FP.halfHeight, x, y);
- var movementSpeed:Number = distance / speed;
- FP.stepTowards(this, target.x - FP.halfWidth, target.y - FP.halfHeight, movementSpeed);
- stayWithin();
- FP.camera.x = int(x);
- FP.camera.y = int(y);
- }
- protected function stayWithin():void
- {
- if(x < within.x) x = within.x;
- if(x + FP.width > within.x + within.width) x = within.x + within.width - FP.width;
- if(y < within.y) y = within.y;
- if(y + FP.height > within.y + within.height) y = within.y + within.height - FP.height;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement