Event와 Delegate를 사용한 Hero의 Move(), Attack() //Monster의 Hit() 구현

Console Programming/C# Console 2019. 11. 29. 15:11
>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1129
{
    public class App
    {
        private Hero hero;
        private Monster monster;
        public App()
        {
            this.hero = new Hero();
            this.monster = new Monster();
            Vector3 movePos = new Vector3(0, 0, 1);

            this.hero.SetTarget(this.monster);   //처음에 한번 타겟을 정한다.         

            //공격 델리게이트 호출시 메서드연결
            this.hero.OnAttack = () =>
            {
                if(this.hero.target!=null)//타겟이 있다면 공격한다.
                {
                    Console.WriteLine("공격");
                    this.monster.Hit(1);
                }
                this.hero.OnAttackComplete();
            };

            //공격완료 델리게이트 호출시 메서드 연결
            this.hero.OnAttackComplete = OnAttackComplete;

            //이동완료 이벤트 구독
            this.hero.OnMoveCompleteHandler2 += (args) =>
            {
                var eventArgs = (HeroEventArgs)args;
                Console.WriteLine("{0}로 이동을 완료했습니다.", eventArgs.vector.ToString());
                this.hero.Attck(this.hero.target);
            };

            this.hero.Move(movePos);

            //this.hero.OnMoveComplete = (tpos) =>
            //{
            //    Console.WriteLine("{0}로 이동을 완료했습니다", tpos.ToString());
            //};

            //this.hero.Move(movePos);

            //this.hero.Move(movePos, (tpos) =>
            // {
            //     Console.WriteLine("{0}로 이동을 완료했습니다", tpos.ToString());
            // });
        }

        //공격완료시 실행할 메서드
        public void OnAttackComplete()
        {
            Console.WriteLine("공격이 완료되었습니다");
            if(this.monster.hp>0)
            {
                this.hero.Attck(this.hero.target);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1129
{
    //EventArgs를 상속받는 HeroEventArgs를 만듦
    //HeroEventArgs는 좌표를 정보로 가지고 있는 argument
    public class HeroEventArgs : EventArgs
    {
        public Vector3 vector;
        public HeroEventArgs(Vector3 pos)
        {
            this.vector = pos;
        }
    }

    public class Hero
    {

        public delegate void EventHandler(EventArgs args); //공용 이벤트를 처리하기 위한 EventHandler 선언

        public Monster target; //Hero의 타겟 필드

        public void SetTarget(Monster target)
        {
            this.target = target;
        }       

        public Action<Vector3> OnMoveComplete;
        public event EventHandler OnMoveCompleteHandler;
        public event EventHandler OnMoveCompleteHandler2;
        public Action OnAttack;
        public Action OnAttackComplete;

        public void Move(Vector3 tpos)
        {
            Console.WriteLine("{0}로 이동합니다 : Move(Vector3 tpos)", tpos.ToString());

            EventArgs args = new HeroEventArgs(tpos);

            this.OnMoveCompleteHandler2.Invoke(args);
        }

        internal void Attck(object target)
        {
            this.OnAttack();
        }


        //public void Move(Vector3 tpos)
        //{
        //    Console.WriteLine("{0}로 이동합니다 : Move(Vector3 tpos)", tpos.ToString());
        //    if(OnMoveComplete!=null)
        //    {
        //        OnMoveComplete(tpos);
        //    }            
        //}
        public void Move(Vector3 tpos, Action<Vector3> action)
        {
            Console.WriteLine("{0}로 이동합니다 : Move(Vector3 tpos, Action<Vector3> action)", tpos.ToString());

            action(tpos);
        }
       
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1129
{
    public class Monster
    {
        public int hp = 3;
        public void Hit(int damage)
        {
            this.hp -= damage;
            Console.WriteLine("몬스터가 공격 받았습니다{0}", this.hp);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1129
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1129
{
    public struct Vector3
    {
        public float x;
        public float y;
        public float z;

        public Vector3(float x, float y, float z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }
        public override string ToString()
        {
            return string.Format($"{x},{y},{z}");
        }
    }
}

 

: