From 2b52451d72da3e6fafd8766fa6668fb708ecb685 Mon Sep 17 00:00:00 2001 From: LBF38 Date: Tue, 3 Jan 2023 03:36:27 +0100 Subject: [PATCH] Cleaned and refactored GameLauncher Created a GameMode file for defining all game modes (enum). --- .../org/enstabretagne/Game/GameLauncher.java | 40 +++++++++---------- .../java/org/enstabretagne/Game/GameMode.java | 11 +++++ .../org/enstabretagne/Utils/assetNames.java | 1 + 3 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 duelinvaders/src/main/java/org/enstabretagne/Game/GameMode.java diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/GameLauncher.java b/duelinvaders/src/main/java/org/enstabretagne/Game/GameLauncher.java index 49e5dc0..f669c3d 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Game/GameLauncher.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Game/GameLauncher.java @@ -14,12 +14,19 @@ 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 org.enstabretagne.Game.GameMode.CLASSIQUE; +import static org.enstabretagne.Game.GameMode.INFINITY_MODE; +import static org.enstabretagne.Game.GameMode.SOLO; import java.util.Arrays; import java.util.EnumSet; import java.util.Map; -import org.enstabretagne.Component.*; +import org.enstabretagne.Component.AlienComponent; +import org.enstabretagne.Component.EntityType; +import org.enstabretagne.Component.LifeComponent; +import org.enstabretagne.Component.PlayerComponent; +import org.enstabretagne.Component.SpaceInvadersFactory; import org.enstabretagne.Core.AlienBulletCollision; import org.enstabretagne.Core.AlienPlayerCollision; import org.enstabretagne.Core.BulletBulletCollision; @@ -60,8 +67,7 @@ public class GameLauncher extends GameApplication { 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 int GameMode = 2; // 0 -> classique, 1 -> InfinityMode, 2->Solo + private GameMode GameMode = CLASSIQUE; /** * Initialisation des paramètres du jeu @@ -73,7 +79,7 @@ protected void initSettings(GameSettings settings) { settings.setWidth(Constant.GAME_WIDTH.intValue()); settings.setHeight(Constant.GAME_HEIGHT.intValue()); settings.setTitle("Duel Invaders"); - settings.setAppIcon("duelinvaders_icon2.png"); + settings.setAppIcon(assetNames.textures.APP_ICON); settings.setVersion("0.2.0"); settings.setMainMenuEnabled(true); settings.setGameMenuEnabled(true); @@ -106,7 +112,6 @@ protected void initSettings(GameSettings settings) { */ @Override protected void initInput() { - onKey(KeyCode.ENTER, () -> { playerComponent1.shoot(); }); @@ -120,7 +125,7 @@ protected void initInput() { }); onKey(KeyCode.SPACE, () -> { - if (GameMode == 2) { + if (GameMode == SOLO) { playerComponent1.shoot(); } else { playerComponent2.shoot(); @@ -128,7 +133,7 @@ protected void initInput() { }); onKey(KeyCode.D, () -> { - if (GameMode == 2) { + if (GameMode == SOLO) { playerComponent1.moveRight(); } else { playerComponent2.moveRight(); @@ -136,7 +141,7 @@ protected void initInput() { }); onKey(KeyCode.Q, () -> { - if (GameMode == 2) { + if (GameMode == SOLO) { playerComponent1.moveLeft(); } else { playerComponent2.moveLeft(); @@ -174,7 +179,7 @@ protected void initGame() { playerComponent1 = player1.getComponent(PlayerComponent.class); playerComponent1.setDirection(Constant.Direction.UP); - if (GameMode != 2) { + if (GameMode != SOLO) { // spawn Player2 player2 = spawn(entityNames.PLAYER); player2.setX(Constant.GAME_WIDTH / 2); @@ -183,9 +188,8 @@ protected void initGame() { playerComponent2.setDirection(Constant.Direction.DOWN); } - if (GameMode == 1) { + 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); @@ -199,9 +203,9 @@ protected void initGame() { alien.getComponent(AlienComponent.class).initialize(Constant.Direction.DOWN); }, Duration.seconds(1.5)); - } else if (GameMode == 0) { + } else if (GameMode == CLASSIQUE) { makeAlienBlock(); - } else if (GameMode == 2) { + } else if (GameMode == SOLO) { makeAlienBlockSolo(); } @@ -239,7 +243,6 @@ private void makeAlienLine(int line, Constant.Direction direction) { alien.getComponent(AlienComponent.class).initialize(direction); alien.getComponent(AlienComponent.class).setAlienNumber(i); } - } } @@ -260,8 +263,7 @@ private void makeAlienBlockSolo() { protected void initPhysics() { getPhysicsWorld().addCollisionHandler(new AlienPlayerCollision()); getPhysicsWorld().addCollisionHandler(new AlienBulletCollision()); - getPhysicsWorld() - .addCollisionHandler(new EnemyShootPlayerCollision()); + getPhysicsWorld().addCollisionHandler(new EnemyShootPlayerCollision()); getPhysicsWorld().addCollisionHandler(new BulletPlayerCollision()); getPhysicsWorld().addCollisionHandler(new BulletBulletCollision()); getPhysicsWorld().addCollisionHandler(new EnemyShootBulletCollision()); @@ -297,14 +299,12 @@ protected void onUpdate(double tpf) { if (getb(GameVariableNames.isGameWon)) winScreen(); - // teste le temps écoulé depuis la dernière fois que le son d'ambiance a été - // joué 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); } - Life_Update(); + updateLife(); run(() -> { getGameWorld().getEntitiesByType(EntityType.ALIEN).forEach((alien) -> { if (FXGLMath.randomBoolean(0.01)) @@ -313,7 +313,7 @@ protected void onUpdate(double tpf) { }, Duration.seconds(Constant.random.nextDouble() * 10)); } - private void Life_Update() { + private void updateLife() { int life_number = geti(GameVariableNames.PLAYERS_LIVES); if (life_number == 3) { life1.getComponent(LifeComponent.class).updateLife(false); diff --git a/duelinvaders/src/main/java/org/enstabretagne/Game/GameMode.java b/duelinvaders/src/main/java/org/enstabretagne/Game/GameMode.java new file mode 100644 index 0000000..d44282b --- /dev/null +++ b/duelinvaders/src/main/java/org/enstabretagne/Game/GameMode.java @@ -0,0 +1,11 @@ +package org.enstabretagne.Game; + +/** + * Modes de jeu disponibles + * + * @author LBF38, MathieuDFS, jufch + * @since 0.2.0 + */ +public enum GameMode { + CLASSIQUE, INFINITY_MODE, SOLO +} diff --git a/duelinvaders/src/main/java/org/enstabretagne/Utils/assetNames.java b/duelinvaders/src/main/java/org/enstabretagne/Utils/assetNames.java index 7f8d81a..0cf661c 100644 --- a/duelinvaders/src/main/java/org/enstabretagne/Utils/assetNames.java +++ b/duelinvaders/src/main/java/org/enstabretagne/Utils/assetNames.java @@ -21,6 +21,7 @@ public static class textures { public static final String ECLAT = "eclat.png"; public static final String ECLAT2 = "eclat2.png"; public static final String BACKGROUND = "background.png"; + public static final String APP_ICON = "duelinvaders_icon2.png"; public static final String EXPLOSION_PLAYER = "explosion_player.png"; public static final String EXPLOSION_FINAL = "explosion_final.png"; public static final ArrayList EXPLOSIONS = new ArrayList() {