package tamagotchi.model;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.Observable;
import java.util.Observer;
import tamagotchi.core.Compteur;
import tamagotchi.core.Etat;
import tamagotchi.core.EventFactory;
import tamagotchi.core.EventItem;
/**
* Cette classe contient les methodes metier ainsi que les données du jeu
* @see EventFactory
* @see EventItem
* @author mastersnes
*/
public class GameModel extends Observable implements EventItem, Observer {
private boolean displayStat = false;
private final Compteur compteur;
private enum CommandeEnum {
ETAT, NOURIR, TOILETTE, JOUER, SOIGNER, EDUQUER, DORMIR, QUITTER;
public String getName(final Tamago tamago) {
final String name = this.name();
if (name.equals(DORMIR.name()) && tamago.getEtatPiece() == Etat.DORT) {
return "REVEIL";
} else {
return name;
}
}
}
private final GUIModel guiModel;
private Tamago tamago;
private CommandeEnum[][] MENU = {{CommandeEnum.ETAT, CommandeEnum.NOURIR}, {CommandeEnum.TOILETTE, CommandeEnum.JOUER}, {CommandeEnum.SOIGNER, CommandeEnum.EDUQUER}, {CommandeEnum.DORMIR, CommandeEnum.QUITTER}};
private Point cursorPosition = new Point(0, 0);
/**
* Constructeur de la classe, initialise le tamagotchi et se declare evenement courant
* @param guiModel
*/
public GameModel(final GUIModel guiModel) {
this.guiModel = guiModel;
compteur = new Compteur();
tamago = new Tamago();
compteur.addCompteurItem(tamago);
tamago.addObserver(this);
EventFactory.setCurrentEvent(this);
}
/**
* Permet de recuperer le tamagotchi
* @return
*/
public Tamago getTamago() {
compteur.tick();
return tamago;
}
@Override
public void dispatchAction(final KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
switch (e.getKeyCode()) {
case KeyEvent.VK_ESCAPE:
guiModel.switchToMenu();
break;
case KeyEvent.VK_Z:
case KeyEvent.VK_UP:
incrCursorPosition(-1, 0);
break;
case KeyEvent.VK_S:
case KeyEvent.VK_DOWN:
incrCursorPosition(1, 0);
break;
case KeyEvent.VK_Q:
case KeyEvent.VK_LEFT:
incrCursorPosition(0, -1);
break;
case KeyEvent.VK_D:
case KeyEvent.VK_RIGHT:
incrCursorPosition(0, 1);
break;
case KeyEvent.VK_ENTER:
enterRequest();
default:
break;
}
}
}
private void incrCursorPosition(final int x, final int y) {
if (!displayStat) {
cursorPosition.x += x;
if (cursorPosition.x >= MENU[0].length) {
cursorPosition.x = 0;
} else if (cursorPosition.x < 0) {
cursorPosition.x = MENU[0].length - 1;
}
cursorPosition.y += y;
if (cursorPosition.y >= MENU.length) {
cursorPosition.y = 0;
} else if (cursorPosition.y < 0) {
cursorPosition.y = MENU.length - 1;
}
setChanged();
notifyObservers();
}
}
private Point getCurrentPosition() {
return cursorPosition;
}
private void enterRequest() {
if (!isDisplayStat()) {
final CommandeEnum commande = MENU[cursorPosition.y][cursorPosition.x];
switch (commande) {
case NOURIR:
tamago.nourrir();
break;
case DORMIR:
if (tamago.getEtatPiece() == Etat.NONE) {
tamago.setEtatPiece(Etat.DORT);
} else {
tamago.checkForEvolve();
tamago.setEtatPiece(Etat.NONE);
}
setChanged();
notifyObservers();
break;
case JOUER:
if (tamago.getEtatPiece() == Etat.NONE) {
tamago.setEtatPiece(Etat.JOUER);
} else {
tamago.setEtatPiece(Etat.NONE);
}
break;
case QUITTER:
tamago.save();
guiModel.switchToMenu();
break;
case EDUQUER:
tamago.eduque();
break;
case ETAT:
this.setDisplayStat(true);
setChanged();
notifyObservers();
break;
case SOIGNER:
tamago.soin();
break;
case TOILETTE:
tamago.toilette();
break;
}
} else {
this.setDisplayStat(false);
setChanged();
notifyObservers();
}
}
@Override
public void update(final Observable o, final Object arg) {
tamago = (Tamago) o;
setChanged();
notifyObservers();
}
public Image getMenu() {
final BufferedImage image = new BufferedImage(GUIModel.SCREEN_SIZE.width, GUIModel.SCREEN_SIZE.height / 6, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g = image.createGraphics();
g.setColor(Color.black);
for (int i = 0; i < MENU.length; i++) {
for (int j = 0; j < MENU[0].length; j++) {
final StringBuffer menuItem = new StringBuffer(MENU[i][j].getName(tamago));
if (getCurrentPosition().x == j && getCurrentPosition().y == i) {
menuItem.insert(0, "*");
menuItem.append("*");
}
g.drawString(menuItem.toString(), 20 + i * 100, (j + 1) * 40);
}
}
g.dispose();
return image;
}
/**
* @return the displayStat
*/
public boolean isDisplayStat() {
return displayStat;
}
/**
* @param displayStat the displayStat to set
*/
private void setDisplayStat(boolean displayStat) {
this.displayStat = displayStat;
}
/**
* Renvoie une image de l'etat du tamagotchi
* @return
*/
public Image getEtat() {
return tamago.getEtat();
}
/**
* Retourne un apercu de l'horloge du jeu
* @return
*/
public Image getHorloge() {
return tamago.getHorloge();
}
}