public void powerUpCollision(PowerUp object, Rectangle2D playerR) {
Ellipse2D powerUpR = object.getShape();
if (powerUpR.intersects(playerR) && !object.isHit()) {
if (this.player.getPowerUp() == null) {
if (object instanceof PUInvincible) {
this.player.setPowerUp((PUInvincible) object);
} else if (object instanceof PUSlowMotion) {
this.player.setPowerUp((PUSlowMotion) object);
} else if (object instanceof PUSpeedBoost) {
this.player.setPowerUp((PUSpeedBoost) object);
}
}
object.setHit(true);
new SecurePlatformRunner(() -> collidedObjects.add(object));
}
}
public void checkCollisions(Player player, List<GameObject> gameObjects) {
collidedObjects = new ArrayList<>();
Rectangle2D.Double playerR = player.getShape();
for (GameObject object : gameObjects) {
if (object instanceof Coin) {
coinCollision((Coin) object, playerR);
} else if (object instanceof Obstacle || object instanceof Rocket) {
obstacleCollision(object, playerR);
} else if (object instanceof PowerUp) {
powerUpCollision((PowerUp) object, playerR);
}
}
}
@ParameterizedTest
@CsvSource({"PUInvincible", "PUSpeedBoost", "PUSlowMotion"})
public void test_checkCollision_powerUp(String powerUpName) {
PowerUp powerUp;
Sprite sprite = Mockito.mock(Sprite.class);
Mockito.when(sprite.getHeight()).thenReturn(10.0);
Mockito.when(sprite.getWidth()).thenReturn(10.0);
Player player = Mockito.mock(Player.class);
Mockito.when(player.getShape()).thenReturn(new Rectangle2D.Double(5.0, 5.0, 5.0, 5.0));
gameController = Mockito.mock(GameController.class);
Launcher.setGameController(gameController);
switch(powerUpName) {
case "PUInvincible":
powerUp = new PUInvincible(5.0, 5.0, sprite);
break;
case "PUSpeedBoost":
powerUp = new PUSpeedBoost(5.0,5.0, sprite);
break;
case "PUSlowMotion":
powerUp = new PUSlowMotion(5.0,5.0, sprite);
break;
default:
powerUp = null;
}
List<GameObject> gameObjects = new ArrayList<>();
gameObjects.add(powerUp);
gameObjects.add(player);
gameScene.checkCollisions(player, gameObjects );
assertThat(player.getPowerUp()).isEqualTo(powerUp);
assertThat(powerUp.isHit()).isTrue();
}
我正在尝试测试checkCollision方法,然后又想测试powerUpCollision。我现在测试它的方式虽然不起作用。出于某种原因,我的测试没有进入if和else-if语句,我在那里检查应该设置哪种powerUp。我不明白为什么这是因为在进入陈述时所有条件都是正确的。