Turn Based Card Game Structure-animations and turn control

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

Turn Based Card Game Structure-animations and turn control

Postby drentsoft » Sun Mar 12, 2017 11:38 pm

Hi,

I'm working on making a turn based card game and I need some structuring advice. I think I've got most of the mechanics done but I'm having trouble getting things to work neatly. I'm using Scene2D UI for the cards and buttons. The turn structure is you select a card, roll dice to determine the outcome and depending on success/failure something should happen and then the next turn should start.

I have a Screen subclass that handles contains the stage and all the UI. I've made a player interface so I can have local human players, AI players and network human players. At the moment I'm working on AI vs human.

I have a game controller class (also implementing an interface) that handles checking if a card can be played, checks the results of a card being played etc but at the moment the UI triggers most functions. For example, the card UI actors have a listener that sets the controller's selected card. The dice button has a listener that triggers the controller's selection check, plays an animation then and then rolls the dice and then plays the card. The played card then runs the relevant CardAction object which will usually do something to the controller and then start a new turn.

One of my main problems occurs when I have the AI player. As soon as it starts its turn (technically triggered by the player pressing the dice button) it runs through it and instantly ends its turn, throwing control back to the human player with no warning that anything's happened. It should at least run the dice animation but it happens so quickly that the previous animation hasn't finished.

I can see that the card actions basically do what they should (at least the ones I've implemented so far) as does playing the card (most times - once I hit turn 3 things start to break down and I can't seem to find out why). I've coded a fair few desktop apps where things generally want to happen as soon as they can and I've coded a fair few 2D games but they tend to be quite simple, not really needing interactive UIs. I'm having trouble with this game because I can't work out how to separate the UI from the general logic.

Ideally I'd like the AI player to emulate human decision making by having a slight random delay and to have the cards animate around the screen as they're played so you can see what's happening and at some point I'd like to have the turns be timed but the way I've implemented things at the moment this seems unlikely to happen.

I had thought of making everything in the game an event and giving the controller an update() method that runs during the screen's render() method but again I’m not sure if this would solve the issues I’m having or just shift them into a different form.

This is my thinking for an event system.
Code: Select all
public interface IGameEvent {
   public void run();
}
//pseudo
controller.update() {
     for( GameEvent event : eventsReceived ) {
   if( event.hasBeenProcessed() ) {
      // remove and don’t process, might do this in a second loop at the end of the method
   }
   if( event instanceof PlayCardEvent ) { // this is just so you can see my thinking, I’d do this as GameEvent interface
      //start an animation of the selected card appearing onscreen then settling in the history queue, set processed
      // trigger a new turn event, this will be handled next turn if all animations have been finished
   }
   if( event instanceof NewTurnEvent ) {
      // reset the player UI, notify the current player that it’s their turn, etc etc, play an animation for the notification label to appear with a “your turn/other player’s turn” notification
   }   
     }
}


A basic overview of my current code structure is as such
Code: Select all
GameScreen - should show the game to a human player
    controller - instance of a GameController implementation
    CardListener – sets the current selected card in the controller
    DiceListener – rolls the dice, adds a sequence action to the dice button, plays the card if allowed
    (other non interactive UI stuff such as current turn, scoreboard and a notification label that hints at player errors and turn changes etc)
GameController -  should be the top level controller of the game
    TurnManager - should control turns for the controller (new turn, start turn, end turn methods)
    TeamManager - should hold info on the players in the game (current team, current player in the team)
    ScoreRules - Handles scoring and deciding if the game's been won.


Code: Select all
public class SomeCardAction implements ICardAction {

   // Run when the game decides this is the action to play
   public void run( IGameController game ) {
      if( condition ) {
         game.scoreRules.scorePoint(game.getCurrentPlayer() );
         game.getTurnManager().newTurn();
      }
   }

}


One thing I’m not clear on from the documentation is if it’s valid to create custom actor actions that modify non-actor, game state. For example dice animation is a sequence (run once for each die on screen):
Code: Select all
moveTo( randomX, randomY, 0.3f, elasticIn ),
moveTo( originalPositionX, originalPositionY, 0.4f, eleasticOut )


Ideally somewhere in the middle of that I’d like to generate the random dice roll and then update the dice icon with the appropriate pips image.
Code: Select all
moveTo( randomX, randomY, 0.3f, elasticIn ),
moveTo( originalPositionX, originalPositionY, 0.4f, eleasticOut ),
rollDiceAction( min, max ), // accesses the controller’s dice object
SetDiceImageAction( dieID, diceValueX ), // where dieID is the current die actor, x is the current image holding x pips
delay(2f)


I’m sorry if this is a bit broad but I’m really confused on the structure and hopefully it’ll help show where I’m going wrong without needing to paste vast amounts of code.

Any help would be greatly appreciated.
drentsoft
 
Posts: 2
Joined: Tue May 31, 2016 9:35 am

Re: Turn Based Card Game Structure-animations and turn contr

Postby DranikProgrammer » Tue Mar 14, 2017 5:57 am

I didn't quite understand your problem, but I think you could add a custom action to the actors sequence, which changes the turn, or says the AI to play on. So you will always wait until the animstion finishes to play on.
Make sure your actions act method returns true, otherwise it will play endless
DranikProgrammer
 
Posts: 34
Joined: Mon Feb 20, 2017 2:51 pm

Re: Turn Based Card Game Structure-animations and turn contr

Postby drentsoft » Wed Mar 15, 2017 12:49 pm

Thanks for the reply. I think really my main problem is keeping concerns separated between the UI and the controller. At the moment I'm working on a basic single player implementation between human and AI but once that's done I'd like to be able to do networked human vs human or even AI vs AI so having everything triggered by user interaction with the UI feels off but I'm struggling to find a suitable structure for the game to take all the little working bits of code that I have and making it feel like an actual game.
drentsoft
 
Posts: 2
Joined: Tue May 31, 2016 9:35 am

Re: Turn Based Card Game Structure-animations and turn contr

Postby DranikProgrammer » Wed Mar 15, 2017 4:06 pm

Yes, main difficulty is writing a clean project structure. I'd recommend you to use an ECS like Ashley in future projects.
DranikProgrammer
 
Posts: 34
Joined: Mon Feb 20, 2017 2:51 pm


Return to General Development

Who is online

Users browsing this forum: No registered users and 2 guests