diff --git a/common/game_basic.js b/common/game_basic.js index d7c1e6e..7cee28c 100644 --- a/common/game_basic.js +++ b/common/game_basic.js @@ -1,10 +1,10 @@ class GameBasic { constructor() { - let player = new Player(true); + let player = new Player(false,true); let entities = [player]; for (let i = 0; i < 50; i++) { - entities.push(new Player()) + entities.push(new Player(false,false)) } this.player = player; diff --git a/common/player.js b/common/player.js index 0396490..1f72ca1 100644 --- a/common/player.js +++ b/common/player.js @@ -1,4 +1,14 @@ -function Player(you) { +function distF(ent, target) { + return ((ent.pos.x - target.pos.x) ** 2) + ((ent.pos.y - target.pos.y) ** 2) +} + +function uuidv4() { + return "#000000-000000".replace(/[018]/g, c => + (+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16) + ); +} + +function Player(you, isPlayer) { 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 }; @@ -12,7 +22,11 @@ function Player(you) { this.health = 100; - this.you = you; + this.you = you || uuidv4(); + + this.isPlayer = isPlayer; + + this.headCount = 0; } Player.prototype.bump = function () { @@ -31,11 +45,13 @@ Player.prototype.bump = function () { player.ticks = 0; } -Player.prototype.handleTick = function(game) { - let { player } = game; +Player.prototype.handleTick = function (game) { + let { entities } = game; let ent = this; + if (ent.health <= 0) return; + ent.pos.x += ent.vel.x; ent.pos.y += ent.vel.y; @@ -50,22 +66,22 @@ Player.prototype.handleTick = function(game) { ent.ticks++; - let dist = ((ent.pos.x - player.pos.x) ** 2) + ((ent.pos.y - player.pos.y) ** 2); + for (let target of entities) { + if (target.you == ent.you) continue; - let dp = (Math.sin(ent.rot) * (ent.pos.x - player.pos.x)) - - (Math.cos(ent.rot) * (ent.pos.y - player.pos.y)); + let dist = distF(ent, target); - dp /= dist; + let dp = (Math.sin(ent.rot) * (ent.pos.x - target.pos.x)) + - (Math.cos(ent.rot) * (ent.pos.y - target.pos.y)); - dp *= 10; + dp /= Math.sqrt(dist + 0.1); - if (ent.you) return; + if (Math.sqrt(dist) < 128 && 1 / dp < -0.2) { + target.health--; - if (Math.random() < -dp && Math.random() > 3 / (ent.ticks+2)) { - ent.bump(); - } - - if (Math.sqrt(dist) < 96 / 2) { - player.health --; + if (target.health == 0) { + ent.headCount++; + } + } } } \ No newline at end of file diff --git a/game.js b/game.js new file mode 100644 index 0000000..04202cf --- /dev/null +++ b/game.js @@ -0,0 +1,22 @@ +import GameBasic from "./common/game_basic.js"; + +class Game extends GameBasic { + constructor() { + super(); + } + createPlayer(user) { + + } + sync() { + + } + init() { + super.init(); + + let that = this; + + setInterval(function () { that.sync() }, 1000 / 10); + } +} + +export default Game; \ No newline at end of file diff --git a/static/index.html b/static/index.html index 099e061..3d05439 100644 --- a/static/index.html +++ b/static/index.html @@ -16,9 +16,6 @@