pl/mbogusz3/invaders/model/InvadersModel.java
/**
	 * Perform player projectile collision detection. Called from recalculate().
	 */
Perform player projectile collision detection. Called from recalculate().
Perform player projectile collision detection . Called from recalculate () .
private void collisionDetectionPlayerProjectile() {
		if(this.playerProjectile == null) {
			return;
		}
		double projectileLeft = this.playerProjectile.getPositionX() - Projectile.width / 2.0;
		double projectileRight = this.playerProjectile.getPositionX() + Projectile.width / 2.0;
		double projectileTop = this.playerProjectile.getPositionY() - Projectile.height / 2.0;
		double projectileBottom = this.playerProjectile.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.playerProjectile = null;
					return;
				}
			}
		}
		double enemiesBottom = this.enemy.getPositionTop() + (2 * this.enemy.getFirstRow() + 1) * Enemy.unitHeight;
		if(projectileTop > enemiesBottom) {
			return;
		}
		for(int i = this.enemy.getFirstRow(); i >= 0; i--) {
			double enemyBottom = enemiesBottom - 2.0 * (this.enemy.getFirstRow() - i) * Enemy.unitHeight;
			double enemyTop = enemyBottom - Enemy.unitHeight;
			if(projectileTop > enemyBottom || projectileBottom < enemyTop) {
				continue;
			}
			double middleColumn = (this.enemy.getLastColumn() - this.enemy.getFirstColumn()) / 2.0;
			for(int j = this.enemy.getFirstColumn(); j <= this.enemy.getLastColumn(); j++) {
				if(!this.enemy.getState()[i][j]) {
					continue;
				}
				double enemyLeft = this.enemy.getPositionLeft() - (middleColumn - j) * 2 * this.enemy.getUnitWidth() - this.enemy.getUnitWidth() / 2.0;
				double enemyRight = enemyLeft + this.enemy.getUnitWidth();
				if(projectileRight > enemyLeft && projectileLeft < enemyRight) {
					this.hasChanged = true;
					this.playerProjectile = null;
					this.enemy.destroyUnit(i, j);
					if(this.enemy.getFirstColumn() > this.enemy.getLastColumn()) {
						this.isGameWon = true;
						this.isGameRunning = false;
					}
					return;
				}
			}
		}
	}
private void collisionDetectionPlayerProjectile ( ) { if ( this . playerProjectile == null ) { return ; } double projectileLeft = this . playerProjectile . getPositionX ( ) - Projectile . width / 2.0 ; double projectileRight = this . playerProjectile . getPositionX ( ) + Projectile . width / 2.0 ; double projectileTop = this . playerProjectile . getPositionY ( ) - Projectile . height / 2.0 ; double projectileBottom = this . playerProjectile . 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 . playerProjectile = null ; return ; } } } double enemiesBottom = this . enemy . getPositionTop ( ) + ( 2 * this . enemy . getFirstRow ( ) + 1 ) * Enemy . unitHeight ; if ( projectileTop > enemiesBottom ) { return ; } for ( int i = this . enemy . getFirstRow ( ) ; i >= 0 ; i -- ) { double enemyBottom = enemiesBottom - 2.0 * ( this . enemy . getFirstRow ( ) - i ) * Enemy . unitHeight ; double enemyTop = enemyBottom - Enemy . unitHeight ; if ( projectileTop > enemyBottom || projectileBottom < enemyTop ) { continue ; } double middleColumn = ( this . enemy . getLastColumn ( ) - this . enemy . getFirstColumn ( ) ) / 2.0 ; for ( int j = this . enemy . getFirstColumn ( ) ; j <= this . enemy . getLastColumn ( ) ; j ++ ) { if ( ! this . enemy . getState ( ) [ i ] [ j ] ) { continue ; } double enemyLeft = this . enemy . getPositionLeft ( ) - ( middleColumn - j ) * 2 * this . enemy . getUnitWidth ( ) - this . enemy . getUnitWidth ( ) / 2.0 ; double enemyRight = enemyLeft + this . enemy . getUnitWidth ( ) ; if ( projectileRight > enemyLeft && projectileLeft < enemyRight ) { this . hasChanged = true ; this . playerProjectile = null ; this . enemy . destroyUnit ( i , j ) ; if ( this . enemy . getFirstColumn ( ) > this . enemy . getLastColumn ( ) ) { this . isGameWon = true ; this . isGameRunning = false ; } return ; } } } }
Perform player projectile collision detection.
Perform player projectile collision detection .
// First, collisions with obstacles (no damage) // Direction is -1 (top has lower value than bottom) thus the reversed comparison // Then, collisions with enemies // First, check whether we're at enemy's "orbit" (height) at all // Then for each row of enemies // Check whether projectile is at row's height // Skip if not // Then for each column // If enemy is already "dead", skip the calculations // Else, calculate enemy horizontal position // (Enemy unit middle is twice the unit width times count of units from middle position) // And compare it to projectile's position // If enemy was shot down do game win check