Phase 9 : ajout des personnages
This commit is contained in:
parent
33204404f6
commit
0a250955b2
8 changed files with 143 additions and 14 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package controleur;
|
||||
import modele.Jeu;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import controleur.GLOBAL;
|
||||
|
|
@ -94,6 +95,14 @@ public class Controle implements AsyncResponse {
|
|||
break;
|
||||
case "ajout panel murs":
|
||||
leJeu.envoi((Connection)info, this.frmArene.jpnMurs());
|
||||
break;
|
||||
case "ajout jlabel jeu":
|
||||
this.frmArene.ajoutLabelJeu((JLabel) info);
|
||||
this.frmArene.jpnJeu.repaint();
|
||||
break;
|
||||
case "ajout joueurs":
|
||||
leJeu.envoi((Connection)info, this.frmArene.getJpnJeu());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -101,6 +110,10 @@ public class Controle implements AsyncResponse {
|
|||
switch(ordre) {
|
||||
case "mettre murs":
|
||||
this.frmArene.setJpnMurs((JPanel)info);
|
||||
break;
|
||||
case "modif panel jeu":
|
||||
this.frmArene.setJpnJeu((JPanel)info);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,4 +14,8 @@ public interface GLOBAL {
|
|||
int tailleYArene = 590;
|
||||
int largeurMur = 34;
|
||||
int hauteurMur = 35;
|
||||
|
||||
int largeurPerso = 39;
|
||||
int hauteurPerso = 44;
|
||||
int hauteurMessage = 8;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import outils.connexion.Connection;
|
|||
public class JeuClient extends Jeu {
|
||||
|
||||
private Connection connection;
|
||||
private boolean mursOk;
|
||||
|
||||
/**
|
||||
* Controleur
|
||||
|
|
@ -28,8 +29,11 @@ public class JeuClient extends Jeu {
|
|||
|
||||
@Override
|
||||
public void reception(Connection connection, Object info) {
|
||||
if(info instanceof JPanel) {
|
||||
if(info instanceof JPanel && !mursOk) {
|
||||
controle.evenementJeuClient("mettre murs", info);
|
||||
mursOk = true;
|
||||
}else if(info instanceof JPanel && mursOk) {
|
||||
this.controle.evenementJeuClient("modif panel jeu", info);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ package modele;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
|
||||
import controleur.Controle;
|
||||
import controleur.GLOBAL;
|
||||
import outils.connexion.Connection;
|
||||
|
|
@ -33,7 +35,7 @@ public class JeuServeur extends Jeu {
|
|||
|
||||
@Override
|
||||
public void connexion(Connection connection) {
|
||||
this.lesJoueurs.put(connection, new Joueur());
|
||||
this.lesJoueurs.put(connection, new Joueur(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -44,7 +46,8 @@ public class JeuServeur extends Jeu {
|
|||
controle.evenementJeuServeur("ajout panel murs", connection);
|
||||
String pseudo = splitted[1];
|
||||
int numPerso = Integer.parseInt(splitted[2]);
|
||||
this.lesJoueurs.get(connection).initPerso(pseudo, numPerso);
|
||||
System.out.println(info);
|
||||
this.lesJoueurs.get(connection).initPerso(pseudo, numPerso, lesMurs, lesJoueurs.values());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -70,4 +73,14 @@ public class JeuServeur extends Jeu {
|
|||
}
|
||||
}
|
||||
|
||||
public void ajoutJLabelJeuArene(JLabel obj) {
|
||||
controle.evenementJeuServeur("ajout jlabel jeu", obj);
|
||||
}
|
||||
|
||||
public void envoiJeuATous() {
|
||||
for(Connection joueur : lesJoueurs.keySet()) {
|
||||
controle.evenementJeuServeur("ajout joueurs", joueur);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,16 @@
|
|||
package modele;
|
||||
|
||||
import java.awt.Font;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.SwingConstants;
|
||||
|
||||
import controleur.GLOBAL;
|
||||
|
||||
/**
|
||||
* Gestion des joueurs
|
||||
*
|
||||
|
|
@ -47,32 +59,67 @@ public class Joueur extends Objet {
|
|||
*/
|
||||
private int orientation ;
|
||||
|
||||
private JLabel message;
|
||||
|
||||
/**
|
||||
* Constructeur
|
||||
*/
|
||||
public Joueur() {
|
||||
public Joueur(JeuServeur jeuServeur) {
|
||||
this.jeuServeur = jeuServeur;
|
||||
|
||||
vie = MAXVIE;
|
||||
etape = 1;
|
||||
orientation = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialisation d'un joueur (pseudo et numéro, calcul de la 1ère position, affichage, création de la boule)
|
||||
*/
|
||||
public void initPerso(String pseudo, int numPerso) {
|
||||
public void initPerso(String pseudo, int numPerso, ArrayList<Mur> listeMurs, Collection<Joueur> joueurs) {
|
||||
this.pseudo = pseudo;
|
||||
this.numPerso = numPerso;
|
||||
|
||||
super.label = new JLabel();
|
||||
this.message = new JLabel();
|
||||
this.premierePosition(listeMurs, joueurs);
|
||||
jeuServeur.ajoutJLabelJeuArene(label);
|
||||
jeuServeur.ajoutJLabelJeuArene(message);
|
||||
this.affiche("marche", this.etape);
|
||||
|
||||
this.message.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
this.message.setFont(new Font("Arial", Font.PLAIN, 8));
|
||||
|
||||
System.out.println("joueur "+pseudo+" - num perso "+numPerso+" créé");
|
||||
}
|
||||
|
||||
/**
|
||||
* Calcul de la première position aléatoire du joueur (sans chevaucher un autre joueur ou un mur)
|
||||
*/
|
||||
private void premierePosition() {
|
||||
private void premierePosition(ArrayList<Mur> listeMurs, Collection<Joueur> joueurs) {
|
||||
this.getJLabel().setBounds(0, 0, GLOBAL.largeurPerso, GLOBAL.hauteurPerso);
|
||||
|
||||
do {
|
||||
posX = (int) Math.round(Math.random() * (GLOBAL.tailleXArene - GLOBAL.largeurPerso)) ;
|
||||
posY = (int) Math.round(Math.random() * (GLOBAL.tailleYArene - GLOBAL.hauteurPerso - GLOBAL.hauteurMessage)) ;
|
||||
System.out.println(posX + " " + posY);
|
||||
} while(this.toucheJoueur(joueurs) || this.toucheMur(listeMurs));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Affiche le personnage et son message
|
||||
*/
|
||||
public void affiche() {
|
||||
public void affiche(String etat, int etape) {
|
||||
super.label.setBounds(posX, posY, GLOBAL.largeurPerso, GLOBAL.hauteurPerso);
|
||||
String chemin = GLOBAL.cheminPersos + "perso" + this.numPerso + etat + etape + "d" + this.orientation + ".gif";
|
||||
URL ressource = getClass().getClassLoader().getResource(chemin);
|
||||
super.label.setIcon(new ImageIcon(ressource));
|
||||
|
||||
System.out.println(super.label.getBounds());
|
||||
this.message.setBounds(posX-10, posY+GLOBAL.hauteurPerso, GLOBAL.largeurPerso+10, GLOBAL.hauteurMessage);
|
||||
this.message.setText(pseudo + " : " + vie);
|
||||
|
||||
this.jeuServeur.envoiJeuATous();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -91,8 +138,13 @@ public class Joueur extends Objet {
|
|||
* Contrôle si le joueur touche un des autres joueurs
|
||||
* @return true si deux joueurs se touchent
|
||||
*/
|
||||
private Boolean toucheJoueur() {
|
||||
return null;
|
||||
private Boolean toucheJoueur(Collection<Joueur> joueurs) {
|
||||
for(Joueur player : joueurs) {
|
||||
if (this != player && this.toucheObjet(player)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -111,8 +163,13 @@ public class Joueur extends Objet {
|
|||
* Contrôle si le joueur touche un des murs
|
||||
* @return true si un joueur touche un mur
|
||||
*/
|
||||
private Boolean toucheMur() {
|
||||
return null;
|
||||
private Boolean toucheMur(ArrayList<Mur> listeMurs) {
|
||||
for(Mur mur : listeMurs) {
|
||||
if(this.toucheObjet(mur)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -26,8 +26,24 @@ public abstract class Objet {
|
|||
* @return true si les 2 objets se touchent
|
||||
*/
|
||||
public Boolean toucheObjet(Objet objet) {
|
||||
return null;
|
||||
int posXObj = objet.posX;
|
||||
int posYObj = objet.posY;
|
||||
int widthObj = objet.getJLabel().getWidth();
|
||||
int heightObj = objet.getJLabel().getHeight();
|
||||
|
||||
int width = this.getJLabel().getWidth();
|
||||
int height = this.getJLabel().getHeight();
|
||||
|
||||
if (this.posX + width > posXObj &&
|
||||
this.posX < posXObj + widthObj &&
|
||||
this.posY + height > posYObj &&
|
||||
this.posY < posYObj + heightObj) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public JLabel getJLabel() {
|
||||
return label;
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ public class Arene extends JFrame {
|
|||
private JPanel contentPane;
|
||||
private JTextField textField;
|
||||
private JPanel jpnMur;
|
||||
public JPanel jpnJeu;
|
||||
|
||||
/**
|
||||
* Launch the application.
|
||||
|
|
@ -58,6 +59,12 @@ public class Arene extends JFrame {
|
|||
System.out.println("❌ Image non trouvée !");
|
||||
}
|
||||
|
||||
jpnJeu = new JPanel();
|
||||
jpnJeu.setBounds(0, 0, 800, 590);
|
||||
contentPane.add(jpnJeu);
|
||||
jpnJeu.setOpaque(false);
|
||||
jpnJeu.setLayout(null);
|
||||
|
||||
jpnMur = new JPanel();
|
||||
jpnMur.setOpaque(false);
|
||||
jpnMur.setBounds(0, 0, 800, 590);
|
||||
|
|
@ -95,4 +102,19 @@ public class Arene extends JFrame {
|
|||
this.jpnMur.add(jpnMurs);
|
||||
this.jpnMur.repaint();
|
||||
}
|
||||
|
||||
public void ajoutLabelJeu(JLabel object) {
|
||||
this.jpnJeu.add(object);
|
||||
this.jpnJeu.repaint();
|
||||
}
|
||||
|
||||
public JPanel getJpnJeu() {
|
||||
return jpnJeu;
|
||||
}
|
||||
|
||||
public void setJpnJeu(JPanel jpnJeu) {
|
||||
this.jpnJeu.removeAll();
|
||||
this.jpnJeu.add(jpnJeu);
|
||||
this.jpnJeu.repaint();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ public class ChoixJoueur extends JFrame {
|
|||
JOptionPane.showMessageDialog(null, "La saisie du pseudo est obligatoire !");
|
||||
txtPseudo.requestFocus();
|
||||
}else {
|
||||
ctrl.evenementChoixJoueur(txtPseudo.getText(), nbPerso);
|
||||
ctrl.evenementChoixJoueur(txtPseudo.getText(), persoNum);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Reference in a new issue