Hi all,I would like to develop small turn based game with slick2d but I cannot imagine a way how to do it in slick2d update() method. It is quite easy to develop own game loop for turn based game but how to do it in slick2d with its update/render methods? Any idea how to tell update() method to wait until something happens (keypress or player action taken)? I have tried something like:

Thanks for reply. Yes, I know that slick2d takes care of update. However it does not seem to be suitable for turn based game (I press some key, slicks move my character, then some NPC tooks his turn, other NPC till my turn starts again)....I mean I cannot put my game logic to update() method because it runs too fast. In other words update would process many turns on "single" key press and thats not what you want in turn based games. However I kind of solved it by putting game logic to overriden keyPressed(int, char) method....hope it will work.And there comes another question....I need custom keyrepeat intervals. How can this be achieved since "public void enableKeyRepeat(int initial,int interval) is deprecated and does not seem to work? Thanks again.

In other words update would process many turns on "single" key press and thats not what you want in turn based games.

change

1

if (input.isKeyDown(Input.KEY_RIGHT)) {

to

1

if (input.isKeyPressed(Input.KEY_RIGHT)) {

To fix

Quote

However it does not seem to be suitable for turn based game (I press some key, slicks move my character, then some NPC tooks his turn, other NPC till my turn starts again)....I mean I cannot put my game logic to update() method because it runs too fast.

Slick2D is an amazing engine, a turn based game can easily be achieved with it if you code the correct logic into it. I'm not a very good programmer yet so I can't give you a straight answer for the game logic on a turn based game (it also depends heavily on what kind of turn based game it will be) but it's very do-able.

To fix

Quote

And there comes another question....I need custom keyrepeat intervals. How can this be achieved since "public void enableKeyRepeat(int initial,int interval) is deprecated and does not seem to work?

Then you should make three classes called TestState1, TestState2, and TestState3. But for your case you can change the code and class names to START, GAME, and BATTLE. Those three classes will each extend StateBasedGame, so each of those three classes will have a init(), update(), and render(). To switch between states you call this in the update method for example

That's how I exit out of my credits screen to my main menu. PsionicEntrance is my main class and it contains static IDs of all the classes. Ah right I forgot to mention, your three classes will also need this code at the end.

I am already using StateBasedGame superclass in my project but it is good for menus, etc. (I can imagine using it for switching levels if count is limited) but probably not for game logic. I would also like to separate key controls from game update (to avoid loong if/else/switch statements in the main update method) so I will probably put it to different class implementing one of the InputListeners (maybe to hero class or to seperate one and use composition...need to think about it). We will see what turns out to be the best way Thanks for suggestions.

Certainly not the most beautiful piece of code but it does it's job. Input is processed when it's the player's turn and no action has been performed for a given time (timeToBlock).When you press a key, the action is processed, the AI can do it's stuff and the timer starts. When the time limit has been reached input is "unlocked" again.

It also allows you to add other effects to the turn-based list. Basically, you register a 'State' with the Context, and the context will keep calling 'actionPerformed()' on the state until it sets its 'actionPerformed' to true. At that point, it'll move that state to the end of the list, then start doing the same on the next state. Probably using the wrong term for doing it this way, but... Anyway, it allows you to add a variable number of entities/effects that have to work in the turn based manner.

Thanks, this one looks like really solid design. Now, I have some testing implementation of flag-based turns and it seems to be working fine....we will see how it works with more features added. If it does not work well, I will definitely move to your state based design...

If you want the game loop to be slower, you could add up the delta time and only execute a block once the delta has built up enough. I wouldn't advise that you do this for turn-based gameplay though. It's rather if you want something executing at a specific pace, like animation.

Is there any benefit in using something like that over a simple enhanced for loop through ArrayList<Entity>?

And is an ArrayList "fit" to be used as a queue(cutting the first element and putting it at the back)? I realize that thinking about the most efficient container can lead to long discussion and is most of the time only noticeable at very high number of elements, so take this question with a grain of salt.

My game is turn based, too, so I have a genuine interest in what needs tweaking/changing in my code.

An ArrayList is not ideal as a queue because it will have to shift the whole array either on every put or on every get. You can maintain a ringbuffer yourself to avoid that, but an easier route would be to just use an ArrayDeque instead

Yeah, thinking about it using an ArrayList isn't the best, as was said. I just used that because it was the first thing that came to mind (Next to using a LinkedList, which would have performed the task rather admirably, since you shouldn't have to worry too much about its performance since changes would only happen during certain steps).

Is there any benefit in using something like that over a simple enhanced for loop through ArrayList<Entity>?

I did it like that for the express reason of allowing you to turn the TurnState into another state machine that would allow you to display different state information based on what's happening. Basically, it keeps returning to whatever the current 'Entity State' is until that state says it's done with its job.

If you turn the Entity's TurnState into another state machine itself, you can make it so that your player's entity is constrained by a set of states/actions it can perform. Think like the older Turn based RPGs where your character can move then make an action. This is modeled fairly well as a state machine.

Further, you can use this method to allow different 'stages' of your AI's decisions to be spread out across multiple frames.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org