I worked further on this.
There is the classic approach I often used. With states and switches.
And the new approach using ONE class for each state. Using interface / implements.
- This class contains a method showState, a method keyPressed and a method mousePressed. Therefore we just call it and don’t need switch. When we change the state, we just say
currentState = new statePause();
for example.
I’d like to discuss which approach might be better.
What do you all think?
My thoughts:
- It’s good to get rid of the switch (pro new approach)
- we change the object inside the class (is this evil?) (in the new approach)
- a variant of the new approach would be an array of the objects of each state and then use int state as index. Might be faster than
currentState = new StateGame();
Thank you all!
Chrisir
// This is the classic approach
// Links:
// https://discourse.processing.org/t/beginner-new-pages/17774
// https://discourse.processing.org/t/scenes-in-processing/40386/5
//
// states
final int stateGame = 0; // consts
final int statePause = 1;
int state = stateGame; // current
// -------------------------------------------------
void setup() {
// init (runs only once)
size(800, 600);
} // func
void draw() {
// runs on and on
switch (state) {
case stateGame:
// Game
handleStateGame();
break;
case statePause:
// Pause
handleStatePause();
break;
default:
// error
println("Error number 939; unknown state : "
+ state
+ ".");
exit();
break;
} // switch
//
} // func
// functions for states - called from draw()
void handleStateGame() {
// Game
background(11);
fill(244, 3, 3); // red
text ("This is the Game....\n Don't touch the red rectangle...", 210, 313);
//
noStroke();
fill(255, 2, 2) ;
rect(100, 100, 100, 100);
fill(255, 2, 255) ;
// rect(300, 100, 100, 100);
if (mouseX > 100 &&
mouseX < 100 + 100 &&
mouseY > 100 &&
mouseY < 100 + 100) {
println ("collide");
state = statePause;
}
} // func
void handleStatePause() {
// Pause
background(255);
fill(244, 3, 3); // red
text ("You LOST.... "
+ " ... ", 210, 313);
text ("Hit p to start Game", 210, 385);
} // func
// ----------------------------------------
// keyboard input
void keyPressed() {
switch (state) {
case stateGame:
// Game
keyPressedForStateGame();
break;
case statePause:
// Pause
keyPressedForStatePause();
break;
default:
// error
println("Error number 1039; unknown state : "
+ state
+ ".");
exit();
break;
} // switch
} // func
// ----
void keyPressedForStateGame() {
if (key == CODED)
{
if (keyCode == UP) {
//
} else if (keyCode == DOWN) {
//
}
if (keyCode == LEFT) {
//
} else if (keyCode == RIGHT) {
//
} else {
// do nothing
} // else
} // if (key == CODED) {
else
{
// not CODED ----------------------
if (key == 'p') {
// Pause
state = statePause;
} else {
// do nothing
} // else
} // else not CODED
} // func
void keyPressedForStatePause() {
if (key == CODED)
{
if (keyCode == UP) {
//
} else if (keyCode == DOWN) {
// none
}
if (keyCode == LEFT) {
//
} else if (keyCode == RIGHT) {
//
} else {
// do nothing
} // else
} // if (key == CODED) {
else
{
// not CODED ----------------------
if (key == 'p') {
//
state = stateGame;
} else {
// do nothing
} // else
} // else not CODED
} // func
//----------------------------------------------------------------------
void mousePressed() {
switch (state) {
case stateGame:
// Game
println("mouse 1");
break;
case statePause:
// Pause
println("mouse 2");
break;
default:
// error
println("Error number 1046; unknown state : "
+ state
+ ".");
exit();
break;
} // switch
} // func
// -------------------------------------------------------
New approach.
// This is the NEW approach
// Links:
// https://discourse.processing.org/t/beginner-new-pages/17774
// https://discourse.processing.org/t/scenes-in-processing/40386/5
// Interface example
// https://discourse.processing.org/t/classes-and-arraylist/9626/3
StateInterface currentState = new StateGame();
// -------------------------------------------------
void setup() {
// init (runs only once)
size(800, 600);
} // func
void draw() {
// runs on and on
currentState.showState();
}
// ----------------------------------------
// keyboard input
void keyPressed() {
currentState.keyPressedState();
}
//----------------------------------------
void mousePressed() {
currentState.mousePressedState();
}
// ===============================================================================================
class StateGame implements StateInterface {
// Game
void showState() {
// Game
background(11);
fill(244, 3, 3); // red
text ("This is the Game....\n Don't touch the red rectangle...", 210, 313);
//
noStroke();
fill(255, 2, 2) ;
rect(100, 100, 100, 100);
fill(255, 2, 255) ;
// rect(300, 100, 100, 100);
if (mouseX > 100 &&
mouseX < 100 + 100 &&
mouseY > 100 &&
mouseY < 100 + 100) {
println ("collide");
currentState = new statePause();
}
} // func
void keyPressedState() {
if (key == CODED)
{
if (keyCode == UP) {
//
} else if (keyCode == DOWN) {
//
}
if (keyCode == LEFT) {
//
} else if (keyCode == RIGHT) {
//
} else {
// do nothing
} // else
} // if (key == CODED) {
else
{
// not CODED ----------------------
if (key == 'p') {
// Pause
currentState = new statePause();
} else {
// do nothing
} // else
} // else not CODED
} // func
void mousePressedState() {
// Game
println("mouse 1");
}//func
}//class
// ===========================================================================================
class statePause implements StateInterface {
void showState() {
// Pause
background(255);
fill(244, 3, 3); // red
text ("You LOST.... "
+ " ... ", 210, 313);
text ("Hit p to start Game", 210, 385);
} // func
void keyPressedState() {
if (key == CODED)
{
if (keyCode == UP) {
//
} else if (keyCode == DOWN) {
// none
}
if (keyCode == LEFT) {
//
} else if (keyCode == RIGHT) {
//
} else {
// do nothing
} // else
} // if (key == CODED) {
else
{
// not CODED ----------------------
if (key == 'p') {
//
currentState = new StateGame();
} else {
// do nothing
} // else
} // else not CODED
} // func
void mousePressedState() {
// Pause
println("mouse 2");
}//func
}
// =====================================
interface StateInterface {
// Let's abstract
// Let's abstract display()
void showState();
void keyPressedState();
void mousePressedState();
}
//
Thank you all.
Chrisir