[Java] concept of a Turn based system (WIP)
Posted: Sun May 19, 2013 10:27 am
Sorry, but grammar still needs work.
I make the assumption you know how to program in java but if you program in some other language then the concept should be very similar but milage may vary based on your API. I am making available my self made library for game development. It is likely going to help if you have the files. There are about 8 files in the spoiler i suggest you have on hand.
Items you might need before we start.
So one of the problems i found was while programming my games is how to get the AI to interact with the player and the world. Less about programming the AI itself and more how to get them to at least drool on the floor in turns with the player. The idea is to make the player think he's the smartest person in the room.
The concept for doing a turn system is to use in your monsters, party members and player; a base class. I always call my base class Entity. You follow up that by using something like a arraylist and adding your based class as the generic type.
Now that we have an arraylist to use, we're going to make a loop that will go through the arraylist objects and run are AI command. Here would be a example assuming that your base class has a public method called "runAICommand();"
Now here's a problem with the loop. This loop will go until the end of time, and wont stop to allow the player to move. the way around that is either make your Player class smart enough to pause this loop somehow or the make the loop itself smart enough.
My solution is to do both and be silly about it. Now this next example i'm taking directly from my bip library and the file name is TurnController.
What really need to do however is to pause the loop using the player class. There are two ways i've figured out how to do this and it really depends how you the player to control the game. The first way is if i you have a console based game. The simplest way is to just do this.
the method scan.nextInt() will wait until the user inputs a values. Just follow that up with some if statements and that's one way of dealing with turns.
Be warned that nextInt() will only work if you the user inputs a integer. However if you want to make this solution work for your needs, than i suggest you read the JAVA API on the class. http://docs.oracle.com/javase/6/docs/api/
Now lets say we have a GUI. We can't just use scanner like we just did. This solution is a bit more of a hack and is a waste of resources but it works for me.
Assume we have a instants variable that is "boolean active = true;".
Basically with this all you have to do is make the action listener in your GUI find this object and use the turnActiveOff(). then when the loop in the loop object runs again, it will notice the active boolean is false and wont run again. If you want a example, in the first spoiler box, there is a class called gui , the action lister does this exact thing.
I assume you have a system set up to move the player object around already. What i did in my last game was make a AI class for entities, and then made a player AI. I put the player AI in the GUI as a instance variable and also in the player object. Then, when i wanted to move my character i could just send the commands to the AI and it would move the player. You could use the same concept here.
I make the assumption you know how to program in java but if you program in some other language then the concept should be very similar but milage may vary based on your API. I am making available my self made library for game development. It is likely going to help if you have the files. There are about 8 files in the spoiler i suggest you have on hand.
Items you might need before we start.
Spoiler (click to show/hide):
So one of the problems i found was while programming my games is how to get the AI to interact with the player and the world. Less about programming the AI itself and more how to get them to at least drool on the floor in turns with the player. The idea is to make the player think he's the smartest person in the room.
The concept for doing a turn system is to use in your monsters, party members and player; a base class. I always call my base class Entity. You follow up that by using something like a arraylist and adding your based class as the generic type.
Spoiler (click to show/hide):
Now that we have an arraylist to use, we're going to make a loop that will go through the arraylist objects and run are AI command. Here would be a example assuming that your base class has a public method called "runAICommand();"
- Code: Select All Code
public void turnManagement(){
while(keepActive){
this.getCurrentEntity().runAICommand();
}
System.out.println("turrnManagement has been turned offline.");
}
Now here's a problem with the loop. This loop will go until the end of time, and wont stop to allow the player to move. the way around that is either make your Player class smart enough to pause this loop somehow or the make the loop itself smart enough.
My solution is to do both and be silly about it. Now this next example i'm taking directly from my bip library and the file name is TurnController.
- Code: Select All Code
public void turnManagement(){
while(keepActive){
boolean i = this.testObjectForTurnManger.contains(getCurrentEntity()); //
if(!i){runNormalRoutine();}
else {runPlayerRoutine();}
}
System.out.println("turrnManagement has been turned offline.");
}
Spoiler (click to show/hide):
What really need to do however is to pause the loop using the player class. There are two ways i've figured out how to do this and it really depends how you the player to control the game. The first way is if i you have a console based game. The simplest way is to just do this.
- Code: Select All Code
public void runAICommand() {
Scanner scan = new Scanner(System.in);
System.out.println(scan.nextInt());
}
the method scan.nextInt() will wait until the user inputs a values. Just follow that up with some if statements and that's one way of dealing with turns.
Be warned that nextInt() will only work if you the user inputs a integer. However if you want to make this solution work for your needs, than i suggest you read the JAVA API on the class. http://docs.oracle.com/javase/6/docs/api/
Now lets say we have a GUI. We can't just use scanner like we just did. This solution is a bit more of a hack and is a waste of resources but it works for me.
Assume we have a instants variable that is "boolean active = true;".
- Code: Select All Code
public void runAICommand() {
//we're going to need this boolen to be true for the while loop.
active = true;
//give information about game state in console.
System.out.println("Game waiting for player command");
while(active){
//must have some statement inside the loop or the loop will never end. Reasons: Unknown.
System.out.print(""); // println must not have any other value other than a empty string or the console will clear all text after a few seconds.
} // pause the game, the brute force method.
//tells the users that the game is no longer paused
System.out.println("Game got player command");
// what the player does.
System.out.print("{Players uses normal attack.}");
}
public void turnActiveOff(){
//changes the active boolean to false to turn off the while loops that would be running in "runAICommand();" method.
this.active = false;
}
Basically with this all you have to do is make the action listener in your GUI find this object and use the turnActiveOff(). then when the loop in the loop object runs again, it will notice the active boolean is false and wont run again. If you want a example, in the first spoiler box, there is a class called gui , the action lister does this exact thing.
I assume you have a system set up to move the player object around already. What i did in my last game was make a AI class for entities, and then made a player AI. I put the player AI in the GUI as a instance variable and also in the player object. Then, when i wanted to move my character i could just send the commands to the AI and it would move the player. You could use the same concept here.