package Gameplay { import flash.display.MovieClip; import flash.geom.Point; // Note: All coordinates are with respect to the map of the level public class Boundary { private var type : String; private var p1 : Point; // center coordinate (circle), top left (rectangle) private var p2 : Point; // bottom right (+x,+y) (rectangle) private var noZone : Boolean = true; //indicates this area is offlimits if true, other areas are off limits if false public function Boundary(typeP:String, point1P:Point, point2P:Point, noZoneP:Boolean = true) { type = typeP; p1 = point1P; p2 = point2P; noZone = noZoneP; } // Returns the position of the player after taking into account this boundary // If the desired position is outside of the boundary, then returns desired position // If the desired position is inside the boundary, returns a point on the edge of the boundary // that is in between the initial and desired positions public function calculatePosition(initialPosition:Point, desiredPosition:Point):Point { //TODO calculate position on the boundary switch (type) { case "FENCE": if (checkCollision(initialPosition, desiredPosition)) { // TODO return the position on the fence or the position beyond the fence return initialPosition; } break; case "RECTANGLE": if (checkCollision(initialPosition, desiredPosition)) { // TODO return the position on the rectangle border return initialPosition; } break; case "CIRCLE": // TODO return the position on the circle border Or the altered position taking to account the circle if (checkCollision(initialPosition, desiredPosition)) { // TODO return the position on the rectangle border return initialPosition; } break; } return desiredPosition; } // Does a quick check to see if the path from the initial point to desired point // intersects with this boundary // TODO make this private and use calculatePosition instead public function checkCollision(initialPosition:Point, desiredPosition:Point):Boolean { switch (type) { case "FENCE": //TODO detect collision break; case "RECTANGLE": if (desiredPosition.x < p2.x && desiredPosition.x > p1.x && desiredPosition.y < p2.y && desiredPosition.y > p1.y) return noZone; break; case "CIRCLE": if (Point.distance(desiredPosition, p1) < Point.distance(p1, p2)) return noZone; break; } return !noZone; } } }