add lasers

This commit is contained in:
Asraelite 2016-03-30 19:15:06 +01:00
parent cf4e8024af
commit f87aa1446d
31 changed files with 298 additions and 125 deletions

View file

Before

Width:  |  Height:  |  Size: 215 B

After

Width:  |  Height:  |  Size: 215 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 293 B

After

Width:  |  Height:  |  Size: 293 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 210 B

After

Width:  |  Height:  |  Size: 210 B

Before After
Before After

View file

@ -13,10 +13,11 @@ Game.prototype.loadAssets = _ => {
},
turrets: {
'01': {
small: 'img/turrets/01/normal.png'
'0': 'img/turrets/01/0.png'
},
'02': {
small: 'img/turrets/02/normal.png'
'0': 'img/turrets/02/0.png',
'1': 'img/turrets/02/1.png'
}
},
projectiles: {

View file

@ -13,12 +13,12 @@ GUI.prototype.Weapons = class {
this.element.innerHTML = '';
ship.fixtures.forEach((fixture, i) => {
if (!fixture.type) return;
if (!fixture.fixture) return;
let div = document.createElement('div');
div.classList.add('weapon');
if (ship.activeFixture == i)
div.classList.add('active');
let img = `url(/img/turrets/${fixture.type}/normal.png)`;
let img = `url(/img/turrets/${fixture.fixture}/normal.png)`;
div.style.backgroundImage = img;
this.element.appendChild(div);
});

View file

@ -17,7 +17,6 @@ class Net {
});
this.socket.on('update', data => {
window.q = data;
game.world.update(data);
});

View file

@ -15,7 +15,7 @@ class Player {
let packet = {};
packet.thrust = ['w', 'a', 'd', 's'].map(k => +input.keys.held[k] || 0);
packet.fire = [1, 3].map(k => +input.mouse.pressed[k] || 0);
packet.fire = [1, 1, 3].map(k => +input.mouse.pressed[k] || 0);
packet.aim = [
+input.mouse.wx.toFixed(2),
+input.mouse.wy.toFixed(2)

View file

@ -9,18 +9,15 @@ class BodyRenderer {
let pos = body.pos;
let x = pos.x * SCALE;
let y = pos.y * SCALE;
let vx = -game.world.center.x;
let vy = -game.world.center.y;
let pallet = this.pallet;
let context = pallet.context;
pallet.view(vx, vy, false, 0);
pallet.view(x, y, false, pos.r);
for (let f of body.fixtures) {
if (!f.fixture || !f.hidden) continue;
let img = game.assets.images.turrets[f.fixture].small;
let img = game.assets.images.turrets[f.fixture][f.state];
this.pallet.image(img, f.x - 32, f.y - 32, f.angle);
}
@ -36,12 +33,11 @@ class BodyRenderer {
for (let f of body.fixtures) {
if (!f.fixture || f.hidden) continue;
let img = game.assets.images.turrets[f.fixture].small;
let img = game.assets.images.turrets[f.fixture][f.state];
this.pallet.image(img, f.x - 32, f.y - 32, f.angle);
}
pallet.restore();
pallet.restore();
}

View file

@ -0,0 +1,21 @@
class DischargeRenderer {
constructor(renderer) {
this.pallet = renderer.pallet;
this.canvas = this.pallet.canvas;
this.context = this.pallet.context;
}
render(discharge) {
let x = discharge.x * SCALE;
let y = discharge.y * SCALE;
let pallet = this.pallet;
let context = this.context;
pallet.view(x, y, false, discharge.r);
pallet.square('#f00', 0, 0, 2);
pallet.restore();
}
}

View file

@ -16,6 +16,7 @@ class Renderer {
window.addEventListener('resize', _ => pallet.fillScreen(1000, 600));
this.bodyRenderer = new BodyRenderer(this);
this.dischargeRenderer = new DischargeRenderer(this);
}
render(state) {
@ -53,10 +54,20 @@ class Renderer {
this.renderGrid();
let vx = -game.world.center.x;
let vy = -game.world.center.y;
this.pallet.view(vx, vy, false, 0);
for (var id in game.world.bodies) {
this.bodyRenderer.render(game.world.bodies[id]);
}
for (var id in game.world.discharges) {
this.dischargeRenderer.render(game.world.discharges[id]);
}
this.pallet.restore();
this.effects.forEach(effect => {
effect.render();
});
@ -76,8 +87,10 @@ class Renderer {
let cw = this.canvas.width;
let ch = this.canvas.height;
var gridx = cx % 50;
var gridy = cy % 50;
let gridx = cx % 50;
let gridy = cy % 50;
let lastBlue = false;
this.pallet.opacity(0.05);
for (var x = gridx - cw / 2 - 50; x < cw + 50; x += 50) {
for (var y = gridy - ch / 2 - 50; y < ch + 50; y += 50) {
@ -85,10 +98,18 @@ class Renderer {
var wy = ((-cy + y) / SCALE) | 0;
var b = game.world.bounds;
if (wx > b.right || wx < b.left || wy > b.bottom || wy < b.top) {
this.pallet.opacity(0.2);
if (!lastBlue) {
this.pallet.opacity(0.2);
lastBlue = true;
}
this.pallet.outline('#8af', x, y, 51, 51, 1);
this.pallet.opacity(0.05);
} else this.pallet.outline('#fff', x, y, 51, 51, 1);
} else {
if (lastBlue) {
this.pallet.opacity(0.05);
lastBlue = false;
}
this.pallet.outline('#fff', x, y, 51, 51, 1);
}
}
}
this.pallet.opacity(1);

View file

@ -9,6 +9,11 @@ class Body {
this.id = data.id
this.frame = data.frame;
this.fixtures = data.fixtures;
this.fixtures = this.fixtures.map(f => {
f.x *= 32;
f.y *= 32;
return f;
});
this.b2body = false;
this.updated = 0;
this.bodyClass = data.class;
@ -49,7 +54,7 @@ class Body {
this.fixtures.forEach(fixture => {
let obj = {};
this.interface.fixtures.order.forEach(v => obj[v] = data.shift());
fixture.angle = obj.angle;
fixture.state = obj.state;
});

View file

@ -0,0 +1,51 @@
//@10
class Discharge {
constructor(data) {
this.interface = {
order: ['x', 'y'],
size: 2
};
this.xvel = data.xvel;
this.yvel = data.yvel;
this.r = data.r;
this.id = data.id;
this.updated = 0;
this.update(data.delta.slice(1));
}
getPos() {
return this.pos;
}
applyForce(x, y) {
var b = this.b2body;
b.ApplyForce(new b2Vec2(x, y), b.GetWorldCenter());
}
applyTorque(f) {
this.b2body.ApplyTorque(f);
}
update(data) {
let values = {};
console.log(data);
this.interface.order.forEach(v => values[v] = data.shift());
this.x = values.x;
this.y = values.y;
this.updated = 10;
}
tick() {
this.x += this.xvel;
this.y += this.yvel;
}
get pos() {
return {
x: this.x,
y: this.y
};
}
}

View file

@ -3,6 +3,7 @@ const SCALE = 32;
class World {
constructor() {
this.bodies = {};
this.discharges = {};
this.playerShip = false;
this.physics = new Physics();
@ -15,6 +16,15 @@ class World {
}
add(data) {
if(data.form != 'body') console.log(data);
if (data.form == 'body') {
this.addBody(data);
} else if (data.form == 'discharge') {
this.addDischarge(data);
}
};
addBody(data) {
var body;
if (data.type == 'asteroid') body = new Asteroid(data);
if (data.type == 'ship') body = new Ship(data);
@ -24,11 +34,18 @@ class World {
this.bodies[body.id] = body;
this.physics.createBody(body);
};
}
addDischarge(data) {
var discharge;
discharge = new Discharge(data);
this.discharges[discharge.id] = discharge;
}
remove(id) {
this.physics.removeBody(this.bodies[id]);
delete this.bodies[id];
delete this.discharges[id];
};
clear() {
@ -43,13 +60,14 @@ class World {
while (i < data.length) {
let id = data[i++];
let body = this.bodies[id];
let discharge = this.discharges[id];
if (!body) {
game.net.send('requestBodyData', id);
return;
if (body) {
body.update(data.slice(i, i + body.interface.size));
} else if (discharge) {
discharge.update(data.slice(i, i + 2));
}
body.update(data.slice(i, i + body.interface.size));
i += body.interface.size;
}
};
@ -66,6 +84,10 @@ class World {
for (var i in this.bodies) {
this.bodies[i].tick();
}
for (var i in this.discharges) {
this.discharges[i].tick();
}
};
get center() {