-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcritter.js
171 lines (162 loc) · 4.89 KB
/
critter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* eslint-disable import/extensions */
import { determineNextCritterPosition } from './physics.js';
import { globals } from './globals.js';
import {
uuid,
degreesToRads,
distanceBetweenCritters,
directionTowardsCritter,
} from './utils.js';
const Critter = class {
constructor(
species,
position,
direction,
scaredBehaviour,
speed,
size,
energy,
scared,
) {
this.id = uuid();
this.species = species;
this.position = position;
this.direction = direction;
this.scaredBehaviour = scaredBehaviour;
this.speed = speed;
this.size = size;
this.energy = energy;
this.scared = scared;
}
move() {
this.position = determineNextCritterPosition(
this.position.x,
this.position.y,
this.speed,
this.direction,
);
}
draw() {
const circle = new Path2D();
circle.arc(this.position.x, this.position.y, this.size, 0, 2 * Math.PI);
globals.ctx.fillStyle = this.scared
? chroma(this.species.colour)
.brighten(2)
.hex()
: this.species.colour;
globals.ctx.fill(circle);
}
normalise() {
// speed
if (this.speed < this.species.critterSpeed) {
this.speed = Math.ceil(
(this.speed *= globals.speedNormalisationConstant),
);
} else {
this.speed = Math.ceil(
(this.speed /= globals.speedNormalisationConstant),
);
}
// energy
if (this.energy < this.species.critterSpeed * this.species.critterSize) {
this.energy = Math.ceil(
(this.energy *= globals.energyNormalisationConstant),
);
} else {
this.energy = Math.ceil(
(this.energy /= globals.energyNormalisationConstant),
);
}
}
calm() {
const closeCritters = this.species.getCrittersInRegion(
this.position,
this.species.safetyRadius,
);
const closeCalmCritters = closeCritters.filter((cc) => !cc.scared);
// If there's more than X critters nearby they will become calm again
if (
closeCalmCritters.length > this.species.calmSafetyNumber ||
closeCritters.length > this.species.scaredSafetyNumber
) {
this.direction = this.species.direction;
this.speed = this.species.critterSpeed;
this.scared = false;
}
}
determineScaredBehaviour() {
switch (this.scaredBehaviour) {
case 'do_nothing':
return this.direction;
case 'go_backwards':
return this.direction + degreesToRads(180);
case 'turn_left':
return this.direction - degreesToRads(90);
case 'turn_right': // TODO: seems to be broken
return this.direction - degreesToRads(270);
case 'alternate':
return Math.random() > 0.5
? this.direction - degreesToRads(90)
: this.direction - degreesToRads(270);
case 'random':
return degreesToRads(180 * (Math.random() - 0.5));
case 'nearest_neighbour': {
if (this.species.critters.length === 1) {
return this.direction;
}
let neighbourRadius = 5;
const getNearCritters = (rad) =>
this.species
.getCrittersInRegion(this.position, rad)
.filter((cs) => !cs.scared);
while (getNearCritters(neighbourRadius).length <= 1) {
// Otherwise it just returns the original critter
neighbourRadius += 1;
if (neighbourRadius >= 250) {
return this.direction;
}
}
const nearCritters = getNearCritters(neighbourRadius);
nearCritters.splice(nearCritters.indexOf(this), 1); // remove the current critters
const distances = nearCritters.map((nc) =>
distanceBetweenCritters(nc, this),
);
const minIdx = distances.indexOf(Math.min(...distances));
const newDirection = directionTowardsCritter(
this,
nearCritters[minIdx],
);
return newDirection;
}
case 'towards_pack': {
// TODO: seems to be broken
if (this.species.critters.length === 1) {
return this.direction;
}
let startRadius = 5;
const getNearCritters = (rad) =>
this.species
.getCrittersInRegion(this.position, rad)
.filter((cs) => !cs.scared);
while (getNearCritters(startRadius).length <= 20) {
// Otherwise it just returns the original critter
startRadius += 1;
// If it's too sparsely populated
if (startRadius >= 250) {
return this.direction;
}
}
const nearCritters = getNearCritters(startRadius);
nearCritters.splice(nearCritters.indexOf(this), 1); // remove the current critters
const direction = nearCritters.reduce(
(p, nc) => p + directionTowardsCritter(nc, this),
0,
);
return direction / nearCritters.length;
}
default:
return this.direction;
}
}
};
export default Critter;