leendert/src/main/java/nl/minkema/leendert/Player.java

146 lines
5.2 KiB
Java

package nl.minkema.leendert;
import javafx.animation.TranslateTransition;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.util.Duration;
import java.util.Random;
/**
* @author J0QUE
* @version 1.0
*/
public class Player {
private static int who = 0;
private final Image image;
private final ImageView view;
private final int ch;
private final int[] add;
private int plaats = 0;
private int[] coords = Game.locations[plaats];
/**
* De constructor voor een speler
*
* @param image de afbeelding die bij de speler hoort
* @param ch het hoeveelste karakter de speler is.
*/
public Player(Image image, int ch) {
this.ch = ch;
Random r = new Random(System.currentTimeMillis());
this.image = image;
this.view = new ImageView(this.image);
//Elke speler staat niet precies op het zelfde punt, ziet er wat leuker uit
this.add = new int[]{r.nextInt(10) - 5 - image.widthProperty().intValue() / 2, r.nextInt(10) - 5 - image.heightProperty().intValue() / 2};// new int[]{r.nextInt(10) - 5, r.nextInt(10) - 5};
coords = new int[]{coords[0] + add[0], coords[1] + add[1]};
getImageView().setTranslateX(coords[0]);
getImageView().setTranslateY(coords[1]);
}
/**
* Een methode om de volgende speler te verkrijgen
* @return het nummer van de volgende speler, niet verwarren met het character id.
*/
public static int getNextActive() {
who++;
if (who >= Game.players.size()) {
return who = 0;
}
return who;
}
//Dit zorgt dat de vorige speler geselecteerd wordt
public static void four() {
who -= 1;
}
//Dit is de afbeelding die bij de speler hoort
public ImageView getImageView() {
return view;
}
public int getCh() {
return ch;
}
/**
* Dit is de methode die wordt gebruikt om de speler te verplaatsen
* @param roll de hoeveelheid die is gegeooid.
*/
public void move(int roll) {
//De standaard tekst
Alert alert = new Alert(Alert.AlertType.NONE, "Je hebt " + roll + " gegooid!", ButtonType.OK);
//Als er 4 is gegooid mag je nog een keer, dus passen we de tekst aan
if (roll == 4)
alert.contentTextProperty().setValue("Je hebt " + roll + " gegooid!\nKlavertje 4! Je mag dus nog een keer!");
//We willen geen titel
alert.titleProperty().setValue("");
//Zolang er geen waterval of klimop is laten we het zien
if (!Game.specials.containsKey(plaats + roll))
alert.show();
//We gaan alleen niet over 100 heen...
if (roll + plaats > 100) {
return;
}
//We gebruiken een TranslateTransition om de speler te verplaatsen
TranslateTransition tt = new TranslateTransition(Duration.millis(200), getImageView());
tt.setFromX(coords[0]);
tt.setFromY(coords[1]);
tt.setToX(coords[0] = Game.locations[plaats + 1][0] + add[0]);
tt.setToY(coords[1] = Game.locations[plaats + 1][1] + add[1]);
TranslateTransition last = tt;
//Dit is voor alles hoger dan 1
for (int i = plaats + 2; i < roll + plaats + 1; i++) {
TranslateTransition tl = new TranslateTransition(Duration.millis(200), getImageView());
tl.setFromX(coords[0]);
tl.setFromY(coords[1]);
tl.setToX(coords[0] = Game.locations[i][0] + add[0]);
tl.setToY(coords[1] = Game.locations[i][1] + add[1]);
//Als we meer dan 1 beweging willen moet dat er na afgespeeld worden, anders gaan ze tegelijk
last.setOnFinished(e -> tl.play());
last = tl;
}
plaats += roll;
//Als er een waterval of klimop is
if (Game.specials.containsKey(plaats)) {
TranslateTransition tl = new TranslateTransition(Duration.millis(200), getImageView());
tl.setFromX(coords[0]);
tl.setFromY(coords[1]);
tl.setToX(coords[0] = Game.locations[Game.specials.get(plaats)][0] + add[0]);
tl.setToY(coords[1] = Game.locations[Game.specials.get(plaats)][1] + add[1]);
last.setOnFinished(e -> tl.play());
String text;
//We passen de tekst aan
if (plaats < Game.specials.get(plaats)) {
text = alert.getContentText() + "\n\nKlimop!\nJe mag " + (Game.specials.get(plaats) - plaats) + " plaatsen vooruit!";
} else {
text = alert.getContentText() + "\n\nWaterval!\nJe moet " + (plaats - Game.specials.get(plaats)) + " plaatsen terug!";
}
Alert alert1 = new Alert(Alert.AlertType.NONE, text, ButtonType.OK);
alert1.show();
//Laten we de plaats van de speler ook even aanpassen
plaats = Game.specials.get(plaats);
}
tt.play();
//Bij 100 heeft er iemand gewonnen!
if (plaats == 100) {
System.out.println("Een speler heeft gewonnen!");
Alert alert2 = new Alert(Alert.AlertType.WARNING, "Speler " + Menu.names[getCh()], ButtonType.OK);
alert2.show();
}
}
}