C#/과제

2020.05.12. 과제 - Scene전환, Json파일 로드, 캐릭터 생성 및 애니메이션 실행

dev_sr 2020. 5. 12. 00:36

해결 못한 것

1) 캐릭터들이 서로를 향해서 이동할 때 속도가 일정하지 않은 것

2) 공격이 연속으로 3번 들어가는 것 

 

 

1. App

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEngine;
using UnityEngine.SceneManagement;
 
public class App : MonoBehaviour
{
    private void Start()
    {
        DontDestroyOnLoad(this.gameObject);
 
        SceneManager.LoadScene("Logo");
        DataManager.GetInstance().Load();
    }
}
 

 

 

2. Logo

 

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.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.SceneManagement;
 
public class Logo : MonoBehaviour
{
 
    private float time;
    private bool isSceneLoad;
    private void Start()
    {
        this.isSceneLoad = true;
    }
 
    private void Update()
    {
        if (this.isSceneLoad)
        {
            this.time += Time.deltaTime;
 
            if (this.time >= 3)
            {
                SceneManager.LoadScene("Title");
                this.isSceneLoad = false;
            }
        }
    }
}
 

 

 

3. Title

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
 
public class Title : MonoBehaviour
{
    
    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
 
        }
        if(Input.GetMouseButtonUp(0))
        {
            SceneManager.LoadScene("Lobby");
        }
    }
}
 

 

 

4. Lobby 

 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
 
public class Lobby : MonoBehaviour
{
    public Button onePickBtn;
    public Button twoPickBtn;
    public int fighterId;
    public int enemyId;
 
    void Start()
    {
        this.onePickBtn.onClick.AddListener(() =>
        {
            Debug.Log("1픽 선택");
            SceneManager.sceneLoaded += SceneManager_sceneLoaded;
            this.fighterId = 100;
            this.enemyId = 101;
            SceneManager.LoadScene("InGame");
 
        });
 
        this.twoPickBtn.onClick.AddListener(() =>
        {
            Debug.Log("2픽 선택");
            SceneManager.sceneLoaded += SceneManager_sceneLoaded;
            this.fighterId = 101;
            this.enemyId = 100;
            SceneManager.LoadScene("InGame");
        });
    }
 
    private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
    {
        var inGameGo = GameObject.Find("InGame");
        var inGame = inGameGo.GetComponent<InGame>();
        inGame.fighterId = this.fighterId;
        inGame.enemyId = this.enemyId;
 
    }
}
 

 

 

5. InGame

 

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
 
public class InGame : MonoBehaviour
{
    public int fighterId;
    public int enemyId;
    public Fighter fighter;
    public Enemy enemy;
    public Button playBtn;
    public Text textBox;
 
    private Animation fighterAnim;
    private Animation enemyAnim;
    private float elapsedTime;
    private bool areRun;
    private float fighterIdleAnimLength;
    private float enemyIdleAnimLength;
    private float fighterAttackAnimLength;
    private float enemyAttackAnimLength;
    private float fighterDamageAnimLength;
    private float enemyDamageAnimLength;
    private bool isFighterAttack;
    private bool isEnemyAttack;
    private float attackTime;
    private float attackElapsedTime;
    private float damageElapsedTime;
    private bool isFighterDead;
    private bool isEnemyDead;
   
 
    private UnityAction onHpZeroComplete;
 
    void Awake()
    {
        Debug.Log("hi");
 
    }
 
    private void Start()
    {
        this.fighter.Init(this.fighterId);
        this.enemy.Init(this.enemyId);
 
        this.enemy.transform.LookAt(this.fighter.transform);
 
        this.fighterAnim = this.fighter.model.GetComponent<Animation>();
        this.enemyAnim = this.enemy.model.GetComponent<Animation>();
 
        this.fighterIdleAnimLength = this.fighterAnim["idle@loop"].length;
        this.enemyIdleAnimLength = this.enemyAnim["idle@loop"].length;
 
        this.fighterDamageAnimLength = this.fighterAnim["damage"].length;
        this.enemyDamageAnimLength = this.enemyAnim["damage"].length;
 
 
        this.fighterAttackAnimLength = this.fighterAnim["attack_sword_01"].length;
        this.enemyAttackAnimLength = this.enemyAnim["attack_sword_01"].length;
 
        float totalFrame = this.fighterAnim["attack_sword_01"].clip.frameRate * this.fighterAttackAnimLength;
 
        this.attackTime = this.fighterAttackAnimLength * 8 / totalFrame;
 
 
        this.playBtn.onClick.AddListener(() =>
        {
 
            this.Run();
 
        });
 
    }
 
    private void Run()
    {
        this.areRun = true;
        this.enemyAnim.Play("run@loop");
        this.fighterAnim.Play("run@loop");
    }
 
    private void Stop()
    {
        this.areRun = false;
        this.enemyAnim.Play("idle@loop");
        this.fighterAnim.Play("idle@loop");
 
        if (this.fighter.speed > this.enemy.speed)
        {
            this.isFighterAttack = true;
        }
 
        else if (this.enemy.speed > this.fighter.speed)
        {
            this.isEnemyAttack = true;
        }
 
    }
 
    private void Update()
    {
        if (this.areRun)
        {
            this.elapsedTime += Time.deltaTime;
 
            float distance = Vector3.Distance(this.fighter.transform.position, this.enemy.transform.position);
 
            this.fighter.transform.Translate(this.elapsedTime * this.fighter.speed * 0.1f * Vector3.forward);
            this.enemy.transform.Translate(this.elapsedTime * this.enemy.speed * 0.1f * Vector3.forward);
 
            if (distance <= 0.5f)
            {
                this.Stop();
            }
        }
 
 
 
        if (this.isFighterAttack)
        {
            this.damageElapsedTime += Time.deltaTime;
            this.attackElapsedTime += Time.deltaTime;
 
            this.FighterAttack();
        }
 
        if (this.isEnemyAttack)
        {
            this.damageElapsedTime += Time.deltaTime;
            this.attackElapsedTime += Time.deltaTime;
            this.EnemyAttack();
        }
 
 
        if (this.isFighterDead == true)
        {
            this.isEnemyAttack = false;
            this.isFighterAttack = false;
            this.isFighterDead = false;
            this.onHpZeroComplete();
        }
 
        if (this.isEnemyDead == true)
        {
            this.isEnemyAttack = false;
            this.isFighterAttack = false;
            this.isEnemyDead = false;
            this.onHpZeroComplete();
        }
    }
 
    private void FighterAttack()
    {
        this.fighterAnim.Play("attack_sword_01");
 
        if (this.attackElapsedTime >= this.attackTime)
        {
            this.attackElapsedTime = 0;
            this.enemy.hp -= this.fighter.damage;
            
            this.EnemyHit();       
            
            Debug.LogFormat("적의 남은 체력: {0}"this.enemy.hp);
 
            
        }
 
        
    }
 
    private void EnemyHit()
    {
        this.enemyAnim.Play("damage");
 
 
        if (this.damageElapsedTime > this.enemyDamageAnimLength)
        {
            if (this.enemy.hp <= 0)
            {
                this.isEnemyDead = true;
            }
 
            this.onHpZeroComplete += () =>
            {
                this.EnemyDie();
            };
 
 
            this.damageElapsedTime = 0;
            this.attackElapsedTime = 0;
 
            this.isEnemyAttack = true;
            this.isFighterAttack = false;
        }
 
    }
 
    private void EnemyAttack()
    {
        this.enemyAnim.Play("attack_sword_01");
 
        if (this.attackElapsedTime >= this.attackTime)
        {
            this.attackElapsedTime = 0;
            this.fighter.hp -= this.enemy.damage;
            this.FighterHit();
 
            Debug.LogFormat("파이터의 남은 체력: {0}"this.fighter.hp);
 
        }
 
    }
 
