1+ let FlockerScript = function ( p ) {
2+ class Flock {
3+ constructor ( ) {
4+ this . flock = [ ] ;
5+ this . maxCapacity = 50 ;
6+ this . curIndex = this . maxCapacity - 1 ;
7+ }
8+
9+ addMover ( x , y , r ) {
10+ this . flock . push ( new AutonMover ( p , x , y , r ) ) ;
11+ }
12+
13+ applyBehaviours ( mover = this . flock [ 0 ] , sepW = sepWeight , cohereW = cohereWeight , alignW = alignWeight ) {
14+ let sepForce = p5 . Vector . mult ( mover . separate ( this . flock ) , sepW ) ;
15+ let alignForce = p5 . Vector . mult ( mover . align ( this . flock ) , alignW ) ;
16+ let cohereForce = p5 . Vector . mult ( mover . cohere ( this . flock ) , cohereW ) ;
17+ mover . applyForce ( p5 . Vector . add ( sepForce , p5 . Vector . add ( cohereForce , alignForce ) ) ) ;
18+ }
19+
20+ run ( chk_edges = true ) {
21+ for ( ; this . curIndex > 0 ; this . curIndex -- ) {
22+ this . flock [ this . curIndex ] . display ( ) ;
23+ this . applyBehaviours ( this . flock [ this . curIndex ] ) ;
24+ this . flock [ this . curIndex ] . update ( chk_edges ) ;
25+ if ( this . flock [ this . curIndex ] . isDead ( ) ) {
26+ this . flock . splice ( this . curIndex , 1 ) ;
27+ }
28+ }
29+ this . curIndex = this . maxCapacity - 1 ;
30+ }
31+ }
32+
33+ let flock = new Flock ( ) ;
34+ let sepSlider , alignSlider , cohereSlider ;
35+ let sepWeight = 1 , alignWeight = 0.5 , cohereWeight = 0.5 ;
36+
37+ p . initializeSketch = function ( ) {
38+ flock . flock = [ ] ;
39+ for ( let i = 0 ; i < flock . maxCapacity ; i ++ ) {
40+ flock . addMover (
41+ p . random ( p . width * 0.3 , p . width * 0.7 ) ,
42+ p . random ( p . height * 0.3 , p . height * 0.7 ) ,
43+ 10
44+ ) ;
45+ }
46+
47+ p . redraw ( ) ;
48+ } ;
49+
50+ p . setup = function ( ) {
51+ p . noLoop ( ) ;
52+
53+ let cnv = p . createCanvas ( 850 , 600 ) ;
54+ cnv . parent ( 'sec2' ) ;
55+ let container = document . getElementById ( 'interactive-controls-sec2' ) ;
56+
57+ sepSlider = p . createSlider ( 0.5 , 2.5 , 1 , 0.25 ) ;
58+ sepSlider . parent ( container ) ;
59+ sepLabel = p . createSpan ( 'Separation: ' + sepSlider . value ( ) )
60+ sepLabel . parent ( container ) ;
61+
62+ alignSlider = p . createSlider ( 0.5 , 2.5 , 0.5 , 0.25 ) ;
63+ alignSlider . parent ( container ) ;
64+ alignLabel = p . createSpan ( 'Align:' + alignSlider . value ( ) ) ;
65+ alignLabel . parent ( container ) ;
66+
67+ cohereSlider = p . createSlider ( 0.5 , 2.5 , 0.5 , 0.25 ) ;
68+ cohereSlider . parent ( container ) ;
69+ cohereLabel = p . createSpan ( 'Cohere:' + cohereSlider . value ( ) ) ;
70+ cohereLabel . parent ( container ) ;
71+
72+ sepSlider . input ( ( ) => {
73+ sepWeight = sepSlider . value ( ) ;
74+ sepLabel . html ( 'Separation: ' + sepWeight ) ;
75+ } ) ;
76+
77+ alignSlider . input ( ( ) => {
78+ alignWeight = alignSlider . value ( ) ;
79+ alignLabel . html ( 'Align: ' + alignWeight ) ;
80+ } ) ;
81+
82+ cohereSlider . input ( ( ) => {
83+ cohereWeight = cohereSlider . value ( ) ;
84+ cohereLabel . html ( 'Cohere: ' + cohereWeight ) ;
85+ } ) ;
86+
87+ p . initializeSketch ( ) ;
88+ } ;
89+
90+ p . draw = function ( ) {
91+ p . background ( 220 ) ;
92+ flock . run ( ) ;
93+ } ;
94+
95+ } ;
96+
97+ let sec2Sketch = new p5 ( FlockerScript , 'sec2' ) ;
0 commit comments