SHOW:
|
|
- or go back to the newest paste.
1 | package { | |
2 | ||
3 | public class LeaderFlocker extends Flocker{ | |
4 | //The constructor class for the LeaderFlocker fish, which is programmed to swim in a figure-eight formation and avoid the other fish. | |
5 | //Passes in a reference to the flock manager class, and the current x and y coordinates, and well as the speed of the fish. | |
6 | public function LeaderFlocker(aMan:Manager, aX:Number=0, aY:Number=0, aSpeed:Number=0 ) | |
7 | { | |
8 | super(aMan, aX, aY, aSpeed); | |
9 | aMan.addChild(this); | |
10 | } | |
11 | ||
12 | //Calculates the Vector force every frame for guiding the fish. | |
13 | package { | |
14 | ||
15 | public class LeaderFlocker extends Flocker{ | |
16 | //The constructor class for the LeaderFlocker fish, which is programmed to swim in a figure-eight formation and avoid the other fish. | |
17 | //Passes in a reference to the flock manager class, and the current x and y coordinates, and well as the speed of the fish. | |
18 | public function LeaderFlocker(aMan:Manager, aX:Number=0, aY:Number=0, aSpeed:Number=0 ) | |
19 | { | |
20 | super(aMan, aX, aY, aSpeed); | |
21 | aMan.addChild(this); | |
22 | } | |
23 | ||
24 | //Calculates the Vector force every frame for guiding the fish. | |
25 | override protected function calcSteeringForce( ):Vector2{ | |
26 | //the initial blank Vector to be eventually returned. | |
27 | var steeringForce:Vector2 = new Vector2( ); | |
28 | ||
29 | //When the user clicks the screen, the seekingfoodpoint boolean is set to true, which overrides main steering force to simply move | |
30 | //towards the point that was clicked. | |
31 | if(seekingfoodpoint){ | |
32 | //the arrival() method calculates the direction towards the spot that was clicked, and has the fish slow down as | |
33 | //the fish gets closer to the spot that was clicked, until eventually the fish reaches the point, which sets | |
34 | //seekingfoodpoint to false. | |
35 | steeringForce = steeringForce.add(arrival().multiply(500)); | |
36 | ||
37 | } | |
38 | //the fish is not seeking food, and will swim around normally. | |
39 | else{ | |
40 | //adds the flowfield steering force, which makes the fish move in the figure-eight formation | |
41 | steeringForce=steeringForce.add(flowField().multiply((500))); | |
42 | ||
43 | //if the fish is in danger of moving off of the screen, then reverse the steering force | |
44 | //so that the fish does not move off-screen. | |
45 | if((this.x<150 && steeringForce.x<0) || (this.x>770 && steeringForce.x>0)) | |
46 | steeringForce=new Vector2(-steeringForce.x,steeringForce.y); | |
47 | if((this.y<200 && steeringForce.y<0) || (this.y>620 && steeringForce.y>0)) | |
48 | steeringForce=new Vector2(steeringForce.x,-steeringForce.y); | |
49 | } | |
50 | ||
51 | //if the force associated with the separation of fishes is present (which it normally is not unless other fish are | |
52 | //very close) then add this force to the steeringForce. | |
53 | var v:Vector2=separation(); | |
54 | if((v.x!=0 && v.y!=0) ){ | |
55 | steeringForce = steeringForce.add(separation().multiply(500)); | |
56 | } | |
57 | return steeringForce; | |
58 | } | |
59 | ||
60 | //This function is responsible for the figure-eight direction that the koi generally moves in. | |
61 | public function flowField():Vector2{ | |
62 | var futurePosition:Vector2; | |
63 | var flowdir:Vector2; | |
64 | //the flowField method is divided into 3 sections, based on where the fish is horizontally. | |
65 | ||
66 | //if the fish has an s value of less than 400, then a spot slightly ahead of the fish is found. We then calculate | |
67 | //where that point is relative to a point near the middle of the screen and off to the left a little. | |
68 | //this vector is rotated 90 degrees to get a vector that turns the fish slowly around the | |
69 | //point near the middle of the screen in a counterclockwise direction. | |
70 | if(this.x< 400){ | |
71 | futurePosition = position.add(fwd.multiply(speed*.1)); | |
72 | flowdir= futurePosition.subtract(new Vector2(600,400)); | |
73 | flowdir=flowdir.perpRight(); | |
74 | flowdir.normalize(); | |
75 | return flowdir; | |
76 | } | |
77 | ||
78 | //if the fish is near the middle of the screen, the vector returned will be in a diagonal direction depending upon | |
79 | //the fish's horizontal velocity. If the fish is moving to the right, the fish will keep moving right and downward. | |
80 | //If it is going left, the fish will move left and downward. | |
81 | else if(this.x>=400 && this.x<600){ | |
82 | if(fwd.x>0) | |
83 | return new Vector2(.333,.9); | |
84 | else | |
85 | return new Vector2(-.333,.9); | |
86 | } | |
87 | //if the fish is far to the right, there is a similar idea from when the fish is far to the left, except since | |
88 | //the fish is on the opposite end, the fish will be turning in the opposite direction. | |
89 | else{ | |
90 | futurePosition = position.add(fwd.multiply(speed*.1)); | |
91 | flowdir= futurePosition.subtract(new Vector2(400,400)); | |
92 | flowdir=flowdir.perpRight().perpRight().perpRight(); | |
93 | flowdir.normalize(); | |
94 | return flowdir; | |
95 | } | |
96 | } | |
97 | } | |
98 | } |