mirror of
https://github.com/guezoloic/L3-racing-game.git
synced 2026-03-31 12:21:34 +00:00
feat: ajout Menu selection
This commit is contained in:
@@ -39,19 +39,19 @@ public class Game {
|
||||
|
||||
public Builder rankboard(String title, int width,
|
||||
int height, int x, int y) {
|
||||
this.OBSERVERS.add(new Rankboard(null, title, width, height, x, y));
|
||||
this.OBSERVERS.add(new RankboardView(null, title, width, height, x, y));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder track(String title, int width,
|
||||
int height, int x, int y) {
|
||||
this.OBSERVERS.add(new Track(null, title, width, height, x, y));
|
||||
this.OBSERVERS.add(new TrackView(null, title, width, height, x, y));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder dashboard(Car car, String title, int width,
|
||||
int height, int x, int y) {
|
||||
this.OBSERVERS.add(new Dashboard(null, car, title, width, height, x, y));
|
||||
this.OBSERVERS.add(new DashboardView(null, car, title, width, height, x, y));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -122,7 +122,7 @@ public class BasicCar implements Car {
|
||||
state = next;
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fait avancer la voiture d'un certain nombre de positions.
|
||||
* <p>
|
||||
@@ -130,73 +130,78 @@ public class BasicCar implements Car {
|
||||
* incrémenté et
|
||||
* la position revient à zéro.
|
||||
* </p>
|
||||
*
|
||||
* @param move nombre de positions à avancer
|
||||
* @return cette même instance (pour chaînage fluide)
|
||||
*/
|
||||
private void move() {
|
||||
int jump = RANDOM.nextInt(state.MIN, state.MAX);
|
||||
|
||||
for (int i = 0; i < jump; i++) {
|
||||
pos = state.move(pos, movement);
|
||||
|
||||
Point point = map.getPath(pos);
|
||||
Circuit element = map.getElement(point.x, point.y);
|
||||
|
||||
if (hasAccident(element, jump)) {
|
||||
setDamage();
|
||||
return;
|
||||
*
|
||||
* @param move nombre de positions à avancer
|
||||
* @return cette même instance (pour chaînage fluide)
|
||||
*/
|
||||
private void move() {
|
||||
int jump = RANDOM.nextInt(state.MIN, state.MAX);
|
||||
|
||||
for (int i = 0; i < jump; i++) {
|
||||
pos = state.move(pos, movement);
|
||||
|
||||
Point point = map.getPath(pos);
|
||||
Circuit element = map.getElement(point.x, point.y);
|
||||
|
||||
if (hasAccident(element, jump)) {
|
||||
setDamage();
|
||||
return;
|
||||
}
|
||||
round = pos / map.getPathSize();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean hasAccident(Circuit element, int jump) {
|
||||
return element.isYRoad() && element.getValue() <= jump;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Consomme du carburant en fonction de l'état du jeu.
|
||||
*
|
||||
* @return cette même instance pour chaînage
|
||||
*/
|
||||
public void consumeFuel() {
|
||||
fuel = state.fuelConsumption(fuel);
|
||||
if (fuel < 0)
|
||||
fuel = 0; // sécurité
|
||||
}
|
||||
*
|
||||
* @return cette même instance pour chaînage
|
||||
*/
|
||||
public void consumeFuel() {
|
||||
fuel = state.fuelConsumption(fuel);
|
||||
if (fuel < 0)
|
||||
fuel = 0; // sécurité
|
||||
}
|
||||
|
||||
public boolean hasFinished() {
|
||||
return 3 < round;
|
||||
}
|
||||
public boolean hasFinished() {
|
||||
return 3 < round;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exécute une "étape" de la voiture : avance d'une position aléatoire et
|
||||
* consomme du carburant.
|
||||
* <p>
|
||||
* La progression est déterminée par un intervalle aléatoire fourni par l'état
|
||||
* du jeu.
|
||||
* </p>
|
||||
*/
|
||||
@Override
|
||||
public boolean apply() {
|
||||
if (state.isDamaged()) {
|
||||
decreaseDamage();
|
||||
} else if (fuel > 0) {
|
||||
move();
|
||||
if (isConsuming)
|
||||
consumeFuel();
|
||||
}
|
||||
return !hasFinished();
|
||||
/**
|
||||
* Exécute une "étape" de la voiture : avance d'une position aléatoire et
|
||||
* consomme du carburant.
|
||||
* <p>
|
||||
* La progression est déterminée par un intervalle aléatoire fourni par l'état
|
||||
* du jeu.
|
||||
* </p>
|
||||
*/
|
||||
@Override
|
||||
public boolean apply() {
|
||||
if (state.isDamaged()) {
|
||||
decreaseDamage();
|
||||
} else if (fuel > 0) {
|
||||
move();
|
||||
if (isConsuming)
|
||||
consumeFuel();
|
||||
}
|
||||
return !hasFinished();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void consumption(boolean active) {
|
||||
isConsuming = active;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void consumption(boolean active) {
|
||||
isConsuming = active;
|
||||
public Car remove() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reverse(boolean active) {
|
||||
@Override
|
||||
public void reverse(boolean active) {
|
||||
movement = (active) ? -1 : 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,18 +13,21 @@ public interface Car extends Observer {
|
||||
public void reverse(boolean active);
|
||||
|
||||
public void consumption(boolean active);
|
||||
|
||||
|
||||
public Car remove();
|
||||
|
||||
public String getName();
|
||||
|
||||
|
||||
public int getPosition();
|
||||
|
||||
|
||||
public int getScore();
|
||||
|
||||
|
||||
public int getRound();
|
||||
|
||||
|
||||
public int getFuel();
|
||||
|
||||
|
||||
public State getState();
|
||||
|
||||
public Color getColor();
|
||||
|
||||
public void setMap(Map map);
|
||||
|
||||
@@ -11,6 +11,11 @@ public abstract class CarDecorator implements Car {
|
||||
this.car = car;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Car remove() {
|
||||
return car;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String accelerate() {
|
||||
return car.accelerate();
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package model.car;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* DrunkCar = décorateur "pilote ivre".
|
||||
*
|
||||
@@ -14,8 +12,7 @@ import java.util.Random;
|
||||
* sans modifier la classe Car.
|
||||
*/
|
||||
public class DrunkCar extends CarDecorator {
|
||||
/** Générateur de nombres aléatoires pour simuler la progression */
|
||||
protected static final Random RANDOM = new Random();
|
||||
private boolean reverse = false;
|
||||
|
||||
public DrunkCar(Car car) {
|
||||
super(car);
|
||||
@@ -25,9 +22,9 @@ public class DrunkCar extends CarDecorator {
|
||||
// 50% : fait l'inverse
|
||||
@Override
|
||||
public boolean apply() {
|
||||
car.reverse(RANDOM.nextBoolean());
|
||||
car.reverse(reverse);
|
||||
car.apply();
|
||||
car.reverse(false);
|
||||
reverse = !reverse;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
80
src/model/car/Selection.java
Normal file
80
src/model/car/Selection.java
Normal file
@@ -0,0 +1,80 @@
|
||||
package model.car;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import model.Game;
|
||||
|
||||
public class Selection {
|
||||
private final Game.Builder gameBuilder;
|
||||
private final List<Car> cars = new ArrayList<>();
|
||||
|
||||
public Selection(Game.Builder gameBuilder) {
|
||||
this.gameBuilder = gameBuilder;
|
||||
}
|
||||
|
||||
public List<Car> getCars() {
|
||||
return new ArrayList<>(cars);
|
||||
}
|
||||
|
||||
public Car addCar(String name, Color color) {
|
||||
Car car = new BasicCar(name, color);
|
||||
cars.add(car);
|
||||
return car;
|
||||
}
|
||||
|
||||
public void removeCar(Car car) {
|
||||
cars.remove(car);
|
||||
}
|
||||
|
||||
private Car addDecorator(Function<Car, Car> fn, Car car) throws IllegalArgumentException {
|
||||
int index = cars.indexOf(car);
|
||||
|
||||
if (index < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Aucune voiture dans la classe interne.");
|
||||
}
|
||||
|
||||
car = fn.apply(car);
|
||||
cars.set(index, car);
|
||||
return car;
|
||||
}
|
||||
|
||||
public Car addHybridCarDecorator(Car car) throws IllegalArgumentException {
|
||||
return addDecorator(HybridCar::new, car);
|
||||
}
|
||||
|
||||
public Car addDrunkCarDecorator(Car car) throws IllegalArgumentException {
|
||||
return addDecorator(DrunkCar::new, car);
|
||||
}
|
||||
|
||||
public Car addBoostCarDecorator(Car car) throws IllegalArgumentException {
|
||||
return addDecorator(BoostCar::new, car);
|
||||
}
|
||||
|
||||
public Car removeDecorator(Car car) throws IllegalArgumentException {
|
||||
int index = cars.indexOf(car);
|
||||
|
||||
if (index < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Aucune voiture dans la classe interne.");
|
||||
}
|
||||
car = car.remove();
|
||||
cars.set(index, car);
|
||||
return car;
|
||||
}
|
||||
|
||||
public Game.Builder select() {
|
||||
for (Car car : cars) {
|
||||
gameBuilder.car(car);
|
||||
System.out.println("hello world");
|
||||
}
|
||||
return gameBuilder;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return cars.size();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user