clustering
This commit is contained in:
parent
a63e44ed6a
commit
476da443b7
2 changed files with 42 additions and 25 deletions
48
js/index.js
48
js/index.js
|
@ -16,7 +16,7 @@ function Game() {
|
||||||
let player = new Player();
|
let player = new Player();
|
||||||
let entities = [player];
|
let entities = [player];
|
||||||
|
|
||||||
for (let i = 0; i < 15; i++) {
|
for (let i = 0; i < 50; i++) {
|
||||||
entities.push(new Player())
|
entities.push(new Player())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,29 +24,39 @@ function Game() {
|
||||||
|
|
||||||
this.canvas = canvas;
|
this.canvas = canvas;
|
||||||
this.ctx = canvas.getContext("2d");
|
this.ctx = canvas.getContext("2d");
|
||||||
this.ticks = 1;
|
|
||||||
this.assetsIn = assetsIn;
|
this.assetsIn = assetsIn;
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.entities = entities;
|
this.entities = entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
Game.prototype.main = function () {
|
Game.prototype.main = function () {
|
||||||
let { entities } = this;
|
let { entities, player } = this;
|
||||||
|
|
||||||
this.ticks++;
|
for (let ent of entities) {
|
||||||
|
ent.pos.x += ent.vel.x;
|
||||||
|
ent.pos.y += ent.vel.y;
|
||||||
|
|
||||||
for (let player of entities) {
|
ent.vel.x *= 0.8;
|
||||||
player.pos.x += player.vel.x;
|
ent.vel.y *= 0.8;
|
||||||
player.pos.y += player.vel.y;
|
|
||||||
|
|
||||||
player.vel.x *= 0.8;
|
ent.rot += 0.03 * ent.dir;
|
||||||
player.vel.y *= 0.8;
|
ent.rot = ent.rot % (Math.PI * 10);
|
||||||
|
|
||||||
player.rot += 0.03 * player.dir;
|
ent.camera.x = -ent.pos.x * 0.1 + ent.camera.x * 0.9;
|
||||||
player.rot = player.rot % (Math.PI * 10);
|
ent.camera.y = -ent.pos.y * 0.1 + ent.camera.y * 0.9;
|
||||||
|
|
||||||
player.camera.x = -player.pos.x * 0.1 + player.camera.x * 0.9;
|
ent.ticks++;
|
||||||
player.camera.y = -player.pos.y * 0.1 + player.camera.y * 0.9;
|
|
||||||
|
let dp = (Math.sin(ent.rot) * (ent.pos.x - player.pos.x))
|
||||||
|
- (Math.cos(ent.rot) * (ent.pos.y - player.pos.y));
|
||||||
|
|
||||||
|
dp /= ((ent.pos.x - player.pos.x) ** 2) + ((ent.pos.y - player.pos.y) ** 2);
|
||||||
|
|
||||||
|
dp *= 30;
|
||||||
|
|
||||||
|
if (Math.random() < -dp && Math.random() > 5 / (ent.ticks+2)) {
|
||||||
|
ent.bump();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,17 +88,7 @@ Game.prototype.render = function () {
|
||||||
Game.prototype.click = function () {
|
Game.prototype.click = function () {
|
||||||
let { player } = this;
|
let { player } = this;
|
||||||
|
|
||||||
if (this.ticks < 10) {
|
player.bump(game);
|
||||||
player.dir *= -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
player.vel.x *= 0.6;
|
|
||||||
player.vel.y *= 0.6;
|
|
||||||
|
|
||||||
player.vel.x += Math.sin(player.rot) * 25;
|
|
||||||
player.vel.y -= Math.cos(player.rot) * 25;
|
|
||||||
|
|
||||||
this.ticks = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var game = new Game();
|
var game = new Game();
|
||||||
|
|
19
js/player.js
19
js/player.js
|
@ -1,5 +1,5 @@
|
||||||
function Player() {
|
function Player() {
|
||||||
let pos = { x: Math.random() * 1000 - 50, y: Math.random() * 1000 - 50 };
|
let pos = { x: Math.random() * 5000 - 50, y: Math.random() * 5000 - 50 };
|
||||||
let camera = { x: -pos.x, y: -pos.y };
|
let camera = { x: -pos.x, y: -pos.y };
|
||||||
let vel = { x: 0, y: 0 };
|
let vel = { x: 0, y: 0 };
|
||||||
|
|
||||||
|
@ -8,4 +8,21 @@ function Player() {
|
||||||
this.vel = vel;
|
this.vel = vel;
|
||||||
this.rot = 0;
|
this.rot = 0;
|
||||||
this.dir = 1;
|
this.dir = 1;
|
||||||
|
this.ticks = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Player.prototype.bump = function () {
|
||||||
|
let player = this;
|
||||||
|
|
||||||
|
if (player.ticks < 10) {
|
||||||
|
player.dir *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
player.vel.x *= 0.6;
|
||||||
|
player.vel.y *= 0.6;
|
||||||
|
|
||||||
|
player.vel.x += Math.sin(player.rot) * 25;
|
||||||
|
player.vel.y -= Math.cos(player.rot) * 25;
|
||||||
|
|
||||||
|
player.ticks = 0;
|
||||||
}
|
}
|
Loading…
Reference in a new issue