add simple fuzz test

This commit is contained in:
Asraelite 2016-03-27 20:26:53 +01:00
parent 0a90b6b77a
commit 27520842e3
59 changed files with 191 additions and 34 deletions

View file

@ -0,0 +1,29 @@
Renderer.prototype.renderAsteroid = (pallet, body) => {
var pos = body.getPos();
var x = pos.x * SCALE;
var y = pos.y * SCALE;
var vx = -game.world.getCenter().x;
var vy = -game.world.getCenter().y;
pallet.view(x + vx, y + vy, false, pos.r);
var context = pallet.context;
var points = body.frame[0];
context.beginPath();
context.moveTo(points[0][0], points[0][1]);
for (var i = 1; i < points.length; i++) {
context.lineTo(points[i][0], points[i][1]);
}
context.closePath();
context.clip();
context.fillStyle = body.debug ? `rgb(${body.debug}, 9, 9)` : '#090909';
context.fill();
context.lineWidth = 7;
context.strokeStyle = '#000';
context.stroke();
context.lineWidth = 3;
context.strokeStyle = '#fff';
context.stroke();
pallet.restore();
};

View file

@ -0,0 +1,27 @@
Renderer.prototype.renderBody = (pallet, body) => {
var pos = body.getPos();
var x = pos.x * SCALE;
var y = pos.y * SCALE;
var vx = -game.world.getCenter().x;
var vy = -game.world.getCenter().y;
pallet.view(x + vx, y + vy, false, pos.r);
var context = pallet.context;
var polys = body.frame;
for (var points of polys) {
context.beginPath();
context.moveTo(points[0][0], points[0][1]);
for (var i = 1; i < points.length; i++) {
context.lineTo(points[i][0], points[i][1]);
}
context.closePath();
context.lineWidth = 0.5;
context.strokeStyle = '#fff';
context.fillStyle = '#200';
context.fill();
context.stroke();
}
pallet.restore();
};

View file

@ -0,0 +1,92 @@
class Effect {
constructor(data) {
for (var i in data) {
this[i] = data[i];
}
this.particles = new Set();
this.pallet = game.renderer.pallet;
if (this.type == 'explosion') {
this.createExplosion();
} else if (this.type == 'rope') {
this.createRope();
this.pos = {
x: 0,
y: 0
};
} else {
}
}
generateParticles(_x, _y, radius, number, colors, sizes, bhv, lf, vel) {
for (var i = 0; i < number; i++) {
let x = _x + (Math.random() - 0.5) * radius * 2;
let y = _y + (Math.random() - 0.5) * radius * 2;
let color = colors[Math.random() * colors.length | 0];
let size = sizes[Math.random() * sizes.length | 0];
let angle = Math.random() * Math.PI * 2;
let v = Math.random() * vel + 0.1;
let xvel = Math.cos(angle) * v + (Math.random() - 0.5);
let yvel = Math.sin(angle) * v + (Math.random() - 0.5);
let p = new Particle(this, x, y, xvel, yvel, color, size, bhv, lf);
this.particles.add(p);
}
}
render() {
let x = this.pos.x * SCALE;
let y = this.pos.y * SCALE;
let vx = -game.world.getCenter().x;
let vy = -game.world.getCenter().y;
this.pallet.view(x + vx, y + vy, false, 0);
this.particles.forEach(p => {
p.render();
p.tick();
});
if (this.particles.size == 0 && !this.keepAlive) {
game.renderer.effects.delete(this);
}
rope: if (this.type == 'rope') {
let bd = game.world.bodies;
if (!bd[this.bodyA.id] || !bd[this.bodyB.id]) {
this.keepAlive = false;
break rope;
}
let p1 = this.posA;
let p2 = this.posB;
let posA = this.bodyA.b2body.GetWorldPoint(new b2Vec2(p1.x, p1.y));
let posB = this.bodyB.b2body.GetWorldPoint(new b2Vec2(p2.x, p2.y));
let context = this.pallet.context;
context.beginPath();
context.moveTo(posA.x * SCALE, posA.y * SCALE);
context.lineTo(posB.x * SCALE, posB.y * SCALE);
context.strokeStyle = '#555';
context.stroke();
}
this.pallet.restore();
}
// Effect generation.
createExplosion() {
let num = this.size * this.size;
let colors = ['#f52', '#ff7'];
let b = 'sizzle';
this.generateParticles(0, 0, 1, num, colors, [1, 2], b, 50, 3);
}
createRope() {
this.bodyA = game.world.bodies[this.bodyA];
this.bodyB = game.world.bodies[this.bodyB];
if (!this.bodyA || !this.bodyB) return;
this.keepAlive = true;
}
}

View file

