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