Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

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
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?

1 decade ago by dominic

There are a couple of things you could do. For instance, you could put the logic in the kill() method of the brick. Something like this:

kill: function() {
	// call the parent first, so the entity 
	// is removed before we check the brick count
	this.parent();
	
	if( ig.game.getEntitiesByType(EntityBrick).length == 0 ) {
		// no more bricks
	}
}

Another approach, if you always want to execute the collideWith() method so that the puck will bounce of the brick, you could remove all the check() functionality and put the receiveDamage() in the collideWith() method. I.e.:

collideWith: function(other, axis) {
	if (axis === 'y' && other instanceof EntityPaddle) {
		this.vel.x = this.calculateAngle( other );
	}
	
	if ( other instanceof EntityBrick ) {
		console.log('Brick hit');
		
		other.receiveDamage(1); // give damage
		
		var bricks = ig.game.getEntitiesByType( EntityBrick );
		if ( !bricks ) {
			console.log('Out of bricks');
		}
		console.log( bricks );
	}
}

1 decade ago by pattentrick

You could check for existing bricks in each update call of your EntityPuck or in your main.js.

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;
            }
        }
        
        if ( !ig.game.getEntitiesByType( EntityBrick )  ) {

            // trigger your "end of level" methods

        }

        this.parent();
        
    },

Since the update method is called no matter if there are any bricks left or not, you could easily try to catch it like this.
Page 1 of 1
« first « previous next › last »