Skip to content
This repository was archived by the owner on Apr 17, 2021. It is now read-only.

Pause&end game menu #35

Merged
merged 3 commits into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified images/WavePanel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/blurtest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/currentlevel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lostTab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pauseTab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pauseTemporaryFile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/quitToMenuButton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/quitToMenuButton_hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/resumeButton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/resumeButton_hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/wonTab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion src/warcraftTD/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public World(int width, int height, MainMenu menu){

public abstract void drawPath();

public abstract int update();
public abstract int update() throws UnsupportedAudioFileException, IOException, LineUnavailableException;

public abstract void mouseClick(double x, double y, int mouseButton) throws UnsupportedAudioFileException, IOException, LineUnavailableException;

Expand All @@ -154,6 +154,8 @@ public void run() throws UnsupportedAudioFileException, IOException, LineUnavail
this.needReleaseMouse = false;
}


this.updateEvent();
this.update();
StdDraw.show();

Expand All @@ -165,6 +167,10 @@ public void run() throws UnsupportedAudioFileException, IOException, LineUnavail
endWorld();
}

public void updateEvent() throws UnsupportedAudioFileException, IOException, LineUnavailableException {

}

public void endWorld() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
this.menu.setNeedReleaseMouse(true);
this.menu.run();
Expand Down
133 changes: 118 additions & 15 deletions src/warcraftTD/WorldGame.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package warcraftTD;

import warcraftTD.hud.InterfaceEndGame;
import warcraftTD.hud.InterfaceGame;
import warcraftTD.hud.InterfacePause;
import warcraftTD.hud.MainMenu;
import warcraftTD.libs.StdDraw;
import warcraftTD.monsters.Monster;
Expand All @@ -12,23 +14,31 @@

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.awt.*;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

