Tip:
Highlight text to annotate it
X
In this video, I'm going to write script code to model the state of the game and draw the
current game state.
If you haven't read the blog posts describing the re-use model code, now would be a good
time to do that.
There are links below.
The game model will include the player state; which is just how many chips they have and
which cards they have taken.
And the non-player state; which includes the player order and current player, the shuffled
deck of gift cards, the current card being bid on, and the number of chips already bid.
The re-use scripts already model the list of players, the player order, and the current
player along with each player's table position and color.
So I'll start by adding code to the Game model for the deck of cards and the number of chips.
Double click on the Game script to bring up visual studio.
The deck of cards can be modeled as a list of integers since the card's value is its
only feature.
I don't need to separately store the current card being auctioned.
I'll just assume that the top card on the deck is the one that is face up.
For the player, I'll use a list of integers to represent the cards that they have taken
and an integer number of chips they currently have.
I'll also write the function to calculate the player's score since I'm going to need
that to display on the player UI.
The player's score is the number of chips that they have; minus the value of the cards
that they have taken.
Except that a sequence of cards only costs the value of the smallest card.
So if the gap between cards is greater than one, I'll include that card's value.
For this function to work correctly, I need to make sure the cards are sorted.
I'll also want to draw the cards sorted, so I'll go ahead and re-sort the list whenever
a new card is added.
Next I'll write code to initialize the game in the EInitialize event.
For "No Thanks!", EInitialize needs to pick a starting player, set the game state to PLAY,
make a gift deck with cards 3-35, shuffle it and remove 9 of the cards randomly.
It also needs to give the players the correct number of chips based on how many players
there are.
The EInitialize event is added to the timeline by the main menu when the game is started.
This event executes after EReset which resets the model, and EAddPlayer which adds the players.
That is the whole game model.
Now I'll write code to draw that game model to the UI.
First I'm going to make a Card GUI class that will draw a face up card.
I always add new script classes through the Unity editor.
The CardGUI class will contain the three text elements (center, upper left and score line)
and will have a draw function that takes the value of the card and a flag for whether or
not to display the score text.
This will allow the CardGUI to draw both the card up for auction in the center and the
cards the player has taken.
The GameGUI script is responsible for drawing the non-player specific areas of the UI.
For "No Thanks!", it will draw the number of cards left in the gift deck, the card on
offer and the number of chips played.
This line of code is finding the TextMesh behavior on a specifically named child of
the chip.
This saves adding another member to the behavior which would need to be set in the editor,
but it also uses a string literal that you have to make sure to not change in the editor.
I end up using these literal lookups more than I should, especially in larger games
where there are a lot of UI elements being set by the script.
I'll only show the chip icon with the number of chips played if chips have been added.
The size of the deck is one less than the number of items in the list because the top
item is the face up card.
If the gift deck is empty I want to hide the face up card instead of drawing it.
The player UI is the most complicated part of this game.
It needs to add card and spacer objects to the horizontal layout of cards based on the
cards the player takes during the game.
To do that, I'll store the card and spacer prefabs along with the card container they
will be put in.
The script also needs the total score and chip objects.
I'll set the player area to the color of the player.
The chip object has two TextMesh children, one for the number of chips displayed on the
chip and another for the score line.
This code will set them both to the current number of chips.
The total score is available from the player class.
The draw function is called by the event system when the player area needs to be re-drawn
because the player model has changed.
However, the cards themselves may not have changed, so I'll only re-draw the card area
if there is a different number of cards than the last time the area was drawn.
I'm doing this check because of the overhead of destroying and creating objects.
But it probably doesn't really matter for this game.
If the number of cards is different, I'll delete all the cards and spacers and re-create
them.
I'll add a spacer if there is a gap between two cards.
I do this by instantiating the spacer prefab and adding it to the card container.
Passing false for "worldPositionStays" allows the container to size and position the spacer.
I'll do the same thing for the card itself and will use the Instantiate function that
sets the parent at the same time.
I'll use the CardGUI class to draw the value of the card.
The score text should be drawn if this is the first card in a sequence.
Finally, I'll add code to the player dialog script to draw the gift icon that indicates
who's turn it is.
The re-use code already draws the ribbon.
The GiftIcon should be active if we are playing the game and the player that is represented
by this dialog is the current player.
Now I can return to Unity and link the script member variables to the UI elements.
First I'll double check that I used the correct name for the chip's "count" TextMesh child.
Add the UI elements and prefabs to the PlayerGUI behavior.
Then apply the change to the PlayerArea prefab.
Next, add the PlayerAreas to the GameGUI by locking the Inspector and dragging in all
the player areas.
Then add the other UI elements.
I'm not able to add the drawnCard because I need to add the CardGUI behavior to the
drawnCard prefab.
Add that behavior and drag in the TextMesh elements.
Apply that to the drawnCard prefab, then drag the card object into the GameGUI.
Finally add the gift icon to the playerDialog behavior and apply it to the prefab
Now we can run the game and see that the UI is being drawn to match the state of the game.
That concludes the model and UI code.
In the next video, I'll create the events that will progress the game and add buttons
with callbacks to allow the players to interact with the UI.