아카이브

Action 대리자 연습(HitDamage) 본문

C#프로그래밍

Action 대리자 연습(HitDamage)

timbercat 2023. 7. 27. 15:16

Hero.cs

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

namespace LearnDotnet
{
    public class Hero
    {
        public int hp;
        public int maxHp;

        //생성자
        public Hero() 
        {
            this.hp = 30;
            this.maxHp = this.hp;
        }

        //남은체력
        public void HitDamage(int damage, Action<int, int> callback)
        {
            this.hp -= damage;
            Console.WriteLine("공격받았습니다. {0}만큼 hp가 깎입니다.",damage);
            callback(this.hp, this.maxHp);
        }

        //죽었는지 살았는지
        public void HitDamage(int damage, Action<bool> callback)
        {
            this.hp -= damage;
            callback(this.hp < 0);
        }
    }
}

App.cs

using LearnDotnet;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace LearnDotnet
{
    public class App
    {
        //생성자 
        public App()
        {
            Hero hero = new Hero();
            //대리자 초기화
            hero.HitDamage(3, (hp, maxHp) =>
            {
                Console.WriteLine("남은 체력을 표시합니다: {0}/{1}", hp, maxHp);
            });

            hero.HitDamage(3, (isDie) => {
                Console.WriteLine("죽었습니까? = {0}", isDie);
            });
        }
    }
}

'C#프로그래밍' 카테고리의 다른 글

직렬화 역직렬화 연습  (0) 2023.07.28
json 연습(몬스터, 영웅, 아이템, 인벤토리 )  (0) 2023.07.28
Action 대리자 연습  (0) 2023.07.27
Dictionary 인벤토리*  (0) 2023.07.26
Dictionary 연습2  (0) 2023.07.26