package jpotter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.Timer;
import jpotter.spells.Accio;
import jpotter.spells.Deprimo;
import jpotter.spells.Expelliarmus;
import jpotter.spells.Incendio;
import jpotter.spells.Spell;
public class FusionManager {
private static final int DURATION = 2000;
private static FusionManager instance;
private String lastReceivedGesture, lastReceivedIncantation;
private Timer timer;
private volatile int tries = 0;
private FusionManager() {
timer = new Timer(DURATION, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
IncantationManager.getInstance().finish();
// GestureManager.getInstance().finish();
tries++;
System.err
.println("received incantation:" + lastReceivedIncantation + ";received gesture:" + lastReceivedGesture);
Spell s = getSpell(lastReceivedIncantation, lastReceivedGesture);
if (s != null) {
System.out.println("casting " + s);
IHM.getInstance().cast(s);
tries = 0;
try {
GestureManager.getInstance().spellCast();
} catch (IOException e1) {
e1.printStackTrace();
}
} else{
System.out.println("spell failed");
if(tries>=3){
cannotCastSpell();
Game.getInstance().casting = false;
tries = 0;
IHM.getInstance().displayText("Spell failed :(");
}else{
IHM.getInstance().displayText("Try again !!");
}
}
// Reset to null otherwise the player can just cast once and idle for
// the rest of the game
lastReceivedIncantation = null;
lastReceivedGesture = null;
}
});
new Thread() {
@Override
public void run() {
GestureManager.getInstance();
}
}.start();
new Thread() {
@Override
public void run() {
IncantationManager.getInstance();
}
}.start();
timer.setRepeats(false);
}
public void postGesture(int id) {
lastReceivedGesture = GestureManager.getInstance().getGesture(id);
}
public void postIncantation(String incantation) {
lastReceivedIncantation = incantation;
}
public String getLastReceivedGesture() {
return lastReceivedGesture;
}
public Spell getSpell(String incantation, String gesture) {
for (Spell s : IHM.getInstance().harry.getSpellList()) {
if (s.getIncantation().equals(incantation) && s.getGesture().equals(gesture))
return s;
}
return null;
}
public void startTimer() {
timer.start();
}
public static FusionManager getInstance(){
if(instance == null){
instance = new FusionManager();
}
return instance;
}
public void canCastSpell() {
GestureManager.getInstance().canCastSpell();
}
public void cannotCastSpell() {
GestureManager.getInstance().cannotCastSpell();
}
}