mirror of
https://github.com/guezoloic/L3-racing-game.git
synced 2026-03-28 19:13:41 +00:00
feat(Car): nouveau constructor et fonction reverse
- ajout nouveau constructor pour clone la voiture - ajout de la fonction reversing pour faire revenir en arriere la voiture
This commit is contained in:
47
src/Car.java
47
src/Car.java
@@ -16,7 +16,7 @@ import java.util.Random;
|
|||||||
*/
|
*/
|
||||||
public class Car {
|
public class Car {
|
||||||
/** Générateur de nombres aléatoires pour simuler la progression */
|
/** Générateur de nombres aléatoires pour simuler la progression */
|
||||||
private static final Random RANDOM = new Random();
|
protected static final Random RANDOM = new Random();
|
||||||
|
|
||||||
/** Couleur de la voiture pour l'affichage */
|
/** Couleur de la voiture pour l'affichage */
|
||||||
private final Color COLOR;
|
private final Color COLOR;
|
||||||
@@ -31,6 +31,8 @@ public class Car {
|
|||||||
/** Carburant restant */
|
/** Carburant restant */
|
||||||
private int fuel = 60;
|
private int fuel = 60;
|
||||||
|
|
||||||
|
private boolean isReversing = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construit une nouvelle voiture.
|
* Construit une nouvelle voiture.
|
||||||
*
|
*
|
||||||
@@ -44,6 +46,12 @@ public class Car {
|
|||||||
this.MAP = map;
|
this.MAP = map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Car(Car other) {
|
||||||
|
this.NAME = other.NAME;
|
||||||
|
this.COLOR = other.COLOR;
|
||||||
|
this.MAP = other.MAP;
|
||||||
|
}
|
||||||
|
|
||||||
public static enum State {
|
public static enum State {
|
||||||
/**
|
/**
|
||||||
* L'état NORMAL du Vehicule avance selon un chiffre au alentour de 1 à 6 cases
|
* L'état NORMAL du Vehicule avance selon un chiffre au alentour de 1 à 6 cases
|
||||||
@@ -207,11 +215,14 @@ public class Car {
|
|||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Clique sur "Accelerer" : change d'état et retourne un message (si nécessaire). */
|
/**
|
||||||
|
* Clique sur "Accelerer" : change d'état et retourne un message (si
|
||||||
|
* nécessaire).
|
||||||
|
*/
|
||||||
public String accelerate() {
|
public String accelerate() {
|
||||||
// Si endommagée => l'énoncé dit qu'on ne peut pas bouger
|
// Si endommagée => l'énoncé dit qu'on ne peut pas bouger
|
||||||
if (state.isDamaged()) {
|
if (state.isDamaged()) {
|
||||||
return "Voiture endommagée : impossible d'accélérer";
|
return "Voiture endommagée : impossible d'accélérer";
|
||||||
}
|
}
|
||||||
State next = state.accelerate();
|
State next = state.accelerate();
|
||||||
|
|
||||||
@@ -223,7 +234,10 @@ public class Car {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Clique sur "Rallentir" : change d'état et retourne un message (si nécessaire). */
|
/**
|
||||||
|
* Clique sur "Rallentir" : change d'état et retourne un message (si
|
||||||
|
* nécessaire).
|
||||||
|
*/
|
||||||
public String decelerate() {
|
public String decelerate() {
|
||||||
if (state.isDamaged()) {
|
if (state.isDamaged()) {
|
||||||
return "Voiture endommagée : impossible de ralentir";
|
return "Voiture endommagée : impossible de ralentir";
|
||||||
@@ -258,24 +272,31 @@ public class Car {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int jump = RANDOM.nextInt(state.MIN, state.MAX);
|
int jump = RANDOM.nextInt(state.MIN, state.MAX);
|
||||||
|
int direction = (isReversing) ? -1 : 1;
|
||||||
|
|
||||||
for (int i = 0; i < jump; i++) {
|
for (int i = 0; i < jump; i++) {
|
||||||
pos = state.move(pos, 1);
|
pos = state.move(pos, direction);
|
||||||
|
|
||||||
Point point = MAP.getPath(pos);
|
Point point = MAP.getPath(pos);
|
||||||
Map.Circuit element = MAP.getElement(point.x, point.y);
|
Map.Circuit element = MAP.getElement(point.x, point.y);
|
||||||
|
|
||||||
if (element.isYRoad() && element.getValue() <= jump) {
|
if (hasAccident(element, jump)) {
|
||||||
System.out.println(NAME + " a un\taccident");
|
System.out.println(NAME + " a un\taccident");
|
||||||
setDamage();
|
setDamage();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
round = pos / MAP.getPathSize();
|
||||||
if (pos >= MAP.getPathSize()) {
|
|
||||||
pos = 0;
|
|
||||||
round++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isReversing = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean hasAccident(Map.Circuit element, int jump) {
|
||||||
|
return element.isYRoad() && element.getValue() <= jump;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reverse() {
|
||||||
|
isReversing = !isReversing;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -324,4 +345,4 @@ public class Car {
|
|||||||
public String getName() {
|
public String getName() {
|
||||||
return NAME;
|
return NAME;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user