package game.entity;
import beans.core.GeneralConstant;
import beans.core.db.PersoControl;
import beans.enumeration.Classe;
import beans.enumeration.Element;
import beans.enumeration.Panoply;
import beans.enumeration.Stat;
import beans.enumeration.Target;
import beans.enumeration.Type;
import beans.enumeration.TypeEquipment;
import beans.enumeration.Utilization;
import beans.serializable.ChangeStatut;
import beans.serializable.Equipment;
import beans.serializable.Objet;
import beans.serializable.Skill;
import beans.serializable.Spell;
import beans.serializable.StatsPerso;
import game.model.Emotion;
import game.model.EmotionFactory;
import game.model.GUIModel;
import game.model.GameModel;
import game.model.inventory.InventoryModel;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
/**
* Classe gérant le joueur
* @author mastersnes
*/
public class Player implements Observer {
private int speed;
private int walking, orientation;
private BufferedImage sprite;
private Point absolute = new Point();
private Point relative = new Point();
private Point ancientBloc;
private int incrMarche = 1;
private GameModel gameModel;
private InventoryModel inventoryModel;
private List<Fighter> equipe = new ArrayList<Fighter>();
/**
* Constante de taille
*/
public int WIDTH = 32, HEIGHT = 48;
/**
* Creer un nouveau joueur
* @param gameModel
* @param guiModel
* @see GameModel
* @see GUIModel
*/
public Player(final GameModel gameModel, final GUIModel guiModel) {
final PersoControl persoControl = new PersoControl();
// StatsPerso stats = persoControl.getPerso("hero");
final StatsPerso stats = new StatsPerso();
stats.setName("hero");
stats.setClasse(Classe.WARRIOR);
stats.setCharisma(5);
stats.setConstitution(5);
stats.setDexterity(5);
stats.setHpMaxModifier(5);
stats.setIntelligence(5);
stats.setMpMaxModifier(5);
stats.setStrength(5);
stats.setWisdom(5);
stats.setSkills(new ArrayList<Skill>());
stats.setSpells(new ArrayList<Spell>());
final Map<Objet, Integer> objets = new HashMap<Objet, Integer>();
List<ChangeStatut> changeStat = new ArrayList<ChangeStatut>();
changeStat.add(new ChangeStatut(Type.HEAL, Element.AQUA, 10, Stat.DEXTERITY, Stat.HP));
Objet objet = new Objet("Potion", "Soin", Target.ALLY, changeStat, Utilization.TWICE);
objets.put(objet, 1);
changeStat = new ArrayList<ChangeStatut>();
changeStat.add(new ChangeStatut(Type.HEAL, Element.AQUA, 10, Stat.DEXTERITY, Stat.MP));
objet = new Objet("Potion mana", "Magie", Target.ALLY, changeStat, Utilization.TWICE);
objets.put(objet, 1);
changeStat = new ArrayList<ChangeStatut>();
objet = new Equipment("Epee", "Epee basique en bois", TypeEquipment.WEAPON, Panoply.NONE, 5, 3, 10, 10);
objets.put(objet, 1);
changeStat = new ArrayList<ChangeStatut>();
objet = new Equipment("Hache", "Hache basique en bois", TypeEquipment.WEAPON, Panoply.NONE, 10, 5, 10, 10);
objets.put(objet, 1);
stats.setObjets(objets);
final Fighter hero = new Fighter(stats, GeneralConstant.MONSTER_IMAGE_PATH + "heros.png");
final Map<TypeEquipment, Equipment> equipment = new EnumMap<TypeEquipment, Equipment>(TypeEquipment.class);
equipment.put(TypeEquipment.WEAPON, (Equipment)objet);
hero.getStat().getPermanentStats().setEquipment(equipment);
hero.initEquipment();
final Emotion emotion = new Emotion(GeneralConstant.PERSO_FACE_PATH, "normal", new Point(0,0));
emotion.addEmotion("triste", new Point(1,0));
emotion.addEmotion("joie", new Point(3,0));
emotion.addEmotion("peur", new Point(3,2));
EmotionFactory.addElement("hero", emotion);
equipe.add(hero);
final StatsPerso stats2 = new StatsPerso();
stats2.setName("snes");
stats2.setClasse(Classe.WARRIOR);
stats2.setCharisma(5);
stats2.setConstitution(5);
stats2.setDexterity(5);
stats2.setHpMaxModifier(5);
stats2.setIntelligence(5);
stats2.setMpMaxModifier(5);
stats2.setStrength(5);
stats2.setWisdom(5);
stats2.setSkills(new ArrayList<Skill>());
stats2.setSpells(new ArrayList<Spell>());
stats2.setObjets(objets);
final Fighter hero2 = new Fighter(stats2, GeneralConstant.MONSTER_IMAGE_PATH + "heros.png");
hero2.getStat().getPermanentStats().setEquipment(equipment);
hero2.initEquipment();
final Emotion emotion2 = new Emotion(GeneralConstant.PERSO_FACE_PATH, "normal", new Point(0,0));
emotion2.addEmotion("triste", new Point(1,0));
emotion2.addEmotion("joie", new Point(3,0));
emotion2.addEmotion("peur", new Point(3,2));
EmotionFactory.addElement("hero2", emotion);
equipe.add(hero2);
this.gameModel = gameModel;
gameModel.addObserver(this);
speed = 3;
walking = 0;
orientation = 0;
try {
sprite = ImageIO.read(new File(GeneralConstant.PERSO_PATH));
} catch (IOException ex) {
Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void initInventory() {
this.inventoryModel = new InventoryModel(gameModel);
}
public InventoryModel getInventory() {
return inventoryModel;
}
/**
* Retourne l'image du personnage principal en fonction de son orientation
* @return
*/
public Image getImage() {
return sprite.getSubimage(walking * WIDTH, orientation * HEIGHT, WIDTH, HEIGHT);
}
/**
* Change l'orientation du joueur
* @param orientation
*/
public void setOrientation(final int orientation) {
this.orientation = orientation;
}
@Override
public void update(final Observable o, final Object arg) {
gameModel = (GameModel) o;
}
/**
*
*/
public void walk () {
walking += incrMarche;
if (incrMarche > 0 && walking == 3) {
incrMarche = -1;
} else if (incrMarche < 0 && walking == 0) {
incrMarche = 1;
}
}
/**
* Change la position du joueur
* @param x
* @param y
*/
public void setMotion(final int x, final int y) {
this.setRelativeX(x);
this.setRelativeY(y);
this.setAbsoluteX(x);
this.setAbsoluteY(y);
}
/**
* Retourne la position absolue du joueur en X
* @return
*/
public int getX() {
return absolute.x;
}
/**
* Retourne la position absolue du joueur en Y
* @return
*/
public int getY() {
return absolute.y;
}
/**
* Retourne la position relative du joueur en X
* @return
*/
public int getRelativeX() {
return relative.x;
}
/**
* Change la position relative du joueur en X
* @param relativeX
*/
public void setRelativeX(final int relativeX) {
this.relative.x = relativeX;
}
/**
* Retourne la position relative du joueur en Y
* @return
*/
public int getRelativeY() {
return relative.y;
}
/**
* Change la position relative du joueur en Y
* @param relativeY the yp to set
*/
public void setRelativeY(final int relativeY) {
this.relative.y = relativeY;
}
/**
* Change la position absolue du joueur en X
* @param absoluteX
*/
public void setAbsoluteX(final int absoluteX) {
this.absolute.x = absoluteX;
}
/**
* Change la position absolue du joueur en X
* @param yAbsolue
*/
public void setAbsoluteY(final int yAbsolue) {
this.absolute.y = yAbsolue;
}
/**
* Incremente la position absolue du joueur en Y
* @param absoluteY
*/
public void incrementeAbsoluteY(final int absoluteY) {
this.absolute.y += absoluteY;
}
/**
* Incremente la position relative du joueur en Y
* @param relativeY
*/
public void incrementeRelativeY(final int relativeY) {
this.relative.y += relativeY;
}
/**
* Incremente la position absolue du joueur en X
* @param absoluteX
*/
public void incrementeAbsoluteX(final int absoluteX) {
this.absolute.x += absoluteX;
}
/**
* Incremente la position relative du joueur en X
* @param relativeX
*/
public void incrementeRelativeX(final int relativeX) {
this.relative.x += relativeX;
}
/**
* Retourne l'orientation du joueur
* @return the position
*/
public int getOrientation() {
return orientation;
}
/**
* Retourne la vitesse du joueur
* @return
*/
public int getSpeed() {
return speed;
}
/**
* Retourne l'ancienne position du joueur
* @return the ancientBloc
*/
public Point getAncientBloc() {
return ancientBloc;
}
/**
* Definie l'ancienne position du joueur
* @param ancientBloc
*/
public void setAncientBloc(final Point ancientBloc) {
this.ancientBloc = ancientBloc;
}
/**
* @return l'equipe
*/
public List<Fighter> getEquipe() {
return equipe;
}
//TODO : à revoir
/**
*
* @param objet
* @param quantity
*/
public void addObjet(final Objet objet, final int quantity) {
equipe.get(0).addObjet(objet, quantity);
}
}