Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Tiny Card Game. Hard to come out of OOP way of thinking

Discussion in 'Project Tiny' started by sathya, Jan 14, 2019.

  1. sathya

    sathya

    Joined:
    Jul 30, 2012
    Posts:
    297
    Hi.
    I am trying to set up a simple card game with a deck of 52 cards. But stuck at a place where I can't think in ECS way to handle deck data. Here is the Deck system

    Code (CSharp):
    1.  
    2. namespace game {
    3.  
    4.     /** New System */
    5.     export class DeckSystem extends ut.ComponentSystem {
    6.  
    7.         runOnce: Boolean = false;
    8.         deck: Deck;
    9.  
    10.  
    11.         OnUpdate(): void {
    12.             var gameConfig = this.world.getConfigData(GameConfig);
    13.  
    14.             //once we are in game scene
    15.             if (gameConfig.gameUIState == GameUiStates.GameScreen) {
    16.                 if (this.runOnce) return;
    17.                 this.runOnce = true;
    18.  
    19.                 //Create deck
    20.                 this.deck = new Deck();
    21.  
    22.                 let deckSize: number = this.deck.RemainingCards();
    23.                 console.log("Deck size " + deckSize);
    24.                 for (let i: number = 0; i < deckSize; i++) {
    25.                     let card: Card = this.deck.GetCard(i);
    26.                     if (card != null)
    27.                         console.log("Deck i " + i + " rank " + card.rank + " suit " + card.suit);              
    28.                 }
    29.                
    30.  
    31.                
    32.             }
    33.         }
    34.     }
    35. }
    36.  
    and a Deck itself
    Code (CSharp):
    1. namespace game {
    2.  
    3.     export class Deck {
    4.         /// <summary>
    5.         /// Contains 52 cards (4 suits with 13 cards each)
    6.         /// </summary>
    7.         public cards: List<Card>;
    8.  
    9.         constructor() {
    10.             this.cards = new List<Card>();
    11.             this.CreateDeck();
    12.         }
    13.  
    14.         /// <summary>
    15.         /// Creates the deck of cards, from Ace to King by suit.
    16.         /// </summary>
    17.         /// <returns>The initialized deck of cards</returns>
    18.         CreateDeck(): void {
    19.             for (let suitIndex: number = 0; suitIndex < 4; suitIndex++) {
    20.                 for (let cardNumberIndex: number = 1; cardNumberIndex < 14; cardNumberIndex++) {
    21.                     let card: Card = new Card();
    22.                     card.suit = suitIndex;
    23.                     card.rank = cardNumberIndex;
    24.                     this.cards.add(card);
    25.                     console.log("CreateDeck rank " + card.rank + " suit " + card.suit);
    26.                 }
    27.             }
    28.         }
    29.  
    30.  
    31.         ReturnCardToDeck(card: Card): void {
    32.             if (card != null)
    33.                 this.cards.add(card);
    34.         }
    35.  
    36.         GetCard(index: number): Card {
    37.             if (index < this.cards.size()) {
    38.                 let card: Card = this.cards.get(index);
    39.                 if (card != null)
    40.                     return card;
    41.                 else
    42.                     console.log("get card failed at " + index + " size " + this.cards.size());
    43.  
    44.             }
    45.             else
    46.                 return null;
    47.         }
    48.  
    49.         GetRandomCard(): Card {
    50.             if (this.cards.size() > 0) {
    51.                 let randIndex: number = Math.random() * this.cards.size() - 1;
    52.                 console.log("GetRandomCard randIndex " + randIndex);
    53.                 return this.cards.get(randIndex);
    54.             }
    55.             else
    56.                 return null;
    57.         }
    58.  
    59.         RemainingCards(): number {
    60.             return this.cards.size();
    61.         }
    62.     }
    63. }
    List is a helper class just to make array handling bit easier
    Code (CSharp):
    1. class List<T> {
    2.     private items: Array<T>;
    3.  
    4.     constructor() {
    5.         this.items = [];
    6.     }
    7.  
    8.     size(): number {
    9.         return this.items.length;
    10.     }
    11.  
    12.     add(value: T): void {
    13.         this.items.push(value);
    14.     }
    15.  
    16.     get(index: number): T {
    17.         return this.items[index];
    18.     }
    19. }
    and Tiny components "Card", "CardRank" and "CardSuit" created in the editor(Check the attached screenshots)

    I am guessing I am going in the wrong direction. Not sure how to how to deal with the data in ECS.
    And this Deck seems more like an OOP object. Please share some ideas and correct me. Thanks
     

    Attached Files: