pl/mbogusz3/invaders/model/InvadersModel.java
/**
	 * Perform enemy projectile collision detection. Called from recalculate().
	 */
Perform enemy projectile collision detection. Called from recalculate().
Perform enemy projectile collision detection . Called from recalculate () .
private void collisionDetectionEnemyProjectile() {
		if(this.enemyProjectile == null) {
			return;
		}
		double projectileLeft = this.enemyProjectile.getPositionX() - Projectile.width / 2.0;
		double projectileRight = this.enemyProjectile.getPositionX() + Projectile.width / 2.0;
		double projectileTop = this.enemyProjectile.getPositionY() - Projectile.height / 2.0;
		double projectileBottom = this.enemyProjectile.getPositionY() + Projectile.height / 2.0;
		double obstacleTop = Obstacles.positionTop - Obstacles.height / 2.0;
		double obstacleBottom = Obstacles.positionTop + Obstacles.height / 2.0;
		if(projectileTop < obstacleBottom && projectileBottom > obstacleTop) {
			for(int i = 0; i < this.obstacles.getCount(); i++) {
				if(this.obstacles.getState()[i] == 0) {
					continue;
				}
				double obstacleLeft = (2.0 * i + 1.0) * this.obstacles.getWidth();
				double obstacleRight = (2.0 * i + 2.0) * this.obstacles.getWidth();
				if(projectileRight > obstacleLeft && projectileLeft < obstacleRight) {
					this.hasChanged = true;
					this.enemyProjectile = null;
					this.obstacles.damage(i);
					return;
				}
			}
		}
		double playerTop = Player.positionTop - Player.height / 2.0;
		double playerBottom = Player.positionTop + Player.height / 2.0;
		double playerLeft = this.player.getPosition() - Player.width / 2.0;
		double playerRight = this.player.getPosition() + Player.width / 2.0;
		if(projectileTop < playerBottom && projectileBottom > playerTop && projectileLeft < playerRight && projectileRight > playerLeft) {
			this.hasChanged = true;
			this.enemyProjectile = null;
			this.player.damage();
			if(this.player.getHealth() <= 0) {
				this.isGameOver = true;
				this.isGameRunning = false;
			}
		}
	}
private void collisionDetectionEnemyProjectile ( ) { if ( this . enemyProjectile == null ) { return ; } double projectileLeft = this . enemyProjectile . getPositionX ( ) - Projectile . width / 2.0 ; double projectileRight = this . enemyProjectile . getPositionX ( ) + Projectile . width / 2.0 ; double projectileTop = this . enemyProjectile . getPositionY ( ) - Projectile . height / 2.0 ; double projectileBottom = this . enemyProjectile . getPositionY ( ) + Projectile . height / 2.0 ; double obstacleTop = Obstacles . positionTop - Obstacles . height / 2.0 ; double obstacleBottom = Obstacles . positionTop + Obstacles . height / 2.0 ; if ( projectileTop < obstacleBottom && projectileBottom > obstacleTop ) { for ( int i = 0 ; i < this . obstacles . getCount ( ) ; i ++ ) { if ( this . obstacles . getState ( ) [ i ] == 0 ) { continue ; } double obstacleLeft = ( 2.0 * i + 1.0 ) * this . obstacles . getWidth ( ) ; double obstacleRight = ( 2.0 * i + 2.0 ) * this . obstacles . getWidth ( ) ; if ( projectileRight > obstacleLeft && projectileLeft < obstacleRight ) { this . hasChanged = true ; this . enemyProjectile = null ; this . obstacles . damage ( i ) ; return ; } } } double playerTop = Player . positionTop - Player . height / 2.0 ; double playerBottom = Player . positionTop + Player . height / 2.0 ; double playerLeft = this . player . getPosition ( ) - Player . width / 2.0 ; double playerRight = this . player . getPosition ( ) + Player . width / 2.0 ; if ( projectileTop < playerBottom && projectileBottom > playerTop && projectileLeft < playerRight && projectileRight > playerLeft ) { this . hasChanged = true ; this . enemyProjectile = null ; this . player . damage ( ) ; if ( this . player . getHealth ( ) <= 0 ) { this . isGameOver = true ; this . isGameRunning = false ; } } }
Perform enemy projectile collision detection.
Perform enemy projectile collision detection .
// First, collisions with obstacles (same as player projectile's but with damage) // Then, collisions with player