With the main menu sorted from last week, the next objective was actually adding cards to the game scene. Rather than layout every card individually and be limited to the space available, we were taught a clever way to set up the scene so that cards are generated upon playing the game. In order to do this I needed to add a background to the game scene, which is what would control the card position and generation for me. The component that allows me to preset the positions and spacing of the cards is a 'Grid Layout Group', which has cell sizing and spacing values. I needed to add a few cards initially and set the cell size to fit the card, then adjust the spacing to my liking. From there I set the starting position (top left), alignment (middle centre) and constraint count i.e. number of rows(4).
 |
Components in the background object |
Once the card layout was done I could clear all cards off the scene and start on the generating scripts. To start a card face is needed so we created a card model class that creates the variables, then a behaviour class which assigns them to the card. The next step was having a game controller that creates a grid list for cards to be generated into and then fills it. However this on it's own would put each matching pair together, so we need something to shuffle the cards around. We were provided with a complex method that made use of the generics we were taught two weeks ago by Richard. The method uses the 'RNGCryptoServiceProvider' class which is essentially a random number generator. From what I can gather, it uses this to generate a byte for a box position and then assign a card to that space. The finished product of this week is a game screen filled with shuffled cards.
 |
Shuffled cards in game |
 |
Shuffling code |
No comments:
Post a Comment