diff --git a/duelinvaders/pom.xml b/duelinvaders/pom.xml index ed04f7e..7ce5968 100644 --- a/duelinvaders/pom.xml +++ b/duelinvaders/pom.xml @@ -5,7 +5,7 @@ org.enstabretagne duelinvaders - 0.3.1 + 1.0.0 UTF-8 diff --git a/duelinvaders/src/main/java/org/enstabretagne/Core/AlienBulletCollision.java b/duelinvaders/src/main/java/org/enstabretagne/Collision/AlienBulletCollision.java similarity index 79% rename from duelinvaders/src/main/java/org/enstabretagne/Core/AlienBulletCollision.java rename to duelinvaders/src/main/java/org/enstabretagne/Collision/AlienBulletCollision.java index 67d16c9..b0b9ddb 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Core/AlienBulletCollision.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Collision/AlienBulletCollision.java @@ -1,4 +1,4 @@ -package org.enstabretagne.Core; +package org.enstabretagne.Collision; import static com.almasb.fxgl.dsl.FXGL.getGameWorld; import static com.almasb.fxgl.dsl.FXGL.play; @@ -6,8 +6,9 @@ import static com.almasb.fxgl.dsl.FXGL.spawn; import org.enstabretagne.Component.BulletComponent; -import org.enstabretagne.Component.EntityType; import org.enstabretagne.Component.PlayerComponent; +import org.enstabretagne.Utils.EntityType; +import org.enstabretagne.Utils.GameVariableNames; import org.enstabretagne.Utils.assetNames; import org.enstabretagne.Utils.entityNames; @@ -43,10 +44,8 @@ public AlienBulletCollision() { protected void onCollisionBegin(Entity bullet, Entity alien) { bullet.removeFromWorld(); alien.removeFromWorld(); - int playerId = bullet.getComponent(BulletComponent.class).getPlayerId(); - Entity player = getGameWorld().getEntitiesByType(EntityType.PLAYER).stream() - .filter(p -> p.getComponent(PlayerComponent.class).getId() == playerId).findFirst().get(); - player.getComponent(PlayerComponent.class).incrementScore(); + PlayerComponent playerComponent = bullet.getComponent(BulletComponent.class).getPlayerComponent(); + playerComponent.incrementScore(); spawn(entityNames.EXPLOSION_ALIEN, alien.getPosition()); play(assetNames.sounds.EXPLOSION_ALIEN); diff --git a/duelinvaders/src/main/java/org/enstabretagne/Core/AlienPlayerCollision.java b/duelinvaders/src/main/java/org/enstabretagne/Collision/AlienPlayerCollision.java similarity index 89% rename from duelinvaders/src/main/java/org/enstabretagne/Core/AlienPlayerCollision.java rename to duelinvaders/src/main/java/org/enstabretagne/Collision/AlienPlayerCollision.java index 178a50b..e9a826b 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Core/AlienPlayerCollision.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Collision/AlienPlayerCollision.java @@ -1,10 +1,11 @@ -package org.enstabretagne.Core; +package org.enstabretagne.Collision; import static com.almasb.fxgl.dsl.FXGL.play; import static com.almasb.fxgl.dsl.FXGL.set; import static com.almasb.fxgl.dsl.FXGL.spawn; -import org.enstabretagne.Component.EntityType; +import org.enstabretagne.Utils.EntityType; +import org.enstabretagne.Utils.GameVariableNames; import org.enstabretagne.Utils.assetNames; import org.enstabretagne.Utils.entityNames; diff --git a/duelinvaders/src/main/java/org/enstabretagne/Core/BulletBulletCollision.java b/duelinvaders/src/main/java/org/enstabretagne/Collision/BulletBulletCollision.java similarity index 92% rename from duelinvaders/src/main/java/org/enstabretagne/Core/BulletBulletCollision.java rename to duelinvaders/src/main/java/org/enstabretagne/Collision/BulletBulletCollision.java index 90ad40a..c565c20 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Core/BulletBulletCollision.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Collision/BulletBulletCollision.java @@ -1,9 +1,9 @@ -package org.enstabretagne.Core; +package org.enstabretagne.Collision; import static com.almasb.fxgl.dsl.FXGL.play; import static com.almasb.fxgl.dsl.FXGL.spawn; -import org.enstabretagne.Component.EntityType; +import org.enstabretagne.Utils.EntityType; import org.enstabretagne.Utils.assetNames; import org.enstabretagne.Utils.entityNames; diff --git a/duelinvaders/src/main/java/org/enstabretagne/Core/BulletPlayerCollision.java b/duelinvaders/src/main/java/org/enstabretagne/Collision/BulletPlayerCollision.java similarity index 76% rename from duelinvaders/src/main/java/org/enstabretagne/Core/BulletPlayerCollision.java rename to duelinvaders/src/main/java/org/enstabretagne/Collision/BulletPlayerCollision.java index 1ea4c44..78a1943 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Core/BulletPlayerCollision.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Collision/BulletPlayerCollision.java @@ -1,13 +1,14 @@ -package org.enstabretagne.Core; +package org.enstabretagne.Collision; import static com.almasb.fxgl.dsl.FXGL.play; import static com.almasb.fxgl.dsl.FXGL.runOnce; import static com.almasb.fxgl.dsl.FXGL.set; import static com.almasb.fxgl.dsl.FXGL.spawn; -import org.enstabretagne.Component.EntityType; import org.enstabretagne.Component.PlayerComponent; +import org.enstabretagne.Utils.EntityType; +import org.enstabretagne.Utils.GameVariableNames; import org.enstabretagne.Utils.assetNames; import org.enstabretagne.Utils.entityNames; @@ -42,14 +43,14 @@ protected void onCollisionBegin(Entity bullet, Entity player) { if (bullet.hasComponent(PlayerComponent.class)) { bullet.getComponent(PlayerComponent.class).incrementScore(); } - if (playerComponent.getLife() == 0) { - spawn(entityNames.EXPLOSION_PLAYER_DEATH, player.getPosition()); - player.removeFromWorld(); - play(assetNames.sounds.EXPLOSION_PLAYER_DEATH); - runOnce(() -> set(GameVariableNames.isGameOver, true), Duration.seconds(2)); - } else { + if (playerComponent.getLife() != 0) { spawn(entityNames.EXPLOSION_PLAYER_BULLET, bullet.getPosition()); play(assetNames.sounds.EXPLOSION_PLAYER_LIFE); + return; } + spawn(entityNames.EXPLOSION_PLAYER_DEATH, player.getPosition()); + player.removeFromWorld(); + play(assetNames.sounds.EXPLOSION_PLAYER_DEATH); + runOnce(() -> set(GameVariableNames.isGameOver, true), Duration.seconds(2)); } } diff --git a/duelinvaders/src/main/java/org/enstabretagne/Core/EnemyShootBulletCollision.java b/duelinvaders/src/main/java/org/enstabretagne/Collision/EnemyShootBulletCollision.java similarity index 93% rename from duelinvaders/src/main/java/org/enstabretagne/Core/EnemyShootBulletCollision.java rename to duelinvaders/src/main/java/org/enstabretagne/Collision/EnemyShootBulletCollision.java index b0c9534..10b0d2f 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Core/EnemyShootBulletCollision.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Collision/EnemyShootBulletCollision.java @@ -1,9 +1,9 @@ -package org.enstabretagne.Core; +package org.enstabretagne.Collision; import static com.almasb.fxgl.dsl.FXGL.play; import static com.almasb.fxgl.dsl.FXGL.spawn; -import org.enstabretagne.Component.EntityType; +import org.enstabretagne.Utils.EntityType; import org.enstabretagne.Utils.assetNames; import org.enstabretagne.Utils.entityNames; diff --git a/duelinvaders/src/main/java/org/enstabretagne/Core/EnemyShootPlayerCollision.java b/duelinvaders/src/main/java/org/enstabretagne/Collision/EnemyShootPlayerCollision.java similarity index 76% rename from duelinvaders/src/main/java/org/enstabretagne/Core/EnemyShootPlayerCollision.java rename to duelinvaders/src/main/java/org/enstabretagne/Collision/EnemyShootPlayerCollision.java index 431770b..ed95438 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Core/EnemyShootPlayerCollision.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Collision/EnemyShootPlayerCollision.java @@ -1,12 +1,13 @@ -package org.enstabretagne.Core; +package org.enstabretagne.Collision; import static com.almasb.fxgl.dsl.FXGL.play; import static com.almasb.fxgl.dsl.FXGL.set; import static com.almasb.fxgl.dsl.FXGL.spawn; -import org.enstabretagne.Component.EntityType; import org.enstabretagne.Component.PlayerComponent; +import org.enstabretagne.Utils.EntityType; +import org.enstabretagne.Utils.GameVariableNames; import org.enstabretagne.Utils.assetNames; import org.enstabretagne.Utils.entityNames; @@ -36,14 +37,14 @@ protected void onCollisionBegin(Entity enemy_shoot, Entity player) { enemy_shoot.removeFromWorld(); PlayerComponent playerComponent = player.getComponent(PlayerComponent.class); playerComponent.decrementLife(); - if (playerComponent.getLife() == 0) { - spawn(entityNames.EXPLOSION_PLAYER_DEATH, player.getPosition()); - play(assetNames.sounds.EXPLOSION_PLAYER_DEATH); - set(GameVariableNames.isGameOver, true); - player.removeFromWorld(); - } else { + if (playerComponent.getLife() != 0) { spawn(entityNames.EXPLOSION_PLAYER_BULLET, enemy_shoot.getPosition()); play(assetNames.sounds.EXPLOSION_PLAYER_LIFE); + return; } + spawn(entityNames.EXPLOSION_PLAYER_DEATH, player.getPosition()); + play(assetNames.sounds.EXPLOSION_PLAYER_DEATH); + set(GameVariableNames.isGameOver, true); + player.removeFromWorld(); } } diff --git a/duelinvaders/src/main/java/org/enstabretagne/Component/AlienComponent.java b/duelinvaders/src/main/java/org/enstabretagne/Component/AlienComponent.java index 36e740c..f3d05a4 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Component/AlienComponent.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Component/AlienComponent.java @@ -1,8 +1,8 @@ package org.enstabretagne.Component; -import org.enstabretagne.Core.Constant; -import org.enstabretagne.Core.Constant.Direction; +import org.enstabretagne.Utils.Settings; import org.enstabretagne.Utils.entityNames; +import org.enstabretagne.Utils.Settings.Direction; import com.almasb.fxgl.core.math.FXGLMath; import com.almasb.fxgl.entity.Entity; @@ -22,7 +22,7 @@ public class AlienComponent extends Component { private Direction movementDirection; private Direction globalDirection; private Double last_shot = 0.0; - private double limit_right = Constant.GAME_WIDTH; + private double limit_right = Settings.GAME_WIDTH; private double limit_left = 0.0; /** @@ -54,12 +54,12 @@ public AlienComponent() { * Initialise l'alien * */ - public void initialize(Constant.Direction direction) { + public void initialize(Settings.Direction direction) { this.globalDirection = direction; - if (direction == Constant.Direction.UP) { + if (direction == Settings.Direction.UP) { entity.rotateBy(180); this.movementDirection = Direction.RIGHT; - } else if (direction == Constant.Direction.DOWN) { + } else if (direction == Settings.Direction.DOWN) { this.movementDirection = Direction.LEFT; } @@ -72,11 +72,15 @@ public void initialize(Constant.Direction direction) { */ @Override public void onUpdate(double tpf) { - dx = tpf * Constant.SPEED_ALIEN; + dx = tpf * Settings.SPEED_ALIEN; dy = entity.getHeight(); this.move(dx); } + public Direction getDirection() { + return globalDirection; + } + /** * Déplacement de l'alien selon la direction et les limites du jeu. * L'alien se déplace de dx pixels. @@ -98,14 +102,15 @@ else if (this.movementDirection == Direction.LEFT) public void moveRight(Double dx) { if (this.entity.getRightX() + dx <= limit_right) { this.entity.translateX(dx); - } else { - if (this.globalDirection == Constant.Direction.DOWN) { - this.entity.translateY(dy); - } else if (this.globalDirection == Constant.Direction.UP) { - this.entity.translateY(-dy); - } - this.movementDirection = Direction.LEFT; + return; + } + + if (this.globalDirection == Settings.Direction.DOWN) { + this.entity.translateY(dy); + } else if (this.globalDirection == Settings.Direction.UP) { + this.entity.translateY(-dy); } + this.movementDirection = Direction.LEFT; } /** @@ -116,19 +121,20 @@ public void moveRight(Double dx) { public void moveLeft(Double dx) { if (this.entity.getX() - dx >= limit_left) { this.entity.translateX(-dx); - } else { - if (this.globalDirection == Constant.Direction.DOWN) { - this.entity.translateY(dy); - } else if (this.globalDirection == Constant.Direction.UP) { - this.entity.translateY(-dy); - } - this.movementDirection = Direction.RIGHT; + return; + } + + if (this.globalDirection == Settings.Direction.DOWN) { + this.entity.translateY(dy); + } else if (this.globalDirection == Settings.Direction.UP) { + this.entity.translateY(-dy); } + this.movementDirection = Direction.RIGHT; } public void setAlienNumber(int AlienNumber) { - limit_right = Constant.GAME_WIDTH - (Constant.ALIEN_WIDTH * (Constant.ALIENS_NUMBER - AlienNumber - 1)); - limit_left = 0.0 + (Constant.ALIEN_WIDTH * AlienNumber); + limit_right = Settings.GAME_WIDTH - (Settings.ALIEN_WIDTH * (Settings.ALIENS_NUMBER - AlienNumber - 1)); + limit_left = 0.0 + (Settings.ALIEN_WIDTH * AlienNumber); } /** @@ -146,13 +152,13 @@ public void randomShoot(double chance) { * L'alien tire un projectile à une cadence définie dans {@code}Constant{@code}. */ public void shoot() { - Boolean canShoot = getGameTimer().getNow() - last_shot.doubleValue() >= Constant.RATE_ALIEN_SHOOT.doubleValue(); + Boolean canShoot = getGameTimer().getNow() - last_shot.doubleValue() >= Settings.RATE_ALIEN_SHOOT.doubleValue(); if (!canShoot) return; double x = this.entity.getX() + this.entity.getWidth() / 2; double y = this.entity.getY(); - if (this.globalDirection == Constant.Direction.DOWN) + if (this.globalDirection == Settings.Direction.DOWN) y += this.entity.getHeight(); spawn(entityNames.ECLAT, x, y); diff --git a/duelinvaders/src/main/java/org/enstabretagne/Component/BulletComponent.java b/duelinvaders/src/main/java/org/enstabretagne/Component/BulletComponent.java index 8661d25..0b10b83 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Component/BulletComponent.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Component/BulletComponent.java @@ -1,6 +1,6 @@ package org.enstabretagne.Component; -import org.enstabretagne.Core.Constant; +import org.enstabretagne.Utils.Settings; import com.almasb.fxgl.dsl.components.ExpireCleanComponent; import com.almasb.fxgl.dsl.components.ProjectileComponent; @@ -17,17 +17,17 @@ * @since 0.1.0 */ public class BulletComponent extends Component { - private Double speed = Constant.SPEED_SHOOT; - private Duration duration = Constant.BULLET_DURATION; + private Double speed = Settings.SPEED_SHOOT; + private Duration duration = Settings.BULLET_DURATION; private Point2D direction = new Point2D(0, -1); - private int playerId; + private PlayerComponent playerComponent; - public int getPlayerId() { - return playerId; + public PlayerComponent getPlayerComponent() { + return playerComponent; } - public void setPlayerId(int player_id) { - this.playerId = player_id; + public void setPlayer(PlayerComponent playerComponent) { + this.playerComponent = playerComponent; } public BulletComponent() { @@ -41,8 +41,8 @@ public BulletComponent() { * Ce sont des composants prédéfinis dans la librairie FXGL qui facilite la * création de projectiles */ - public void initialize(Constant.Direction UporDown) { - this.direction = new Point2D(0, UporDown == Constant.Direction.UP ? -1 : 1); + public void initialize(Settings.Direction UporDown) { + this.direction = new Point2D(0, UporDown == Settings.Direction.UP ? -1 : 1); this.entity.addComponent(new ProjectileComponent(direction, speed)); this.entity.addComponent(new ExpireCleanComponent(duration)); } diff --git a/duelinvaders/src/main/java/org/enstabretagne/Component/LifeComponent.java b/duelinvaders/src/main/java/org/enstabretagne/Component/LifeComponent.java deleted file mode 100644 index 56e7aea..0000000 --- a/duelinvaders/src/main/java/org/enstabretagne/Component/LifeComponent.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.enstabretagne.Component; - -import org.enstabretagne.Core.Constant; - -import com.almasb.fxgl.entity.component.Component; - -/** - * Cette classe représente l'affichage du nombre de vies restantes. - * - * @author MathieuDFS, jufch - * @since 0.2.0 - * @deprecated since 0.3.0 - */ -public class LifeComponent extends Component { - /** - * Initialise le composant affichage de vie - */ - public void initialize(Constant.Direction direction) { - if (direction == Constant.Direction.UP) { - entity.setX(Constant.GAME_WIDTH - Constant.LIFE_DISPLAY_WIDTH); - entity.setY(0); - } else if (direction == Constant.Direction.DOWN) { - entity.setX(Constant.GAME_WIDTH - Constant.LIFE_DISPLAY_WIDTH); - entity.setY(Constant.GAME_HEIGHT - Constant.LIFE_DISPLAY_HEIGHT); - } - } - - public void updateLife(boolean isActive) { - entity.getViewComponent().setOpacity(isActive ? 1 : 0); - } -} diff --git a/duelinvaders/src/main/java/org/enstabretagne/Component/PlayerComponent.java b/duelinvaders/src/main/java/org/enstabretagne/Component/PlayerComponent.java index 21456f6..88c7bfb 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Component/PlayerComponent.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Component/PlayerComponent.java @@ -1,18 +1,15 @@ package org.enstabretagne.Component; import static com.almasb.fxgl.dsl.FXGL.getGameTimer; -import static com.almasb.fxgl.dsl.FXGL.geti; import static com.almasb.fxgl.dsl.FXGL.runOnce; -import static com.almasb.fxgl.dsl.FXGL.set; import static com.almasb.fxgl.dsl.FXGL.spawn; -import static org.enstabretagne.Core.Constant.DELAY_BETWEEN_SHOOT; -import static org.enstabretagne.Core.Constant.GAME_WIDTH; -import static org.enstabretagne.Core.Constant.PLAYER_HEIGHT; -import static org.enstabretagne.Core.Constant.SPEED_SPACESHIP; +import static org.enstabretagne.Utils.Settings.DELAY_BETWEEN_SHOOT; +import static org.enstabretagne.Utils.Settings.GAME_WIDTH; +import static org.enstabretagne.Utils.Settings.PLAYER_HEIGHT; +import static org.enstabretagne.Utils.Settings.SPEED_SPACESHIP; -import org.enstabretagne.Core.Constant; -import org.enstabretagne.Core.Constant.Direction; -import org.enstabretagne.Core.GameVariableNames; +import org.enstabretagne.Utils.Settings; +import org.enstabretagne.Utils.Settings.Direction; import org.enstabretagne.Utils.entityNames; import com.almasb.fxgl.entity.Entity; @@ -31,9 +28,11 @@ public class PlayerComponent extends Component { private Double dx; private Double last_shot = 0.0; private Direction side_shoot = Direction.LEFT; - private Constant.Direction direction = Constant.Direction.UP; + private Settings.Direction direction = Settings.Direction.UP; private int id; - private static int counter; + private static int counter = 1; + private int score = 0; + private int life = 5; public int getId() { return id; @@ -51,8 +50,8 @@ public PlayerComponent() { * * @param direction */ - public void setDirection(Constant.Direction direction) { - if (direction == Constant.Direction.LEFT || direction == Constant.Direction.RIGHT) + public void setDirection(Settings.Direction direction) { + if (direction == Settings.Direction.LEFT || direction == Settings.Direction.RIGHT) return; if (this.direction == direction) return; @@ -109,7 +108,7 @@ private Point2D calculateShootPosition() { int decalage = 20; int shift_x; int shift_y; - if (this.direction == Constant.Direction.UP) { + if (this.direction == Settings.Direction.UP) { shift_y = -decalage; } else { shift_y = PLAYER_HEIGHT.intValue() + decalage; @@ -131,7 +130,7 @@ private Point2D calculateShootPosition() { private void createBullet(Point2D position) { Entity bullet = spawn(entityNames.BULLET, position); bullet.getComponent(BulletComponent.class).initialize(this.direction); - bullet.getComponent(BulletComponent.class).setPlayerId(this.getId()); + bullet.getComponent(BulletComponent.class).setPlayer(this); last_shot = getGameTimer().getNow(); shootingRecoil(); } @@ -140,7 +139,7 @@ private void createBullet(Point2D position) { * Simule le recul du vaisseau lors du tir */ private void shootingRecoil() { - if (this.direction == Constant.Direction.DOWN) { + if (this.direction == Settings.Direction.DOWN) { this.entity.translateY(-10); runOnce(() -> this.entity.translateY(10), Duration.seconds(0.1)); } else if (this.direction == Direction.UP) { @@ -162,104 +161,56 @@ private void createSmoke(Point2D position) { * Incrémente le score du joueur */ public void incrementScore() { - if (isPlayer1()) - set(GameVariableNames.PLAYER1_SCORE, geti(GameVariableNames.PLAYER1_SCORE) + 1); - else - set(GameVariableNames.PLAYER2_SCORE, geti(GameVariableNames.PLAYER2_SCORE) + 1); - } - - /** - * @return boolean - */ - private boolean isPlayer1() { - return id % 2 == 1; - } - - /** - * Initialisation du score du joueur - */ - public void initializeScore() { - if (isPlayer1()) - set(GameVariableNames.PLAYER1_SCORE, 0); - else - set(GameVariableNames.PLAYER2_SCORE, 0); + setScore(getScore() + 1); + System.out.println("Score Player " + getId() + ": " + getScore()); } /** * @return int */ public int getScore() { - if (isPlayer1()) - return geti(GameVariableNames.PLAYER1_SCORE); - else - return geti(GameVariableNames.PLAYER2_SCORE); + return score; } /** - * @param score + * + * @param newScore */ - public void setScore(int score) { - if (isPlayer1()) - set(GameVariableNames.PLAYER1_SCORE, score); - else - set(GameVariableNames.PLAYER2_SCORE, score); + public void setScore(int newScore) { + score = Math.max(0, newScore); } public void decrementScore() { - if (isPlayer1()) - set(GameVariableNames.PLAYER1_SCORE, geti(GameVariableNames.PLAYER1_SCORE) - 1); - else - set(GameVariableNames.PLAYER2_SCORE, geti(GameVariableNames.PLAYER2_SCORE) - 1); + setScore(getScore() - 1); } /** * Incrémente le nombre de vie du joueur */ public void incrementLife() { - if (isPlayer1()) - set(GameVariableNames.PLAYER1_LIFE, geti(GameVariableNames.PLAYER1_LIFE) + 1); - else - set(GameVariableNames.PLAYER2_LIFE, geti(GameVariableNames.PLAYER2_LIFE) + 1); - } - - /** - * Initialise le nombre de vie du joueur - */ - public void initializeLife() { - int life = 5; - if (isPlayer1()) - set(GameVariableNames.PLAYER1_LIFE, life); - else - set(GameVariableNames.PLAYER2_LIFE, life); + setLife(getLife() + 1); } /** * @return int */ public int getLife() { - if (isPlayer1()) - return geti(GameVariableNames.PLAYER1_LIFE); - else - return geti(GameVariableNames.PLAYER2_LIFE); + return life; } /** - * @param life + * + * @param newLife */ - public void setLife(int life) { - if (isPlayer1()) - set(GameVariableNames.PLAYER1_LIFE, life); - else - set(GameVariableNames.PLAYER2_LIFE, life); + public void setLife(int newLife) { + life = Math.max(0, newLife); } /** * Décrémente le nombre de vie du joueur */ public void decrementLife() { - if (isPlayer1()) - set(GameVariableNames.PLAYER1_LIFE, geti(GameVariableNames.PLAYER1_LIFE) - 1); - else - set(GameVariableNames.PLAYER2_LIFE, geti(GameVariableNames.PLAYER2_LIFE) - 1); + setLife(getLife() - 1); + System.out.println("Player " + getId() + " lost a life" + " (" + getLife() + " left)"); } } diff --git a/duelinvaders/src/main/java/org/enstabretagne/Component/ShootingSmokeComponent.java b/duelinvaders/src/main/java/org/enstabretagne/Component/ShootingSmokeComponent.java index 38db5bc..d0215d0 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Component/ShootingSmokeComponent.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Component/ShootingSmokeComponent.java @@ -1,8 +1,8 @@ package org.enstabretagne.Component; -import static org.enstabretagne.Core.Constant.SMOKE_DURATION; +import static org.enstabretagne.Utils.Settings.SMOKE_DURATION; -import org.enstabretagne.Core.Constant; +import org.enstabretagne.Utils.Settings; import com.almasb.fxgl.dsl.components.ExpireCleanComponent; import com.almasb.fxgl.entity.component.Component; @@ -20,11 +20,11 @@ public class ShootingSmokeComponent extends Component { * * @param direction */ - public void initialize(Constant.Direction direction) { + public void initialize(Settings.Direction direction) { this.entity.addComponent(new ExpireCleanComponent(SMOKE_DURATION)); - double dx = Constant.SHOOTING_SMOKE_WIDTH / 2; + double dx = Settings.SHOOTING_SMOKE_WIDTH / 2; double dy = 30; - if (direction == Constant.Direction.DOWN) { + if (direction == Settings.Direction.DOWN) { this.entity.rotateBy(180); this.entity.translate(dx, dy); } diff --git a/duelinvaders/src/main/java/org/enstabretagne/Component/ShootingStartComponent.java b/duelinvaders/src/main/java/org/enstabretagne/Component/ShootingStartComponent.java index 6665f1f..76abf5b 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Component/ShootingStartComponent.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Component/ShootingStartComponent.java @@ -1,9 +1,9 @@ package org.enstabretagne.Component; -import static org.enstabretagne.Core.Constant.FIRE_DURATION; -import static org.enstabretagne.Core.Constant.SHOOTING_START_WIDTH; +import static org.enstabretagne.Utils.Settings.FIRE_DURATION; +import static org.enstabretagne.Utils.Settings.SHOOTING_START_WIDTH; -import org.enstabretagne.Core.Constant.Direction; +import org.enstabretagne.Utils.Settings.Direction; import com.almasb.fxgl.dsl.components.ExpireCleanComponent; import com.almasb.fxgl.entity.component.Component; diff --git a/duelinvaders/src/main/java/org/enstabretagne/Component/SpaceInvadersFactory.java b/duelinvaders/src/main/java/org/enstabretagne/Component/SpaceInvadersFactory.java index b018c3f..ac5d0ad 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Component/SpaceInvadersFactory.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Component/SpaceInvadersFactory.java @@ -5,7 +5,8 @@ import static com.almasb.fxgl.dsl.FXGL.spawn; import static com.almasb.fxgl.dsl.FXGL.texture; -import org.enstabretagne.Core.Constant; +import org.enstabretagne.Utils.EntityType; +import org.enstabretagne.Utils.Settings; import org.enstabretagne.Utils.assetNames; import org.enstabretagne.Utils.entityNames; @@ -29,254 +30,237 @@ * @since 0.1.0 */ public class SpaceInvadersFactory implements EntityFactory { - /** - * Définition de l'entité joueur, nommé player - * - * @param data - * @return Entity - */ - @Spawns(entityNames.PLAYER) - public Entity newPlayer(SpawnData data) { - Texture texture = texture(assetNames.textures.SPACESHIP, Constant.PLAYER_WIDTH, Constant.PLAYER_HEIGHT); - return entityBuilder() - .type(EntityType.PLAYER) - .at(data.getX(), data.getY()) - .viewWithBBox(texture) - .with(new PlayerComponent()) - .collidable() - .build(); - } + /** + * Définition de l'entité joueur, nommé player + * + * @param data + * @return Entity + */ + @Spawns(entityNames.PLAYER) + public Entity newPlayer(SpawnData data) { + Texture texture = texture(assetNames.textures.SPACESHIP, Settings.PLAYER_WIDTH, Settings.PLAYER_HEIGHT); - /** - * Définition de l'entité alien, nommé alien - * - * @param data - * @return Entity - */ - @Spawns(entityNames.ALIEN) - public Entity newAlien(SpawnData data) { - int randomIndex = Constant.random.nextInt(Constant.AlienColor.values().length); - Constant.AlienColor randomColor = Constant.AlienColor.values()[randomIndex]; - Color color = Color.valueOf(randomColor.name()); - - Texture texture = texture(assetNames.textures.ALIEN, Constant.ALIEN_WIDTH, Constant.ALIEN_HEIGHT) - .multiplyColor(color); - return entityBuilder() - .type(EntityType.ALIEN) - .at(data.getX(), data.getY()) - .viewWithBBox(texture) - .with(new AlienComponent()) - .collidable() - .build(); - } + return entityBuilder() + .type(EntityType.PLAYER) + .at(data.getX(), data.getY()) + .viewWithBBox(texture) + .with(new PlayerComponent()) + .collidable() + .build(); + } - /** - * Définition de l'entité tir du joueur, nommé bullet - * Elle est uniquement utilisée par le joueur - * - * @param data - * @return Entity - */ - @Spawns(entityNames.BULLET) - public Entity newBullet(SpawnData data) { - int bulletWidth = 20; - int bulletHeight = 20; - Texture texture = texture(assetNames.textures.ROCKET, bulletWidth, bulletHeight); - texture.setRotate(90); - play(assetNames.sounds.CANNON_SHOT); + /** + * Définition de l'entité alien, nommé alien + * + * @param data + * @return Entity + */ + @Spawns(entityNames.ALIEN) + public Entity newAlien(SpawnData data) { + int randomIndex = Settings.random.nextInt(Settings.AlienColor.values().length); + Settings.AlienColor randomColor = Settings.AlienColor.values()[randomIndex]; + Color color = Color.valueOf(randomColor.name()); - return entityBuilder() - .type(EntityType.BULLET) - .at(data.getX() - bulletWidth / 2, data.getY()) - .viewWithBBox(texture) - .with(new BulletComponent()) - .with(new OffscreenCleanComponent()) - .collidable() - .build(); - } + Texture texture = texture(assetNames.textures.ALIEN, Settings.ALIEN_WIDTH, Settings.ALIEN_HEIGHT) + .multiplyColor(color); + return entityBuilder() + .type(EntityType.ALIEN) + .at(data.getX(), data.getY()) + .viewWithBBox(texture) + .with(new AlienComponent()) + .collidable() + .build(); + } - /** - * Définition de l'entité tir de l'alien, nommé alienBullet - * Elle est uniquement utilisée par les aliens - * - * @param data - * @return Entity - */ - @Spawns(entityNames.BULLET_ALIEN) - public Entity newAlienBullet(SpawnData data) { - int laserWidth = 20; - int laserHeight = 20; - Texture texture = texture(assetNames.textures.LASER, laserWidth, laserHeight); - String randomLaserSound = assetNames.sounds.LASER_SOUNDS - .get(FXGLMath.random(0, Constant.NUMBER_OF_LASER_SOUNDS - 1)); - play(randomLaserSound); + /** + * Définition de l'entité tir du joueur, nommé bullet + * Elle est uniquement utilisée par le joueur + * + * @param data + * @return Entity + */ + @Spawns(entityNames.BULLET) + public Entity newBullet(SpawnData data) { + int bulletWidth = 20; + int bulletHeight = 20; + Texture texture = texture(assetNames.textures.ROCKET, bulletWidth, bulletHeight); + texture.setRotate(90); + play(assetNames.sounds.CANNON_SHOT); - return entityBuilder() - .type(EntityType.ENEMY_SHOOT) - .at(data.getX() - laserWidth / 2, data.getY()) - .viewWithBBox(texture) - .with(new BulletComponent()) - .collidable() - .build(); - } + return entityBuilder() + .type(EntityType.BULLET) + .at(data.getX() - bulletWidth / 2, data.getY()) + .viewWithBBox(texture) + .with(new BulletComponent()) + .with(new OffscreenCleanComponent()) + .collidable() + .build(); + } - /** - * Définition de l'entité eclat, nommé eclat - * Décoration pour agrémenter le jeu lors d'un tir alien - * - * @param data - * @return Entity - */ - @Spawns(entityNames.ECLAT) - public Entity newEclat(SpawnData data) { - int eclatWidth = 150; - int eclatHeight = 150; - Texture texture = texture(assetNames.textures.ECLAT2, eclatWidth, eclatHeight); - texture.setRotate(180); - return entityBuilder() - .at(data.getX() - eclatWidth / 2, data.getY() - eclatHeight / 2) - .viewWithBBox(texture) - .with(new ExpireCleanComponent(Duration.seconds(0.05))) - .build(); - } + /** + * Définition de l'entité tir de l'alien, nommé alienBullet + * Elle est uniquement utilisée par les aliens + * + * @param data + * @return Entity + */ + @Spawns(entityNames.BULLET_ALIEN) + public Entity newAlienBullet(SpawnData data) { + int laserWidth = 20; + int laserHeight = 20; + Texture texture = texture(assetNames.textures.LASER, laserWidth, laserHeight); + String randomLaserSound = assetNames.sounds.LASER_SOUNDS + .get(FXGLMath.random(0, Settings.NUMBER_OF_LASER_SOUNDS - 1)); + play(randomLaserSound); - /** - * Définition de l'entité pour l'arrière-plan, nommé background - * Elle est utilisée pour l'arrière-plan du jeu - * - * @param data - * @return Entity - */ - @Spawns(entityNames.BACKGROUND) - public Entity newBackground(SpawnData data) { - return entityBuilder() - .at(-10, -10) - .view(texture(assetNames.textures.GAME_BACKGROUND, Constant.GAME_WIDTH + 20, Constant.GAME_HEIGHT + 20)) - .zIndex(-500) - .build(); - } + return entityBuilder() + .type(EntityType.ENEMY_SHOOT) + .at(data.getX() - laserWidth / 2, data.getY()) + .viewWithBBox(texture) + .with(new BulletComponent()) + .collidable() + .build(); + } - /** - * Définition de l'entité pour le tir du joueur, nommé shooting_start - * A but décoratif pour agrémenter le jeu - * - * @param data - * @return Entity - */ - @Spawns(entityNames.SHOOTING_START) - public Entity shooting_start(SpawnData data) { - Texture texture = texture(assetNames.textures.FIRE, Constant.SHOOTING_START_WIDTH, - Constant.SHOOTING_START_HEIGHT); - texture.setRotate(180); + /** + * Définition de l'entité eclat, nommé eclat + * Décoration pour agrémenter le jeu lors d'un tir alien + * + * @param data + * @return Entity + */ + @Spawns(entityNames.ECLAT) + public Entity newEclat(SpawnData data) { + int eclatWidth = 150; + int eclatHeight = 150; + Texture texture = texture(assetNames.textures.ECLAT2, eclatWidth, eclatHeight); + texture.setRotate(180); + return entityBuilder() + .at(data.getX() - eclatWidth / 2, data.getY() - eclatHeight / 2) + .viewWithBBox(texture) + .with(new ExpireCleanComponent(Duration.seconds(0.05))) + .build(); + } - return entityBuilder() - .at(data.getX(), data.getY()) - .view(texture) - .with(new ShootingStartComponent()) - .build(); - } + /** + * Définition de l'entité pour l'arrière-plan, nommé background + * Elle est utilisée pour l'arrière-plan du jeu + * + * @param data + * @return Entity + */ + @Spawns(entityNames.BACKGROUND) + public Entity newBackground(SpawnData data) { + return entityBuilder() + .at(-10, -10) + .view(texture(assetNames.textures.GAME_BACKGROUND, Settings.GAME_WIDTH + 20, + Settings.GAME_HEIGHT + 20)) + .zIndex(-500) + .build(); + } - /** - * Définition de l'entité pour le tir du joueur, nommé shooting_smoke - * A but décoratif pour agrémenter le jeu - * - * @param data - * @return Entity - */ - @Spawns(entityNames.SHOOTING_SMOKE) - public Entity shooting_smoke(SpawnData data) { - Texture texture = texture(assetNames.textures.SMOKE, Constant.SHOOTING_SMOKE_WIDTH, - Constant.SHOOTING_SMOKE_HEIGHT); - return entityBuilder() - .at(data.getX(), data.getY()) - .view(texture) - .with(new ShootingSmokeComponent()) - .build(); - } + /** + * Définition de l'entité pour le tir du joueur, nommé shooting_start + * A but décoratif pour agrémenter le jeu + * + * @param data + * @return Entity + */ + @Spawns(entityNames.SHOOTING_START) + public Entity shooting_start(SpawnData data) { + Texture texture = texture(assetNames.textures.FIRE, Settings.SHOOTING_START_WIDTH, + Settings.SHOOTING_START_HEIGHT); + texture.setRotate(180); - /** - * Définition de l'entité pour l'explosion, nommé explosion_alien - * A but décoratif pour agrémenter le jeu - * - * @param data - * @return Entity - */ - @Spawns(entityNames.EXPLOSION_ALIEN) - public Entity explosion_alien(SpawnData data) { - int explosion_width = 60; - int explosion_height = 60; - String randomTexture = assetNames.textures.EXPLOSIONS - .get(FXGLMath.random(0, Constant.NUMBER_OF_EXPLOSIONS - 1)); - Texture texture = texture(randomTexture, explosion_width, explosion_height); - return entityBuilder() - .at(data.getX(), data.getY()) - .view(texture) - .with(new ExpireCleanComponent(Duration.seconds(0.3))) - .build(); - } + return entityBuilder() + .at(data.getX(), data.getY()) + .view(texture) + .with(new ShootingStartComponent()) + .build(); + } - /** - * Définition de l'entité pour l'explosion, nommé explosion_player_bullet - * A but décoratif pour agrémenter le jeu - * - * @param data - * @return Entity - */ - @Spawns(entityNames.EXPLOSION_PLAYER_BULLET) - public Entity explosion_player_bullet(SpawnData data) { - int explosion_width = 70; - int explosion_height = 60; - Texture texture = texture(assetNames.textures.EXPLOSION_PLAYER, explosion_width, explosion_height); - return entityBuilder() - .at(data.getX() - explosion_width / 2, data.getY() - explosion_height / 2) - .view(texture) - .with(new ExpireCleanComponent(Duration.seconds(0.3))) - .build(); - } + /** + * Définition de l'entité pour le tir du joueur, nommé shooting_smoke + * A but décoratif pour agrémenter le jeu + * + * @param data + * @return Entity + */ + @Spawns(entityNames.SHOOTING_SMOKE) + public Entity shooting_smoke(SpawnData data) { + Texture texture = texture(assetNames.textures.SMOKE, Settings.SHOOTING_SMOKE_WIDTH, + Settings.SHOOTING_SMOKE_HEIGHT); + return entityBuilder() + .at(data.getX(), data.getY()) + .view(texture) + .with(new ShootingSmokeComponent()) + .build(); + } - /** - * Définition de l'entité pour l'explosion, nommé explosion_player_death - * A but décoratif pour agrémenter le jeu - * - * @param data - * @return Entity - */ - @Spawns(entityNames.EXPLOSION_PLAYER_DEATH) - public Entity explosion_player_death(SpawnData data) { - int explosion_width = 200; - int explosion_height = 200; - Texture texture = texture(assetNames.textures.EXPLOSION_FINAL, explosion_width, explosion_height); + /** + * Définition de l'entité pour l'explosion, nommé explosion_alien + * A but décoratif pour agrémenter le jeu + * + * @param data + * @return Entity + */ + @Spawns(entityNames.EXPLOSION_ALIEN) + public Entity explosion_alien(SpawnData data) { + int explosion_width = 60; + int explosion_height = 60; + String randomTexture = assetNames.textures.EXPLOSIONS + .get(FXGLMath.random(0, Settings.NUMBER_OF_EXPLOSIONS - 1)); + Texture texture = texture(randomTexture, explosion_width, explosion_height); + return entityBuilder() + .at(data.getX(), data.getY()) + .view(texture) + .with(new ExpireCleanComponent(Duration.seconds(0.3))) + .build(); + } - for (int i = 0; i < 20; i++) { - double x = data.getX() + FXGLMath.random(-100, 100); - double y = data.getY() + FXGLMath.random(-100, 100); - spawn(entityNames.EXPLOSION_ALIEN, x, y); + /** + * Définition de l'entité pour l'explosion, nommé explosion_player_bullet + * A but décoratif pour agrémenter le jeu + * + * @param data + * @return Entity + */ + @Spawns(entityNames.EXPLOSION_PLAYER_BULLET) + public Entity explosion_player_bullet(SpawnData data) { + int explosion_width = 70; + int explosion_height = 60; + Texture texture = texture(assetNames.textures.EXPLOSION_PLAYER, explosion_width, explosion_height); + return entityBuilder() + .at(data.getX() - explosion_width / 2, data.getY() - explosion_height / 2) + .view(texture) + .with(new ExpireCleanComponent(Duration.seconds(0.3))) + .build(); } - return entityBuilder() - .at(data.getX(), data.getY()) - .view(texture) - .zIndex(-100) - .build(); - } + /** + * Définition de l'entité pour l'explosion, nommé explosion_player_death + * A but décoratif pour agrémenter le jeu + * + * @param data + * @return Entity + */ + @Spawns(entityNames.EXPLOSION_PLAYER_DEATH) + public Entity explosion_player_death(SpawnData data) { + int explosion_width = 200; + int explosion_height = 200; + Texture texture = texture(assetNames.textures.EXPLOSION_FINAL, explosion_width, explosion_height); + + for (int i = 0; i < 20; i++) { + double x = data.getX() + FXGLMath.random(-100, 100); + double y = data.getY() + FXGLMath.random(-100, 100); + spawn(entityNames.EXPLOSION_ALIEN, x, y); + } - /** - * Définition de l'entité affichage des vies restantes - * - * @param data - * @return Entity - * @deprecated - */ - @Spawns(entityNames.LIFE) - public Entity life(SpawnData data) { - int x = (int) data.getX(); - Texture texture = texture(assetNames.textures.LIFES.get(x - 1), Constant.LIFE_DISPLAY_WIDTH, - Constant.LIFE_DISPLAY_HEIGHT); - return entityBuilder() - .at(data.getX(), data.getY()) - .view(texture) - .with(new LifeComponent()) - .build(); - } + return entityBuilder() + .at(data.getX(), data.getY()) + .view(texture) + .zIndex(-100) + .build(); + } } diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/GameLauncher.java b/duelinvaders/src/main/java/org/enstabretagne/Game/GameLauncher.java index c5dd3d5..2684245 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Game/GameLauncher.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Game/GameLauncher.java @@ -1,41 +1,30 @@ package org.enstabretagne.Game; -import static com.almasb.fxgl.dsl.FXGL.getDialogService; -import static com.almasb.fxgl.dsl.FXGL.getGameController; import static com.almasb.fxgl.dsl.FXGL.getGameScene; import static com.almasb.fxgl.dsl.FXGL.getGameWorld; import static com.almasb.fxgl.dsl.FXGL.getPhysicsWorld; -import static com.almasb.fxgl.dsl.FXGL.getUIFactoryService; -import static com.almasb.fxgl.dsl.FXGL.getb; import static com.almasb.fxgl.dsl.FXGL.loopBGM; import static com.almasb.fxgl.dsl.FXGL.onKey; import static com.almasb.fxgl.dsl.FXGL.play; -import static com.almasb.fxgl.dsl.FXGL.run; import static com.almasb.fxgl.dsl.FXGL.spawn; -import static com.almasb.fxgl.dsl.FXGL.texture; -import static org.enstabretagne.Game.GameMode.CLASSIQUE; -import static org.enstabretagne.Game.GameMode.INFINITY_MODE; -import static org.enstabretagne.Game.GameMode.SOLO; +import static org.enstabretagne.UI.UI_Factory.showPlayersLivesAndScores; -import java.util.ArrayList; import java.util.Arrays; import java.util.EnumSet; -import java.util.List; import java.util.Map; -import java.util.stream.Collectors; -import org.enstabretagne.Component.AlienComponent; -import org.enstabretagne.Component.EntityType; -import org.enstabretagne.Component.PlayerComponent; +import org.enstabretagne.Collision.AlienBulletCollision; +import org.enstabretagne.Collision.AlienPlayerCollision; +import org.enstabretagne.Collision.BulletBulletCollision; +import org.enstabretagne.Collision.BulletPlayerCollision; +import org.enstabretagne.Collision.EnemyShootBulletCollision; +import org.enstabretagne.Collision.EnemyShootPlayerCollision; import org.enstabretagne.Component.SpaceInvadersFactory; -import org.enstabretagne.Core.AlienBulletCollision; -import org.enstabretagne.Core.AlienPlayerCollision; -import org.enstabretagne.Core.BulletBulletCollision; -import org.enstabretagne.Core.BulletPlayerCollision; -import org.enstabretagne.Core.Constant; -import org.enstabretagne.Core.EnemyShootBulletCollision; -import org.enstabretagne.Core.EnemyShootPlayerCollision; -import org.enstabretagne.Core.GameVariableNames; +import org.enstabretagne.Game.GameModes.ClassicGameMode; +import org.enstabretagne.Game.GameModes.GameMode; +import org.enstabretagne.Game.GameModes.GameModeTypes; +import org.enstabretagne.Utils.GameVariableNames; +import org.enstabretagne.Utils.Settings; import org.enstabretagne.Utils.assetNames; import org.enstabretagne.Utils.entityNames; @@ -45,16 +34,8 @@ import com.almasb.fxgl.app.MenuItem; import com.almasb.fxgl.app.scene.FXGLMenu; import com.almasb.fxgl.app.scene.SceneFactory; -import com.almasb.fxgl.core.math.FXGLMath; -import com.almasb.fxgl.entity.Entity; -import javafx.geometry.Pos; import javafx.scene.input.KeyCode; -import javafx.scene.layout.HBox; -import javafx.scene.layout.VBox; -import javafx.scene.paint.Color; -import javafx.scene.text.Text; -import javafx.util.Duration; /** * Classe principale du jeu @@ -64,18 +45,14 @@ * @since 0.1.0 */ public class GameLauncher extends GameApplication { - private PlayerComponent playerComponent1; - private PlayerComponent playerComponent2; - private Entity player1; - private Entity player2; - private long last_ambient_sound = System.currentTimeMillis(); - private int delay_ambient_sound = FXGLMath.random(Constant.AMBIENT_SOUND_DELAY_MIN, - Constant.AMBIENT_SOUND_DELAY_MAX); - private static GameMode GameMode = CLASSIQUE; - VBox playersUI = new VBox(); + private static GameMode game_mode = new ClassicGameMode(); public static void setGameMode(GameMode gameMode) { - GameMode = gameMode; + game_mode = gameMode; + } + + public static GameModeTypes getGameModeType() { + return game_mode.getGameModeType(); } /** @@ -85,15 +62,15 @@ public static void setGameMode(GameMode gameMode) { */ @Override protected void initSettings(GameSettings settings) { - settings.setWidth(Constant.GAME_WIDTH.intValue()); - settings.setHeight(Constant.GAME_HEIGHT.intValue()); + settings.setWidth(Settings.GAME_WIDTH.intValue()); + settings.setHeight(Settings.GAME_HEIGHT.intValue()); settings.setTitle("Duel Invaders"); settings.setAppIcon(assetNames.textures.APP_ICON); - settings.setVersion("0.2.0"); + settings.setVersion("1.0.0"); settings.setMainMenuEnabled(true); settings.setGameMenuEnabled(true); settings.setFullScreenAllowed(true); - settings.setFullScreenFromStart(true); + // settings.setFullScreenFromStart(true); settings.setCredits(Arrays.asList( "Duel Invaders project by:", "@MathieuDFS", @@ -125,42 +102,30 @@ public FXGLMenu newMainMenu() { */ @Override protected void initInput() { - onKey(KeyCode.ENTER, () -> { - playerComponent1.shoot(); - }); - - onKey(KeyCode.RIGHT, () -> { - playerComponent1.moveRight(); + onKey(KeyCode.Q, () -> { + game_mode.getPlayerComponent1().moveLeft(); }); - - onKey(KeyCode.LEFT, () -> { - playerComponent1.moveLeft(); + onKey(KeyCode.D, () -> { + game_mode.getPlayerComponent1().moveRight(); }); - onKey(KeyCode.SPACE, () -> { - if (GameMode == SOLO) { - playerComponent1.shoot(); - } else { - playerComponent2.shoot(); + if (game_mode.getGameModeType().equals(GameModeTypes.MULTIPLAYER)) { + GameVariableNames.isShooting = true; } + game_mode.getPlayerComponent1().shoot(); }); - - onKey(KeyCode.D, () -> { - if (GameMode == SOLO) { - playerComponent1.moveRight(); - } else { - playerComponent2.moveRight(); + onKey(KeyCode.ENTER, () -> { + if (game_mode.getGameModeType().equals(GameModeTypes.MULTIPLAYER)) { + GameVariableNames.isShooting = true; } + game_mode.getPlayerComponent2().shoot(); }); - - onKey(KeyCode.Q, () -> { - if (GameMode == SOLO) { - playerComponent1.moveLeft(); - } else { - playerComponent2.moveLeft(); - } + onKey(KeyCode.LEFT, () -> { + game_mode.getPlayerComponent2().moveLeft(); + }); + onKey(KeyCode.RIGHT, () -> { + game_mode.getPlayerComponent2().moveRight(); }); - } /** @@ -172,6 +137,8 @@ protected void initInput() { protected void initGameVars(Map vars) { vars.put(GameVariableNames.isGameOver, false); vars.put(GameVariableNames.isGameWon, false); + GameVariableNames.multiplayerGameInProgress = false; + GameVariableNames.multiplayerGameWaiting = false; } /** @@ -183,82 +150,12 @@ protected void initGame() { play(assetNames.sounds.START_CLAIRON); getGameWorld().addEntityFactory(new SpaceInvadersFactory()); - player1 = spawn(entityNames.PLAYER); - player1.setX(Constant.GAME_WIDTH / 2); - player1.setY(Constant.GAME_HEIGHT - player1.getHeight()); - playerComponent1 = player1.getComponent(PlayerComponent.class); - playerComponent1.setDirection(Constant.Direction.UP); - playerComponent1.initializeScore(); - playerComponent1.initializeLife(); - - if (GameMode != SOLO) { - player2 = spawn(entityNames.PLAYER); - player2.setX(Constant.GAME_WIDTH / 2); - player2.setY(0); - playerComponent2 = player2.getComponent(PlayerComponent.class); - playerComponent2.setDirection(Constant.Direction.DOWN); - playerComponent2.initializeScore(); - playerComponent2.initializeLife(); - } - - if (GameMode == INFINITY_MODE) { - // spawn Aliens pour infinity mode - Entity alien1 = spawn(entityNames.ALIEN, 0, Constant.GAME_HEIGHT / 2 - Constant.ALIEN_HEIGHT); - alien1.getComponent(AlienComponent.class).initialize(Constant.Direction.UP); - Entity alien2 = spawn(entityNames.ALIEN, 0, Constant.GAME_HEIGHT / 2 - Constant.ALIEN_HEIGHT); - alien2.getComponent(AlienComponent.class).initialize(Constant.Direction.DOWN); - run(() -> { - Entity alien = spawn(entityNames.ALIEN, 0, Constant.GAME_HEIGHT / 2 - Constant.ALIEN_HEIGHT); - alien.getComponent(AlienComponent.class).initialize(Constant.Direction.UP); - }, Duration.seconds(1.4)); - run(() -> { - Entity alien = spawn(entityNames.ALIEN, 0, Constant.GAME_HEIGHT / 2 - Constant.ALIEN_HEIGHT); - alien.getComponent(AlienComponent.class).initialize(Constant.Direction.DOWN); - }, Duration.seconds(1.5)); - - } else if (GameMode == CLASSIQUE) { - makeAlienBlock(); - } else if (GameMode == SOLO) { - makeAlienBlockSolo(); - } + game_mode.initGameMode(); spawn(entityNames.BACKGROUND); loopBGM(assetNames.music.MUSIC_ACROSS_THE_UNIVERSE); } - private void makeAlienBlock() { - for (int i = 0; i < 2; i++) { - makeAlienLine(i, Constant.Direction.DOWN); - makeAlienLine(i, Constant.Direction.UP); - } - } - - private void makeAlienLine(int line, Constant.Direction direction) { - for (int i = 0; i < Constant.ALIENS_NUMBER; i++) { - if (direction == Constant.Direction.DOWN) { - Entity alien = spawn(entityNames.ALIEN, i * Constant.ALIEN_WIDTH, - Constant.GAME_HEIGHT / 2 + (line - 1) * Constant.ALIEN_HEIGHT); - alien.getComponent(AlienComponent.class).initialize(direction); - alien.getComponent(AlienComponent.class).setAlienNumber(i); - } else { - Entity alien = spawn(entityNames.ALIEN, i * Constant.ALIEN_WIDTH, - Constant.GAME_HEIGHT / 2 + (line - 2) * Constant.ALIEN_HEIGHT); - alien.getComponent(AlienComponent.class).initialize(direction); - alien.getComponent(AlienComponent.class).setAlienNumber(i); - } - } - } - - private void makeAlienBlockSolo() { - for (int line = 0; line < 4; line++) { - for (int k = 0; k < Constant.ALIENS_NUMBER; k++) { - Entity alien = spawn(entityNames.ALIEN, k * Constant.ALIEN_WIDTH, (line - 1) * Constant.ALIEN_HEIGHT); - alien.getComponent(AlienComponent.class).initialize(Constant.Direction.DOWN); - alien.getComponent(AlienComponent.class).setAlienNumber(k); - } - } - } - /** * Initialisation des propriétés physiques du jeu liées aux collisions */ @@ -277,43 +174,7 @@ protected void initPhysics() { */ @Override protected void initUI() { - showPlayersLivesAndScores(); - } - - private void showPlayersLivesAndScores() { - getGameScene().removeChild(playersUI); - - List playersViews = new ArrayList<>(); - List players = getGameWorld().getEntitiesByType(EntityType.PLAYER).stream() - .map(player -> player.getComponent(PlayerComponent.class)).collect(Collectors.toList()); - for (PlayerComponent playerComponent : players) { - HBox scoreUI = createScoreUI(playerComponent.getScore(), playerComponent.getId()); - scoreUI.setTranslateY(scoreUI.getHeight() * playerComponent.getId()); - HBox lifeUI = createLifeUI(playerComponent.getLife()); - var playerUI = new HBox(30, scoreUI, lifeUI); - playersViews.add(playerUI); - } - playersUI = new VBox(20, playersViews.toArray(new HBox[0])); - getGameScene().addChild(playersUI); - } - - private HBox createScoreUI(int score, int player_id) { - Text scoreText = getUIFactoryService().newText(Integer.toString(score), Color.WHITE, 24.0); - Text playerText = getUIFactoryService().newText("Player " + Integer.toString(player_id % 2 + 1), Color.WHITE, - 24.0); - var scoreView = new HBox(10, playerText, scoreText); - scoreView.setAlignment(Pos.CENTER); - return scoreView; - } - - private HBox createLifeUI(int life) { - var lifeTexture = texture(assetNames.textures.LIFE, 30, 30); - var lifeView = new HBox(10); - for (int i = 0; i < life; i++) { - lifeView.getChildren().add(lifeTexture.copy()); - } - lifeView.setAlignment(Pos.CENTER); - return lifeView; + showPlayersLivesAndScores(getGameWorld(), getGameScene()); } /** @@ -323,75 +184,7 @@ private HBox createLifeUI(int life) { */ @Override protected void onUpdate(double tpf) { - if (getb(GameVariableNames.isGameOver)) - gameOverScreen(); - if (getb(GameVariableNames.isGameWon)) - winScreen(); - - if ((System.currentTimeMillis() - last_ambient_sound) > delay_ambient_sound) { - ambientSound(); - last_ambient_sound = System.currentTimeMillis(); - delay_ambient_sound = FXGLMath.random(Constant.AMBIENT_SOUND_DELAY_MIN, Constant.AMBIENT_SOUND_DELAY_MAX); - } - if (getGameScene().getContentRoot().getChildren().contains(playersUI)) - showPlayersLivesAndScores(); - run(() -> { - getGameWorld().getEntitiesByType(EntityType.ALIEN).forEach((alien) -> { - if (FXGLMath.randomBoolean(0.01)) - alien.getComponent(AlienComponent.class).randomShoot(Constant.ALIEN_SHOOT_CHANCE); - }); - }, Duration.seconds(Constant.random.nextDouble() * 10)); - } - - /** - * Affichage de l'écran de fin de partie - */ - private void gameOverScreen() { - play(assetNames.sounds.DEFEAT_CLAIRON); - String message = "Game Over ! \n Scores are as follows : \n" + - "Player 1 : " + playerComponent1.getScore() + "\n"; - if (playerComponent2 != null) { - String player2 = "Player 2 : " + playerComponent2.getScore(); - message += player2; - } - getDialogService().showMessageBox(message, () -> { - getDialogService().showConfirmationBox("Do you want to play again?", (yes) -> playAgain(yes)); - }); - } - - /** - * Affichage de l'écran pour jouer une nouvelle partie - */ - private void playAgain(Boolean yes) { - if (yes) - getGameController().startNewGame(); - else - getGameController().gotoMainMenu(); - } - - /** - * Affichage de l'écran de victoire - */ - private void winScreen() { - play(assetNames.sounds.VICTORY_CLAIRON); - String message = "You won ! \n Scores are as follows : \n" + - "Player 1 : " + playerComponent1.getScore() + "\n"; - if (playerComponent2 != null) { - String player2 = "Player 2 : " + playerComponent2.getScore(); - message += player2; - } - getDialogService().showMessageBox(message, () -> { - getDialogService().showConfirmationBox("Do you want to play again?", (yes) -> playAgain(yes)); - }); - } - - /** - * Joue un son d'ambiance aléatoire parmi ceux disponibles - */ - private void ambientSound() { - String ambientMusic = assetNames.sounds.AMBIENT_SOUNDS - .get(FXGLMath.random(0, Constant.NUMBER_OF_AMBIENT_SOUND - 1)); - play(ambientMusic); + game_mode.onUpdate(tpf); } public static void main(String[] args) { diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/GameMode.java b/duelinvaders/src/main/java/org/enstabretagne/Game/GameMode.java deleted file mode 100644 index 057c3cf..0000000 --- a/duelinvaders/src/main/java/org/enstabretagne/Game/GameMode.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.enstabretagne.Game; - -/** - * Modes de jeu disponibles - * - * @author LBF38, MathieuDFS, jufch - * @since 0.2.0 - */ -public enum GameMode { - CLASSIQUE, INFINITY_MODE, SOLO, MUSIC_DEMO -} diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/AlienFactory.java b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/AlienFactory.java new file mode 100644 index 0000000..7fc92a5 --- /dev/null +++ b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/AlienFactory.java @@ -0,0 +1,78 @@ +package org.enstabretagne.Game.GameModes; + +import static com.almasb.fxgl.dsl.FXGL.getGameWorld; +import static com.almasb.fxgl.dsl.FXGL.run; +import static com.almasb.fxgl.dsl.FXGL.spawn; + +import org.enstabretagne.Component.AlienComponent; +import org.enstabretagne.Utils.EntityType; +import org.enstabretagne.Utils.Settings; +import org.enstabretagne.Utils.entityNames; +import org.enstabretagne.Utils.Settings.Direction; + +import com.almasb.fxgl.core.math.FXGLMath; +import com.almasb.fxgl.entity.Entity; + +import javafx.util.Duration; + +public class AlienFactory { + static protected void makeAlienBlock() { + for (int i = 0; i < 2; i++) { + makeAlienLine(i, Settings.Direction.DOWN); + makeAlienLine(i, Settings.Direction.UP); + } + } + + static protected void makeAlienLine(int line, Settings.Direction direction) { + for (int i = 0; i < Settings.ALIENS_NUMBER; i++) { + if (direction == Settings.Direction.DOWN) { + Entity alien = spawn(entityNames.ALIEN, i * Settings.ALIEN_WIDTH, + Settings.GAME_HEIGHT / 2 + (line - 1) * Settings.ALIEN_HEIGHT); + alien.getComponent(AlienComponent.class).initialize(direction); + alien.getComponent(AlienComponent.class).setAlienNumber(i); + } else { + Entity alien = spawn(entityNames.ALIEN, i * Settings.ALIEN_WIDTH, + Settings.GAME_HEIGHT / 2 + (line - 2) * Settings.ALIEN_HEIGHT); + alien.getComponent(AlienComponent.class).initialize(direction); + alien.getComponent(AlienComponent.class).setAlienNumber(i); + } + } + } + + static protected void makeAlienBlockSolo() { + for (int line = 0; line < 4; line++) { + for (int k = 0; k < Settings.ALIENS_NUMBER; k++) { + Entity alien = spawn(entityNames.ALIEN, k * Settings.ALIEN_WIDTH, (line - 1) * Settings.ALIEN_HEIGHT); + alien.getComponent(AlienComponent.class).initialize(Settings.Direction.DOWN); + alien.getComponent(AlienComponent.class).setAlienNumber(k); + } + } + } + + static protected void makeAliensInfinitely() { + makeOneAlien(Direction.UP); + makeOneAlien(Direction.DOWN); + run(() -> { + makeOneAlien(Direction.UP); + makeOneAlien(Direction.DOWN); + }, Duration.seconds(1.5)); + } + + static protected void makeOneAlien(Direction direction) { + Entity alien = spawn(entityNames.ALIEN, 0, Settings.GAME_HEIGHT / 2 - Settings.ALIEN_HEIGHT); + alien.getComponent(AlienComponent.class).initialize(direction); + } + + public static void aliensRandomlyShoot(double shootingChance) { + run(() -> { + getGameWorld().getEntitiesByType(EntityType.ALIEN).forEach((alien) -> { + if (FXGLMath.randomBoolean(shootingChance)) + alien.getComponent(AlienComponent.class).randomShoot(Settings.ALIEN_SHOOT_CHANCE); + }); + }, Duration.seconds(Settings.random.nextDouble() * 10)); + } + + public static void aliensRandomlyShoot() { + aliensRandomlyShoot(0.005); + } +} diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/ClassicGameMode.java b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/ClassicGameMode.java new file mode 100644 index 0000000..41af58a --- /dev/null +++ b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/ClassicGameMode.java @@ -0,0 +1,18 @@ +package org.enstabretagne.Game.GameModes; + +import static org.enstabretagne.Game.GameModes.AlienFactory.makeAlienBlock; + +public class ClassicGameMode extends TwoPlayerGameMode { + + @Override + public void initGameMode() { + super.initTwoPlayerGameMode(); + makeAlienBlock(); + } + + @Override + public GameModeTypes getGameModeType() { + return GameModeTypes.CLASSIC; + } + +} diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/GameMode.java b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/GameMode.java new file mode 100644 index 0000000..99e61f1 --- /dev/null +++ b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/GameMode.java @@ -0,0 +1,22 @@ +package org.enstabretagne.Game.GameModes; + +import org.enstabretagne.Component.PlayerComponent; + +/** + * Defines the contract for a game mode. + * + * @author LBF38 + */ +public interface GameMode { + public PlayerComponent getPlayerComponent1(); + + public PlayerComponent getPlayerComponent2(); + + public void initGameMode(); + + public GameModeTypes getGameModeType(); + + public void onUpdate(double tpf); + + public void gameFinished(); +} \ No newline at end of file diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/GameModeTypes.java b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/GameModeTypes.java new file mode 100644 index 0000000..160e5b7 --- /dev/null +++ b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/GameModeTypes.java @@ -0,0 +1,11 @@ +package org.enstabretagne.Game.GameModes; + +/** + * Modes de jeu disponibles + * + * @author LBF38, MathieuDFS, jufch + * @since 0.2.0 + */ +public enum GameModeTypes { + CLASSIC, INFINITY, SOLO, DUO, MUSIC_DEMO, MULTIPLAYER +} diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/InfinityGameMode.java b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/InfinityGameMode.java new file mode 100644 index 0000000..e1b2acd --- /dev/null +++ b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/InfinityGameMode.java @@ -0,0 +1,18 @@ +package org.enstabretagne.Game.GameModes; + +import static org.enstabretagne.Game.GameModes.AlienFactory.makeAliensInfinitely; + +public class InfinityGameMode extends TwoPlayerGameMode { + + @Override + public void initGameMode() { + super.initTwoPlayerGameMode(); + makeAliensInfinitely(); + } + + @Override + public GameModeTypes getGameModeType() { + return GameModeTypes.INFINITY; + } + +} diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/MultiplayerGameMode.java b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/MultiplayerGameMode.java new file mode 100644 index 0000000..44551bc --- /dev/null +++ b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/MultiplayerGameMode.java @@ -0,0 +1,430 @@ +package org.enstabretagne.Game.GameModes; + +import static com.almasb.fxgl.dsl.FXGL.getDialogService; +import static com.almasb.fxgl.dsl.FXGL.getGameController; +import static com.almasb.fxgl.dsl.FXGL.getGameWorld; +import static com.almasb.fxgl.dsl.FXGL.getNetService; +import static com.almasb.fxgl.dsl.FXGL.getNotificationService; +import static com.almasb.fxgl.dsl.FXGL.getb; +import static com.almasb.fxgl.dsl.FXGL.run; +import static com.almasb.fxgl.dsl.FXGL.runOnce; +import static com.almasb.fxgl.dsl.FXGL.showConfirm; +import static org.enstabretagne.UI.UI_Factory.gameOverScreen; +import static org.enstabretagne.UI.UI_Factory.winScreen; + +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Pattern; + +import org.enstabretagne.Component.AlienComponent; +import org.enstabretagne.Component.PlayerComponent; +import org.enstabretagne.Utils.EntityType; +import org.enstabretagne.Utils.GameVariableNames; +import org.enstabretagne.Utils.Settings; + +import com.almasb.fxgl.core.math.FXGLMath; +import com.almasb.fxgl.core.serialization.Bundle; +import com.almasb.fxgl.entity.Entity; +import com.almasb.fxgl.logging.Logger; +import com.almasb.fxgl.net.Client; +import com.almasb.fxgl.net.Server; + +import javafx.animation.PauseTransition; +import javafx.util.Duration; + +public class MultiplayerGameMode extends TwoPlayerGameMode { + private enum DialogType { + SERVER_CHOICE, PORT_CHOICE, IP_CHOICE + } + + private class BundleKey { + public static final String TYPE = "type"; + public static final String X = "x"; + public static final String Y = "y"; + public static final String SCORE = "score"; + public static final String LIFE = "life"; + } + + private class BundleType { + public static final String PLAYER1 = "Player1"; + public static final String PLAYER2 = "Player2"; + public static final String PLAYER1_SHOOT = "Player1Shoot"; + public static final String PLAYER2_SHOOT = "Player2Shoot"; + public static final String CLIENT_CONNECTED = "Client Connected"; + public static final String SERVER_START = "Server Start"; + public static final String GAME_OVER = "Game Over"; + public static final String GAME_WIN = "Game Win"; + } + + private boolean isServer = false; + private Server server; + private Client client; + private int serverPort = 55555; + private String serverIPaddress = "localhost"; + + @Override + public void initGameMode() { + super.initTwoPlayerGameMode(); + dialogQueue().play(); + } + + @Override + public GameModeTypes getGameModeType() { + return GameModeTypes.MULTIPLAYER; + } + + @Override + public PlayerComponent getPlayerComponent2() { + // Même logique car mode multijoueur + return getPlayerComponent1(); + } + + @Override + public PlayerComponent getPlayerComponent1() { + if (GameVariableNames.isShooting) { + onShootBroadcastLogic(); + } + if (isServer) { + return playerComponent1; + } else { + return playerComponent2; + } + } + + @Override + public void onUpdate(double tpf) { + if (GameVariableNames.multiplayerGameInProgress) { + onUpdateBroadcastLogic(); + super.onUpdate(tpf); + aliensShootInPlayersDirection(); + } else { + // Synchronise le début de la partie entre les deux joueurs + if (!isServer && GameVariableNames.multiplayerGameWaiting) { + client.broadcast(new Bundle(BundleType.CLIENT_CONNECTED)); + } + runOnce(() -> waitingForConnection(), Duration.seconds(3)); + } + } + + @Override + public void gameFinished() { + if (getb(GameVariableNames.isGameOver)) { + GameEndBroadcastLogic(BundleType.GAME_OVER); + gameOverScreen(playerComponent1.getScore(), playerComponent2.getScore()); + } + if (getb(GameVariableNames.isGameWon)) { + GameEndBroadcastLogic(BundleType.GAME_WIN); + winScreen(playerComponent1.getScore(), playerComponent2.getScore()); + } + } + + /** + * Logique d'envoi des données à chaque frame + */ + private void onUpdateBroadcastLogic() { + if (isServer) { + Bundle bundle = createPlayerInfosBundle(player1, playerComponent1, BundleType.PLAYER1, BundleType.PLAYER1); + server.broadcast(bundle); + } else { + Bundle bundle = createPlayerInfosBundle(player2, playerComponent2, BundleType.PLAYER2, BundleType.PLAYER2); + client.broadcast(bundle); + } + } + + /** + * Wait for a connexion to be established + */ + private void waitingForConnection() { + if (isServer) { + if (server.getConnections().size() == 0) { + getDialogService().showConfirmationBox("En attente d'un joueur...\n" + + "Voulez-vous quitter la partie ?", (yes) -> { + if (yes) { + server.stop(); + getGameController().gotoMainMenu(); + } + }); + } + } + if (client == null) + return; + if (client.getConnections().size() == 0) { + getDialogService().showConfirmationBox( + "En attente de la connexion...\n" + "Voulez-vous quitter la partie ?", (yes) -> { + if (yes) { + client.disconnect(); + getGameController().gotoMainMenu(); + } + }); + } + } + + private void aliensShootInPlayersDirection() { + run(() -> { + getGameWorld().getEntitiesByType(EntityType.ALIEN).forEach((alien) -> { + if (FXGLMath.randomBoolean(0.005)) { + AlienComponent alienComponent = alien.getComponent(AlienComponent.class); + if (isServer + && alienComponent.getDirection() == Settings.Direction.DOWN) { + alienComponent.randomShoot(Settings.ALIEN_SHOOT_CHANCE); + } else if (!isServer + && alienComponent.getDirection() == Settings.Direction.UP) { + alienComponent.randomShoot(Settings.ALIEN_SHOOT_CHANCE); + } + } + }); + }, Duration.seconds(Settings.random.nextDouble() * 10)); + } + + private void GameEndBroadcastLogic(String bundleType) { + Bundle bundle = new Bundle(bundleType); + if (isServer) { + server.broadcast(bundle); + server.stop(); + } else { + client.broadcast(bundle); + client.disconnect(); + } + } + + private PauseTransition dialogQueue() { + Map dialogQueueMessages = new HashMap<>(); + dialogQueueMessages.put(DialogType.SERVER_CHOICE, "Voulez-vous être le serveur ?"); + dialogQueueMessages.put(DialogType.PORT_CHOICE, "Entrez le port (ex:55555)"); + dialogQueueMessages.put(DialogType.IP_CHOICE, + "Entrez l'adresse IP du serveur (format:255.255.255.255 ou localhost)"); + PauseTransition pause = new PauseTransition(Duration.seconds(2)); + pause.setOnFinished(event -> { + showConfirm(dialogQueueMessages.get(DialogType.SERVER_CHOICE), + (yes) -> { + areYouTheServer(yes); + showPortInput(dialogQueueMessages); + }); + }); + return pause; + } + + /** + * Choix utilisateur pour héberger/rejoindre une partie multijoueur + */ + private Boolean areYouTheServer(Boolean yes) { + isServer = yes; + Logger.get(getClass()).info("Are you the server ? : " + isServer); + return isServer; + } + + private void showPortInput(Map dialogQueueMessages) { + getDialogService().showInputBox(dialogQueueMessages.get(DialogType.PORT_CHOICE), (port) -> { + selectPort(port); + if (isServer) { + initializePlayers(); + return; + } + getDialogService().showInputBox(dialogQueueMessages.get(DialogType.IP_CHOICE), + (IPaddress) -> { + selectIPaddress(IPaddress); + Logger.get(getClass()).info("IP : " + serverIPaddress); + initializePlayers(); + }); + }); + } + + /** + * Choix du port du serveur + */ + private void selectPort(String port) { + try { + serverPort = Integer.parseInt(port); + } catch (Exception e) { + serverPort = 55555; + } + Logger.get(getClass()).info("Port : " + serverPort); + } + + /** + * Input box pour la sélection de l'adresse IP du serveur + */ + private void selectIPaddress(String IPaddress) { + // Regex expression for validating IPv4 + final String regex_ipv4 = "^\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}$"; + // Regex expression for validating IPv6 + final String regex_ipv6 = "^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"; + Pattern pattern_ipv4 = Pattern.compile(regex_ipv4); + Pattern pattern_ipv6 = Pattern.compile(regex_ipv6); + if (!pattern_ipv4.matcher(IPaddress).matches() && !pattern_ipv6.matcher(IPaddress).matches()) { + serverIPaddress = "localhost"; + return; + } + serverIPaddress = IPaddress; + } + + private void initializePlayers() { + if (isServer) { + initializeServer(); + return; + } + initializeClient(); + } + + /** + * Initialisation du serveur + */ + private void initializeServer() { + Logger.get(getClass()).info("Server starting..."); + getNotificationService().pushNotification("Server started at " + serverIPaddress + + ":" + serverPort); + server = getNetService().newTCPServer(serverPort); + onReceiveMessageServer(); + server.startAsync(); + GameVariableNames.multiplayerGameWaiting = true; + Logger.get(getClass()).info("Server started on port : " + serverPort); + } + + /** + * Logique lors de la réception des données du client par le serveur + */ + private void onReceiveMessageServer() { + server.setOnConnected(connection -> { + connection.addMessageHandlerFX((conn, message) -> { + switch (message.getName()) { + case BundleType.PLAYER2: + updatePlayersInfos(player2, playerComponent2, message); + break; + case BundleType.PLAYER2_SHOOT: + playerComponent2.shoot(); + break; + case BundleType.CLIENT_CONNECTED: + if (!GameVariableNames.multiplayerGameInProgress) { + server.broadcast(new Bundle(BundleType.SERVER_START)); + GameVariableNames.multiplayerGameInProgress = true; + startMultiGame(); + } + break; + case BundleType.GAME_OVER: + gameOverScreen(playerComponent1.getScore(), playerComponent2.getScore()); + server.stop(); + break; + case BundleType.GAME_WIN: + winScreen(playerComponent1.getScore(), playerComponent2.getScore()); + server.stop(); + break; + default: + Logger.get(getClass()).info("Server received unknown message: " + message); + break; + } + }); + }); + } + + /** + * Update the players infos from the bundle received (client or server) + * + * @param player + * @param playerComponent + * @param bundle + */ + private void updatePlayersInfos(Entity player, PlayerComponent playerComponent, Bundle bundle) { + player.setX(bundle.get(BundleKey.X)); + player.setY(bundle.get(BundleKey.Y)); + playerComponent.setScore(bundle.get(BundleKey.SCORE)); + playerComponent.setLife(bundle.get(BundleKey.LIFE)); + } + + /** + * Initialisation du client + */ + private void initializeClient() { + Logger.get(getClass()).info("Client starting..."); + getNotificationService() + .pushNotification("Connecting to the following server : " + serverIPaddress + + ":" + serverPort); + client = getNetService().newTCPClient(serverIPaddress, serverPort); + onReceiveMessageClient(); + client.connectAsync(); + GameVariableNames.multiplayerGameWaiting = true; + Logger.get(getClass()).info("Client connected to server !"); + getNotificationService().pushNotification("Connected to the server !"); + } + + /** + * Logique lors de la réception des données du serveur par le client + */ + private void onReceiveMessageClient() { + client.setOnConnected(connection -> { + connection.addMessageHandlerFX((conn, message) -> { + switch (message.getName()) { + case BundleType.PLAYER1: + updatePlayersInfos(player1, playerComponent1, message); + break; + case BundleType.PLAYER1_SHOOT: + playerComponent1.shoot(); + break; + case BundleType.SERVER_START: + GameVariableNames.multiplayerGameInProgress = true; + startMultiGame(); + break; + case BundleType.GAME_OVER: + gameOverScreen(playerComponent1.getScore(), playerComponent2.getScore()); + client.disconnect(); + break; + case BundleType.GAME_WIN: + winScreen(playerComponent1.getScore(), playerComponent2.getScore()); + client.disconnect(); + break; + default: + Logger.get(getClass()).info("Client received unknown message: " + message); + break; + } + }); + }); + } + + /** + * Création d'un bundle contenant les données du joueur + * + * Informations transmises : + *
    + *
  • type : type de joueur
  • + *
  • x : position x du joueur
  • + *
  • y : position y du joueur
  • + *
  • score : score du joueur
  • + *
  • life : vie du joueur
  • + *
+ * + * @param player + * @param playerComponent + * @param bundleName + * @param bundleType + * @return bundle contenant les données du joueur + * + * @author LBF38 + */ + private Bundle createPlayerInfosBundle(Entity player, PlayerComponent playerComponent, String bundleName, + String bundleType) { + Bundle bundle = new Bundle(bundleName); + bundle.put(BundleKey.TYPE, bundleType); + bundle.put(BundleKey.X, player.getX()); + bundle.put(BundleKey.Y, player.getY()); + bundle.put(BundleKey.SCORE, playerComponent.getScore()); + bundle.put(BundleKey.LIFE, playerComponent.getLife()); + return bundle; + } + + private void startMultiGame() { + long startGameTime = System.currentTimeMillis(); + Logger.get(getClass()).info("startGameTime : " + startGameTime); + AlienFactory.makeAlienBlock(); + } + + /** + * Logique d'envoi des données lors du tir d'un joueur + */ + private void onShootBroadcastLogic() { + if (isServer) { + server.broadcast(new Bundle(BundleType.PLAYER1_SHOOT)); + } else { + client.broadcast(new Bundle(BundleType.PLAYER2_SHOOT)); + } + } + +} diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/MusicDemoGameMode.java b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/MusicDemoGameMode.java new file mode 100644 index 0000000..559c01b --- /dev/null +++ b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/MusicDemoGameMode.java @@ -0,0 +1,10 @@ +package org.enstabretagne.Game.GameModes; + +public class MusicDemoGameMode extends TwoPlayerGameMode { + + @Override + public void initGameMode() { + super.initTwoPlayerGameMode(); + } + +} diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/OnePlayerGameMode.java b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/OnePlayerGameMode.java new file mode 100644 index 0000000..5dfeab9 --- /dev/null +++ b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/OnePlayerGameMode.java @@ -0,0 +1,91 @@ +package org.enstabretagne.Game.GameModes; + +import static com.almasb.fxgl.dsl.FXGL.getb; +import static com.almasb.fxgl.dsl.FXGL.*; +import static org.enstabretagne.UI.UI_Factory.gameOverScreen; +import static org.enstabretagne.UI.UI_Factory.*; + +import org.enstabretagne.Component.PlayerComponent; +import org.enstabretagne.Utils.GameVariableNames; +import org.enstabretagne.Utils.Settings; +import org.enstabretagne.Utils.Settings.Direction; +import org.enstabretagne.Utils.entityNames; + +import com.almasb.fxgl.core.math.FXGLMath; +import com.almasb.fxgl.entity.Entity; + +public abstract class OnePlayerGameMode implements GameMode { + protected Entity player1; + protected PlayerComponent playerComponent1; + protected long last_ambient_sound = System.currentTimeMillis(); + protected int delay_ambient_sound = FXGLMath.random(Settings.AMBIENT_SOUND_DELAY_MIN, + Settings.AMBIENT_SOUND_DELAY_MAX); + + @Override + public GameModeTypes getGameModeType() { + return GameModeTypes.SOLO; + } + + public void initOnePlayerGameMode() { + player1 = initPlayer(player1, Settings.GAME_WIDTH / 2); + player1.setY(Settings.GAME_HEIGHT - player1.getHeight()); + playerComponent1 = initPlayerComponent(player1, Direction.UP); + } + + @Override + public void gameFinished() { + if (getb(GameVariableNames.isGameOver)) { + gameOverScreen(Integer.toString(playerComponent1.getScore())); + } + if (getb(GameVariableNames.isGameWon)) { + winScreen(Integer.toString(playerComponent1.getScore())); + } + } + + @Override + public PlayerComponent getPlayerComponent1() { + return playerComponent1; + } + + @Override + public PlayerComponent getPlayerComponent2() { + return playerComponent1; + } + + @Override + public void onUpdate(double tpf) { + if (getb(GameVariableNames.isGameOver) || getb(GameVariableNames.isGameWon)) { + gameFinished(); + } + + if ((System.currentTimeMillis() - last_ambient_sound) > delay_ambient_sound) { + ambientSound(); + last_ambient_sound = System.currentTimeMillis(); + delay_ambient_sound = FXGLMath.random(Settings.AMBIENT_SOUND_DELAY_MIN, Settings.AMBIENT_SOUND_DELAY_MAX); + } + + showPlayersLivesAndScores(getGameWorld(), getGameScene()); + + if (getGameModeType() != GameModeTypes.MULTIPLAYER) + AlienFactory.aliensRandomlyShoot(); + + } + + protected Entity initPlayer(Entity player, double positionX, double positionY) { + player = spawn(entityNames.PLAYER); + player.setX(positionX); + player.setY(positionY); + return player; + } + + protected Entity initPlayer(Entity player, double positionX) { + return initPlayer(player, positionX, 0); + } + + protected PlayerComponent initPlayerComponent(Entity player, Direction direction) { + PlayerComponent playerComponent = player.getComponent(PlayerComponent.class); + playerComponent.setDirection(direction); + return playerComponent; + } + +} diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/SoloGameMode.java b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/SoloGameMode.java new file mode 100644 index 0000000..d42a2d2 --- /dev/null +++ b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/SoloGameMode.java @@ -0,0 +1,18 @@ +package org.enstabretagne.Game.GameModes; + +import static org.enstabretagne.Game.GameModes.AlienFactory.makeAlienBlockSolo; + +public class SoloGameMode extends OnePlayerGameMode { + + @Override + public void initGameMode() { + super.initOnePlayerGameMode(); + makeAlienBlockSolo(); + } + + @Override + public GameModeTypes getGameModeType() { + return GameModeTypes.SOLO; + } + +} diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/TwoPlayerGameMode.java b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/TwoPlayerGameMode.java new file mode 100644 index 0000000..821c1ee --- /dev/null +++ b/duelinvaders/src/main/java/org/enstabretagne/Game/GameModes/TwoPlayerGameMode.java @@ -0,0 +1,43 @@ +package org.enstabretagne.Game.GameModes; + +import static com.almasb.fxgl.dsl.FXGL.getb; +import static org.enstabretagne.UI.UI_Factory.gameOverScreen; +import static org.enstabretagne.UI.UI_Factory.winScreen; + +import org.enstabretagne.Component.PlayerComponent; +import org.enstabretagne.Utils.GameVariableNames; +import org.enstabretagne.Utils.Settings; +import org.enstabretagne.Utils.Settings.Direction; + +import com.almasb.fxgl.entity.Entity; + +public abstract class TwoPlayerGameMode extends OnePlayerGameMode { + protected Entity player2; + protected PlayerComponent playerComponent2; + + @Override + public GameModeTypes getGameModeType() { + return GameModeTypes.DUO; + } + + public void initTwoPlayerGameMode() { + super.initOnePlayerGameMode(); + player2 = initPlayer(player2, Settings.GAME_WIDTH / 2, 0); + playerComponent2 = initPlayerComponent(player2, Direction.DOWN); + } + + @Override + public void gameFinished() { + if (getb(GameVariableNames.isGameOver)) { + gameOverScreen(playerComponent1.getScore(), playerComponent2.getScore()); + } + if (getb(GameVariableNames.isGameWon)) { + winScreen(playerComponent1.getScore(), playerComponent2.getScore()); + } + } + + @Override + public PlayerComponent getPlayerComponent2() { + return playerComponent2; + } +} diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/NewMainMenu.java b/duelinvaders/src/main/java/org/enstabretagne/Game/NewMainMenu.java index b9e5f3c..cd60422 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Game/NewMainMenu.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Game/NewMainMenu.java @@ -5,7 +5,12 @@ import static com.almasb.fxgl.dsl.FXGL.getWindowService; import static com.almasb.fxgl.dsl.FXGL.texture; -import org.enstabretagne.Core.Constant; +import org.enstabretagne.Game.GameModes.ClassicGameMode; +import org.enstabretagne.Game.GameModes.InfinityGameMode; +import org.enstabretagne.Game.GameModes.MultiplayerGameMode; +import org.enstabretagne.Game.GameModes.MusicDemoGameMode; +import org.enstabretagne.Game.GameModes.SoloGameMode; +import org.enstabretagne.Utils.Settings; import org.enstabretagne.Utils.assetNames; import com.almasb.fxgl.app.scene.FXGLMenu; @@ -44,7 +49,7 @@ public NewMainMenu() { var titleBox = new HBox(title); titleBox.setAlignment(Pos.CENTER); titleBox.setTranslateY(100); - titleBox.setTranslateX(Constant.GAME_WIDTH / 2 - title.getLayoutBounds().getWidth() / 2); + titleBox.setTranslateX(Settings.GAME_WIDTH / 2 - title.getLayoutBounds().getWidth() / 2); var pauseText = getUIFactoryService().newText("PAUSE", Color.WHITE, 20); var pauseKey = new KeyView(KeyCode.ESCAPE, Color.BLUE, 20.0); @@ -65,34 +70,39 @@ private VBox createMenu() { VBox options = createOptions(); FXGLTextFlow creditsText = createCredits(); - SpaceButton buttonClassicMode = new SpaceButton("Play Classique", () -> { - GameLauncher.setGameMode(GameMode.CLASSIQUE); + SpaceButton buttonClassicMode = new SpaceButton("Play Classic", () -> { + GameLauncher.setGameMode(new ClassicGameMode()); fireNewGame(); }); SpaceButton buttonInfinityMode = new SpaceButton("Play Infinity", () -> { - GameLauncher.setGameMode(GameMode.INFINITY_MODE); + GameLauncher.setGameMode(new InfinityGameMode()); fireNewGame(); }); SpaceButton buttonSoloMode = new SpaceButton("Play Solo", () -> { - GameLauncher.setGameMode(GameMode.SOLO); + GameLauncher.setGameMode(new SoloGameMode()); + fireNewGame(); + }); + + SpaceButton buttonMultiMode = new SpaceButton("Play Multiplayer", () -> { + GameLauncher.setGameMode(new MultiplayerGameMode()); fireNewGame(); }); SpaceButton buttonMusicDemo = new SpaceButton("Play Music Demo", () -> { - GameLauncher.setGameMode(GameMode.MUSIC_DEMO); + GameLauncher.setGameMode(new MusicDemoGameMode()); fireNewGame(); }); - SpaceButton buttonOption = new SpaceButton("Option", () -> { + SpaceButton buttonOption = new SpaceButton("Options", () -> { if (!getContentRoot().getChildren().contains(options)) { getWindowService().getCurrentScene().removeChild(creditsText); getContentRoot().getChildren().add(options); } }); - SpaceButton buttonCredit = new SpaceButton("Credit", () -> { + SpaceButton buttonCredit = new SpaceButton("Credits", () -> { if (!getContentRoot().getChildren().contains(creditsText)) { getWindowService().getCurrentScene().removeChild(options); getContentRoot().getChildren().add(creditsText); @@ -105,6 +115,7 @@ private VBox createMenu() { buttonClassicMode, buttonInfinityMode, buttonSoloMode, + buttonMultiMode, buttonMusicDemo, buttonOption, buttonCredit, @@ -113,7 +124,7 @@ private VBox createMenu() { new Separator(Orientation.HORIZONTAL), getUIFactoryService().newText("Select Game Mode", Color.WHITE, 22)); box.setTranslateX(100); - box.setTranslateY(Constant.GAME_HEIGHT / 2); + box.setTranslateY(Settings.GAME_HEIGHT / 2); box.setAlignment(Pos.CENTER); return box; } @@ -138,7 +149,7 @@ private VBox createOptions() { var soundBox = new HBox(10, soundText, soundSlider); var box = new VBox(10, musicBox, soundBox); box.setTranslateX(getAppHeight() / 2 + 100); - box.setTranslateY(Constant.GAME_HEIGHT / 2); + box.setTranslateY(Settings.GAME_HEIGHT / 2); musicBox.setAlignment(Pos.CENTER); soundBox.setAlignment(Pos.CENTER); box.setAlignment(Pos.CENTER); @@ -163,15 +174,13 @@ private FXGLTextFlow createCredits() { FXGLTextFlow creditsText = getUIFactoryService().newTextFlow(); creditsText.append(creditsString, Color.GRAY, 16); creditsText.setTranslateX(getAppHeight() / 2 + 100); - creditsText.setTranslateY(Constant.GAME_HEIGHT / 2); + creditsText.setTranslateY(Settings.GAME_HEIGHT / 2); return creditsText; } /** * Define the button - * - * @param name - * @param action + * * @return SpaceButton * @see SpaceButton * @see StackPane diff --git a/duelinvaders/src/main/java/org/enstabretagne/UI/UI_Factory.java b/duelinvaders/src/main/java/org/enstabretagne/UI/UI_Factory.java new file mode 100644 index 0000000..16e4d00 --- /dev/null +++ b/duelinvaders/src/main/java/org/enstabretagne/UI/UI_Factory.java @@ -0,0 +1,136 @@ +package org.enstabretagne.UI; + +import static com.almasb.fxgl.dsl.FXGL.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import org.enstabretagne.Component.PlayerComponent; +import org.enstabretagne.Utils.EntityType; +import org.enstabretagne.Utils.Settings; +import org.enstabretagne.Utils.assetNames; + +import com.almasb.fxgl.app.scene.GameScene; +import com.almasb.fxgl.core.math.FXGLMath; +import com.almasb.fxgl.entity.GameWorld; + +import javafx.geometry.Pos; +import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; +import javafx.scene.paint.Color; +import javafx.scene.text.Text; + +public class UI_Factory { + private static VBox playersUI = new VBox(); + + public static VBox showPlayersLivesAndScores(GameWorld gameWorld,GameScene gameScene) { + if (getGameScene().getContentRoot().getChildren().contains(playersUI)) + getGameScene().removeChild(playersUI); + // System.out.println("Players Lives and Scores"); + + List playersViews = new ArrayList<>(); + List players = gameWorld.getEntitiesByType(EntityType.PLAYER).stream() + .map(player -> player.getComponent(PlayerComponent.class)).collect(Collectors.toList()); + for (PlayerComponent playerComponent : players) { + HBox scoreUI = createScoreUI(playerComponent.getScore(), playerComponent.getId()); + scoreUI.setTranslateY(scoreUI.getHeight() * playerComponent.getId()); + HBox lifeUI = createLifeUI(playerComponent.getLife()); + var playerUI = new HBox(30, scoreUI, lifeUI); + playersViews.add(playerUI); + } + playersUI = new VBox(20, playersViews.toArray(new HBox[0])); + + getGameScene().addChild(playersUI); + return playersUI; + } + + public static HBox createScoreUI(int score, int player_id) { + Text scoreText = getUIFactoryService().newText(Integer.toString(score), Color.WHITE, 24.0); + Text playerText = getUIFactoryService().newText("Player " + Integer.toString(player_id), Color.WHITE, + 24.0); + var scoreView = new HBox(10, playerText, scoreText); + scoreView.setAlignment(Pos.CENTER); + return scoreView; + } + + public static HBox createLifeUI(int life) { + var lifeTexture = texture(assetNames.textures.LIFE, 30, 30); + var lifeView = new HBox(10); + for (int i = 0; i < life; i++) { + lifeView.getChildren().add(lifeTexture.copy()); + } + lifeView.setAlignment(Pos.CENTER); + return lifeView; + } + + /** + * Affichage de l'écran pour jouer une nouvelle partie + */ + public static void playAgain(Boolean yes) { + if (yes) + getGameController().startNewGame(); + else + getGameController().gotoMainMenu(); + } + + /** + * Affichage de l'écran de victoire + */ + public static void winScreen(String score_player1, String score_player2) { + play(assetNames.sounds.VICTORY_CLAIRON); + String message = "You won ! \nScores are as follows : \n" + + "Player 1 : " + score_player1 + "\n"; + if (score_player2 != null) { + String player2 = "Player 2 : " + score_player2; + message += player2; + } + askConfirmationToUser(message, (yes) -> playAgain(yes)); + } + + private static void askConfirmationToUser(String message, Consumer responseCallback) { + getDialogService().showMessageBox(message, () -> { + getDialogService().showConfirmationBox("Do you want to play again?", responseCallback); + }); + } + + public static void winScreen(String score_player1) { + winScreen(score_player1, null); + } + + public static void winScreen(int score_player1, int score_player2) { + winScreen(Integer.toString(score_player1), Integer.toString(score_player2)); + } + + /** + * Affichage de l'écran de fin de partie + */ + public static void gameOverScreen(String score_player1, String score_player2) { + play(assetNames.sounds.DEFEAT_CLAIRON); + String message = "Game Over ! \nScores are as follows : \n" + + "Player 1 : " + score_player1 + "\n"; + if (score_player2 != null) { + String player2 = "Player 2 : " + score_player2; + message += player2; + } + askConfirmationToUser(message, (yes) -> playAgain(yes)); + } + + public static void gameOverScreen(String score_player1) { + gameOverScreen(score_player1, null); + } + + public static void gameOverScreen(int score_player1, int score_player2) { + gameOverScreen(Integer.toString(score_player1), Integer.toString(score_player2)); + } + + /** + * Joue un son d'ambiance aléatoire parmi ceux disponibles + */ + public static void ambientSound() { + String ambientMusic = assetNames.sounds.AMBIENT_SOUNDS + .get(FXGLMath.random(0, Settings.NUMBER_OF_AMBIENT_SOUND - 1)); + play(ambientMusic); + } +} diff --git a/duelinvaders/src/main/java/org/enstabretagne/Component/EntityType.java b/duelinvaders/src/main/java/org/enstabretagne/Utils/EntityType.java similarity index 83% rename from duelinvaders/src/main/java/org/enstabretagne/Component/EntityType.java rename to duelinvaders/src/main/java/org/enstabretagne/Utils/EntityType.java index 7dcad8c..8196817 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Component/EntityType.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Utils/EntityType.java @@ -1,4 +1,4 @@ -package org.enstabretagne.Component; +package org.enstabretagne.Utils; /** * Définition des types d'entités disponibles dans le jeu diff --git a/duelinvaders/src/main/java/org/enstabretagne/Core/GameVariableNames.java b/duelinvaders/src/main/java/org/enstabretagne/Utils/GameVariableNames.java similarity index 56% rename from duelinvaders/src/main/java/org/enstabretagne/Core/GameVariableNames.java rename to duelinvaders/src/main/java/org/enstabretagne/Utils/GameVariableNames.java index 6b48aac..9e5aef8 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Core/GameVariableNames.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Utils/GameVariableNames.java @@ -1,4 +1,4 @@ -package org.enstabretagne.Core; +package org.enstabretagne.Utils; /** * Classe contenant les noms des variables de jeu @@ -9,10 +9,12 @@ * @since 0.1.0 */ public class GameVariableNames { - public static final String PLAYER1_SCORE = "Player1_score"; - public static final String PLAYER2_SCORE = "Player2_score"; - public static final String PLAYER1_LIFE = "Player1_lives"; - public static final String PLAYER2_LIFE = "Player2_lives"; public static final String isGameOver = "isGameOver"; public static final String isGameWon = "isGameWon"; + + // LBF : dans le mode multi ?? + // utile pour la synchro du lancement du multijoueur + public static boolean multiplayerGameInProgress = false; + public static boolean multiplayerGameWaiting = false; + public static boolean isShooting = false; } diff --git a/duelinvaders/src/main/java/org/enstabretagne/Core/Constant.java b/duelinvaders/src/main/java/org/enstabretagne/Utils/Settings.java similarity index 97% rename from duelinvaders/src/main/java/org/enstabretagne/Core/Constant.java rename to duelinvaders/src/main/java/org/enstabretagne/Utils/Settings.java index 2c9e7ef..e46bfb0 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Core/Constant.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Utils/Settings.java @@ -1,4 +1,4 @@ -package org.enstabretagne.Core; +package org.enstabretagne.Utils; import javafx.util.Duration; import java.util.Random; @@ -11,7 +11,7 @@ * @author jufch, LBF38, MathieuDFS * @since 0.1.0 */ -public class Constant { +public class Settings { public static final Double GAME_WIDTH = 1366.0; public static final Double GAME_HEIGHT = 768.0; diff --git a/duelinvaders/src/main/java/org/enstabretagne/Utils/assetNames.java b/duelinvaders/src/main/java/org/enstabretagne/Utils/assetNames.java index a2f3344..123da82 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Utils/assetNames.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Utils/assetNames.java @@ -2,8 +2,6 @@ import java.util.ArrayList; -import org.enstabretagne.Core.Constant; - /** * Classe contenant les noms des assets du jeu * Cela permet d'éviter les risques de fautes de frappe et centralise l'ensemble @@ -27,7 +25,7 @@ public static class textures { public static final String EXPLOSION_FINAL = "explosion_final.png"; public static final ArrayList EXPLOSIONS = new ArrayList() { { - for (int i = 1; i <= Constant.NUMBER_OF_EXPLOSIONS; i++) { + for (int i = 1; i <= Settings.NUMBER_OF_EXPLOSIONS; i++) { add("explosion" + i + ".png"); } } @@ -62,7 +60,7 @@ public static class sounds { public static final String VICTORY_CLAIRON = "autre/claironVictory.wav"; public static final ArrayList AMBIENT_SOUNDS = new ArrayList() { { - for (int i = 1; i < Constant.NUMBER_OF_AMBIENT_SOUND + 1; i++) { + for (int i = 1; i < Settings.NUMBER_OF_AMBIENT_SOUND + 1; i++) { add("ambiance/ambientSound" + i + ".wav"); } } @@ -70,7 +68,7 @@ public static class sounds { public static final String CANNON_SHOT = "Tir/canon.wav"; public static final ArrayList LASER_SOUNDS = new ArrayList<>() { { - for (int i = 1; i <= Constant.NUMBER_OF_LASER_SOUNDS; i++) { + for (int i = 1; i <= Settings.NUMBER_OF_LASER_SOUNDS; i++) { add("Tir/laser" + i + ".wav"); } } diff --git a/duelinvaders/src/test/java/org/enstabretagne/Component/AlienComponentTest.java b/duelinvaders/src/test/java/org/enstabretagne/Component/AlienComponentTest.java index 337873f..a3055ed 100644 --- a/duelinvaders/src/test/java/org/enstabretagne/Component/AlienComponentTest.java +++ b/duelinvaders/src/test/java/org/enstabretagne/Component/AlienComponentTest.java @@ -1,6 +1,7 @@ package org.enstabretagne.Component; -import org.enstabretagne.Core.Constant; +import org.enstabretagne.Utils.EntityType; +import org.enstabretagne.Utils.Settings; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; @@ -12,7 +13,7 @@ public class AlienComponentTest { @BeforeEach public void setup() { - alienComponent = new AlienComponent(Constant.Direction.DOWN); + alienComponent = new AlienComponent(Settings.Direction.DOWN); entityBuilder() .type(EntityType.ALIEN) .at(0, 0) diff --git a/duelinvaders/src/test/java/org/enstabretagne/Component/BulletComponentTest.java b/duelinvaders/src/test/java/org/enstabretagne/Component/BulletComponentTest.java index b058855..afd9a50 100644 --- a/duelinvaders/src/test/java/org/enstabretagne/Component/BulletComponentTest.java +++ b/duelinvaders/src/test/java/org/enstabretagne/Component/BulletComponentTest.java @@ -5,6 +5,8 @@ import static com.almasb.fxgl.dsl.FXGL.entityBuilder; import static org.junit.jupiter.api.Assertions.*; +import org.enstabretagne.Utils.EntityType; + public class BulletComponentTest { private BulletComponent bulletComponent; // private Double dy = 1.0; diff --git a/duelinvaders/src/test/java/org/enstabretagne/Component/PlayerComponentTest.java b/duelinvaders/src/test/java/org/enstabretagne/Component/PlayerComponentTest.java index 9e4de69..db1c048 100644 --- a/duelinvaders/src/test/java/org/enstabretagne/Component/PlayerComponentTest.java +++ b/duelinvaders/src/test/java/org/enstabretagne/Component/PlayerComponentTest.java @@ -1,6 +1,7 @@ package org.enstabretagne.Component; -import org.enstabretagne.Core.Constant; +import org.enstabretagne.Utils.EntityType; +import org.enstabretagne.Utils.Settings; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; @@ -8,7 +9,7 @@ public class PlayerComponentTest { private PlayerComponent playerComponent; - private Double dx = Constant.SPEED_SPACESHIP; + private Double dx = Settings.SPEED_SPACESHIP; @BeforeEach public void setup() { diff --git a/duelinvaders/src/test/java/org/enstabretagne/Core/AlienBulletCollisionTest.java b/duelinvaders/src/test/java/org/enstabretagne/Core/AlienBulletCollisionTest.java index 9961e77..10d8669 100644 --- a/duelinvaders/src/test/java/org/enstabretagne/Core/AlienBulletCollisionTest.java +++ b/duelinvaders/src/test/java/org/enstabretagne/Core/AlienBulletCollisionTest.java @@ -6,8 +6,10 @@ import com.almasb.fxgl.entity.Entity; import com.almasb.fxgl.entity.GameWorld; +import org.enstabretagne.Collision.AlienBulletCollision; // import static org.junit.jupiter.api.Assertions.*; import org.enstabretagne.Component.*; +import org.enstabretagne.Utils.EntityType; // import static com.almasb.fxgl.dsl.FXGL.*; diff --git a/duelinvaders/src/test/java/org/enstabretagne/Core/AlienPlayerCollisionTest.java b/duelinvaders/src/test/java/org/enstabretagne/Core/AlienPlayerCollisionTest.java index dec722e..3acb28d 100644 --- a/duelinvaders/src/test/java/org/enstabretagne/Core/AlienPlayerCollisionTest.java +++ b/duelinvaders/src/test/java/org/enstabretagne/Core/AlienPlayerCollisionTest.java @@ -1,8 +1,9 @@ package org.enstabretagne.Core; +import org.enstabretagne.Collision.AlienPlayerCollision; import org.enstabretagne.Component.AlienComponent; -import org.enstabretagne.Component.EntityType; import org.enstabretagne.Component.PlayerComponent; +import org.enstabretagne.Utils.EntityType; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test;