diff --git a/common/entity.js b/common/entity.js new file mode 100644 index 0000000..e532aef --- /dev/null +++ b/common/entity.js @@ -0,0 +1,23 @@ +class Entity { + constructor(game) { + let pos = { x: (Math.random() - 0.5) * (game ? game.width : 1000), y: (Math.random() - 0.5) * (game ? game.height : 1000) }; + let vel = { x: 0, y: 0 }; + + this.pos = pos; + this.vel = vel; + } + bounce(game) { + let ent = this; + let { width, height } = game; + + if (Math.abs(ent.pos.x) >= width / 2) { + ent.vel.x = (Math.abs(ent.vel.x) + 10) * -Math.sign(ent.pos.x); + } + + if (Math.abs(ent.pos.y) >= height / 2) { + ent.vel.y = (Math.abs(ent.vel.y) + 10) * -Math.sign(ent.pos.y); + } + } +} + +export default Entity; \ No newline at end of file diff --git a/common/game_basic.js b/common/game_basic.js index 9788af8..d9617c3 100644 --- a/common/game_basic.js +++ b/common/game_basic.js @@ -2,7 +2,7 @@ import Player from "./player.js"; class GameBasic { constructor(width = 7500, height = 7500) { - let player = new Player(false, true); + let player = new Player(false, true, this); let entities = [player]; /*for (let i = 0; i < 50; i++) { diff --git a/common/npc.js b/common/npc.js index 3fbd087..893824a 100644 --- a/common/npc.js +++ b/common/npc.js @@ -1,12 +1,9 @@ import { distF, uuidv4 } from './util.js' +import Entity from "./entity.js"; -class NPC { - constructor(you) { - let pos = { x: Math.random() * 1000 - 50, y: Math.random() * 1000 - 50 }; - let vel = { x: 0, y: 0 }; - - this.pos = pos; - this.vel = vel; +class NPC extends Entity { + constructor(you, game = false) { + super(game); this.you = you || uuidv4(); @@ -38,14 +35,16 @@ class NPC { let distX = (this.destX - this.pos.x); let distY = (this.destY - this.pos.y) - this.vel.x += (distX + distY * this.rFac / 1000) * 0.001; - this.vel.y += (distY - distX * this.rFac / 1000) * 0.001; + this.vel.x += (distX + distY * this.rFac / 1000) * 0.0003; + this.vel.y += (distY - distX * this.rFac / 1000) * 0.0003; - this.vel.x *= 0.8; - this.vel.y *= 0.8; + this.vel.x *= 0.98; + this.vel.y *= 0.98; this.pos.x += this.vel.x; this.pos.y += this.vel.y; + + this.bounce(game); } } diff --git a/common/player.js b/common/player.js index 0781444..6acf3a8 100644 --- a/common/player.js +++ b/common/player.js @@ -1,14 +1,12 @@ import { distF, uuidv4 } from './util.js' +import Entity from './entity.js'; -class Player { +class Player extends Entity { constructor(you, isPlayer, game = false) { - let pos = { x: (Math.random() - 0.5) * (game ? game.width : 1000), y:(Math.random() - 0.5) * (game ? game.height : 1000) }; - let camera = { x: -pos.x, y: -pos.y }; - let vel = { x: 0, y: 0 }; + super(game); + + this.camera = { x: -this.pos.x, y: -this.pos.y }; - this.camera = camera; - this.pos = pos; - this.vel = vel; this.rot = 0; this.dir = 1; this.ticks = 0; @@ -65,13 +63,7 @@ class Player { ent.pos.x = Math.max(Math.min(ent.pos.x, width / 2), - width / 2); ent.pos.y = Math.max(Math.min(ent.pos.y, height / 2), - height / 2); - if (Math.abs(ent.pos.x) >= width / 2 ) { - ent.vel.x = (Math.abs(ent.vel.x) + 10) * -Math.sign(ent.pos.x); - } - - if (Math.abs(ent.pos.y) >= height / 2) { - ent.vel.y = (Math.abs(ent.vel.y) + 10) * -Math.sign(ent.pos.y); - } + ent.bounce(game); ent.vel.x *= 0.9; ent.vel.y *= 0.9; @@ -97,7 +89,7 @@ class Player { dp /= Math.sqrt(dist + 0.1); if (Math.sqrt(dist) < 128 && 1 / dp < -0.2) { - if (target.type == 'NPC' && velness > 0.2) { + if (target.type == 'NPC' && velness > 0.2 && ent.ticks >= 15) { ent.isMenu = true; ent.rot = 1.2; } @@ -105,9 +97,9 @@ class Player { let oldHealth = target.health - let dmg = Math.floor( (dp-0.001) * 10); + let dmg = Math.floor((dp - 0.001) * 10); - target.health += Math.floor( (dp-0.001) * 10); + target.health += Math.floor((dp - 0.001) * 10); target.vel.x += (target.pos.x - ent.pos.x) * 0.1; target.vel.y += (target.pos.y - ent.pos.y) * 0.1; diff --git a/game.js b/game.js index 7722191..5e5fd01 100644 --- a/game.js +++ b/game.js @@ -99,7 +99,7 @@ class Game extends GameBasic { that.entities = []; for (let i = 0; i < 20; i++) { - that.entities.push(new NPC()) + that.entities.push(new NPC(false,that)) } setInterval(function () { that.sync(false) }, 1000 / 10); diff --git a/static/js/index.js b/static/js/index.js index b59fcde..e64ca71 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -175,6 +175,7 @@ class Game extends GameBasic { } x.handleTick = type.prototype.handleTick; + x.bounce = type.prototype.bounce; if (x.r != 1 && type == Player) { let a = new Audio(`sfx/${emojis[x.r]}.wav`); a.addEventListener('ended', () => {