@ -0,0 +1,39 @@
class Particle {
constructor(effect, x, y, xvel, yvel, color, size, behaviour, lifetime) {
this.effect = effect;
this.x = x;
this.y = y;
this.xvel = xvel || 0;
this.yvel = yvel || 0;
this.color = color || '#f00';
this.size = size || 1;
this.behaviour = behaviour;
this.lifetime = lifetime * (1 + (Math.random() - 0.5) * 0.5) || 100;
}
tick() {
this.x += this.xvel;
this.y += this.yvel;
if (this.behaviour == 'sizzle') {
this.xvel *= 0.98;
this.yvel *= 0.98;
this.x += (Math.random() - 0.5) * 2;
this.y += (Math.random() - 0.5) * 2;
}
if (this.lifetime-- <= 0) {
this.destroy();
}
}
render() {
let x = this.x;
let y = this.y;
this.effect.pallet.square(this.color, x, y, this.size);
}
destroy() {
this.effect.particles.delete(this);
}
}

View file

@ -0,0 +1,94 @@
//@10
class Renderer {
constructor() {
let pallet = new Pallet();
let canvas = pallet.canvas;
let context = pallet.context;
this.pallet = pallet;
this.canvas = canvas;
this.context = context;
this.effects = new Set();
pallet.fillScreen();
window.addEventListener('resize', pallet.fillScreen);
}
render(state) {
let canvas = this.canvas;
let context = this.context;
let pallet = this.pallet;
let ship = game.world.playerShip;
let cpos = game.world.getCenter();
let cx = -cpos.x;
let cy = -cpos.y;
let cw = canvas.width;
let ch = canvas.height;
if (state == 'connecting' || state == 'disconnected') {
pallet.clear();
pallet.fill('#111');
var str = state == 'connecting' ? 'Connecting' : 'Shit\'s ' +
'diconnected, yo!';
pallet.text(str, canvas.width / 2, canvas.height / 2, '#fff',
'FreePixel', 16, 'center', 'middle');
return;
}
pallet.clear();
pallet.fill('#020202');
context.save();
pallet.view(cw / 2, ch / 2, 1, 0);
//context.translate(-cx / s, -cy / s);
// Grid
var gridx = cx % 50;
var gridy = cy % 50;
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) {
var wx = (-cx + x) / SCALE;
var wy = (-cy + y) / SCALE;
var b = game.world.bounds;
if (wx > b.right || wx < b.left || wy > b.bottom || wy < b.top) {
pallet.opacity(0.2);
pallet.outline('#8af', x, y, 51, 51, 1);
pallet.opacity(0.05);
} else pallet.outline('#fff', x, y, 51, 51, 1);
}
}
pallet.opacity(1);
for (var id in game.world.bodies) {
var body = game.world.bodies[id];
if (body.bodyType == 'ship') {
this.renderShip(pallet, body);
} else if (body.bodyType == 'asteroid') {
this.renderAsteroid(pallet, body);
} else {
this.renderBody(pallet, body);
// Render structures, projectiles etc..
}
}
this.effects.forEach(effect => {
effect.render();
});
pallet.restore();
}
renderGrid() {
}
addEffect(data) {
this.effects.add(new Effect(data));
}
}

View file

@ -0,0 +1,35 @@
Renderer.prototype.renderShip = (pallet, ship) => {
let img = game.assets.images.ships[ship.hull].hull;
let teama = game.assets.images.ships[ship.hull].teama;
let teamb = game.assets.images.ships[ship.hull].teamb;
let thr0 = game.assets.images.ships[ship.hull].thrust0;
let thr5 = game.assets.images.ships[ship.hull].thrust5;
let thr8 = game.assets.images.ships[ship.hull].thrust8;
let turr = game.assets.images.turrets['01'].small;
//pallet.view(ship.x, ship.y, false, ship.r);
let pos = ship.getPos();
let x = pos.x * SCALE;
let y = pos.y * SCALE;
let vx = -game.world.getCenter().x;
let vy = -game.world.getCenter().y;
pallet.view(x + vx, y + vy, false, pos.r);
let ts = ship.size / 2;
for (let i = 0; i < ship.mounts.length; i++) {
if (ship.turrets[i]) {
pallet.image(turr, ship.mounts[i][0] - ts, ship.mounts[i][1] - ts, 0);
}
}
pallet.image(ship.team == 'a' ? teama : teamb, 0, 0, 0);
pallet.image(img, 0, 0, 0);
pallet.image(ship.thrust.forward ? thr8 : thr0, 0, 0, 0);
if (ship.debug) {
pallet.square('#f00', ship.debug.x * SCALE, ship.debug.y * SCALE, 2);
}
pallet.restore();
//pallet.text(ship.name, x + vx | 0, y + vy | 0, '#fff', 'FreePixel', 16, 'center', 'bottom');
}