Select Git revision
-
Maxime Wallart authoredMaxime Wallart authored
Unite.java 6.47 KiB
package units;
import java.util.List;
import java.util.Random;
import events.Evenements;
import items.Items;
import main.Case;
import main.Joueur;
import main.Plateau;
import main.Type;
public abstract class Unite {
private int id;
private Plateau plateau;
private char symbol;
private int armor;
private int damage;
private int x;
private int y;
private int vision;
public static int generalId = 1;
private Joueur joueur;
private Items item;
private Type temp = Type.GRASS;
private Type temp2 = null;
public Unite(int x, int y, Plateau plateau, char symbol, int armor, int damage, Joueur joueur, int vision) {
this.id = generalId;
generalId++;
this.plateau = plateau;
this.symbol = symbol;
this.armor = armor;
this.damage = damage;
this.x = x;
this.y = y;
this.joueur = joueur;
this.joueur.ajoutUnit(this);
this.item = null;
this.vision = vision;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Plateau getPlateau() {
return plateau;
}
public char getSymbol() {
return symbol;
}
public void setSymbol(char symbol) {
this.symbol = symbol;
}
public int getArmor() {
return armor;
}
public void setArmor(int armor) {
this.armor = armor;
}
public int getDamage() {
return damage;
}
public Joueur getJoueur() {
return joueur;
}
public void setJoueur(Joueur joueur) {
this.joueur = joueur;
}
public void setDamage(int damage) {
this.damage = damage;
}
public Items getItem() {
return item;
}
public void setItem(Items item) {
this.item = item;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public static void resetNumbering() {
generalId = 0;
}
public boolean moove(Case unitCase) {
return false;
}
public boolean updatePosition(int x,int y) {
if (x==33 || x==1 || y==29 || y==33 || y==0) {
return false;
}
if(x>=0 && x<((plateau.getPlateau().length*plateau.getRegion(0, 0).region.length)-1) && y>=0 && y<(plateau.getPlateau().length*plateau.getRegion(0, 0).region.length)-1 ) {
if(this.plateau.getCase(x, y).getType() != Type.MOUNTAIN ) {
temp2 = temp;
temp = this.plateau.getCase(x, y).getType();
this.plateau.getCase(x, y).setUnite(this);
System.out.println(temp);
this.plateau.getCase(x, y).setType(Type.UNITE);
if(this.plateau.getCase(x, y).getEvents().size() > 0){
List<Evenements> temp = this.plateau.getCase(x, y).getEvents();
for (Evenements evenements : temp) {
evenements.action(this.plateau.getCase(x, y));
}
}
return true;
}
}
return false;
}
//à voir le type
public void updateItem() {
Case unitcase = plateau.getCase(x,y);
if(this.getItem() == null && unitcase.getItem() != null) {
unitcase.getItem().recup(joueur,unitcase.getItem(),this);
}
}
public boolean move(char c) {
if(c == 's') {
if (plateau.getCase(x+1,y).getUnite() != null && plateau.getCase(x+1, y).getUnite().getJoueur() != this.joueur) {
return false;
}
if (updatePosition(x+1,y)) {
this.plateau.getCase(x, y).setUnite(null);
this.plateau.getCase(x, y).setType(temp2);
this.x+=1;
updateDecouverte(this.plateau.getCase(x+1, y), this.vision);
updateItem();
return true;
}
}
else if(c == 'd') {
if (plateau.getCase(x+1,y).getUnite() != null && plateau.getCase(x+1, y).getUnite().getJoueur() != this.joueur) {
return false;
}
if (updatePosition(x,y+1)) {
this.plateau.getCase(x, y).setUnite(null);
this.plateau.getCase(x, y).setType(temp2);
this.y+=1;
updateDecouverte(this.plateau.getCase(x, y+1), this.vision);
updateItem();
return true;
}
}
else if(c == 'q') {
if (plateau.getCase(x+1,y).getUnite() != null && plateau.getCase(x+1, y).getUnite().getJoueur() != this.joueur) {
return false;
}
if (updatePosition(x,y-1)) {
this.plateau.getCase(x, y).setUnite(null);
this.plateau.getCase(x, y).setType(temp2);
this.y-=1;
updateDecouverte(this.plateau.getCase(x, y-1), this.vision);
updateItem();
return true;
}
}
else if(c == 'z') {
if (plateau.getCase(x+1,y).getUnite() != null && plateau.getCase(x+1, y).getUnite().getJoueur() != this.joueur) {
return false;
}
if (updatePosition(x-1, y)) {
this.plateau.getCase(x, y).setUnite(null);
this.plateau.getCase(x, y).setType(temp2);
this.x-=1;
updateDecouverte(this.plateau.getCase(x-1, y), this.vision);
updateItem();
return true;
}
}
return false;
}
public void teleporte() {
Random alea = new Random();
int longueur = plateau.getPlateau().length*plateau.getRegion(0, 0).region.length;
int rd1 = alea.nextInt(longueur);
int rd2 = alea.nextInt(longueur);
while(plateau.getCase(rd1, rd2).getType()!=Type.GRASS) {
rd1 = alea.nextInt(longueur);
rd2 = alea.nextInt(longueur);
}
updatePosition(rd1,rd2);
this.plateau.getCase(x, y).setUnite(null);
this.plateau.getCase(x, y).setType(Type.GRASS);
this.x=rd1;
this.y=rd2;
}
public void updateDecouverte (Case emplacement, int vision) {
if (vision > -1) {
if (emplacement.getX() < 29) {
this.plateau.getCase(emplacement.getX()+1, emplacement.getY()).setDecouverte(true);
if (this.plateau.getCase(emplacement.getX()+1, emplacement.getY()).getType() != Type.MOUNTAIN) {
updateDecouverte(this.plateau.getCase(emplacement.getX()+1, emplacement.getY()), vision-1);
}
}
if (emplacement.getY() > 0) {
this.plateau.getCase(emplacement.getX(), emplacement.getY() -1).setDecouverte(true);
if (this.plateau.getCase(emplacement.getX(), emplacement.getY()-1).getType() != Type.MOUNTAIN) {
updateDecouverte(this.plateau.getCase(emplacement.getX(), emplacement.getY() -1), vision-1);
}
}
if (emplacement.getY() < 29) {
this.plateau.getCase(emplacement.getX(), emplacement.getY() +1).setDecouverte(true);
if (this.plateau.getCase(emplacement.getX()+1, emplacement.getY()+1).getType() != Type.MOUNTAIN) {
updateDecouverte(this.plateau.getCase(emplacement.getX(), emplacement.getY() +1), vision-1);
}
}
if (emplacement.getX() > 0) {
this.plateau.getCase(emplacement.getX()-1, emplacement.getY()).setDecouverte(true);
if (this.plateau.getCase(emplacement.getX()-1, emplacement.getY()).getType() != Type.MOUNTAIN) {
updateDecouverte(this.plateau.getCase(emplacement.getX()-1, emplacement.getY()), vision-1);
}
}
}
}
public abstract String toString();
public abstract boolean action();
}