diff --git a/src/ihm/Die.java b/src/ihm/Die.java new file mode 100644 index 0000000..00d69f5 --- /dev/null +++ b/src/ihm/Die.java @@ -0,0 +1,40 @@ +package ihm; + +//adaptation du code a la source : https://stackoverflow.com/questions/50021161/java-how-to-update-dice-image-when-button-clicked-and-equal-to-number-given +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; + +/** + * + * @author blj0011 + */ +public class Die { + ImageView dieFace; + Image[] images; + int currentFace; + + public Die(Image[] images) { + this.images = images; + dieFace = new ImageView(this.images[0]);// set default to image 0 + } + + public Die(Image[] images, int dieFaceValue) { + // Need to catch for values less than 1 and greater than 6! + this.images = images; + dieFace = new ImageView(this.images[dieFaceValue - 1]); + } + + public ImageView getdieFace() { + return dieFace; + } + + public int getCurrentFace() { + return currentFace; + } + + public void setDieFace(int dieFaceValue) { + // Need to catch for values less than 1 and greater than 6! + dieFace.setImage(this.images[dieFaceValue - 1]); + currentFace = dieFaceValue; + } +} \ No newline at end of file diff --git a/src/ihm/DieImages.java b/src/ihm/DieImages.java new file mode 100644 index 0000000..0839ecf --- /dev/null +++ b/src/ihm/DieImages.java @@ -0,0 +1,36 @@ +package ihm; + +//adaptation du code a la source : https://stackoverflow.com/questions/50021161/java-how-to-update-dice-image-when-button-clicked-and-equal-to-number-given + +import javafx.scene.image.Image; + +/** + * + * @author blj0011 + */ +public class DieImages { + final Image die1 = new Image(getClass().getResourceAsStream("ressources/img/des1.png")); + final Image die2 = new Image(getClass().getResourceAsStream("ressources/img/des2.png")); + final Image die3 = new Image(getClass().getResourceAsStream("ressources/img/des3.png")); + final Image die4 = new Image(getClass().getResourceAsStream("ressources/img/des4.png")); + final Image die5 = new Image(getClass().getResourceAsStream("ressources/img/des5.png")); + final Image die6 = new Image(getClass().getResourceAsStream("ressources/img/des6.png")); + + final Image[] images = new Image[6]; + + public DieImages() { + images[0] = die1; + images[1] = die2; + images[2] = die3; + images[3] = die4; + images[4] = die5; + images[5] = die6; + for (int i = 0; i < images.length; i++) { + + } + } + + public Image[] getImages() { + return images; + } +} \ No newline at end of file diff --git a/src/ihm/controller/JouerSonTour4Controller.java b/src/ihm/controller/JouerSonTour4Controller.java deleted file mode 100644 index a9d8064..0000000 --- a/src/ihm/controller/JouerSonTour4Controller.java +++ /dev/null @@ -1,20 +0,0 @@ -package ihm.controller; - -import java.net.URL; -import java.util.ResourceBundle; - -import javafx.fxml.FXML; -import javafx.scene.control.Label; -import main.Joueur; - -public class JouerSonTour4Controller extends LancerDes{ - @FXML private Label defenseur; - - private Joueur j; - - @Override - public void initialize(URL arg0, ResourceBundle arg1) { - super.initialize(arg0, arg1); - //defenseur.setText(j.getNom()); - } -} diff --git a/src/ihm/controller/LancerDes.java b/src/ihm/controller/LancerDes.java index 5a99d58..39601fb 100644 --- a/src/ihm/controller/LancerDes.java +++ b/src/ihm/controller/LancerDes.java @@ -1,57 +1,125 @@ package ihm.controller; -import java.net.URL; -import java.util.ResourceBundle; +import java.util.Random; -import javafx.fxml.FXML; -import javafx.fxml.Initializable; +import ihm.Die; +import ihm.DieImages; +import javafx.animation.KeyFrame; +import javafx.animation.Timeline; +import javafx.event.ActionEvent; +import javafx.geometry.Pos; import javafx.scene.control.Button; -import javafx.scene.control.Label; +import javafx.scene.image.ImageView; +import javafx.scene.layout.HBox; +import javafx.scene.layout.StackPane; +import javafx.scene.layout.VBox; +import javafx.scene.paint.Color; +import javafx.scene.text.Font; +import javafx.scene.text.Text; +import javafx.util.Duration; +import main.Contexte; +import main.GestionnaireJeu; -public class LancerDes implements Initializable{ - @FXML private Label d6; - @FXML private Label d4; - @FXML private Button btnStop; - @FXML private Button btnLancer; +public class LancerDes { + private int resultat; + private Contexte contexte; + public LancerDes(Contexte c){ + contexte = c; + } - private int[] valeurD6 = {1, 2, 3, 4, 5, 6}; - private int[] valeurD4 = {1, 2, 3, 4}; + public VBox initLancer() { + switch (contexte) { + case LANCER_DES_4: + return initLancerD4(); + case LANCER_DES_6: + return initLancerD6(); + default : + return null; + } + } - private int resultatD6; - private int resultatD4; - - private boolean lancement = true; - @Override - public void initialize(URL arg0, ResourceBundle arg1) { - // TODO Auto-generated method stub - btnStop.setVisible(false); - - btnLancer.setOnAction(e -> { - try { - btnLancer.setVisible(false); - btnStop.setVisible(true); - lancement(); - } catch (InterruptedException e1) { - e1.printStackTrace(); - } + private VBox initLancerD4() { + DieImages images = new DieImages(); + Die die = new Die(images.getImages()); + ImageView stackpane = die.getdieFace(); + stackpane.setFitHeight(100); + stackpane.setFitWidth(100); + Button btn = new Button(); + Text txt = new Text("Lancez le dés pour attaquer"); + txt.setFont(Font.font(null, null, null, 12)); + txt.setFill(Color.WHITE); + btn.setText("Lancer dés"); + btn.setOnAction((ActionEvent event) -> { + btn.setDisable(true);// Disable Button + Random random = new Random(); + Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(.3), (actionEvent) -> { + int tempRandom = random.nextInt(4) + 1; + die.setDieFace(tempRandom); + })); + + timeline.setCycleCount(random.nextInt(20) + 1); + timeline.play(); + timeline.setOnFinished(actionEvent -> { + int res = die.getCurrentFace(); + txt.setText("Vous avez obtenu "+res+"!"); + resultat = res; + GestionnaireJeu.notifyPlateau(); + }); }); - btnStop.setOnAction(e -> { - //à remplir avec les valeurs donné par le gestionnaire de jeux - lancement = false; - d6.setText(Integer.toString(resultatD6)); - d4.setText(Integer.toString(resultatD4)); - }); + HBox des = new HBox(stackpane); + des.setAlignment(Pos.CENTER); + VBox root = new VBox(txt,des, new StackPane(btn)); + root.setAlignment(Pos.CENTER); + root.setSpacing(20); + return root; } - public void lancement() throws InterruptedException { - /*int i=0; - while (lancement) { - d6.setText(Integer.toString(valeurD6[i%6])); - d4.setText(Integer.toString(valeurD4[i%4])); - i++; - //Thread.sleep(500); - }*/ + private VBox initLancerD6() { + DieImages images = new DieImages(); + Die die = new Die(images.getImages()); + Die die2 = new Die(images.getImages()); + ImageView stackpane = die.getdieFace(); + ImageView stackpane2 = die2.getdieFace(); + stackpane.setFitHeight(100); + stackpane2.setFitHeight(100); + stackpane.setFitWidth(100); + stackpane2.setFitWidth(100); + Button btn = new Button(); + Text txt = new Text("Lancez les dés pour vous deplacer"); + txt.setFont(Font.font(null, null, null, 12)); + txt.setFill(Color.WHITE); + btn.setText("Lancer dés"); + btn.setOnAction((ActionEvent event) -> { + btn.setDisable(true);// Disable Button + Random random = new Random(); + Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(.3), (actionEvent) -> { + int tempRandom = random.nextInt(6) + 1; + int tempRandom2 = random.nextInt(6) + 1; + die.setDieFace(tempRandom); + die2.setDieFace(tempRandom2); + })); + + timeline.setCycleCount(random.nextInt(20) + 1); + timeline.play(); + timeline.setOnFinished(actionEvent -> { + int res = die.getCurrentFace()+die2.getCurrentFace(); + txt.setText("Vous avez obtenu "+res+"!"); + resultat = res; + GestionnaireJeu.notifyPlateau(); + }); + }); + + HBox des = new HBox(stackpane, stackpane2); + des.setAlignment(Pos.CENTER); + VBox root = new VBox(txt,des, new StackPane(btn)); + root.setAlignment(Pos.CENTER); + root.setSpacing(20); + return root; + } + + public int getResult() { + return resultat; } } diff --git a/src/ihm/controller/PlateauController.java b/src/ihm/controller/PlateauController.java index 42388a4..02a5550 100644 --- a/src/ihm/controller/PlateauController.java +++ b/src/ihm/controller/PlateauController.java @@ -57,6 +57,7 @@ public class PlateauController implements Initializable { private ChoisirBoolean cb; private ChoisirEquipement ce; private ChoisirJoueur cj; + private LancerDes ld; @@ -208,8 +209,6 @@ public class PlateauController implements Initializable { } } - - private void applyImageLieu(ImageView iv, Image im) { StackPane sp = (StackPane) iv.getParent(); @@ -479,6 +478,22 @@ public class PlateauController implements Initializable { } + public Integer getChoixLancerDes(Joueur joueur) { + JoueurIHM jihm = getJoueurIHM(joueur); + int result = this.ld.getResult(); + this.ld = null; + jihm.getZoneJoueur().getChildren().setAll(); + return result; + } + + + public void afficherLancerDes(Joueur j, Contexte c) throws IOException { + this.ld=new LancerDes(c); + JoueurIHM jihm = getJoueurIHM(j); + jihm.setZoneJoueur(ld.initLancer()); + } + + public void afficherLT(Joueur j, CartePiochable cartePiochable) throws IOException { Image i = getImageCarte(cartePiochable); diff --git a/src/ihm/ressources/img/des1.png b/src/ihm/ressources/img/des1.png new file mode 100644 index 0000000..bb88d34 Binary files /dev/null and b/src/ihm/ressources/img/des1.png differ diff --git a/src/ihm/ressources/img/des2.png b/src/ihm/ressources/img/des2.png new file mode 100644 index 0000000..c65025f Binary files /dev/null and b/src/ihm/ressources/img/des2.png differ diff --git a/src/ihm/ressources/img/des3.png b/src/ihm/ressources/img/des3.png new file mode 100644 index 0000000..7ad9bda Binary files /dev/null and b/src/ihm/ressources/img/des3.png differ diff --git a/src/ihm/ressources/img/des4.png b/src/ihm/ressources/img/des4.png new file mode 100644 index 0000000..d8006e6 Binary files /dev/null and b/src/ihm/ressources/img/des4.png differ diff --git a/src/ihm/ressources/img/des5.png b/src/ihm/ressources/img/des5.png new file mode 100644 index 0000000..52f4475 Binary files /dev/null and b/src/ihm/ressources/img/des5.png differ diff --git a/src/ihm/ressources/img/des6.png b/src/ihm/ressources/img/des6.png new file mode 100644 index 0000000..77513e3 Binary files /dev/null and b/src/ihm/ressources/img/des6.png differ diff --git a/src/main/Contexte.java b/src/main/Contexte.java index d3cda40..9fe0354 100644 --- a/src/main/Contexte.java +++ b/src/main/Contexte.java @@ -5,6 +5,10 @@ public enum Contexte { ATTAQUER, VOLER_EQUIP, EFFET_NEGATIF_SUR_AUTRES, - EFFET_POSITIF_SUR_AUTRES, ACTIVER_EFFET_LIEU, EFFET_BOB + EFFET_POSITIF_SUR_AUTRES, + ACTIVER_EFFET_LIEU, + EFFET_BOB, + LANCER_DES_4, + LANCER_DES_6 } diff --git a/src/main/GestionnaireJeu.java b/src/main/GestionnaireJeu.java index 104f679..2ca066b 100644 --- a/src/main/GestionnaireJeu.java +++ b/src/main/GestionnaireJeu.java @@ -163,7 +163,37 @@ public class GestionnaireJeu { }); this.waitPlateau(); - } + } + + public int jouerDes(Joueur joueur, Contexte c) { + Platform.runLater(() -> { + try { + pc.afficherLancerDes(joueur, c); + } catch (IOException e) { + e.printStackTrace(); + } + }); + + this.waitPlateau(); + + final FutureTask query = new FutureTask(new Callable() { + @Override + public Integer call() throws Exception { + return pc.getChoixLancerDes(joueur); + } + }); + + Platform.runLater(query); + + try { + return query.get(); + } catch (InterruptedException | ExecutionException e) { + + e.printStackTrace(); + } + + return 1; + } public Joueur choisirJoueur(Joueur joueur, List joueurs, Contexte contexte) { diff --git a/src/main/Joueur.java b/src/main/Joueur.java index 6264fe4..ba95a6a 100644 --- a/src/main/Joueur.java +++ b/src/main/Joueur.java @@ -246,6 +246,10 @@ public class Joueur { return this.plateau.choisir(this, activerEffetLieu); } + public int lancerDes(Contexte typeLancer) { + return this.plateau.lancerDes(this, typeLancer); + } + public Object choisir(List list,Class cls) { return this.plateau.choisir(this,list, cls); } diff --git a/src/main/Plateau.java b/src/main/Plateau.java index 3436b57..5fe6964 100644 --- a/src/main/Plateau.java +++ b/src/main/Plateau.java @@ -266,8 +266,9 @@ public class Plateau extends Thread{ System.out.println(joueurs.size()); Joueur currentJoueur = this.joueurs.get(i % nbJoueurs); - currentJoueur.choisir(joueurs, Joueur.class); + int lancer = currentJoueur.lancerDes(Contexte.LANCER_DES_4); System.out.println("\n\n\n\n\n"); + System.out.println(lancer); System.out.println("Au tour de "+currentJoueur.getNom()); System.out.println("Lancement des dés."); deplacer(currentJoueur); @@ -476,4 +477,8 @@ public class Plateau extends Thread{ gj.retirerEquipement(joueur,e); } + + public int lancerDes(Joueur joueur, Contexte typeLancer) { + return gj.jouerDes(joueur, typeLancer); + } }