1. App.cs

Hero hero2 = new Hero(3, 3);
            hero2.Move((x, y) =>
            {
                Console.WriteLine("({0}, {1})로 이동 완료", x, y);
            });

            Hero hero3 = new Hero(2);
            Monster mon = new Monster("용", 10);
            hero3.SetTarget(mon);
            hero3.Attack((target) =>
            {
                target.hitAction = (id) =>
                {
                    Console.WriteLine("{0}이 피해를 받았습니다.", id);
                };

                target.Hit(2);
            });

2. Hero.cs

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

namespace Study06
{
    public class Hero
    {
        public Action attackCompleteAction;
        
        private int damage;

        private int x;
        private int y;
        private Monster target;

        // 생성자
        public Hero(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public Hero(int damage)
        {
            this.damage = damage;
        }

        public void Move(Action<int, int> callback)
        {
            callback(this.x, this.y);
        }

        public void Attack(Action<Monster> callback)
        {
            this.target.hp -= this.damage;
            callback(this.target);
        }

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

3. Monster.cs

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

namespace Study06
{
    public class Monster
    {
        public Action<string> hitAction;
        public Action<string> dieAction;

        private string id;
        public int hp;
        private int maxHp;

        // 생성자
        public Monster(string id, int maxHp)
        {
            this.id = id;
            this.maxHp = maxHp;
            this.hp = this.maxHp;
        }


        public void DieAction(string id)
        {
            this.dieAction(id);
        }

        public void Hit(int damage)
        {
            this.hp -= damage;
            this.hitAction(this.id);
        }
    }
}

실행 결과

 

'C# > 수업 내용' 카테고리의 다른 글

Json 연습 2 - 캐릭터, 경험치 데이터  (1) 2022.06.21
Json 연습 - 같은 아이템의 개수(amount) 증가  (0) 2022.06.20
2차원 배열 맵타일  (0) 2022.06.15
2048 왼쪽 이동  (0) 2022.06.15
인벤토리 예제  (0) 2022.06.14

+ Recent posts