Recherche d'objets et personnage avec la portee.

This commit is contained in:
aminecmi 2015-02-01 10:33:56 +01:00
parent bcd693aba7
commit 6e4e647866
12 changed files with 150 additions and 353 deletions

View File

@ -25,24 +25,24 @@ public abstract class CaseAbstraite {
}
public void generateVoisins(CaseAbstraite[][] plateau) {
for (int i = coord.getHor() - 1; i < coord.getHor() + 1; i++) {
for (int j = coord.getVert() - 1; j < coord.getVert() + 1; j++) {
if (i > 0 && i < plateau.length && j > 0 && j < plateau[0].length) {
if (i == coord.getHor() && j < coord.getVert()) {
for (int i = coord.getLigne() - 1; i <= coord.getLigne() + 1; i++) {
for (int j = coord.getCol() - 1; j <= coord.getCol() + 1; j++) {
if (i >= 0 && i < plateau.length && j >= 0 && j < plateau[0].length) {
if (i < coord.getLigne() && j == coord.getCol()) {
voisins.put(PointsCardinaux.N, plateau[i][j]);
} else if (i > coord.getHor() && j < coord.getVert()) {
} else if (i < coord.getLigne() && j > coord.getCol()) {
voisins.put(PointsCardinaux.NE, plateau[i][j]);
} else if (i > coord.getHor() && j == coord.getVert()) {
} else if (i == coord.getLigne() && j > coord.getCol()) {
voisins.put(PointsCardinaux.E, plateau[i][j]);
} else if (i > coord.getHor() && j > coord.getVert()) {
voisins.put(PointsCardinaux.NE, plateau[i][j]);
} else if (i == coord.getHor() && j > coord.getVert()) {
} else if (i > coord.getLigne() && j > coord.getCol()) {
voisins.put(PointsCardinaux.SE, plateau[i][j]);
} else if (i > coord.getLigne() && j == coord.getCol()) {
voisins.put(PointsCardinaux.S, plateau[i][j]);
} else if (i < coord.getHor() && j > coord.getVert()) {
} else if (i > coord.getLigne() && j < coord.getCol()) {
voisins.put(PointsCardinaux.SW, plateau[i][j]);
} else if (i < coord.getHor() && j == coord.getVert()) {
} else if (i == coord.getLigne() && j < coord.getCol()) {
voisins.put(PointsCardinaux.W, plateau[i][j]);
} else if (i < coord.getHor() && j < coord.getVert()) {
} else if (i < coord.getLigne() && j < coord.getCol()) {
voisins.put(PointsCardinaux.NW, plateau[i][j]);
}
}

View File

@ -1,27 +1,27 @@
package Cases;
public class Coordonnees {
private int vert;
private int hor;
private int col;
private int ligne;
public Coordonnees(int vert, int hor) {
this.vert = vert;
this.hor = hor;
this.col = vert;
this.ligne = hor;
}
public int getVert() {
return vert;
public int getLigne() {
return ligne;
}
public void setVert(int vert) {
this.vert = vert;
public void setLigne(int ligne) {
this.ligne = ligne;
}
public int getHor() {
return hor;
public int getCol() {
return col;
}
public void setHor(int hor) {
this.hor = hor;
public void setCol(int col) {
this.col = col;
}
}

View File

@ -10,9 +10,5 @@ public abstract class EtatPersonnageAbstrait {
this.joueur = perso;
}
public abstract void AnalyseJoueur();
public abstract void ExecutionJoueur();
public abstract void ConflitJoueur();
public abstract void mediationConflits();
protected abstract void execute();
}

View File

@ -1,37 +0,0 @@
package Etats;
import Person.Personnage;
public class EtatPersonnageCombattant extends EtatPersonnageAbstrait {
//Etat d'un personnage avec un marteau, cible les joueurs et ignore le reste.
protected EtatPersonnageCombattant(Personnage perso) {
super(perso);
// TODO Auto-generated constructor stub
}
@Override
public void AnalyseJoueur() {
// TODO Auto-generated method stub
}
@Override
public void ExecutionJoueur() {
// TODO Auto-generated method stub
}
@Override
public void ConflitJoueur() {
// TODO Auto-generated method stub
}
@Override
public void mediationConflits() {
}
}

View File

@ -8,37 +8,12 @@ public class EtatPersonnageKO extends EtatPersonnageAbstrait {
public EtatPersonnageKO(Personnage perso) {
super(perso);
// TODO Auto-generated constructor stub
this.nbTourKO = 3;
}
@Override
public void AnalyseJoueur() {
// TODO Auto-generated method stub
}
@Override
public void ExecutionJoueur() {
// TODO Auto-generated method stub
nbTourKO--;
if (nbTourKO==0){
this.joueur.ChangeEtat(EEtat.EtatOK);
}
}
@Override
public void ConflitJoueur() {
// TODO Auto-generated method stub
}
@Override
public void mediationConflits() {
protected void execute() {
if (this.joueur.getPointsDeVie() > 0) {
this.joueur.ChangeEtat(EEtat.EtatOK);
}
}
}

View File

@ -1,72 +1,21 @@
package Etats;
import Comportements.*;
import Person.Personnage;
public class EtatPersonnageOK extends EtatPersonnageAbstrait {
private ComportementAction Action;
private int nbTourKO;
public EtatPersonnageOK(Personnage perso) {
super(perso);
// TODO Auto-generated constructor stub
this.ChangerAction(EAction.SeDeplacer);
public EtatPersonnageOK(Personnage perso) {
super(perso);
this.nbTourKO = 3;
}
@Override
public void AnalyseJoueur() {
// TODO Auto-generated method stub
}
@Override
public void ExecutionJoueur() {
// TODO Auto-generated method stub
Action.executerAction(this.joueur);
}
@Override
public void ConflitJoueur() {
// TODO Auto-generated method stub
}
@Override
public void mediationConflits() {
protected void execute() {
if (this.joueur.getPointsDeVie() == 0) {
this.joueur.ChangeEtat(EEtat.EtatKO);
}
}
public void ChangerAction(EAction nouvelAction) {
switch(nouvelAction)
{
case ChangerCouleurCase:
Action = new ComportementActionChangerCouleurCase();
break;
case RamasserNeige:
Action = new ComportementActionRamasserNeige();
break;
case SeDeplacer:
Action = new ComportementActionSeDeplacer();
break;
case TirerBalon:
Action = new ComportementActionTirerBalon();
break;
case TirerBouleDeNeige:
Action = new ComportementActionTirerBouleDeNeige();
break;
//case Null:
//Action = null;
//break;
default:
break;
}
this.joueur.setAction(nouvelAction);
}
}

View File

@ -14,14 +14,14 @@ public class FabriquePersonnagesBattleZone extends FabriquePersonnagesAbstraite
public ArrayList<Personnage> CreerPersonages(Organisation o) {
ArrayList<Personnage> list = new ArrayList<Personnage>();
PersonnageBattleZone p1 = new PersonnageBattleZone("Bob", 100.0, 1.0, 1.0, parDefaut);
PersonnageBattleZone p1 = new PersonnageBattleZone("Bob", 100.0, 1.0, 1.0, 2, parDefaut);
list.add(p1);
PersonnageBattleZone p2 = new PersonnageBattleZone("Jo", 100.0, 1.0, 1.0, parDefaut);
list.add(p2);
PersonnageBattleZone p3 = new PersonnageBattleZone("Max", 100.0, 1.0, 1.0, parDefaut);
list.add(p3);
PersonnageBattleZone p4 = new PersonnageBattleZone("Zac", 100.0, 1.0, 1.0, parDefaut);
list.add(p4);
//PersonnageBattleZone p2 = new PersonnageBattleZone("Jo", 100.0, 1.0, 1.0, 2, parDefaut);
//list.add(p2);
//PersonnageBattleZone p3 = new PersonnageBattleZone("Max", 100.0, 1.0, 1.0, 2, parDefaut);
//list.add(p3);
//PersonnageBattleZone p4 = new PersonnageBattleZone("Zac", 100.0, 1.0, 1.0, 2, parDefaut);
//list.add(p4);
return list;
}
}

View File

@ -2,8 +2,7 @@ package Person;
import Cases.CaseAbstraite;
import Cases.PointsCardinaux;
import Comportements.ComportementAction;
import Comportements.EAction;
import Comportements.*;
import Composition.PersonnagesAbstraits;
import Etats.EEtat;
import Etats.EtatPersonnageAbstrait;
@ -11,7 +10,9 @@ import Etats.EtatPersonnageKO;
import Etats.EtatPersonnageOK;
import Objets.ObjetAbstrait;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
public class Personnage extends PersonnagesAbstraits {
@ -22,33 +23,38 @@ public class Personnage extends PersonnagesAbstraits {
protected double pointsDeVie;
protected double force;
protected double vitesse;
protected double porter;
protected EAction action;
private EtatPersonnageAbstrait etatCourant;
protected double portee;
protected EAction action;
protected ComportementAction Action;
private EtatPersonnageAbstrait etatCourant;
private HashMap<PointsCardinaux, CaseAbstraite> voisins;
protected Personnage(String name, ComportementAction c) {
this.nom = name;
protected Personnage(String name, ComportementAction c) {
this.nom = name;
this.pointsDeVie = 100;
this.force = 10;
this.vitesse = 1;
this.porter = 2;
this.portee = 2;
this.caseCourante = null;
this.etatCourant = new EtatPersonnageOK(this);
this.action = EAction.Null;
}
this.ChangerAction(EAction.SeDeplacer);
protected Personnage(String name, double lifePoint, double strength, double speed, ComportementAction a) {
this.nom = name;
}
protected Personnage(String name, double lifePoint, double strength, double speed, int portee, ComportementAction a) {
this.nom = name;
this.pointsDeVie=lifePoint;
this.force=strength;
this.vitesse=speed;
this.portee = portee;
this.caseCourante=null;
this.etatCourant= new EtatPersonnageOK(this);
this.action = EAction.Null;
}
this.ChangerAction(EAction.SeDeplacer);
}
public void ChangeEtat(EEtat NouvelEtat)
@ -127,13 +133,13 @@ public class Personnage extends PersonnagesAbstraits {
}
public double getPorter() {
return porter;
}
public double getPortee() {
return portee;
}
public void setPorter(double porter) {
this.porter = porter;
}
public void setPortee(double portee) {
this.portee = portee;
}
public EAction getAction() {
return action;
@ -144,188 +150,97 @@ public class Personnage extends PersonnagesAbstraits {
}
public void AnalyseSituation() {
etatCourant.AnalyseJoueur();
ArrayList<ObjetAbstrait> objets = rechercheObjetProche();
ArrayList<Personnage> personnages = rechercheJoueur();
}
public void Execution() {
etatCourant.ExecutionJoueur();
Action.executerAction(this);
}
public void ResoudreLesConflits(){
etatCourant.ConflitJoueur();
}
public ObjetAbstrait rechercheObjetProche() {
public HashMap<PointsCardinaux, CaseAbstraite> voisinsPortee(CaseAbstraite c, int rayon) {
HashMap<PointsCardinaux, CaseAbstraite> voisinsActuels = new HashMap<PointsCardinaux, CaseAbstraite>();
HashMap<PointsCardinaux, CaseAbstraite> v = c.getVoisins();
if ((rayon + 1) == this.getPortee()) {
// voisinsActuels.putAll(v);
return c.getVoisins();
} else {
int r = rayon + 1;
for (PointsCardinaux mapKey : v.keySet()) {
voisinsActuels.putAll(voisinsPortee(v.get(mapKey), r));
}
return voisinsActuels;
}
}
ObjetAbstrait objetDeLaCase = null;
public ArrayList<ObjetAbstrait> rechercheObjetProche() {
for (PointsCardinaux mapKey : voisins.keySet()) {
if (voisins.get(mapKey).getObjetOccupant() != null) {
objetDeLaCase = voisins.get(mapKey).getObjetOccupant();
ArrayList<ObjetAbstrait> objetsVoisins = new ArrayList<ObjetAbstrait>();
HashSet<CaseAbstraite> voisinsDesVoisins = getCaseAbstraitesForPortee();
for (CaseAbstraite c : voisinsDesVoisins) {
if (c.getObjetOccupant() != null) {
objetsVoisins.add(c.getObjetOccupant());
}
}
if (objetDeLaCase != null) {
for (PointsCardinaux mapKey : voisins.keySet()) {
switch (mapKey) {
case N:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.N) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.N).getObjetOccupant();
}
break;
case NE:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.N) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.N).getObjetOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.NE) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.NE).getObjetOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.E) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.E).getObjetOccupant();
}
break;
case E:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.E) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.E).getObjetOccupant();
}
break;
case SE:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.E) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.E).getObjetOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.SE) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.SE).getObjetOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.S) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.S).getObjetOccupant();
}
break;
case S:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.S) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.S).getObjetOccupant();
}
break;
case SW:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.S) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.S).getObjetOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.SW) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.SW).getObjetOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.W) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.W).getObjetOccupant();
}
break;
case W:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.W) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.W).getObjetOccupant();
}
break;
case NW:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.W) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.W).getObjetOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.NW) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.NW).getObjetOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.N) != null) {
objetDeLaCase = voisins.get(mapKey).getVoisins().get(PointsCardinaux.N).getObjetOccupant();
}
break;
default:
break;
return objetsVoisins;
}
}
}
}
return objetDeLaCase;
protected HashSet<CaseAbstraite> getCaseAbstraitesForPortee() {
HashSet<CaseAbstraite> voisinsDesVoisins = new HashSet<CaseAbstraite>();
voisinsDesVoisins.addAll(voisinsPortee(this.caseCourante, 0).values());
return voisinsDesVoisins;
}
public Personnage rechercheJoueur() {
Personnage personne = null;
public ArrayList<Personnage> rechercheJoueur() {
ArrayList<Personnage> personnes = new ArrayList<Personnage>();
for (PointsCardinaux mapKey : voisins.keySet()) {
if (voisins.get(mapKey).getOccupant() != null) {
personne = voisins.get(mapKey).getOccupant();
HashSet<CaseAbstraite> voisinsDesVoisins = getCaseAbstraitesForPortee();
for (CaseAbstraite c : voisinsDesVoisins) {
if (c.getOccupant() != null) {
personnes.add(c.getOccupant());
}
}
if (personne != null) {
for (PointsCardinaux mapKey : voisins.keySet()) {
switch (mapKey) {
case N:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.N) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.N).getOccupant();
}
break;
case NE:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.N) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.N).getOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.NE) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.NE).getOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.E) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.E).getOccupant();
}
break;
case E:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.E) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.E).getOccupant();
}
break;
case SE:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.E) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.E).getOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.SE) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.SE).getOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.S) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.S).getOccupant();
}
break;
case S:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.S) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.S).getOccupant();
}
break;
case SW:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.S) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.S).getOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.SW) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.SW).getOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.W) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.W).getOccupant();
}
break;
case W:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.W) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.W).getOccupant();
}
break;
case NW:
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.W) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.W).getOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.NW) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.NW).getOccupant();
}
if (voisins.get(mapKey).getVoisins().get(PointsCardinaux.N) != null) {
personne = voisins.get(mapKey).getVoisins().get(PointsCardinaux.N).getOccupant();
}
break;
default:
break;
}
}
}
return personne;
return personnes;
}
public void mediationConflits() {
etatCourant.mediationConflits();
}
public void ChangerAction(EAction nouvelAction) {
switch (nouvelAction) {
case ChangerCouleurCase:
Action = new ComportementActionChangerCouleurCase();
break;
case RamasserNeige:
Action = new ComportementActionRamasserNeige();
break;
case SeDeplacer:
Action = new ComportementActionSeDeplacer();
break;
case TirerBalon:
Action = new ComportementActionTirerBalon();
break;
case TirerBouleDeNeige:
Action = new ComportementActionTirerBouleDeNeige();
break;
default:
break;
}
this.setAction(nouvelAction);
}
}

