개인 프로젝트

인벤토리 구현 하기

OSOR2 2020. 2. 2. 20:21

단순하게 다형성을 이용한 인벤토리 시스템. 쓸일이 있어 프로토타입용으로 구현

모자란 지식으로 만들었기에 지적할 사항 있으면 댓글로 남겨주세요. 

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
 
namespace inventoryExample
{
 
    class Program
    {
        public enum itemType { Weapon, Supply, Ingredient }
        static void Main(string[] args)
        {
            Sword sword = new Sword();
            Gun gun = new Gun();
            Postion postion = new Postion();
            List<Item> items = new List<Item>();
            items.Add(postion);
            items.Add(gun);
            items.Add(sword);
            foreach (Item item in items)
            {
                switch(item.itemType)
                {
                    case itemType.Weapon:
                        Weapon weapon = (Weapon)item;
                        weapon.Animation();
                        break;
                    case itemType.Ingredient:
                        ingredient ingredient = (ingredient)item;
                        
                        break;
                    case itemType.Supply:
                        Supply supply = (Supply)item;
                        supply.Use();
                        break;
                   
                }
            }
 
        }
    }
    class Sword : Weapon
    {
        public Sword()
        {
            itemCode = 0;
            displayName = "칼";
            detail = "기본적인 검";
            damage = 10;
        }
        public override void Animation()
        {
            Console.WriteLine("검을 휘두른다");
        }
    }
    class Gun : Weapon
    {
        public Gun()
        {
            itemCode = 1;
            displayName = "총";
            detail = "그냥 권총";
            damage = 50;
        }
        public override void Animation()
        {
            Console.WriteLine("총을 쏜다");
        }
    }
    class Postion : Supply
    {
        public Postion()
        {
            itemCode = 2;
            displayName = "물약";
            detail = "마시멱 피가 차는 물약";
        }
        public override void Use()
        {
            Console.WriteLine("물약을 먹는다");
        }
    }
    abstract class Weapon : Item  //무기
    {
        public Weapon()
        {
            itemType = Program.itemType.Weapon;
        }
        public int damage { get; set; } //데미지
 
        public abstract void Animation();
    }
    abstract class Supply : Item//소모품
    {
        public Supply()
        {
            itemType = Program.itemType.Supply;
        }
        public abstract void Use();
    }
    abstract class ingredient : Item //재료
    {
        public ingredient()
        {
            itemType = Program.itemType.Ingredient;
        }
    }
     class Item //아이템 
    {
        public inventoryExample.Program.itemType itemType;
        public  int itemCode { get; set; } //아이템 코드
        public  string displayName { get; set; }
        public  string detail { get; set; }
    }
}
 
cs

결과