mirror of
https://github.com/guezoloic/L3-racing-game.git
synced 2026-03-28 19:13:41 +00:00
feat: modification et correction decorator voiture
This commit is contained in:
@@ -36,6 +36,8 @@ public class BasicCar implements Car {
|
|||||||
|
|
||||||
private int movement = 1;
|
private int movement = 1;
|
||||||
|
|
||||||
|
private boolean isConsuming = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construit une nouvelle voiture.
|
* Construit une nouvelle voiture.
|
||||||
*
|
*
|
||||||
@@ -67,14 +69,12 @@ public class BasicCar implements Car {
|
|||||||
state = State.DAMAGED;
|
state = State.DAMAGED;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean decreaseDamage() {
|
private void decreaseDamage() {
|
||||||
if (state.isDamaged()) {
|
if (state.isDamaged()) {
|
||||||
if (--damageRound <= 0) {
|
if (--damageRound <= 0) {
|
||||||
state = state.onDamageEnd();
|
state = state.onDamageEnd();
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BasicCar setState(State state) {
|
public BasicCar setState(State state) {
|
||||||
@@ -134,11 +134,7 @@ public class BasicCar implements Car {
|
|||||||
* @param move nombre de positions à avancer
|
* @param move nombre de positions à avancer
|
||||||
* @return cette même instance (pour chaînage fluide)
|
* @return cette même instance (pour chaînage fluide)
|
||||||
*/
|
*/
|
||||||
public void move() {
|
private void move() {
|
||||||
if (decreaseDamage()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int jump = RANDOM.nextInt(state.MIN, state.MAX);
|
int jump = RANDOM.nextInt(state.MIN, state.MAX);
|
||||||
|
|
||||||
for (int i = 0; i < jump; i++) {
|
for (int i = 0; i < jump; i++) {
|
||||||
@@ -184,13 +180,21 @@ public class BasicCar implements Car {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean apply() {
|
public boolean apply() {
|
||||||
if (fuel > 0) {
|
if (state.isDamaged()) {
|
||||||
|
decreaseDamage();
|
||||||
|
} else if (fuel > 0) {
|
||||||
move();
|
move();
|
||||||
consumeFuel();
|
if (isConsuming)
|
||||||
|
consumeFuel();
|
||||||
}
|
}
|
||||||
return !hasFinished();
|
return !hasFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void consumption(boolean active) {
|
||||||
|
isConsuming = active;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reverse(boolean active) {
|
public void reverse(boolean active) {
|
||||||
movement = (active) ? -1 : 1;
|
movement = (active) ? -1 : 1;
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ package model.car;
|
|||||||
* Décorateur Sound :
|
* Décorateur Sound :
|
||||||
* affiche un message sonore quand la voiture accélère.
|
* affiche un message sonore quand la voiture accélère.
|
||||||
*/
|
*/
|
||||||
public class SoundCar extends CarDecorator {
|
public class BoostCar extends CarDecorator {
|
||||||
public SoundCar(Car car) {
|
public BoostCar(Car car) {
|
||||||
super(car);
|
super(car);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -10,25 +10,23 @@ public interface Car extends Observer {
|
|||||||
|
|
||||||
public String decelerate();
|
public String decelerate();
|
||||||
|
|
||||||
public void move();
|
|
||||||
|
|
||||||
public void reverse(boolean active);
|
public void reverse(boolean active);
|
||||||
|
|
||||||
public Color getColor();
|
public void consumption(boolean active);
|
||||||
|
|
||||||
public String getName();
|
public String getName();
|
||||||
|
|
||||||
public int getPosition();
|
public int getPosition();
|
||||||
|
|
||||||
public int getScore();
|
public int getScore();
|
||||||
|
|
||||||
public int getRound();
|
public int getRound();
|
||||||
|
|
||||||
public int getFuel();
|
public int getFuel();
|
||||||
|
|
||||||
public State getState();
|
public State getState();
|
||||||
|
public Color getColor();
|
||||||
|
|
||||||
public void setMap(Map map);
|
public void setMap(Map map);
|
||||||
|
|
||||||
public void consumeFuel();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,11 +21,6 @@ public abstract class CarDecorator implements Car {
|
|||||||
return car.decelerate();
|
return car.decelerate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void move() {
|
|
||||||
car.move();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean apply() {
|
public boolean apply() {
|
||||||
return car.apply();
|
return car.apply();
|
||||||
@@ -46,6 +41,11 @@ public abstract class CarDecorator implements Car {
|
|||||||
return car.getName();
|
return car.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void consumption(boolean active) {
|
||||||
|
car.consumption(active);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getPosition() {
|
public int getPosition() {
|
||||||
return car.getPosition();
|
return car.getPosition();
|
||||||
@@ -75,9 +75,4 @@ public abstract class CarDecorator implements Car {
|
|||||||
public void setMap(Map map) {
|
public void setMap(Map map) {
|
||||||
car.setMap(map);
|
car.setMap(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void consumeFuel() {
|
|
||||||
car.consumeFuel();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -13,6 +13,7 @@ public class HybridCar extends CarDecorator {
|
|||||||
|
|
||||||
public HybridCar(Car car) {
|
public HybridCar(Car car) {
|
||||||
super(car);
|
super(car);
|
||||||
|
car.consumption(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** pour afficher l'énergie dans le Dashboard */
|
/** pour afficher l'énergie dans le Dashboard */
|
||||||
@@ -20,31 +21,27 @@ public class HybridCar extends CarDecorator {
|
|||||||
return energy;
|
return energy;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
|
||||||
@Override
|
@Override
|
||||||
public boolean apply() {
|
public boolean apply() {
|
||||||
car.move();
|
if (!car.getState().isDamaged()) {
|
||||||
consumeFuel();
|
if (energy > 5) {
|
||||||
return true;
|
car.consumption(false);
|
||||||
}
|
energy -= 10;
|
||||||
|
if (energy < 0)
|
||||||
// TODO
|
energy = 0;
|
||||||
@Override
|
} else {
|
||||||
public void consumeFuel() {
|
car.consumption(true);
|
||||||
if (energy > 0) {
|
}
|
||||||
energy -= 10;
|
|
||||||
} else {
|
|
||||||
car.consumeFuel();
|
|
||||||
}
|
}
|
||||||
|
return car.apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String decelerate() {
|
public String decelerate() {
|
||||||
String msg = car.decelerate();
|
if (car.getState() != State.STOPPED) {
|
||||||
|
energy += (energy >= 100) ? 0 : 5;
|
||||||
|
}
|
||||||
|
|
||||||
if (energy <= 100)
|
return car.decelerate();
|
||||||
energy += 5;
|
|
||||||
|
|
||||||
return msg;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user