public class WorldGame extends World {
// l'ensemble des monstres, pour gerer (notamment) l'affichage
private List<Monster> monsters = new ArrayList<Monster>();
private int totalMonsterAmount;


private boolean needKeyRelease;
private boolean pause;
// l'ensemble des cases du chemin
private List<Position> paths;

private Image pauseBackground;
private Image levelBackground;

// L'interface du jeu
private InterfaceGame HUD;
private InterfacePause hudPause;
private InterfaceEndGame hudEnd;

// le porte monnaie du joueur
private Wallet player_wallet;
Expand Down Expand Up @@ -57,6 +67,12 @@ public class WorldGame extends World {

String musicPath;

private StateGame currentStateGame;

private enum StateGame{
Game, Pause, End
}

/**
* Initialisation du monde en fonction de la largeur, la hauteur et le nombre de cases données
*
Expand Down Expand Up @@ -154,6 +170,21 @@ public WorldGame(int nbSquareX, int nbSquareY, int money, int health, boolean di
this.totalMonsterAmount = this.waves.get(0).monsterAmount();
}

this.needKeyRelease = false;
this.pause = false;
this.initTerrain();
this.currentStateGame = StateGame.Game;
}

public void initTerrain(){
this.drawBackground();
if (this.displayWater) {
this.drawWater();
}
this.drawPath();
StdDraw.show();
StdDraw.save("images/currentlevel.png");
this.levelBackground = StdDraw.pictureNget(0.5,0.5,"images/currentlevel.png",1.0,1.0);
}

/**
Expand Down Expand Up @@ -347,7 +378,17 @@ public void drawWater() {
*/
@Override
public void drawInfos() {
this.HUD.updateInterface(StdDraw.mouseX(), StdDraw.mouseY(), this.getDelta_time());
switch(currentStateGame){
case Game:
this.HUD.updateInterface(StdDraw.mouseX(), StdDraw.mouseY(), this.getDelta_time());
break;
case Pause:
this.hudPause.updateInterface(StdDraw.mouseX(), StdDraw.mouseY(), this.getDelta_time());
break;
case End:
this.hudEnd.updateInterface(StdDraw.mouseX(), StdDraw.mouseY(), this.getDelta_time());
break;
}
}

/**
Expand Down Expand Up @@ -422,8 +463,7 @@ public void updateTowers() {
}
}


public void updateWave() {
public void updateWave() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
if (this.waves.size() > 0) {
Wave currentWave = this.waves.get(0);
if (currentWave.getCurrentTimeBeforeStartingSpawn() > 0) {
Expand All @@ -440,27 +480,36 @@ public void updateWave() {
}
}

public void drawLevel(){
StdDraw.picture(0.5, 0.5, "images/currentlevel.png", 1.0, 1.0);
}

/**
* Met à jour toutes les informations du plateau de jeu ainsi que les déplacements des monstres et les attaques des tours.
*
* @return les points de vie restants du joueur
*/
@Override
public int update() {
this.drawBackground();
if (this.displayWater) {
this.drawWater();
public int update() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
switch(currentStateGame){
case Game:
this.drawLevel();
this.updateWave();
this.updateMonsters();
this.updateTowers();
this.drawMouse();
this.drawInfos();
break;
case Pause:
case End:
StdDraw.picture(0.5, 0.5, "images/pauseTemporaryFile.png", 1.0, 1.0);
StdDraw.picture(0.5, 0.5, "images/blurtest.png", 1.0, 1.0);
this.drawInfos();
break;
}
this.drawPath();
this.updateWave();
this.updateMonsters();
this.updateTowers();
this.drawMouse();
this.drawInfos();
return this.life;
}


/**
* Vérifie lorsque l'utilisateur clique sur sa souris qu'il peut:
* - Ajouter une tour à la position indiquée par la souris.
Expand All @@ -477,6 +526,15 @@ public void mouseClick(double x, double y, int mouseButton) throws UnsupportedAu
Position p = new Position(normalizedX, normalizedY);
Position mousep = new Position((int) ((normalizedX * this.getNbSquareX())), (int) ((normalizedY * this.getNbSquareY())));

switch (currentStateGame) {
case Pause:
this.hudPause.onClick(x,y, mouseButton);
return;
case End:
this.hudEnd.onClick(x, y, mouseButton);
break;
}

if (this.HUD.onClick(x, y, mouseButton)) return;

if (this.building_class != null && !this.isNeedReleaseMouse()) {
Expand Down Expand Up @@ -519,6 +577,25 @@ public void stopBuilding() {
this.setNeedReleaseMouse(true);
}

public void pressingEscape() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
this.needKeyRelease = true;
if(this.currentStateGame == StateGame.Game){

this.currentStateGame = StateGame.Pause;

if(this.hudPause==null) this.hudPause = new InterfacePause(this);
this.hudPause.getBox().showBox(0.8,0.0,1.5);
StdDraw.save("images/pauseTemporaryFile.png");
this.pauseBackground = StdDraw.pictureNget(0.5,0.5,"images/pauseTemporaryFile.png",1.0,1.0);

} else exitPause();
}

public void exitPause(){
this.currentStateGame = StateGame.Game;
if(this.pauseBackground!=null) this.pauseBackground.flush();
}

/**
* Récupère la touche entrée au clavier ainsi que la position de la souris et met à jour le plateau en fonction de ces interractions
*/
Expand All @@ -535,6 +612,32 @@ public void run() throws UnsupportedAudioFileException, IOException, LineUnavail
super.run();
}

public void endGame(boolean win) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
StdDraw.save("images/pauseTemporaryFile.png");
this.pauseBackground = StdDraw.pictureNget(0.5,0.5,"images/pauseTemporaryFile.png",1.0,1.0);
this.hudEnd = new InterfaceEndGame(this, win);
this.hudEnd.getBox().showBox(0.8,0.0,1.0);
this.currentStateGame = StateGame.End;
}

@Override
public void endWorld() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
if(this.levelBackground!=null) this.levelBackground.flush();
if(this.pauseBackground!=null) this.pauseBackground.flush();
super.endWorld();
}

@Override
public void updateEvent() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
if (StdDraw.isKeyPressed(27)) {
if(!needKeyRelease) pressingEscape();
} else if(this.needKeyRelease) this.needKeyRelease = false;
if(currentStateGame == StateGame.Game){
if(this.life<=0) endGame(false);
if(this.waves.size()==0) endGame(this.life>0);
}
}

public List<Monster> getMonsters() {
return this.monsters;
}
Expand Down
33 changes: 29 additions & 4 deletions src/warcraftTD/hud/HorizontalGroupBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class HorizontalGroupBox extends ClickableElement {
private double fromy;
private double fromx;
private boolean forward_anim;
private final double speed;
private double speed;
private boolean moving;

private final Position initialPos;
private String background = "";
Expand Down Expand Up @@ -83,8 +84,14 @@ public void removeHUDElement(Element element) {
public void update(double MouseX, double MouseY, double delta_time) {
if (this.isVisible()) {

if (this.deltay > 0.0) this.deltay -= this.speed * delta_time;
if (this.deltax > 0.0) this.deltax -= this.speed * delta_time;
if (this.deltay > 0.0) {
this.deltay -= this.speed * delta_time;
if(this.deltay<0.0) this.deltay = 0.0;
}
if (this.deltax > 0.0){
this.deltax -= this.speed * delta_time;
if(this.deltax<0.0) this.deltax = 0.0;
}

if (this.forward_anim)
this.setPosition(new Position(this.initialPos.getX() - this.deltax, this.initialPos.getY() - this.deltay));
Expand All @@ -99,9 +106,14 @@ public void update(double MouseX, double MouseY, double delta_time) {
while (i.hasNext()) {
el = i.next();
if(this.deltay > 0.0 || this.deltax > 0.0) el.element.setPosition(new Position((this.getPosition().getX() - (this.getWidth() / 2) + el.relativepos.getX() * this.getWidth()), (this.getPosition().getY() - (this.getHeight() / 2) + el.relativepos.getY() * this.getHeight())));
else if(this.moving) {
el.element.setPosition(new Position((this.getPosition().getX() - (this.getWidth() / 2) + el.relativepos.getX() * this.getWidth()), (this.getPosition().getY() - (this.getHeight() / 2) + el.relativepos.getY() * this.getHeight())));
}
el.element.update(MouseX, MouseY, delta_time);
}

if(this.moving && !(this.deltay > 0.0 || this.deltax > 0.0)) this.moving = false;

if(this.garbage.size()>0){
i = this.garbage.iterator();
while (i.hasNext()) {
Expand Down Expand Up @@ -142,14 +154,27 @@ public void initialUpdateRelativePosition(){
}
}

public void ShowBox(double fromy, double fromx) {
public void showBox(double fromy, double fromx) {
this.deltax = fromx;
this.deltay = fromy;
this.fromy = fromy;
this.fromx = fromx;
this.setVisible(true);
this.forward_anim = true;
if(deltax==0.0 && deltay==0.0) initialUpdateRelativePosition();
else this.moving = true;
}

public void showBox(double fromy, double fromx, double speed) {
this.speed = speed;
this.deltax = fromx;
this.deltay = fromy;
this.fromy = fromy;
this.fromx = fromx;
this.setVisible(true);
this.forward_anim = true;
if(deltax==0.0 && deltay==0.0) initialUpdateRelativePosition();
else this.moving = true;
}

public void HideBox() {
Expand Down
7 changes: 4 additions & 3 deletions src/warcraftTD/hud/InterfaceEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public void startBuilding(TypeBuildEditor type){
}

public void stopBuilding(){
this.settingsBox.ShowBox(0.0,0.0);
this.settingsBox.showBox(0.0,0.0);
this.building_text.setVisible(false);
this.building_type = TypeBuildEditor.None;
}
Expand Down Expand Up @@ -428,7 +428,7 @@ public void makeAction(String action, Element from) throws UnsupportedAudioFileE
this.toggleBottomToolbar(false);
this.world.setNeedReleaseMouse(true);

this.settingsBox.ShowBox(0.0,0.0);
this.settingsBox.showBox(0.0,0.0);
break;
case "ClosingSettings":
this.toggleBottomToolbar(true);
Expand Down Expand Up @@ -534,7 +534,7 @@ public void makeAction(String action, Element from) throws UnsupportedAudioFileE
break;
case "wave":
toggleBottomToolbar(false);
this.waveBox.ShowBox(1.0,0.0);
this.waveBox.showBox(1.0,0.0);
this.world.setNeedReleaseMouse(true);
break;
case "addWave":
Expand Down Expand Up @@ -666,6 +666,7 @@ public void makeAction(String action, Element from) throws UnsupportedAudioFileE
this.world.setNeedReleaseMouse(true);
break;
case "saveMap":
if(this.world.getPaths().size()<2) return;
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Specify a file to save");
FileNameExtensionFilter filter = new FileNameExtensionFilter("Tower Defense Level", "tdl", "tdl");
Expand Down
Loading