Procházet zdrojové kódy

neat things

master
mort před 10 roky
rodič
revize
d73d5ac220
3 změnil soubory, kde provedl 33 přidání a 15 odebrání
  1. 1
    0
      .gitignore
  2. 1
    1
      conf.json
  3. 31
    14
      js/game.js

+ 1
- 0
.gitignore Zobrazit soubor

node_modules node_modules
.jshintrc .jshintrc
npm-debug.log

+ 1
- 1
conf.json Zobrazit soubor

{ {
"port": 89
"port": 8085
} }

+ 31
- 14
js/game.js Zobrazit soubor

intersects(b) { intersects(b) {
let a = this; let a = this;


//console.log("if ("+a.x+", "+a.y+") intersects ("+b.x+", "+b.y+")");

return ( return (
(inRange(a.x, b.x, b.x + b.width) || inRange(b.x, a.x, a.x + a.width)) && (inRange(a.x, b.x, b.x + b.width) || inRange(b.x, a.x, a.x + a.width)) &&
(inRange(a.y, b.y, b.y + b.height) || inRange(b.y, a.y, a.y + a.height)) (inRange(a.y, b.y, b.y + b.height) || inRange(b.y, a.y, a.y + a.height))
this.ownerId = ownerId; this.ownerId = ownerId;
this.vel = vel; this.vel = vel;


setTimeout(() => this.despawn(), 4000);
setTimeout(() => this.despawn(), 2000);


this.send(true); this.send(true);
} }


class Player extends Entity { class Player extends Entity {
constructor(sock, id, game) { constructor(sock, id, game) {
super(randint(-50, 50), randint(-50, 50), 25, 60, id, game);
super(randint(-300, 300), randint(-300, 300), 25, 60, id, game);
this.sock = sock; this.sock = sock;
this.keys = {}; this.keys = {};
this.dead = false; this.dead = false;
update(dt) { update(dt) {
let f = new Vec2(0, 0); let f = new Vec2(0, 0);


if (this.keys.up)
f.set(0, -0.9);
if (this.keys.up) {
if (this.keys.sprint)
f.set(0, -5);
else
f.set(0, -2);
}
if (this.keys.down) if (this.keys.down)
f.set(0, 0.9);
f.set(0, 2);

if (this.keys.left) if (this.keys.left)
this.rotForce -= 0.005;
this.rotForce -= 0.03;
if (this.keys.right) if (this.keys.right)
this.rotForce += 0.005;
this.rotForce += 0.03;


if (this.keys.shoot && this.canShoot) { if (this.keys.shoot && this.canShoot) {
let vel = new Vec2(0, -1).rotate(this.rot).add(this.vel); let vel = new Vec2(0, -1).rotate(this.rot).add(this.vel);
let b = new Bullet(pos, vel, this.id, this.game.id, this.game); let b = new Bullet(pos, vel, this.id, this.game.id, this.game);
this.game.spawn(b); this.game.spawn(b);
this.canShoot = false; this.canShoot = false;
setTimeout(() => this.canShoot = true, 50);
setTimeout(() => this.canShoot = true, 100);
} }


f.rotate(this.rot); f.rotate(this.rot);
this.force(f.x, f.y); this.force(f.x, f.y);


this.vel.scale(0.99);
this.vel.scale(0.9);
this.rotVel *= 0.86;


//Detect collissions //Detect collissions
this.game.entities.forEach((e) => { this.game.entities.forEach((e) => {
if (e instanceof Bullet) { if (e instanceof Bullet) {
if (e.ownerId !== this.id && this.intersectsPoint(e)) { if (e.ownerId !== this.id && this.intersectsPoint(e)) {
this.health -= 3;
this.health -= 10;
e.despawn(); e.despawn();
if (this.health <= 0) if (this.health <= 0)
this.despawn(); this.despawn();
this.sendTimeout = null; this.sendTimeout = null;
this.prevTime = null; this.prevTime = null;


this.updateInterval = 1000/60;
this.updateInterval = 1000/30;
this.sendInterval = 1000/15; this.sendInterval = 1000/15;
this.dt = 0; this.dt = 0;


this.dt = new Date().getTime() - this.prevTime; this.dt = new Date().getTime() - this.prevTime;
this.prevTime = new Date().getTime(); this.prevTime = new Date().getTime();


this.entities.forEach((e) => e.move(this.dt));
let dimx = 40000;
let dimy = 40000;
this.entities.forEach((e) => {
e.move(this.dt);

if (e.pos.x > dimx)
e.pos.x = -dimx;
else if (e.pos.x < -dimx)
e.pos.x = dimx;
if (e.pos.y > dimy)
e.pos.y = -dimy;
else if (e.pos.y < -dimy)
e.pos.y = dimy;
});
this.entities.forEach((e) => e.update()); this.entities.forEach((e) => e.update());
this.updateTimeout = setTimeout(this.update.bind(this), this.updateInterval); this.updateTimeout = setTimeout(this.update.bind(this), this.updateInterval);
} }

Načítá se…
Zrušit
Uložit