-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththing.js
164 lines (149 loc) · 3.64 KB
/
thing.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
class Thing {
constructor(index, x, y, world) {
this.index = index;
this.myWorld = world;
this.position = createVector(x, y);
this.velocity = createVector();
this.acceleration = createVector();
this.target = createVector();
this.color = color(random(255), random(255), random(255));
this.health = 200;
this.r = 5;
this.maxSpeed = Math.round(random(2, 20));
this.maxForce = 0.8;
}
behaviors() {
let steerFood = this.eat();
//let steerOthers = this.align();
let steerAway = this.separate();
steerAway.mult(2);
this.applyForce(steerFood);
//this.applyForce(steerOthers);
this.applyForce(steerAway);
}
update() {
if (this.health <= 0 || this.health > 500) {
this.die();
}
this.health -= this.myWorld.healthLostPerTick;
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxSpeed);
this.position.add(this.velocity);
this.acceleration.mult(0);
}
eat() {
let closestFood = null;
let distMin = Infinity;
let self = this;
this.myWorld.food.forEach(function(food, i) {
let desired = p5.Vector.sub(food, self.position);
let dist = desired.mag();
if (dist < 6) {
self.myWorld.food.splice(i, 1);
self.health += self.myWorld.foodEnergy;
} else {
if (dist < distMin) {
distMin = dist;
closestFood = food;
}
}
});
if (closestFood) {
this.target = closestFood;
return this.seek(closestFood);
} else {
this.target = createVector(0, 0);
return createVector(0, 0);
}
}
arrive() {
let desired = p5.Vector.sub(this.myWorld.food, this.position);
let dist = desired.mag();
let speed = this.maxSpeed;
let distmin = 5;
if (dist < distmin) {
speed = map(dist, 0, distmin, 0, this.maxSpeed);
}
desired.setMag(speed);
let steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxForce);
return steer;
}
separate() {
let sum = createVector();
let count = 0;
for (let i in this.myWorld.getThings()) {
let thing = this.myWorld.getThings()[i];
let d = p5.Vector.dist(this.position, thing.position);
if (d > 0 && d < 50) {
let diff = p5.Vector.sub(this.position, thing.position);
diff.normalize();
sum.add(diff);
count++;
}
}
if (count > 0) {
sum.div(count);
sum.normalize();
sum.mult(this.maxSpeed);
let steer = p5.Vector.sub(sum, this.velocity);
steer.limit(this.maxForce);
return steer;
} else {
return createVector(0, 0);
}
}
align() {
let sum = createVector();
let count = 0;
for (let i in this.myWorld.getThings()) {
let thing = this.myWorld.getThings()[i];
let d = p5.Vector.dist(this.position, thing.position);
if (d > 0 && d < 50) {
sum.add(thing.velocity);
count++;
}
}
if (count > 0) {
sum.div(count);
sum.mag(this.maxSpeed);
let steer = p5.Vector.sub(sum, this.velocity);
steer.limit(this.maxForce);
return steer;
} else {
return createVector(0, 0);
}
}
seek(target) {
let desired = p5.Vector.sub(target, this.position);
desired.setMag(this.maxSpeed);
let steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxForce);
return steer;
}
applyForce(f) {
this.acceleration.add(f);
}
die() {
this.myWorld.removeThing(this);
this.myWorld.addFood(this.position.x, this.position.y);
}
display() {
let angle = this.velocity.heading() + Math.PI / 2;
//show target
fill(this.color);
stroke(this.color);
strokeWeight(1);
push();
translate(this.position.x, this.position.y);
rotate(angle);
beginShape();
vertex(0, -this.r * 2);
vertex(-this.r, this.r * 2);
vertex(this.r, this.r * 2);
endShape(CLOSE);
noFill();
ellipse(0, 0, this.health < 200 ? this.health/2 : 100);
pop();
}
}