Browse Source

solved performance issues

master
mort 8 years ago
parent
commit
7ea1186d5e
2 changed files with 38 additions and 24 deletions
  1. 37
    24
      js/game.js
  2. 1
    0
      server.js

+ 37
- 24
js/game.js View File

let Vec2 = require("./vec2"); let Vec2 = require("./vec2");


function round(n) {
return Math.round(n * 1000) / 1000
}

function randint(min, max) { function randint(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min; return Math.floor(Math.random() * (max - min + 1)) + min;
} }
if (!first) if (!first)
return; return;


this.game.players.forEach((p) => p.sock.send("set", {
this.game.players.forEach((p) => p.sendSet({
type: "bullet", type: "bullet",
id: this.id, id: this.id,
ownerId: this.ownerId, ownerId: this.ownerId,
this.rotVel = 0; this.rotVel = 0;
this.canShoot = true; this.canShoot = true;
this.health = 100; this.health = 100;
this.sendSetQueue = [];


sock.on("request", (req) => { sock.on("request", (req) => {
if (req.url == "get_id") { if (req.url == "get_id") {
this.force(f.x, f.y); this.force(f.x, f.y);


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


//Detect collissions //Detect collissions
this.game.entities.forEach((e) => { this.game.entities.forEach((e) => {
this.health -= 10; this.health -= 10;
e.despawn(); e.despawn();
if (this.health <= 0) if (this.health <= 0)
this.despawn();
this.despawn();
} }
} }
}); });
//3 2 //3 2
var rotated = [ var rotated = [
new Vec2(-this.width/2, -this.height/2), new Vec2(-this.width/2, -this.height/2),
new Vec2(this.width/2, -this.height/2),
new Vec2(this.width/2, this.height/2),
new Vec2(-this.width/2, this.height/2)
].map((p) => p.rotate(this.rot));
new Vec2(this.width/2, -this.height/2),
new Vec2(this.width/2, this.height/2),
new Vec2(-this.width/2, this.height/2)
].map((p) => p.rotate(this.rot));


let tl = new Vec2(0, 0); let tl = new Vec2(0, 0);
let br = new Vec2(0, 0); let br = new Vec2(0, 0);


rotated.forEach((p) => { rotated.forEach((p) => {
if (p.x < tl.x) if (p.x < tl.x)
tl.x = p.x;
if (p.y < tl.y)
tl.y = p.y;
if (p.x > br.x)
br.x = p.x;
if (p.y > br.y)
br.y = p.y;
tl.x = p.x;
if (p.y < tl.y)
tl.y = p.y;
if (p.x > br.x)
br.x = p.x;
if (p.y > br.y)
br.y = p.y;
}); });


this.boundingRectCache = new Rectangle( this.boundingRectCache = new Rectangle(
this.pos.x + tl.x,
this.pos.y + tl.y,
br.x - tl.x,
br.y - tl.y
);
this.pos.x + tl.x,
this.pos.y + tl.y,
br.x - tl.x,
br.y - tl.y
);


return this.boundingRectCache; return this.boundingRectCache;
} }
this.rotForce = 0; this.rotForce = 0;
} }


sendSet(obj) {
this.sendSetQueue.push(obj);
}

send(first) { send(first) {
let obj = { let obj = {
id: this.id, id: this.id,
pos: this.pos,
vel: this.vel,
rot: this.rot,
rotVel: this.rotVel,
pos: {x: round(this.pos.x), y: round(this.pos.y)},
vel: {x: round(this.vel.x), y: round(this.vel.y)},
rot: round(this.rot),
rotVel: round(this.rotVel),
keys: this.keys, keys: this.keys,
health: this.health health: this.health
} }
if (first) if (first)
obj.type = "player"; obj.type = "player";


this.game.players.forEach((p) => p.sock.send("set", obj));
this.game.players.forEach((p) => p.sendSet(obj));
} }
} }




send() { send() {
this.entities.forEach((e) => e.send()); this.entities.forEach((e) => e.send());
this.players.forEach((p) => {
p.sock.send("set", p.sendSetQueue);
p.sendSetQueue = [];
});
this.sendTimeout = setTimeout(this.send.bind(this), this.sendInterval); this.sendTimeout = setTimeout(this.send.bind(this), this.sendInterval);
} }
} }

+ 1
- 0
server.js View File

}); });


log.info("Server started on port "+conf.port+"."); log.info("Server started on port "+conf.port+".");
log.info("PID: "+process.pid);

Loading…
Cancel
Save