I have a state called SetupState and a class SetupGame, which job is to initalize GameState with a values from SetupGame class before I enter GameState.
The problem is, how I’m going to pass SetupGame instance to GameState constructor as a parameter since enterState method from StateBasedGame, which method takes only ID parameter.
Should I make my SetupGame static with a static attributes so I can access it anytime, or there’s some other way ?
public class SetupScreenState extends PAbstractState {
public static final int ID = 2;
private Image titleImage;
private PButton backButton;
private PButton startButton;
private SetupScreenUI setupScreenUI;
public SetupScreenState() {
super();
}
@Override
public int getID() {
return ID;
}
@Override
public void enter(GameContainer gc, StateBasedGame sg) throws SlickException {
setupScreenUI = new SetupScreenUI();
setupScreenUI.init(gc);
}
@Override
public void init(GameContainer gc, StateBasedGame sg) throws SlickException {
titleImage = new Image("res/gui/titleSetUpTable.png");
backButton = new PButton("buttonBack");
startButton = new PButton("buttonStart");
}
@Override
public void render(GameContainer gc, StateBasedGame sg, Graphics g) throws SlickException {
PTheme.bgTheme(gc, g);
float titleX = 50;
float titleY = 50;
titleImage.draw(titleX, titleY);
float backButtonX = (gc.getWidth() / 2) - backButton.getWidth() - 10;
float backButtonY = gc.getHeight() - backButton.getHeight() - 30;
backButton.render(g, backButtonX, backButtonY);
float startButtonX = (gc.getWidth() / 2) + 10;
float startButtonY = backButtonY;
startButton.render(g, startButtonX, startButtonY);
setupScreenUI.render(gc, g);
}
@Override
public void update(GameContainer gc, StateBasedGame sg, int delta) throws SlickException {
Input input = gc.getInput();
int mx = input.getMouseX();
int my = input.getMouseY();
if (!hasFocus) return;
setupScreenUI.update(gc, sg, delta);
if (startButton.isActionPerformed(input, mx, my)) {
// this way I can't because method does not include
// example: sg.enterState(PlayScreenState.ID, setupScreenUI.getSetupGameInstance());
sg.enterState(PlayScreenState.ID, new FadeOutTransition(), new FadeInTransition());
}
if (backButton.isActionPerformed(input, mx, my)) {
sg.enterState(StartScreenState.ID, new FadeOutTransition(), new FadeInTransition());
}
}
}
public class SetupScreenUI extends PAbstractUI implements ComponentListener {
private PLabel attributeLabel;
private PLabel valueLabel;
private TextField nameField;
private SetupGame setupGame;
public SetupScreenUI() {
super();
}
@Override
public void init(GameContainer gc) throws SlickException {
attributeLabel = new PLabel(PFontType.CharlemagneStd, 22, false, false);
valueLabel = new PLabel(PFontType.CharlemagneStd, 22, false, false);
float nameFieldX = panelMinX + 235;
float nameFieldY = panelMinY + 122.5f;
Font nameFieldFont = valueLabel.getFont();
nameField = new TextField(gc, nameFieldFont, (int) nameFieldX, (int) nameFieldY, 225, 25, this);
}
@Override
public void update(GameContainer gc, StateBasedGame sg, int delta) throws SlickException {
if (!this.hasFocus) return;
}
@Override
public void componentActivated(AbstractComponent source) {
if (!this.hasFocus) return;
if (source == nameField) {
// do something nameField.getText();
}
}
@Override
public void render(GameContainer gc, Graphics g) throws SlickException {
String attName = "Player Name: ";
float attNameX = panelMinX + 55;
float attNameY = panelMinY + 125;
attributeLabel.setTextColor(Color.decode("#ffffbe"));
attributeLabel.render(attName, attNameX, attNameY);
attributeLabel.setTextColor(attributeLabel.getDefaultColor());
attributeLabel.render(attName, attNameX + 1, attNameY + 1);
float b1X1 = attNameX;
float b1X2 = attNameX + attributeLabel.getTextWidth(attName) + 225;
float b1Y1 = attNameY + attributeLabel.getTextHeight(attName) + 20;
float b1Y2 = attNameY + attributeLabel.getTextHeight(attName) + 20;
g.setColor(attributeLabel.getDefaultColor()); // get default color
g.drawLine(b1X1, b1Y1, b1X2, b1Y2);
float cornerRadius = 0;
float miniTableWidth = 260;
float miniTableHeight = 180;
float miniTableX = panelMaxX - miniTableWidth - 55;
float miniTableY = Math.max(b1Y1, b1Y2);
g.setColor(new Color(0, 0, 0, .5f));
g.fillRoundRect(miniTableX, miniTableY, miniTableWidth, miniTableHeight, (int) cornerRadius);
g.setColor(Color.decode("#ffffbe"));
g.drawRoundRect(miniTableX, miniTableY, miniTableWidth, miniTableHeight, (int) cornerRadius);
String attTable = "Table";
float mtNameX = miniTableX + (((miniTableX + miniTableWidth) - miniTableX) / 2) - (attributeLabel.getTextWidth(attTable) / 2);
float mtNameY = miniTableY + miniTableHeight + 10;
attributeLabel.setTextColor(Color.decode("#ffffbe"));
attributeLabel.render(attTable, mtNameX, mtNameY);
attributeLabel.setTextColor(attributeLabel.getDefaultColor());
attributeLabel.render(attTable, mtNameX + 1, mtNameY + 1);
g.setColor(Color.decode("#ffffbe"));
nameField.render(gc, g);
}
public SetupGame getSetupGameInstance() {
return setupGame;
}
}
public class SetupGame {
private int name;
// and more....
public SetupGame() {}
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
}