본문 바로가기
c#/수업 내용

오전 수업 대리자 연습 (고블린, SCV)

by 이지훈26 2021. 9. 7.

위 사진을 수정

----------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld1
{
    class App
    {
        public Goblin goblin;

        //생성자
        public App()
        {
            this.goblin = new Goblin();


            //3.대리자 인스턴스화
            goblin.onAttackComplete = this.OnattackComplete;
            goblin.ChangeState(Goblin.eState.Idle);
            goblin.ChangeState(Goblin.eState.Attack);
            Goblin.eState state = goblin.GetState();
            Console.WriteLine("state: {0}", state); //Idle
        }


        //2.대리자에 연결할 메서드 정의
        public void OnattackComplete()
        {
            this.goblin.ChangeState(Goblin.eState.Idle);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld1
{
    class Goblin
    {
        public enum eState
        {
            Idle, Attack
        }

        //1. 대리자 선언
        public delegate void Del();
        //대리자 변수 정의
        public Del onAttackComplete;

        private eState State;

        //생성자
        public Goblin()
        {

        }

        public void ChangeState(eState state)
        {
            this.State = state;
            Console.WriteLine(this.State);
            if(this.State == eState.Attack)
            {
                this.onAttackComplete();
            }
        }

        public eState GetState()
        {
            return this.State;
        }
    }
}

-----------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld1
{
    class App
    {
        

        //생성자
        public App()
        {
            SCV scv = new SCV(100);
            

            //3.대리자 인스턴스화
            scv.Build(this.BuildconpleteHandler);
        }

        //3.대리자에 연결할 메서드 정의
        void BuildconpleteHandler(int id)
        {
            Console.WriteLine("conplete! : {0}", id);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld1
{
    class SCV
    {
        //1.대리자 정의
        public delegate void Del(int id);

        
        public int id;

        public SCV(int id)
        {
            this.id = id;
        }

        public void Build(Del del)
        {
            //4.대리자 호출
            del(this.id);
        }
    }
}

-----------------------------------------------------------------------------------------------------------------------------------