Skip to content

Commit

Permalink
Added Level Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomarrelli committed Feb 15, 2024
1 parent 353edee commit 847ecf4
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 31 deletions.
5 changes: 4 additions & 1 deletion src/main/java/pvzclone/PvzStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
/**
* Plants Vs Zombies Application's Entry Point.
*
* @author Sofia Caberletti, Margherita Zanchini, Sofia Lotti, Marco Marrelli.
* @author Sofia Caberletti
* @author Margherita Zanchini
* @author Sofia Lotti
* @author Marco Marrelli
*/
public final class PvzStart {

Expand Down
9 changes: 8 additions & 1 deletion src/main/java/pvzclone/controller/api/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ public interface Controller extends ViewEventListener {
* Retrieves the number of the chosen level if the level was chosen,
* if not returns an empty Optional.
*
* @return number of the level the we chose;
* @return number of the level the we chose.
*/
Optional<Integer> getChosenLevel();

/**
* Returns the number of levels available in the application.
*
* @return number of levels available in the application.
*/
int getLevelCount();
}
27 changes: 15 additions & 12 deletions src/main/java/pvzclone/controller/impl/ControllerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pvzclone.model.api.Game;
import pvzclone.model.api.World;
import pvzclone.model.impl.GameImpl;
import pvzclone.model.impl.LevelImpl;
import pvzclone.model.impl.LevelsManager;
import pvzclone.model.impl.Pair;
import pvzclone.model.impl.WorldImpl;
import pvzclone.view.api.View;
Expand All @@ -20,8 +20,9 @@
*/
public final class ControllerImpl implements Controller {

private static final long PERIOD = 80;

private static final long PERIOD = 60;
private static final int LEVEL_COUNT = 10;

private World world;
private View view;
private Game game;
Expand All @@ -30,6 +31,8 @@ public final class ControllerImpl implements Controller {
@Override
public void initGame() {
this.world = new WorldImpl();
this.world.setLevelManager(new LevelsManager(LEVEL_COUNT));

this.view = new SwingViewImpl(this);
this.chosenLevel = Optional.empty();
}
Expand All @@ -46,7 +49,7 @@ private void mainLoop() {
if (this.world == null || this.view == null) {
return;
}
this.world.setLevel(new LevelImpl());
this.world.setLevel(this.world.getLevelManager().getLevel(chosenLevel));
this.game = new GameImpl(this.world);
this.world.setGame(game);
long startTime = System.currentTimeMillis();
Expand Down Expand Up @@ -112,12 +115,12 @@ public Optional<Integer> getChosenLevel() {
return this.chosenLevel;
}

/*
* @Override
* public void notifyWorldEvent(WorldEvent ev) {
* //qua ho un dubbio, se è gia il model che gestisce le collisioni interne
* //già lui controlla che ci siano state cose
* //il controller lo comunica alla view?
* }
*/
@Override
public int getLevelCount() {
if (this.world.getLevelManager() == null) {
throw new IllegalStateException("There are no valid levels to load!");
}

return this.world.getLevelManager().getLevelCount();
}
}
39 changes: 39 additions & 0 deletions src/main/java/pvzclone/model/api/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,50 @@

/**
* This interface models a Level.
*
* @author Marco Marrelli
*/
public interface Level {
/**
* Returns the numbers of zombie in one level.
*
* @return the numbers of zombie in one level.
*/
int getZombieCount();

/**
* Returns the numbers of zombie waves in one level.
* The value is based on the zombie count.
*
* @return the numbers of zombie waves in one level.
*/
int getZombieWaveCount();

/**
* Returns the sun spawn rate.
*
* @return the sun spawn rate.
*/
long getSunSpawnRate();

/**
* Returns the zombie spawn rate.
*
* @return the zombie spawn rate.
*/
long getZombieSpawnRate();

/**
* Returns the sun spawn rate.
*
* @return the sun decrement spawn rate.
*/
long getSunSpawnRateDecrementRange();

/**
* Returns the zombie spawn rate.
*
* @return the zombie decrement spawn rate.
*/
long getZombieSpawnRateDecrementRange();
}
13 changes: 13 additions & 0 deletions src/main/java/pvzclone/model/api/World.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pvzclone.model.api;

import pvzclone.model.impl.LevelsManager;

/**
* Interface of the Model of the game.
* It contains methods to set the Level and Game that will
Expand All @@ -22,6 +24,13 @@ public interface World {
*/
void setGame(Game game);

/**
* Sets the level manager.
*
* @param levelsManager the level manager of the World.
*/
void setLevelManager(LevelsManager levelsManager);

/**
* @return the level.
*/
Expand All @@ -32,4 +41,8 @@ public interface World {
*/
Game getGame();

/**
* @return the level manager.
*/
LevelsManager getLevelManager();
}
28 changes: 20 additions & 8 deletions src/main/java/pvzclone/model/impl/GameImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pvzclone.view.impl.SwingViewImpl;

import java.util.HashSet;
import java.util.Random;

/**
* class that implements the interface Game.
Expand All @@ -21,13 +22,12 @@ public final class GameImpl implements Game {

// sun
private static final int HOUSE_X_POSITION = 150;
private static final long DELTA_TIME_SUN = 3000;
private static final int BULLET_SPEED = 10;
private static final int BULLET_SPEED = 15;

// zombie
private static final int DELTA_ZOMBIE = 10;
private static final long DELTA_TIME_ZOMBIE_START = 12_000;
private static final int DEC_ZOMBIE_TIME_GENERATE = 500;
private static final long DEC_ZOMBIE_TIME_GENERATE = 250;


// base plant
private static final int PLANT_COST = 100;
Expand All @@ -51,7 +51,11 @@ public final class GameImpl implements Game {

private long timeOfLastCreatedSun;
private long timeOfLastCreatedZombie;
private long deltaTimeZombie = DELTA_TIME_ZOMBIE_START;

private long deltaTimeSun;
private long deltaTimeZombie;
private long deltaTimeSunDecrement;
private long deltaTimeZombieDecrement;

/**
*
Expand All @@ -62,6 +66,11 @@ public GameImpl(final World world) {
this.gameState = new GameStateImpl(this.world.getLevel().getZombieCount());
this.sunFactory = new SunsFactory();
this.zombiesFactory = new ZombiesFactory();

this.deltaTimeSun = this.world.getLevel().getSunSpawnRate();
this.deltaTimeZombie = this.world.getLevel().getZombieSpawnRate();
this.deltaTimeSunDecrement = this.world.getLevel().getSunSpawnRateDecrementRange();
this.deltaTimeZombieDecrement = this.world.getLevel().getZombieSpawnRateDecrementRange();
}

@Override
Expand Down Expand Up @@ -114,9 +123,11 @@ private boolean hasDeltaTimePassed(final long previousTime, final long currentTi
}

private void newSunGenerate(final long currentTime) {
if (this.hasDeltaTimePassed(this.timeOfLastCreatedSun, currentTime, DELTA_TIME_SUN)) {
this.suns.add((SunImpl) this.sunFactory.createEntity());
if (this.hasDeltaTimePassed(this.timeOfLastCreatedSun, currentTime, this.deltaTimeSun)) {
this.timeOfLastCreatedSun = currentTime;
this.suns.add((SunImpl) this.sunFactory.createEntity());
final long deltaDecrement = new Random().nextLong((2 * this.deltaTimeSunDecrement)) - this.deltaTimeSunDecrement;
this.deltaTimeSun = this.deltaTimeSun - deltaDecrement;
}
}

Expand All @@ -125,7 +136,8 @@ private void newZombieGenerate(final long elapsed) {
&& this.gameState.getZombiesGenerated() < this.world.getLevel().getZombieCount()) {
this.timeOfLastCreatedZombie = elapsed;
this.zombies.add((Zombie) this.zombiesFactory.createEntity());
this.deltaTimeZombie = this.deltaTimeZombie - DEC_ZOMBIE_TIME_GENERATE;
final long deltaDecrement = new Random().nextLong((2 * this.deltaTimeZombieDecrement)) - this.deltaTimeZombieDecrement;
this.deltaTimeZombie = this.deltaTimeZombie - deltaDecrement;
this.gameState.incZombiesGenerated();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pvzclone/model/impl/GameStateImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
public final class GameStateImpl implements GameState {

private static final int INC_SUN = 25;
private final int INITIAL_SUNS = 100;
private final int totZombies;
private int killedZombies;
private int zombiesGenerated;
Expand All @@ -29,7 +30,7 @@ public final class GameStateImpl implements GameState {
public GameStateImpl(final int totZombies) {
this.totZombies = totZombies;
this.killedZombies = 0;
this.sunScore = 100;
this.sunScore = INITIAL_SUNS;
this.winState = Optional.empty();
}

Expand Down
58 changes: 54 additions & 4 deletions src/main/java/pvzclone/model/impl/LevelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,65 @@
/**
* Class that implements a Level.
*
* @author Sofia Lotti.
* @author Marco Marrelli
*/
public final class LevelImpl implements Level {
public class LevelImpl implements Level {

private static final int TOTAL_ZOMBIES = 5;
private final int zombieCount;
private final int zombieWaveCount;
private final long sunSpawnRate;
private final long zombieSpawnRate;
private final long sunSpawnRateDecrementRange;
private final long zombieSpawnRateDecrementRange;

public LevelImpl() {
this.zombieCount = LevelsManager.ZOMBIE_COUNT;
this.zombieWaveCount = 1;
this.sunSpawnRate = LevelsManager.SUN_SPAWN_RATE;
this.sunSpawnRateDecrementRange = LevelsManager.SUN_SPAWN_RATE_DECREMENT_RANGE;
this.zombieSpawnRate = LevelsManager.ZOMBIE_SPAWN_RATE;
this.zombieSpawnRateDecrementRange = LevelsManager.ZOMBIE_SPAWN_RATE_DECREMENT_RANGE;
}

public LevelImpl(final int zombieCount, final int zombieWaveCount,
final long sunSpawnRate, final long zombieSpawnRate,
final long sunSpawnRateDecrementRange,
final long zombieSpawnRateDecrementRange) {
this.zombieCount = zombieCount;
this.zombieWaveCount = zombieWaveCount;
this.sunSpawnRate = sunSpawnRate;
this.sunSpawnRateDecrementRange = sunSpawnRateDecrementRange;
this.zombieSpawnRate = zombieSpawnRate;
this.zombieSpawnRateDecrementRange = zombieSpawnRateDecrementRange;
}

@Override
public int getZombieCount() {
return TOTAL_ZOMBIES;
return this.zombieCount;
}

@Override
public int getZombieWaveCount() {
return this.zombieWaveCount;
}

@Override
public long getSunSpawnRate() {
return this.sunSpawnRate;
}

@Override
public long getZombieSpawnRate() {
return this.zombieSpawnRate;
}

@Override
public long getSunSpawnRateDecrementRange() {
return this.sunSpawnRateDecrementRange;
}

@Override
public long getZombieSpawnRateDecrementRange() {
return this.zombieSpawnRateDecrementRange;
}
}
Loading

0 comments on commit 847ecf4

Please sign in to comment.