Skip to content

Commit

Permalink
Cleaned and refactored GameLauncher
Browse files Browse the repository at this point in the history
Created a GameMode file for defining all game modes (enum).
  • Loading branch information
LBF38 committed Jan 3, 2023
1 parent 23e72dd commit 2b52451
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
40 changes: 20 additions & 20 deletions duelinvaders/src/main/java/org/enstabretagne/Game/GameLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -106,7 +112,6 @@ protected void initSettings(GameSettings settings) {
*/
@Override
protected void initInput() {

onKey(KeyCode.ENTER, () -> {
playerComponent1.shoot();
});
Expand All @@ -120,23 +125,23 @@ protected void initInput() {
});

onKey(KeyCode.SPACE, () -> {
if (GameMode == 2) {
if (GameMode == SOLO) {
playerComponent1.shoot();
} else {
playerComponent2.shoot();
}
});

onKey(KeyCode.D, () -> {
if (GameMode == 2) {
if (GameMode == SOLO) {
playerComponent1.moveRight();
} else {
playerComponent2.moveRight();
}
});

onKey(KeyCode.Q, () -> {
if (GameMode == 2) {
if (GameMode == SOLO) {
playerComponent1.moveLeft();
} else {
playerComponent2.moveLeft();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
}

Expand Down Expand Up @@ -239,7 +243,6 @@ private void makeAlienLine(int line, Constant.Direction direction) {
alien.getComponent(AlienComponent.class).initialize(direction);
alien.getComponent(AlienComponent.class).setAlienNumber(i);
}

}
}

Expand All @@ -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());
Expand Down Expand Up @@ -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))
Expand All @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions duelinvaders/src/main/java/org/enstabretagne/Game/GameMode.java
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> EXPLOSIONS = new ArrayList<String>() {
Expand Down

0 comments on commit 2b52451

Please sign in to comment.