    private void FighterHit()
    {
        this.fighterAnim.Play("damage");
        
 
        if (this.damageElapsedTime > this.fighterDamageAnimLength)
        {
            
 
            if (this.fighter.hp <= 0)
            {
                this.isFighterDead = true;
            }
 
            this.onHpZeroComplete += () =>
            {
                this.FighterDie();
            };
 
            this.damageElapsedTime = 0;
            this.attackElapsedTime = 0;
 
            this.isFighterAttack = true;
            this.isEnemyAttack = false;
        }
    }
 
    private void FighterDie()
    {
 
        this.fighterAnim.Play("die");
        this.enemyAnim.Play("tumbling");
 
        this.textBox.text = this.enemy.enemyName + " 승리!";
 
    }
 
    private void EnemyDie()
    {
        this.enemyAnim.Play("die");
        this.fighterAnim.Play("tumbling");
 
        this.textBox.text = this.fighter.fighterName + " 승리!";
    }
}
 
 
 

 

 

6. DataManager

 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.Linq;
 
public class DataManager
{
 
    private static DataManager instance;
    private Dictionary<int, FighterData> dicFighterdatas;
 
    private DataManager()
    {
        this.dicFighterdatas = new Dictionary<int, FighterData>();
    }
 
    public static DataManager GetInstance()
    {
        if(DataManager.instance==null)
        {
            DataManager.instance = new DataManager();
            return DataManager.instance;
        }
        return DataManager.instance;
    }
 
    public void Load()
    {
        TextAsset textAsset = Resources.Load("Data/fighter_data"as TextAsset;
        string json = textAsset.text;
 
        this.dicFighterdatas = JsonConvert.DeserializeObject<FighterData[]>(json).ToDictionary(x => x.id, x=> x);
 
    }
 
    public FighterData GetFighterData(int id)
    {
        return this.dicFighterdatas[id];
    }
 
}
 

 

 

7. FighterData 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class FighterData 
{
    public int id;
    public string name;
    public string res_name;
    public int hp;
    public int damage;
    public float speed;
 
}
 

 

 

8. Fighter 

 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Fighter : MonoBehaviour
{
    public int fighaterId;
    public int enemyId;
    public float speed;
    public int hp;
    public int damage;
    public string fighterName;
    public GameObject model;
    
    public void Init(int fighaterId)
    {
        this.fighaterId = fighaterId;
 
        FighterData fighterData = DataManager.GetInstance().GetFighterData(this.fighaterId);
 
        this.speed = fighterData.speed;
        this.hp = fighterData.hp;
        this.damage = fighterData.damage;
        this.fighterName = fighterData.name;
 
        string path = string.Format("Prefabs/{0}", fighterData.res_name);
        var fighterGo = Resources.Load(path) as GameObject;
 
        this.model = Instantiate(fighterGo);
        this.model.transform.SetParent(this.gameObject.transform);
        this.transform.position = new Vector3(000);
 
        
    }
 
 
}
 

 

 

9. Enemy 

 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Enemy : MonoBehaviour
{
    public int enemyId;
    public float speed;
    public int hp;
    public int damage;
    public string enemyName;
    public GameObject model;
 
    public void Init(int enemyId)
    {
        this.enemyId = enemyId;
 
        FighterData enemyData = DataManager.GetInstance().GetFighterData(this.enemyId);
 
        this.speed = enemyData.speed;
        this.hp = enemyData.hp;
        this.damage = enemyData.damage;
        this.enemyName = enemyData.name;
        Debug.Log(this.speed);
        Debug.Log(enemyData.speed);
 
        string path = string.Format("Prefabs/{0}", enemyData.res_name);
        var enemyGo = Resources.Load(path) as GameObject;
 
        this.model = Instantiate(enemyGo);
        this.model.transform.SetParent(this.gameObject.transform);
        this.transform.position = new Vector3(004);
 
       
        
    }
}