2018.03.26 수업내용 (열거형 형식, LINQ)
카테고리 없음 2018. 5. 15. 21:23반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class App : MonoBehaviour { // Use this for initialization void Start () { Inventory inventory = new Inventory(); Gold gold = new Gold(); gold.SetItemData(new ItemData(0, "골드", ItemData.ItemType.ORE)); Flounder flounder1 = new Flounder(); flounder1.SetItemData(new ItemData(0, "가재미", ItemData.ItemType.FISH)); Flounder flounder2 = new Flounder(); flounder2.SetItemData(new ItemData(1, "눈이 큰 가재미", ItemData.ItemType.FISH)); Flounder flounder3 = new Flounder(); flounder3.SetItemData(new ItemData(2, "길 ~ 쭉한 가재미", ItemData.ItemType.FISH)); inventory.Add(gold); inventory.Add(flounder1); inventory.Add(flounder2); inventory.Add(flounder3); var fishes = inventory.GetItemList(ItemData.ItemType.FISH); foreach (var fish in fishes) { Debug.Log(fish.data.name); } } // Update is called once per frame void Update () { } } | cs |
1 2 3 4 5 6 7 8 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class FishItem : Item { } | cs |
1 2 3 4 5 6 7 8 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Flounder : FishItem { } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class ItemData { public enum ItemType { NONE, WAEPON, ORE, //광물 FISH //물고기 } public ItemType itemType; public int id; public string name; public ItemData(int id, string name, ItemType itemType) { this.id = id; this.name = name; this.itemType = itemType; } } | cs |
1 2 3 4 5 6 7 8 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Gold : OreItem { } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; public class Inventory { private List<Item> itemList = new List<Item>(); public Inventory() { //생성자 } public void Add(Item item) { itemList.Add(item); } public List<Item> GetItemList(ItemData.ItemType itemType) { return this.itemList.Where(x => x.data.itemType == itemType).ToList(); } public Item GetItem(ItemData.ItemType itemType, int id) { return this.itemList.Find(x => x.data.id == id && x.data.itemType == itemType); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Item { public ItemData data; public void SetItemData(ItemData data) { this.data = data; } } | cs |
1 2 3 4 5 6 7 8 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class OreItem : Item { } |
반응형