1+ import java .util .List ;
2+
3+ public class Steering {
4+
5+ private List <Entity > entities ;
6+ public Steering (List <Entity > entities ) {
7+ this .entities = entities ;
8+ }
9+
10+ public Location steer (Entity e ) {
11+
12+ Location a = align (e , 10 );
13+ Location s = separation (e , 2 );
14+ Location c = cohesion (e , 10 );
15+
16+ Location l = new Location (s .x + c .x + a .x , s .y + c .y + a .y );
17+ l .normalize (1 );
18+ return l ;
19+ }
20+
21+ public Location align (Entity e , double distance ) {
22+ int neigth = 0 ;
23+ Location v = new Location (0 , 0 );
24+
25+ for (Entity en : entities ) {
26+ if (!e .equals (en )) {
27+ if (insight (e , en , distance )) {
28+ v .add (en .getVelocity ().x , en .getVelocity ().y );
29+ neigth ++;
30+ }
31+ }
32+ }
33+
34+ if (neigth == 0 ) return v ;
35+
36+ v .divide (neigth , neigth );
37+ v .normalize (1 );
38+
39+ return v ;
40+ }
41+
42+ public Location cohesion (Entity e , double distance ) {
43+ int neigth = 0 ;
44+ Location v = new Location (0 , 0 );
45+
46+ for (Entity en : entities ) {
47+ if (!e .equals (en )) {
48+ if (insight (e , en , distance )) {
49+ v .add (en .getPosition ().x , en .getPosition ().y );
50+ neigth ++;
51+ }
52+ }
53+ }
54+
55+ if (neigth == 0 ) return v ;
56+
57+ v .divide (neigth , neigth );
58+ Location a = new Location (v .x - e .getPosition ().x , v .y - e .getPosition ().y );
59+ a .normalize (1 );
60+
61+ return a ;
62+ }
63+
64+ public Location separation (Entity e , double distance ) {
65+ int neigth = 0 ;
66+ Location v = new Location (0 , 0 );
67+
68+ for (Entity en : entities ) {
69+ if (!e .equals (en )) {
70+ if (insight (e , en , distance )) {
71+ v .add (en .getPosition ().x - e .getPosition ().x , en .getPosition ().y - e .getPosition ().y );
72+ neigth ++;
73+ }
74+ }
75+ }
76+
77+ if (neigth == 0 ) return v ;
78+
79+ v .divide (neigth , neigth );
80+ v .multi (-1 , -1 );
81+ v .normalize (1 );
82+
83+ return v ;
84+ }
85+
86+ private boolean inside (double x1 , double y1 , double width1 , double height1 , double x2 , double y2 , double width2 , double height2 ) {
87+ return (x2 + width2 > x1 && y2 + height2 > y1 && x1 + width1 > x2 && y1 + height1 > y2 );
88+ }
89+
90+ private boolean insight (Entity e1 , Entity e2 , double distance ) {
91+ Location l = e1 .getPosition ();
92+ return insight_1 (l , e2 , distance ) || insight_1 (l .clone ().add (e1 .getWidth (), 0 ), e2 , distance ) || insight_1 (l .clone ().add (e1 .getWidth (), e1 .getHeight ()), e2 , distance ) || insight_1 (l .clone ().add (0 , e1 .getHeight ()), e2 , distance ) || inside (l .x , l .y , e1 .getWidth (), e1 .getHeight (), e2 .getPosition ().x , e2 .getPosition ().y , e2 .getWidth (), e2 .getHeight ());
93+ }
94+
95+ private boolean insight_1 (Location l1 , Entity e , double distance ) {
96+ Location l2 = e .getPosition ();
97+
98+ return l2 .distanceTo (l1 ) < distance || l2 .clone ().add (0 , e .getHeight ()).distanceTo (l1 ) < distance || l2 .clone ().add (e .getWidth (), e .getHeight ()).distanceTo (l1 ) < distance || l2 .clone ().add (e .getWidth (), 0 ).distanceTo (l1 ) < distance ;
99+ }
100+ }
0 commit comments