Improve Paperoids example.

This commit is contained in:
Jonathan Puckey 2013-03-10 15:58:27 +01:00
parent fd3fc7f2d7
commit f6cf20fa8c

View file

@ -245,24 +245,47 @@
var Bullets = new function() { var Bullets = new function() {
var group = new Group(); var group = new Group();
var children = group.children; var children = group.children;
function checkHits(bullet) {
for (var r = 0; r < Rocks.children.length; r++) {
var rock = Rocks.children[r];
if (rock.bounds.contains(bullet.position) ) {
Score.update(rock.shapeType);
Rocks.explode(rock);
if (rock.shapeType < Rocks.TYPE_SMALL ) {
for (var j = 0; j < 2; j++) {
Rocks.add(1, rock.shapeType + 4, rock.position);
}
}
rock.remove();
bullet.remove();
}
}
}
return { return {
fire: function(position, angle) { fire: function(position, angle) {
if (children.length > 4) return; // We can only fire 5 bullets at a time:
var bullet = new Path.Circle(position, 0.5); if (children.length == 5)
bullet.fillColor = 'white'; return;
bullet.strokeWidth = 0; var vector = new Point({
bullet.data = { angle: angle,
vector: new Point({ length: 10
angle: angle, });
length: 10 var bullet = new Path.Circle({
}), center: position + vector,
timeToDie: 58 radius: 0.5,
}; parent: group,
bullet.position += bullet.data.vector; fillColor: 'white',
group.addChild(bullet); strokeWidth: 'white',
strokeWidth: 0,
data: {
vector: vector,
timeToDie: 58
}
});
}, },
move: function() { move: function() {
// check for bullet hit
for (var i = 0; i < children.length; i++) { for (var i = 0; i < children.length; i++) {
var bullet = children[i]; var bullet = children[i];
bullet.data.timeToDie--; bullet.data.timeToDie--;
@ -270,20 +293,7 @@
bullet.remove(); bullet.remove();
} else { } else {
bullet.position += bullet.data.vector; bullet.position += bullet.data.vector;
for (var r = 0; r < Rocks.children.length; r++) { checkHits(bullet);
var rock = Rocks.children[r];
if (rock.bounds.contains(bullet.position) ) {
Score.update(rock.shapeType);
Rocks.explode(rock);
if (rock.shapeType < Rocks.TYPE_SMALL ) {
for (var j = 0; j < 2; j++) {
Rocks.add(1, rock.shapeType + 4, rock.position);
}
}
rock.remove();
bullet.remove();
}
}
keepInView(bullet); keepInView(bullet);
} }
} }