11.26 Hero와 Monster의 App에 의한 수동적 싸움 (delegate)
C#/실습 2019. 11. 26. 17:40코드:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace _11._26_study2
{
class App
{
private delegate void Del(Monster monster, Action<Monster> action);
Action<Monster> monsterHit;
Action<Hero, Monster> attackAction;
public App()
{
var hero = new Hero(10);
var monster = new Monster(100);
//monsterHit = (monster) =>
//{
// Console.WriteLine("몬스터의 체력이 감소되었습니다. 몬스터의 현재체력: {0}", monster.hp);
// if (monster.hp > 0)
// {
// }
// else
// {
// }
//};
//**********************************************
//모든 과정을 App을 통해 전달:
//어떻게 맞는지 정의
monster.GetDamageAc = (damage) =>
{
monster.GetDamage(damage);
};
//어떻게 때리는지 정의
hero.iDamaged = (damage) =>
{
Console.WriteLine("영웅이 때렸습니다. 데미지:{0}", damage);
monster.GetDamageAc(damage); //너 맞으래
};
//얘네 서로 어떻게싸우는지 정의
attackAction = (hero, monster) =>
{
{
Thread.Sleep(300);
hero.iDamaged(hero.damage); //얘가 때린다
AttackUntilDie(hero, monster, attackAction);
}
else
{
}
};
AttackUntilDie(hero, monster, attackAction);
}
public void AttackUntilDie(Hero hero, Monster monster, Action<Hero, Monster> attackAction)
{
attackAction(hero, monster); //얘네 서로 싸운다
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
using System;
using System.Collections.Generic;
using System.Text;
namespace _11._26_study2
{
class Hero
{
public int damage;
public Action<int> iDamaged;
public Hero(int damage)
{
this.damage = damage;
}
//public void Attack(Monster monster, Action<Monster> attackComplete)
//{
// //몬스터의 체력을 감소시킨다
// attackComplete(monster);
//}
public void BlindAttack()
{
iDamaged(this.damage);
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
using System;
using System.Collections.Generic;
using System.Text;
namespace _11._26_study2
{
class Monster
{
public int hp;
public Action onDie;
public Action<int> GetDamageAc;
public Monster(int hp)
{
this.hp = hp;
}
public void MyHpIsZero()
{
if(this.hp <=0)
{
onDie(); //나 죽었소
}
}
public void GetDamage(int damage)
{
this.hp -= damage;
GetDamageAc(hp); // 나 맞았소
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
출력:

'C# > 실습' 카테고리의 다른 글
12.05 유닛 바라보기 (LookAt 메서드 사용X) (1) | 2019.12.05 |
---|---|
11.29 캐릭터 움직이기 (Coroutine, Update 비교 및 활용) (0) | 2019.11.29 |
11.13 unity 유닛 동적생성, 유닛 이동 및 사정거리 내 진입시 공격하기 (0) | 2019.11.13 |
11.12 Unity 케릭터 공격모션 + 상대방 피격모션 구현하기 (0) | 2019.11.12 |
Unity 11.11 케릭터 동적 생성하고 무기 장착및 교체하기 (0) | 2019.11.11 |