make npcs less annoying
This commit is contained in:
parent
7d83d31746
commit
276e11fb49
6 changed files with 45 additions and 30 deletions
23
common/entity.js
Normal file
23
common/entity.js
Normal file
|
@ -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;
|
|
@ -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++) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
2
game.js
2
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);
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
Loading…
Reference in a new issue