스토리지

[3.24] Action을 이용한 배럭스 본문

Unity/수업내용(C#)

[3.24] Action을 이용한 배럭스

ljw4104 2021. 3. 24. 12:31
using System;

namespace Study10
{
    public class Unit
    {
        public eUnitType unitType;
        public Unit(eUnitType type)
        {
            Console.WriteLine("{0}이 생성되었습니다.", type);
            unitType = type;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Threading;

namespace Study10
{
    public enum eUnitType
    {
        MARINE,
        FIREBAT,
        MEDIC
    }

    public class Barracks
    {
        Queue<eUnitType> process;

        public Barracks()
        {
            Console.WriteLine("Barracks가 생성되었습니다.");
            process = new Queue<eUnitType>();
        }

        public void CreateUnit(eUnitType[] unitTypes, Action<Unit> callback)
        {
            foreach (var unitType in unitTypes)
            {
                process.Enqueue(unitType);
            }

            while (process.Count != 0)
            {
                Console.Write("대기열 : ");
                foreach (var unitType in process)
                {
                    Console.Write("{0} ", unitType);
                }
                Console.WriteLine();

                for (int i = 10; i <= 100; i += 10)
                {
                    Console.Write("III");
                    Thread.Sleep(100);
                }
                Console.WriteLine();
                callback(new Unit(process.Dequeue()));
                Thread.Sleep(500);
                Console.WriteLine();
            }
        }
    }
}
using System;

namespace Study10
{
    public class App
    {
        public App()
        {
            Barracks barracks = new Barracks();
            eUnitType[] order = { 
                eUnitType.FIREBAT, 
                eUnitType.MARINE, 
                eUnitType.MARINE, 
                eUnitType.MEDIC 
            };

            barracks.CreateUnit(order, (unit) =>
            {
                Console.WriteLine(unit.unitType);    //Marine
            });
        }
    }
}

대기열 구현은 Queue로 하였다.

Comments