|
@@ -1,8 +1,3 @@
|
|
|
-/*
|
|
|
- * To change this license header, choose License Headers in Project Properties.
|
|
|
- * To change this template file, choose Tools | Templates
|
|
|
- * and open the template in the editor.
|
|
|
- */
|
|
|
package bs;
|
|
|
|
|
|
import bswfx.BomberStudent;
|
|
@@ -13,17 +8,35 @@ import org.json.JSONObject;
|
|
|
import wfx.WebFx;
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
- * @author loquicom
|
|
|
+ * Gestion des parties
|
|
|
*/
|
|
|
public class Game {
|
|
|
|
|
|
+ /**
|
|
|
+ * Liste des joueurs avec comme clef leur id
|
|
|
+ */
|
|
|
protected HashMap<Integer, Player> player = new HashMap<>();
|
|
|
+ /**
|
|
|
+ * L'id du joueur principale
|
|
|
+ */
|
|
|
protected int mainPlayer;
|
|
|
+ /**
|
|
|
+ * Largeur de la map
|
|
|
+ */
|
|
|
protected int width;
|
|
|
+ /**
|
|
|
+ * Hauteur de la map
|
|
|
+ */
|
|
|
protected int height;
|
|
|
+ /**
|
|
|
+ * La map
|
|
|
+ */
|
|
|
protected String map[][];
|
|
|
|
|
|
+ /**
|
|
|
+ * Création d'une partie à partir du JSON du serveur
|
|
|
+ * @param jo Le JSON du serveur
|
|
|
+ */
|
|
|
public Game(JSONObject jo) {
|
|
|
//Recup la map
|
|
|
JSONObject joMap = jo.getJSONObject("map");
|
|
@@ -52,12 +65,23 @@ public class Game {
|
|
|
}
|
|
|
|
|
|
/* --- Méthodes jeu --- */
|
|
|
+ /**
|
|
|
+ * Ajoute un joueur dans la partie
|
|
|
+ * @param id Id du joueur
|
|
|
+ * @param pos Position du joueur
|
|
|
+ */
|
|
|
public void join(int id, String pos) {
|
|
|
Player p = new Player(id, pos);
|
|
|
this.player.put(id, p);
|
|
|
this.map[p.getX()][p.getY()] = "" + id;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Déplace un joueur
|
|
|
+ * @param player Id du joueur
|
|
|
+ * @param dir La direction du déplacement
|
|
|
+ * @return Reussite
|
|
|
+ */
|
|
|
public boolean move(int player, int dir) {
|
|
|
//Recup le joueur
|
|
|
Player p = this.player.get(player);
|
|
@@ -136,7 +160,13 @@ public class Game {
|
|
|
p.setY(y);
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Ajoute uene bombe sur le terrain
|
|
|
+ * @param type Le type de la bombe
|
|
|
+ * @param coord Coordonnées de la bombe
|
|
|
+ * @return Reussite
|
|
|
+ */
|
|
|
public boolean newBomb(int type, String coord) {
|
|
|
//Recup la position
|
|
|
int pos[] = this.parseCoord(coord);
|
|
@@ -157,6 +187,10 @@ public class Game {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Retire une bombe du terrain
|
|
|
+ * @param coord Position de la bombe
|
|
|
+ */
|
|
|
public void removeBomb(String coord) {
|
|
|
int pos[] = this.parseCoord(coord);
|
|
|
if (this.map[pos[0]][pos[1]].equals("b") || this.map[pos[0]][pos[1]].equals("m") || this.map[pos[0]][pos[1]].equals("r")) {
|
|
@@ -164,6 +198,12 @@ public class Game {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Ajoute un objet sur le terrain
|
|
|
+ * @param type Le type de l'objet
|
|
|
+ * @param coord La position de l'objet
|
|
|
+ * @return Reussite
|
|
|
+ */
|
|
|
public boolean newObject(int type, String coord) {
|
|
|
//Recup la position
|
|
|
int pos[] = this.parseCoord(coord);
|
|
@@ -208,6 +248,11 @@ public class Game {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Récupération d'un objet sur le terrain
|
|
|
+ * @param type Le type de l'objet
|
|
|
+ * @return Reussite
|
|
|
+ */
|
|
|
public boolean getObject(int type) {
|
|
|
JSONObject param = new JSONObject();
|
|
|
//Creation json en fonction type
|
|
@@ -286,6 +331,10 @@ public class Game {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Met à jour la carte
|
|
|
+ * @param mapContent La carte en une ligne
|
|
|
+ */
|
|
|
public void updateMap(String mapContent) {
|
|
|
String map[][] = this.parseMap(mapContent);
|
|
|
//Compare les 2 cartes
|
|
@@ -298,6 +347,10 @@ public class Game {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Met à jour la vie
|
|
|
+ * @param newLife La nouvelle valeur
|
|
|
+ */
|
|
|
public void updateLife(int newLife) {
|
|
|
Player p = this.player.get(this.mainPlayer);
|
|
|
//Si non invincible
|
|
@@ -310,6 +363,11 @@ public class Game {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Quitte une partie
|
|
|
+ * @param id Id du joueur qui quitte
|
|
|
+ * @return Reussite
|
|
|
+ */
|
|
|
public boolean quit(int id) {
|
|
|
//Recup joueur
|
|
|
Player p = this.player.get(id);
|
|
@@ -322,6 +380,9 @@ public class Game {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Ecran de game over
|
|
|
+ */
|
|
|
public void gameOver(){
|
|
|
//Quitte la partie
|
|
|
BomberStudent.bsc.send("POST", "game/quit");
|
|
@@ -334,6 +395,10 @@ public class Game {
|
|
|
BomberStudent.menu.show();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Affiche les explosions des bombes
|
|
|
+ * @param explo JSON avec les coordonnées des explosions
|
|
|
+ */
|
|
|
public void explosion(JSONArray explo){
|
|
|
int x, y;
|
|
|
JSONObject pos;
|
|
@@ -360,10 +425,13 @@ public class Game {
|
|
|
t.start();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Supprime les explosions du terrain
|
|
|
+ */
|
|
|
public void removeExplosion(){
|
|
|
for (int i = 0; i < height; i++) {
|
|
|
for (int j = 0; j < width; j++) {
|
|
|
- if(this.map[j][i].charAt(0) == 'e'){
|
|
|
+ while(this.map[j][i].charAt(0) == 'e'){
|
|
|
this.map[j][i] = this.map[j][i].substring(1);
|
|
|
}
|
|
|
}
|
|
@@ -371,6 +439,10 @@ public class Game {
|
|
|
}
|
|
|
|
|
|
/* --- Méthodes affichage --- */
|
|
|
+ /**
|
|
|
+ * Genere l'HTML pour l'affichage
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public String toHtml() {
|
|
|
Player pl = this.player.get(this.mainPlayer);
|
|
|
String html = "<!DOCTYPE html><html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"file:" + System.getProperty("user.dir") + "/file/css/map.css\"></head><body><div id=\"game\">";
|
|
@@ -508,6 +580,9 @@ public class Game {
|
|
|
return html;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Affiche la map dans le terminal
|
|
|
+ */
|
|
|
public void showMap() {
|
|
|
for (int i = 0; i < height; i++) {
|
|
|
for (int j = 0; j < width; j++) {
|
|
@@ -535,6 +610,11 @@ public class Game {
|
|
|
}
|
|
|
|
|
|
/* --- Methodes privée --- */
|
|
|
+ /**
|
|
|
+ * Parse la map 1D en 2D
|
|
|
+ * @param mapContent
|
|
|
+ * @return
|
|
|
+ */
|
|
|
private String[][] parseMap(String mapContent) {
|
|
|
String[][] map = new String[this.width][this.height];
|
|
|
for (int i = 0; i < this.height; i++) {
|
|
@@ -545,6 +625,11 @@ public class Game {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Parse les coordonnées
|
|
|
+ * @param coord Coordonnées
|
|
|
+ * @return 0 => X, 1 => Y
|
|
|
+ */
|
|
|
private int[] parseCoord(String coord) {
|
|
|
int res[] = new int[2];
|
|
|
//Cherche le separateur
|