Input class - how to write you own in 5 hours or less
Posted: 31 Jul 2009, 17:51
Rob had a question about how to write an Input class so I thought I might tell you a little bit about it.
The Input class handles all of the mouse input. All phases are Input objects. The Input class handles choosing a card’s target, paying the mana cost and all of the phases. I explain the Input class here on my blog.
Let me tell you a little about the background of the Input class. It is supposed to be modeled from the State Pattern which changes how the object's internal guts and how it acts while externally the object looks the same.
Let me try to show you some pseudocode.
All Input classes extend the class "Input" which has a stop() method. When an Input class calls stop() it means, "I'm done so you can go to the next Input."
The user interface (gui) uses the InputControl.getInput() to decide which Input is next. (InputControl only deals with Inputs that represent phases.) The InputControl has different internal states representing phases (like Main1 or declare attackers) as well as cards such as Giant Growth where you choose a card.
The methods that you typically override in Input are:
The showMessage() method is always the first method that is called, typically is shows a message in the upper left part of the screen in the text box such as "Choose a card to discard." You can go crazy and use showMessage() as init() or the object's constructor since it is always called first, sometimes a dialog box is put into showMessage(), JOptionPane.showMessage(...). For examples of craziness see any of the planeswalkers in CardFactory.
selectCard() is called when you, the human player, clicks on a card whether it is in your hand, on the battlefield, or one of your opponents cards on the battlefield. (I'm not sure how playing flashback cards are handled.) You will typically use on of these two PlayerZone methods in order to restrict which cards can be chosen. String zone is anything in Constant.Zone and String player is either Constant.Player.Human or Constant.Player.Computer.
p.s. I hope that helps. That is probably more of a brain dump than a tutorial. Dennis feel free to cut and paste this to the top of the Input.java which might help others.
The Input class handles all of the mouse input. All phases are Input objects. The Input class handles choosing a card’s target, paying the mana cost and all of the phases. I explain the Input class here on my blog.
Let me tell you a little about the background of the Input class. It is supposed to be modeled from the State Pattern which changes how the object's internal guts and how it acts while externally the object looks the same.
Let me try to show you some pseudocode.
- Code: Select all
Interface (or abstract class) Sound
public void makeSound()
class Duck implements Sound
public void makeSound() {print("quack");}
class Cat implements Sound
public void makeSound() {print("meow");}
class Speaker
private Sound s;
public void makeSound() {s.makeSound();}
public void setSound(Sound s); {this.s = s;}
All Input classes extend the class "Input" which has a stop() method. When an Input class calls stop() it means, "I'm done so you can go to the next Input."
The user interface (gui) uses the InputControl.getInput() to decide which Input is next. (InputControl only deals with Inputs that represent phases.) The InputControl has different internal states representing phases (like Main1 or declare attackers) as well as cards such as Giant Growth where you choose a card.
The methods that you typically override in Input are:
- Code: Select all
public void showMessage() {AllZone.Display.showMessage("Blank Input");}
public void selectCard(Card c, PlayerZone zone) {}
public void selectPlayer(String player) {}
public void selectButtonOK() {}
public void selectButtonCancel() {}
The showMessage() method is always the first method that is called, typically is shows a message in the upper left part of the screen in the text box such as "Choose a card to discard." You can go crazy and use showMessage() as init() or the object's constructor since it is always called first, sometimes a dialog box is put into showMessage(), JOptionPane.showMessage(...). For examples of craziness see any of the planeswalkers in CardFactory.
selectCard() is called when you, the human player, clicks on a card whether it is in your hand, on the battlefield, or one of your opponents cards on the battlefield. (I'm not sure how playing flashback cards are handled.) You will typically use on of these two PlayerZone methods in order to restrict which cards can be chosen. String zone is anything in Constant.Zone and String player is either Constant.Player.Human or Constant.Player.Computer.
- Code: Select all
public boolean is(String zone);
public boolean is(String zone, String player);
p.s. I hope that helps. That is probably more of a brain dump than a tutorial. Dennis feel free to cut and paste this to the top of the Input.java which might help others.