Monday, November 18, 2019

How to implement turn-based game engine?


Let's imagine game like Heroes of Might and Magic, or Master of Orion, or your turn-based game of choice. What is the game logic behind making next turn? Are there any materials or books to read about the topic? To be specific, let's imagine game loop:


void eventsHandler(); //something that responds to input

void gameLogic(); //something that decides whats going to be output on the screen
void render(); //this function outputs stuff on screen

All those are getting called say 60 times a second. But how turn-based enters here? I might imagine that in gameLogic() there is a function like endTurn() that happens when a player clicks that button, but how do I handle it all? Need insights.



Answer



A turn based game is going to be governed by a state machine. Basically, you would lay out a series of states that can occur in a logical order.


At a high level, a player's turn could be the start of a new state, followed by all the possible actions that are allowed during that turn.


For instance



  • State - change player


    • it is now player 1's turn



  • Actions allowed

    • attack

      • select enemy to attack




    • defend

      • select unit to defend



    • move unit

      • select unit to move

      • check to ensure movement is allowed




    • etc




Obviously this will balloon quite quickly, as I've only sketched out an extremely limited plan. Having a good grasp on possible states early on will mean that you should be in a good position to implement. I'd highly stress sketching out exactly how you want the game to run....a good turn-based game requires a lot of planning IMO.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...