View File

@ -4,9 +4,9 @@ import Comportements.ComportementAction;
public class PersonnageBattleGoal extends Personnage {
protected PersonnageBattleGoal(String name, double lifePoint, double strength, double speed, ComportementAction parDefaut) {
super(name, lifePoint, strength, speed, parDefaut);
// TODO Auto-generated constructor stub
protected PersonnageBattleGoal(String name, double lifePoint, double strength, double speed, int portee, ComportementAction parDefaut) {
super(name, lifePoint, strength, speed, portee, parDefaut);
// TODO Auto-generated constructor stub
}
}

View File

@ -4,9 +4,9 @@ import Comportements.ComportementAction;
public class PersonnageBattleSnow extends Personnage{
protected PersonnageBattleSnow(String name, double lifePoint, double strength, double speed, ComportementAction parDefaut) {
super(name, lifePoint, strength, speed, parDefaut);
// TODO Auto-generated constructor stub
protected PersonnageBattleSnow(String name, double lifePoint, double strength, double speed, int portee, ComportementAction parDefaut) {
super(name, lifePoint, strength, speed, portee, parDefaut);
// TODO Auto-generated constructor stub
}
}

View File

@ -8,8 +8,8 @@ import java.util.Random;
public class PersonnageBattleZone extends Personnage {
Color couleur;
public PersonnageBattleZone(String name, double lifePoint, double strength, double speed, ComportementAction parDefaut) {
super(name, lifePoint, strength, speed, parDefaut);
public PersonnageBattleZone(String name, double lifePoint, double strength, double speed, int portee, ComportementAction parDefaut) {
super(name, lifePoint, strength, speed, portee, parDefaut);
// TODO Auto-generated constructor stub

View File

@ -48,14 +48,13 @@ public class SimulationJeu {
boolean continuer = true;
while (continuer) {
afficheTous();
for (Personnage p : personnages) {
p.AnalyseSituation();
p.Execution();
mediationDesConflits(); // utiliser le pattern avec l'historique pour les actions. On pourra faire un retour arrière si conflit + réexecturer
recupererInformations();
}
afficheTous();
// bloquer le tour jusqu'a toucher une touche du clavier.
Scanner s = new Scanner(System.in);