mirror of
https://github.com/guezoloic/L3-racing-game.git
synced 2026-03-31 04:11:33 +00:00
feat: enlever gameview observer et ajout dans l'observer de base
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import java.awt.Color;
|
||||
import java.awt.GraphicsEnvironment;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
@@ -22,11 +21,14 @@ public class Game
|
||||
public final String name;
|
||||
/** Couleur de la voiture */
|
||||
public final Color color;
|
||||
/** Visuel de la voiture */
|
||||
private final boolean visual;
|
||||
|
||||
public CarInfo(String name, Color color)
|
||||
public CarInfo(String name, Color color, boolean visual)
|
||||
{
|
||||
this.name = name;
|
||||
this.color = color;
|
||||
this.visual = visual;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +45,7 @@ public class Game
|
||||
private ArrayList<CarInfo> cars = new ArrayList<>();
|
||||
/** État initial du jeu */
|
||||
private State state = new State();
|
||||
/** Temps entre chaque "step" du jeu (en millisecondes) */
|
||||
/** Temps entre chaque step du jeu */
|
||||
private int time = 1000;
|
||||
/** Carte sur laquelle se déroule le jeu */
|
||||
private Map map = null;
|
||||
@@ -115,18 +117,6 @@ public class Game
|
||||
/** Indique si le jeu est en pause */
|
||||
private boolean isPaused = false;
|
||||
|
||||
/**
|
||||
* Bloc statique exécuté au chargement de la classe pour vérifier
|
||||
* si le programme dispose d'un environnement graphique.
|
||||
*/
|
||||
static {
|
||||
if (GraphicsEnvironment.isHeadless())
|
||||
{
|
||||
System.err.println("Aucun serveur d'affichage trouvé");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructeur principal.
|
||||
*
|
||||
@@ -157,8 +147,8 @@ public class Game
|
||||
private Game init(ArrayList<CarInfo> carInfos)
|
||||
{
|
||||
// Création des vues principales
|
||||
new Track(map, cars, "Piste Formule 1", 1000, 500, 1, 1);
|
||||
new Rankboard("Score", cars, 200, 200, 0, 510);
|
||||
new Track(this, "Piste Formule 1", 1000, 500, 1, 1);
|
||||
new Rankboard(this, "Score", 200, 200, 0, 510);
|
||||
|
||||
final int loop = map.getPathSize();
|
||||
|
||||
@@ -167,12 +157,12 @@ public class Game
|
||||
for (CarInfo ci : carInfos)
|
||||
{
|
||||
Car car = new Car(ci.name, ci.color, loop, state);
|
||||
new Dashboard(car, car.toString(), this::togglePause, 300, 200, 1000, 200*i);
|
||||
String name = car.getName();
|
||||
new Dashboard(this, car, name, 300, 200, 1000, 200*i);
|
||||
cars.add(car);
|
||||
i++;
|
||||
}
|
||||
|
||||
GameView.update();
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -230,10 +220,23 @@ public class Game
|
||||
{
|
||||
if (isPaused) notifyAll();
|
||||
isPaused = !isPaused;
|
||||
GameView.update();
|
||||
return isPaused;
|
||||
}
|
||||
|
||||
public void notifyObservers()
|
||||
{
|
||||
for (GObserver o : obs)
|
||||
{
|
||||
boolean isSuccess = o.apply();
|
||||
System.out.println(o.getClass());
|
||||
if (!isSuccess)
|
||||
{
|
||||
System.err.println("Une erreur s'est produite pendant le jeu.");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exécute un cycle du jeu
|
||||
* <p>
|
||||
@@ -254,15 +257,7 @@ public class Game
|
||||
}
|
||||
|
||||
car.run();
|
||||
|
||||
for (GObserver o : obs)
|
||||
{
|
||||
if (!o.apply())
|
||||
{
|
||||
System.err.println("Une erreur s'est produite pendant le jeu.");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
notifyObservers();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -280,7 +275,6 @@ public class Game
|
||||
try
|
||||
{
|
||||
step();
|
||||
GameView.update();
|
||||
Thread.sleep(time);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
@@ -289,4 +283,24 @@ public class Game
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<Car> getCars()
|
||||
{
|
||||
return cars;
|
||||
}
|
||||
|
||||
public Map getMap()
|
||||
{
|
||||
return map;
|
||||
}
|
||||
|
||||
public State getState()
|
||||
{
|
||||
return state;
|
||||
}
|
||||
|
||||
public boolean getPause()
|
||||
{
|
||||
return isPaused;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user