Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CalW256 · Jun 14, 2019 at 05:21 AM · boardgame

How to make card effects in board game?

I am making a board game. In the game there is a set of task tiles, where each tile has a requirement for the player to meet. For example: Tile 1: The player needs 3 stone to complete this task. Tile 2: The player needs 2 blue cards to complete this task. Tile 3: The player needs 3 red cards to complete this task. etc. Players can collect these tiles and, if they meet the conditions of the tile, score points. All of a player's inventory data (amount of resources, number of cards, etc.) is held in a Player class: public class Player : Monobehaviour { // The int value is the amount of the resource the player owns public Dictionary ResourceHolder = new Dictionary { {ResourceType.stone, 0}, {ResourceType.wood, 0} };

     public Dictionary<CardType, int> CardHolder
       = new Dictionary<CardType, int>
       { {CardType.blue, 0}, {CardType.red, 0} };
 }
 
 public enum ResourceType
 {
     stone,
     wood
 }
 
 public enum CardType
 {
     blue,
     red
 }

Instead of creating a new class for every tile (which seems like a bad idea, but I could be wrong), I've thought about creating a single Task class and having each tile do something different based on an array of methods. Each tile would have an index, which is how it's CheckRequirements() method would be determined: public class Task : MonoBehaviour { public Player Owner; public int TileIndex;

 // All the different checks a tile could make
     private Func<Player, bool>[] CheckArray = new Func<Player, bool>[]
     { player => CheckStone3(player), () => CheckRedCard2(player) };
 
     public bool CheckRequirements()
     {
         return CheckArray[TileIndex](Owner);
     }
 
 // Actions that CheckArray holds
     private bool CheckStone3(Player p)
     {
         if (p.ResourceHolder[ResourceType.stone] >= 3)
             return true;
         return false;
     }
 
     private bool CheckRedCard2(Player p)
     {
         if (p.CardHolder[CardType.red] >= 2)
             return true;
         return false;
     }
 
 // etc. etc. ...
 
 }

The Player could have a List storing all the task tiles they have, then the Player would merely need to call CheckRequirements() on the task to see if they meet the task’s requirements. However, this is still hard-coded. Is there a way to make an array of methods where each method can take in different parameters? Then I could do something like: private bool CheckResource(Player p, ResourceType resource, int amount) { if (p.ResourceHolder[resource] >= amount) return true; return false; } In the future there may be a great number of things to check, and they may require checking things not stored in the Player class (e.g. It needs to be Round 2 to complete this task). The array of methods seems better than making a bunch of classes, but still not great. Plus each Tile will be holding this massive array and only access a small portion of it, which seems wasteful. What are some suggestions as to how to create these tiles so that it's easy to expand in the future? Should I continue with the array of functions or do something else?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kennai · Jun 14, 2019 at 07:03 AM

Hello! I've made few classes for you. You only need to create new script for your card and add field Item and assign needed Item there. You can create a list for Items and easely add new items like

 list.Add(new ResourceItem(ResourceType.stone, 4));

or

 list.Add(new CardItem(CardType.red, 2));

also you can check in loop if player have needed card or resource

 for(int i=0;i<list.Count;i++)
   if(list[i].CheckResource(ResourceType.stone, 2) )
   {
     if(list[i].TakeItem(2))
     {//TakeItem returns true if Item.Amount == TakenAmount. It means you take whole Item from inventory 
       //dont forget to remove card (list[i] item) from UI too

       list.RemoveAt(i);
     }
     break;
   }

here is classes:

 public class Item
 {
     public Item()
     {
     }
 
     public virtual bool CheckResource(ResourceType resType, int amount)
     { return false; }
 
     public virtual bool CheckCard(CardType cardType, int amount)
     { return false; }
 
     public virtual bool TakeItem(int amount)
     {//return true if taken amount == amount of item, then you will remove item (card) completelly from player inventory 
         return false; 
     }
 }
 
 public class ResourceItem : Item
 {
     public ResourceType ResourceType { get; private set; }
     public int amount;
 
     public ResourceItem(ResourceType resourceType, int amount)
     {
         this.ResourceType = resourceType;
         this.amount = amount;
     }
 
     public override bool CheckResource(ResourceType resType, int amount)
     {
         return ResourceType == resType && this.amount >= amount;
     }
 
     public override bool TakeItem(int amount)
     {
         this.amount -= amount;
         if (this.amount == 0)
             return true;
         else
             return false;
     }
 }
 
 public class CardItem : Item
 {
     public CardType cardType { get; private set; }
     public int amount;
 
     public CardItem(CardType cardType, int amount)
     {
         this.cardType = cardType;
         this.amount = amount;
     }
 
     public override bool CheckCard(CardType cardType, int amount)
     {
         return this.cardType == cardType && this.amount >= amount;
     }
 
     public override bool TakeItem(int amount)
     {
         this.amount -= amount;
         if (this.amount == 0)
             return true;
         else
             return false;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

yield for animation 0 Answers

Board Game Movement 0 Answers

How do you move a character to another waypoint after landing on a specific waypoint 1 Answer

how i can Roll a dice?? 2 Answers

What is the best way to make cards that move game peices 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges