-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
223 lines (190 loc) · 5.05 KB
/
app.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
'use strict';
const getRandomInt = (max) => Math.floor(Math.random() * max);
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
this.radius = 3;
this.color = '#e48';
}
distance(point) {
return Point.distanceTwo(this, point);
}
static distanceTwo(p1, p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
equals(x, y) {
return this.x === x && this.y === y;
}
draw(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fillStyle = this.color;
ctx.fill();
}
}
class MovablePoint extends Point {
constructor(x, y) {
super(x, y);
this.vx = Math.random();
this.vy = Math.random();
this.pBest = Infinity;
this.pBestX = x;
this.pBestY = y;
}
psoMovement(c1, c2, vMax) {
let vx = c1 * Math.random() * (this.pBestX - this.x) + c2 * Math.random() * (gBestX - this.x);
this.vx = Math.abs(this.vx + vx) <= vMax ? this.vx + vx : Math.sign(this.vx + vx) * vMax;
let vy = c1 * Math.random() * (this.pBestY - this.y) + c2 * Math.random() * (gBestY - this.y);
this.vy = Math.abs(this.vy + vy) <= vMax ? this.vy + vy : Math.sign(this.vy + vy) * vMax;
if ((this.x + this.vx) < 800 && (this.x + this.vx) > 0) {
this.x += this.vx;
}
if ((this.y + this.vy) < 600 && (this.y + this.vy) > 0) {
this.y += this.vy;
}
}
movementPositions() {
let coordinates = [-1, 0, 1];
let res = [];
for (let i = 0; i < coordinates.length; i++) {
for (let j = 0; j < coordinates.length; j++) {
res.push({x: this.x + coordinates[i], y: this.y + coordinates[j]})
}
}
return res.filter((el) => {
return !(el.x > 800 || el.y > 600 || el.x < 0 || el.y < 0)
})
}
movement(player) {
this.distance(player) <= 100 ? this.escapeMovement(player, this.movementPositions()) : this.randomMovement();
}
escapeMovement(player, positions) {
let maxDist = 0;
let index;
for (let i = 0; i < positions.length; i++) {
let newDist = Point.distanceTwo(player, new Point(positions[i].x, positions[i].y));
if (newDist > maxDist) {
maxDist = newDist;
index = i;
}
}
if (positions[index] && positions[index]) {
this.x = positions[index].x;
this.y = positions[index].y;
} else {
console.warn(positions);
}
}
randomMovement() {
this.x += this.vx;
this.y += this.vy;
if (this.x > 800 || this.x < 0) {
this.vx = -this.vx;
}
if (this.y > 600 || this.y < 0) {
this.vy = -this.vy;
}
}
fitness(player) {
let fitness = this.distance(player);
if (fitness < this.pBest) {
this.pBest = fitness;
this.pBestX = this.x;
this.pBestY = this.y;
}
if (fitness < gBest) {
gBest = fitness;
gBestX = this.x;
gBestY = this.y;
}
}
}
class Player extends Point {
constructor(x, y) {
super(x, y);
this.radius = 8;
this.color = '#5b5';
this.vx = 1;
this.vy = 1;
}
move(cb) {
this.x = mouseX;
this.y = mouseY;
cb();
}
}
class World {
constructor(width = 800, height = 600) {
// initiate the world
this.width = width;
this.height = height;
this.angryPoints = Array.from({length: 100}, () => new MovablePoint(getRandomInt(width), getRandomInt(height)));
this.player = new Player(width / 2, height / 2);
this.mode = 1
}
simulate() {
this.player.move(() => {
this.angryPoints.forEach(p => {
p.pBest = Infinity;
p.pBestX = p.x;
p.pBestY = p.y;
});
gBest = Infinity;
gBestX = Infinity;
gBestY = Infinity;
});
if (this.mode === 1) {
this.angryPoints.forEach(p => p.fitness(this.player));
this.angryPoints.forEach(p => p.psoMovement(2, 2, 3));
} else if (this.mode === 2) {
this.angryPoints.forEach(p => p.movement(this.player));
}
}
draw(ctx) {
this.player.draw(ctx);
this.angryPoints.forEach(p => p.draw(ctx));
}
}
const world = new World();
let raf;
let gBest = Infinity;
let gBestX = Infinity;
let gBestY = Infinity;
const canvas = document.getElementById('canvas');
canvas.width = world.width;
canvas.height = world.height;
const ctx = canvas.getContext('2d');
function getPosition(el) {
let xPosition = 0;
let yPosition = 0;
while (el) {
xPosition += (el.offsetLeft - el.scrollLeft + el.clientLeft);
yPosition += (el.offsetTop - el.scrollTop + el.clientTop);
el = el.offsetParent;
}
return {
x: xPosition,
y: yPosition
};
}
const canvasPos = getPosition(canvas);
let mouseX = 0;
let mouseY = 0;
canvas.addEventListener('mousemove', setMousePosition, false);
canvas.onmousedown = () => {
world.mode === 1 ? world.mode = 2 : world.mode = 1;
};
function setMousePosition(e) {
mouseX = e.clientX - canvasPos.x;
mouseY = e.clientY - canvasPos.y;
}
function draw() {
// ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
ctx.clearRect(0, 0, canvas.width, canvas.height);
world.draw(ctx);
world.simulate();
raf = window.requestAnimationFrame(draw);
}
draw();