1 decade ago by Ralliare
I'm trying to write the logic for finishing a stage in a breakout game. I have a few entities setup.
EntityGoal - Resets the puck onto the paddle when it hits the player side of the screen
EntityPaddle - Responde to character movement
EntityBrick - Has health per brick variable, point value
EntityPuck
My problem is I want to check to see if all the bricks have been destroyed. Now I see the best way of doing this is when the puck collides with a brick, check if there are any other bricks. But if a brick has one health and is hit, it gets killed before the collideWith is called.
How would I go about having the puck hit logic being called even when the collided entity is killed?
EntityGoal - Resets the puck onto the paddle when it hits the player side of the screen
EntityPaddle - Responde to character movement
EntityBrick - Has health per brick variable, point value
update: function(){
this.parent();
},
check: function( other ){
this.receiveDamage( 1 );
this.currentAnim = this.anims[this.health];
if ( this.health < 1 ) {
this.die();
}
},
die: function(){
ig.game.score += this.points;
}
EntityPuck
update: function(){
if ( this.launched == null ) {
var paddle = ig.game.getEntitiesByType( EntityPaddle )[0];
// Move the puck to above the paddle in the center of the paddle
this.pos.x = paddle.pos.x + paddle.size.x/2 - 8 ;
this.pos.y = paddle.pos.y - 16;
if( ig.input.state('space') ) {
this.launched = true
this.vel.y = -6000;
}
}
this.parent();
},
collideWith: function(other, axis) {
if (axis === 'y' && other instanceof EntityPaddle) {
this.vel.x = this.calculateAngle( other );
}
if ( other instanceof EntityBrick ) {
console.log('Brick hit');
var bricks = ig.game.getEntitiesByType( EntityBrick );
if ( !bricks ) {
console.log('Out of bricks');
}
console.log( bricks );
}
}
My problem is I want to check to see if all the bricks have been destroyed. Now I see the best way of doing this is when the puck collides with a brick, check if there are any other bricks. But if a brick has one health and is hit, it gets killed before the collideWith is called.
How would I go about having the puck hit logic being called even when the collided entity is killed?
