clustering

This commit is contained in:
biglyderv 2024-09-27 22:05:46 -04:00
parent a63e44ed6a
commit 476da443b7
No known key found for this signature in database
GPG key ID: 33AC87E9ACE66954
2 changed files with 42 additions and 25 deletions

View file

@ -16,7 +16,7 @@ function Game() {
let player = new Player();
let entities = [player];
for (let i = 0; i < 15; i++) {
for (let i = 0; i < 50; i++) {
entities.push(new Player())
}
@ -24,29 +24,39 @@ function Game() {
this.canvas = canvas;
this.ctx = canvas.getContext("2d");
this.ticks = 1;
this.assetsIn = assetsIn;
this.player = player;
this.entities = entities;
}
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) {
player.pos.x += player.vel.x;
player.pos.y += player.vel.y;
ent.vel.x *= 0.8;
ent.vel.y *= 0.8;
player.vel.x *= 0.8;
player.vel.y *= 0.8;
ent.rot += 0.03 * ent.dir;
ent.rot = ent.rot % (Math.PI * 10);
player.rot += 0.03 * player.dir;
player.rot = player.rot % (Math.PI * 10);
ent.camera.x = -ent.pos.x * 0.1 + ent.camera.x * 0.9;
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;
player.camera.y = -player.pos.y * 0.1 + player.camera.y * 0.9;
ent.ticks++;
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 () {
let { player } = this;
if (this.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;
this.ticks = 0;
player.bump(game);
}
var game = new Game();

View file

@ -1,5 +1,5 @@
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 vel = { x: 0, y: 0 };
@ -8,4 +8,21 @@ function Player() {
this.vel = vel;
this.rot = 0;
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;
}