this.on("inserted");
this.on("hit",this,"collide");
},
inserted: function() {
this.stage.on("start",this,"start");
},
collide: function(col) {
if(col.obj.isA("Paddle")) {
var dx = (this.p.x - col.obj.p.x) / col.obj.p.w * 2.5;
if(col.normalY <= 0) {
this.p.vy = -this.p.speed;
}
this.p.vx = dx * this.p.speed;
} else {
if(col.normalY < -0.3) {
this.p.vy = -Math.abs(this.p.vy);
}
if(col.normalY > 0.3) {
this.p.vy = Math.abs(this.p.vy);
}
if(col.normalX < -0.3) {
this.p.vx = -Math.abs(this.p.vx);
}
if(col.normalX > 0.3) {
this.p.vx = Math.abs(this.p.vx);
}
if(col.obj.isA("Block")) {
Q.audio.play("brickDeath.ogg");
col.obj.play("hit",2);
}
}
this.p.x -= col.separate[0];
this.p.y -= col.separate[1];
},
start:function() {
this.p.vy = this.p.speed;
this.p.vx = this.p.speed;
},
step: function(dt) {
this.p.x += this.p.vx * dt;
this.p.y += this.p.vy * dt;
this.stage.collide(this);
if(!Q.useTiles) {
if(this.p.x < 24) { this.p.vx = Math.abs(this.p.vx); }
if(this.p.x > Q.width - 24) { this.p.vx = -Math.abs(this.p.vx); }
if(this.p.y < 24) { this.p.vy = Math.abs(this.p.vy); }
}
if(this.p.y > Q.height) {
this.destroy();
}
}
});
Q.Sprite.extend("Block", {
init: function(p) {
this._super({
sheet: "block" + p.num,
sprite: "block"
},p);
this.add("animation");
this.play("appear");
this.on("destroy");
},
destroyed: function() {
Q.state.inc("score",100);
var rand = Math.round(Math.random()*7);
if(rand == 1 && Q("Powerdown").length == 0) {
this.stage.insert(new Q.Powerdown({ x: this.c.x,
y: this.c.y }));
} else if(rand == 2 && Q("Powerup").length == 0) {
this.stage.insert(new Q.Powerup({ x: this.c.x,
y: this.c.y }));
}
}
});
Q.Sprite.extend("Paddle", {
init: function(p) {
this._super({
sheet: "paddlelg",
type: Q.SPRITE_DEFAULT | Q.SPRITE_FRIENDLY,
y: 376,
x: 0,
powerdown: 0
},p);
this.on("powerdown");
this.on("powerup");
},
step: function(dt) {
this.p.x = Q.inputs['mouseX'];
if(Q("Ball").length == 0) {
Q.state.dec("lives",1);
if(Q.state.get("lives") == 0) {
Q.stageScene("gameOver");
} else {
this.stage.insert(new Q.Ball());
this.stage.insert(new Q.Countdown());
}
}
if(this.p.powerdown > 0) {
this.p.powerdown -= dt;
if(this.p.powerdown <= 0) {
Q.audio.play("recover.ogg");
this.sheet("paddlelg",true);
}
}
},
powerup: function() {
var ball = this.stage.insert(new Q.Ball());
ball.start();
},
powerdown: function() {
this.sheet("paddlesm",true);
this.p.powerdown = 10;
}
});
Q.Sprite.extend("Powerdown", {
init: function(p) {
this._super({
sheet: "powerdown",
type: Q.SPRITE_POWERUP,
collisionMask: Q.SPRITE_FRIENDLY,
vy: 100
},p);
this.add("2d")
this.on("hit")
},
hit: function() {
this.destroy();
Q.audio.play("powerdown.ogg");
Q("Paddle").trigger("powerdown");
},
step: function(dt) {
if(this.p.y > Q.height) this.destroy();
}
});
Q.Sprite.extend("Powerup", {
init: function(p) {
this._super({
sheet: "powerup",
type: Q.SPRITE_POWERUP,
collisionMask: Q.SPRITE_FRIENDLY,
vy: 100
},p);
this.add("2d")
this.on("hit")
},
hit: function() {
this.destroy();
Q.audio.play("powerup.ogg");
Q("Paddle").trigger("powerup");
},
step: function(dt) {
if(this.p.y > Q.height) this.destroy();
}
});
Q.Sprite.extend("Countdown", {
init: function(p) {
this._super({
sheet: "count",
sprite: "countdown",
x: Q.width/2,
y: 200
},p);
this.add("animation").play("countdown");