Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- public class LeaderFlocker extends Flocker{
- //The constructor class for the LeaderFlocker fish, which is programmed to swim in a figure-eight formation and avoid the other fish.
- //Passes in a reference to the flock manager class, and the current x and y coordinates, and well as the speed of the fish.
- public function LeaderFlocker(aMan:Manager, aX:Number=0, aY:Number=0, aSpeed:Number=0 )
- {
- super(aMan, aX, aY, aSpeed);
- aMan.addChild(this);
- }
- //Calculates the Vector force every frame for guiding the fish.
- package {
- public class LeaderFlocker extends Flocker{
- //The constructor class for the LeaderFlocker fish, which is programmed to swim in a figure-eight formation and avoid the other fish.
- //Passes in a reference to the flock manager class, and the current x and y coordinates, and well as the speed of the fish.
- public function LeaderFlocker(aMan:Manager, aX:Number=0, aY:Number=0, aSpeed:Number=0 )
- {
- super(aMan, aX, aY, aSpeed);
- aMan.addChild(this);
- }
- //Calculates the Vector force every frame for guiding the fish.
- override protected function calcSteeringForce( ):Vector2{
- //the initial blank Vector to be eventually returned.
- var steeringForce:Vector2 = new Vector2( );
- //When the user clicks the screen, the seekingfoodpoint boolean is set to true, which overrides main steering force to simply move
- //towards the point that was clicked.
- if(seekingfoodpoint){
- //the arrival() method calculates the direction towards the spot that was clicked, and has the fish slow down as
- //the fish gets closer to the spot that was clicked, until eventually the fish reaches the point, which sets
- //seekingfoodpoint to false.
- steeringForce = steeringForce.add(arrival().multiply(500));
- }
- //the fish is not seeking food, and will swim around normally.
- else{
- //adds the flowfield steering force, which makes the fish move in the figure-eight formation
- steeringForce=steeringForce.add(flowField().multiply((500)));
- //if the fish is in danger of moving off of the screen, then reverse the steering force
- //so that the fish does not move off-screen.
- if((this.x<150 && steeringForce.x<0) || (this.x>770 && steeringForce.x>0))
- steeringForce=new Vector2(-steeringForce.x,steeringForce.y);
- if((this.y<200 && steeringForce.y<0) || (this.y>620 && steeringForce.y>0))
- steeringForce=new Vector2(steeringForce.x,-steeringForce.y);
- }
- //if the force associated with the separation of fishes is present (which it normally is not unless other fish are
- //very close) then add this force to the steeringForce.
- var v:Vector2=separation();
- if((v.x!=0 && v.y!=0) ){
- steeringForce = steeringForce.add(separation().multiply(500));
- }
- return steeringForce;
- }
- //This function is responsible for the figure-eight direction that the koi generally moves in.
- public function flowField():Vector2{
- var futurePosition:Vector2;
- var flowdir:Vector2;
- //the flowField method is divided into 3 sections, based on where the fish is horizontally.
- //if the fish has an s value of less than 400, then a spot slightly ahead of the fish is found. We then calculate
- //where that point is relative to a point near the middle of the screen and off to the left a little.
- //this vector is rotated 90 degrees to get a vector that turns the fish slowly around the
- //point near the middle of the screen in a counterclockwise direction.
- if(this.x< 400){
- futurePosition = position.add(fwd.multiply(speed*.1));
- flowdir= futurePosition.subtract(new Vector2(600,400));
- flowdir=flowdir.perpRight();
- flowdir.normalize();
- return flowdir;
- }
- //if the fish is near the middle of the screen, the vector returned will be in a diagonal direction depending upon
- //the fish's horizontal velocity. If the fish is moving to the right, the fish will keep moving right and downward.
- //If it is going left, the fish will move left and downward.
- else if(this.x>=400 && this.x<600){
- if(fwd.x>0)
- return new Vector2(.333,.9);
- else
- return new Vector2(-.333,.9);
- }
- //if the fish is far to the right, there is a similar idea from when the fish is far to the left, except since
- //the fish is on the opposite end, the fish will be turning in the opposite direction.
- else{
- futurePosition = position.add(fwd.multiply(speed*.1));
- flowdir= futurePosition.subtract(new Vector2(400,400));
- flowdir=flowdir.perpRight().perpRight().perpRight();
- flowdir.normalize();
- return flowdir;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement