상세 컨텐츠

본문 제목

Study_004 - UnityActionExmaple

C#/수업내용

by McRobbin 2020. 5. 11. 18:36

본문

 

UnityActionExmaple.unitypackage
1.51MB

프로젝트 파일 입니다.

 

 

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;
 
public class InGame : MonoBehaviour
{
    public Hero user;
    public Hero target;
 
    public Text text;
 
    private void Start()
    {
        
    }
 
    public void InitHero(int id)
    {
        int userId = id;
        int targetId = (userId == 101) ? 103 : 101;
 
        CharacterData data1 = DataManager.Instance.GetDataById(userId);
        CharacterData data2 = DataManager.Instance.GetDataById(targetId);
 
        var modelGo1 = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/" + data1.res_name));
        var modelGo2 = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/" + data2.res_name));
 
        var heroGo1 = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/" + "Hero"));
        var heroGo2 = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/" + "Hero"));
 
        this.user = heroGo1.AddComponent<Hero>();
        this.target = heroGo2.AddComponent<Hero>();
 
        modelGo1.transform.SetParent(heroGo1.transform, false);
        modelGo2.transform.SetParent(heroGo2.transform, false);
 
        heroGo1.transform.Translate(heroGo1.transform.right * -2);
        heroGo2.transform.Translate(heroGo2.transform.right * 2);
 
        heroGo1.transform.LookAt(heroGo2.transform.position);
        heroGo2.transform.LookAt(heroGo1.transform.position);
 
        user.name = "User";
        target.name = "Target";
 
        user.Init(userId, modelGo1);
        target.Init(targetId, modelGo2);
 
        user.target = target.gameObject;
        target.target = user.gameObject;
 
        user.Move();
        target.Move();
 
        user.onArriveAction += () =>
        {
            user.Attack();
        };
 
        target.onArriveAction += () =>
        {
            target.Attack();
        };
 
        user.onDieAction += () =>
        {
            this.text.text = "";
            this.text.text = "승자 : target";
            this.user.Die();
            this.target.Idle();
            this.text.gameObject.SetActive(true);
        };
 
        target.onDieAction += () =>
        {
            this.text.text = "";
            this.text.text = "승자 : user";
            this.target.Die();
            this.user.Idle();
            this.text.gameObject.SetActive(true);
        };
    }
 
 
}
 
 
cs

InGame.cs 입니다.

 

 

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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
 
public class Lobby : MonoBehaviour
{
    public Button leftBtn;
    public Button rightBtn;
 
    private int choose;
 
    // Start is called before the first frame update
    void Start()
    {
        SceneManager.sceneLoaded += SceneManager_sceneLoaded;
        leftBtn.onClick.AddListener(() =>
        {
            //101
            this.choose = 101;
            SceneManager.LoadScene("InGame");
        });
 
        rightBtn.onClick.AddListener(() => 
        {
            //103
            this.choose = 103;
            SceneManager.LoadScene("InGame");
        });
    }
 
    private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
    {
        FindObjectOfType<InGame>().InitHero(this.choose);
    }
}
 
 
cs

Lobby.cs 입니다.

 

 

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.UI;
using UnityEngine;
using UnityEngine.Events;
 
public class Hero : MonoBehaviour
{
    public enum eState
    {
        Run,
        Idle,
        Attack,
        Die
    }
 
    public UnityAction onDieAction;
    public UnityAction onArriveAction;
    public GameObject target;
 
    private string heroName;
    private int hp;
    private int damage;
    private eState state;
 
    private GameObject model;
    private Animation anim;
    private float time;
    private string animName;
 
    private int seed;
    private System.Random rand;
    public void Init(int id, GameObject model)
    {
        var data = DataManager.Instance.GetDataById(id);
 
        this.seed = id;
        this.rand = new System.Random(this.seed);
        this.state = eState.Idle;
        this.heroName = data.name;
        this.hp = data.hp;
        this.damage = data.damage;
        this.model = model;
        this.anim = this.model.GetComponent<Animation>();
    }
 
    public void Move()
    {
        this.animName = "run@loop";
        this.anim.Play(this.animName);
        this.state = eState.Run;
    }
 
    public void Attack()
    {
        this.state = eState.Attack;
        this.time = 2.0f;
    }
 
    public void Idle()
    {
        this.state = eState.Idle;
        this.animName = "idle@loop";
        this.anim.Play(this.animName);
    }
 
    public void Die()
    {
        this.state = eState.Die;
        this.animName = "die";
 
    }
 
    private void Update()
    {
        
        switch (this.state)
        {
            case eState.Attack:
                this.time += Time.deltaTime;
                if(this.anim[this.animName].length <= this.time)
                {
                    var targetComp = target.GetComponent<Hero>();
                    targetComp.hp -= this.damage;
                    if (targetComp.hp <= 0)
                        targetComp.onDieAction();
                    animName = "attack_sword_0" + (rand.Next(3+ 1);
                    this.anim.Play(animName);
                    this.time = 0.0f;
                }
 
                break;
            case eState.Idle:
 
                break;
            case eState.Run:
                this.transform.position += ((target.transform.position - this.transform.position).normalized * 1.0f * Time.deltaTime);
                if(Vector3.Distance(this.gameObject.transform.position, target.transform.position) <= 0.5f)
                {
                    this.onArriveAction();
                }
                break;
 
            case eState.Die:
                this.anim.Play(this.animName);
                break;
 
            default:
 
                break;
 
        }
    }
 
}
 
 
cs

 

Hero.cs 입니다.

 

 

 

 

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

Unity Study_007 무기변경, 닭 잡기.  (0) 2020.05.14
Unity Study_006 Effect and WeaponChange  (0) 2020.05.13
Unity Study_003 5Run  (0) 2020.05.08
Study_019_2 일일 출석 보상  (0) 2020.04.29
Study_018-1 아이템 상점 구현.  (0) 2020.04.28

관련글 더보기