C#/과제

2020.05.10. 과제 - 캐릭터 달리기, 순위 매기기

dev_sr 2020. 5. 10. 01:27

캐릭터 5명이 달려서 목적지에 도달하는 순서대로 등수를 매기고

등수에 따라 다른 애니메이션을 한다(1등은 텀블링, 2,3,등은 idle, 4,5등은 die)

또한 1,2,3등의 복제가 만들어져서 등수대로 단상 위에 올라간다.

 

1. App

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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.UI;
 
public class App : MonoBehaviour
{
    private GameObject heroPrefab;
    private GameObject hero;
    private List<GameObject> heroPrefabList;
    private int heroCount;
    private Hero heroInstance;
    private int nameCount;
    private int posCount;
    private int buttonCount;
    private bool isRun;
    private int isArriveCount;
    private List<Hero> arriveHeroList;
    private bool isGetGrade;
    private Camera cameraMainCamera;
    private Camera cameraSecondCamera;
 
    public GameObject mainCamera;
    public GameObject secondCamera;
    public string[] arrName;
    public Button makeBtn;
    public Button runBtn;
    public Button resultBtn;
    public List<Hero> heroList;
    public Text textBox;
    public GameObject GradeCube1;
    public GameObject GradeCube2;
    public GameObject GradeCude3;
 
 
    // Start is called before the first frame update
    void Start()
    {
        this.cameraMainCamera = this.mainCamera.GetComponent<Camera>();
        this.cameraSecondCamera = this.secondCamera.GetComponent<Camera>();
 
        this.cameraMainCamera.enabled = true;
        this.cameraSecondCamera.enabled = false;
 
        this.heroPrefabList = new List<GameObject>();
 
        this.heroList = new List<Hero>();
 
        this.arrName = new string[5] { "가가""나나""다다""라라""마마" };
 
        //this.runBtn.enabled = false;
        //this.resultBtn.enabled = false;
        this.runBtn.GetComponent<Button>().interactable = false;
        this.resultBtn.GetComponent<Button>().interactable = false;
 
        this.makeBtn.onClick.AddListener(() =>
        {
            this.Load();
 
            var heroGo = this.CreateInstanceHero();
            this.heroInstance = heroGo.AddComponent<Hero>();
            var herPrefabGo = this.CreateInstanceHeroPrefab();
            herPrefabGo.transform.SetParent(heroGo.transform);
 
            string heroName = this.GiveName();
            this.SetPostion(herPrefabGo);
 
            this.heroInstance.Init(herPrefabGo, heroName);
 
            this.heroList.Add(heroInstance);
 
            this.buttonCount++;
 
            if (this.buttonCount == 5)
            {
                //버튼 비활성화
                this.makeBtn.GetComponent<Button>().interactable = false;
                //버튼 활성화
                this.runBtn.GetComponent<Button>().interactable = true;
 
                //this.runBtn.enabled = true;
                //this.makeBtn.enabled = false;
 
                this.RunButtonAction();
 
            }
        });
 
    }
 
    private void Load()
    {
        this.hero = Resources.Load("Prefabs/Hero"as GameObject;
        this.heroPrefabList.Add(Resources.Load<GameObject>("Prefabs/ch_01_01"));
        this.heroPrefabList.Add(Resources.Load<GameObject>("Prefabs/ch_02_01"));
        this.heroPrefabList.Add(Resources.Load<GameObject>("Prefabs/ch_03_01"));
        this.heroPrefabList.Add(Resources.Load<GameObject>("Prefabs/ch_04_01"));
        this.heroPrefabList.Add(Resources.Load<GameObject>("Prefabs/ch_05_01"));
 
        this.heroPrefab = this.heroPrefabList[this.heroCount];
        this.heroCount++;
    }
 
    private GameObject CreateInstanceHero()
    {
        return Instantiate(this.hero);
    }
 
    private GameObject CreateInstanceHeroPrefab()
    {
        return Instantiate(this.heroPrefab);
    }
 
    private string GiveName()
    {
 
        string heroName = this.arrName[nameCount];
        this.nameCount++;
        return heroName;
 
    }
 
    private void SetPostion(GameObject heroPrefabs)
    {
        heroPrefabs.transform.position = new Vector3(this.posCount, 00);
        this.posCount++;
    }
 
    private void RunButtonAction()
    {
 
        this.runBtn.onClick.AddListener(() =>
        {
            this.Run();
            this.runBtn.GetComponent<Button>().interactable = false;
 
        });
    }
 
    private void Update()
    {
 
        if (this.isRun ==true)
        {
            foreach (var data in this.heroList)
            {
                data.transform.Translate(Vector3.forward * data.speed * Time.deltaTime);
 
                if(data.isArriveCount==1)
                {
                    this.isArriveCount += data.isArriveCount;
                    data.isArriveCount = 0;               
                }
            }
 
            if(this.isArriveCount==5)
            {
                this.Stop();
            }
                             
        }
        if(this.isGetGrade)
        {
            this.Ceremony();
        }
        
    }
 
    private void Run()
    {
        foreach (var data in this.heroList)
        {
            data.anim.Play("run@loop");
            data.isRun = true;
        }
        this.isRun = true;
    }
 
 
    private void Stop()
    {
        this.isRun = false;
 
        this.GetGrade();
    }
 
    private void GetGrade()
    {
        this.arriveHeroList = new List<Hero>();
 
        foreach (var data in this.heroList)
        {
            arriveHeroList.Add(data);
        }
 
        Hero temp = null;
 
        for(int j=0; j<arriveHeroList.Count; j++)
        {
            for (int i = 0; i < arriveHeroList.Count; i++)
            {
                if (i == arriveHeroList.Count - 1)
                {
                    break;
                }
 
                if (arriveHeroList[i].elapsedTime > arriveHeroList[i + 1].elapsedTime)
                {
                    temp = arriveHeroList[i];
                    arriveHeroList[i] = arriveHeroList[i + 1];
                    arriveHeroList[i + 1= temp;
                }
 
            }
        }
 
        this.isGetGrade = true;
        this.Die();
        this.Print();
    }
 
    private void Ceremony()
    {
        arriveHeroList[0].anim.Play("tumbling");
    }
 
    private void Die()
    {
        arriveHeroList[arriveHeroList.Count-2].anim.Play("die");
        arriveHeroList[arriveHeroList.Count-1].anim.Play("die");
        this.resultBtn.GetComponent<Button>().interactable = true;
        this.ResultButtonAction();
    }
 
    private void Print()
    {
        for(int i=0; i<this.arriveHeroList.Count; i++)
        {
            Debug.LogFormat("순위: {0}\t이름: {1}\t시간: {2:f2}"
                i + 1this.arriveHeroList[i].heroName, this.arriveHeroList[i].elapsedTime);
            
 
        }
 
        textBox.text = "1등  " + this.arriveHeroList[0].heroName.ToString() + " 시간 (" + this.arriveHeroList[0].elapsedTime.ToString("N2"+ ")\n"+
                       "2등  " + this.arriveHeroList[1].heroName.ToString() + " 시간 (" + this.arriveHeroList[1].elapsedTime.ToString("N2"+ ")\n"+
                       "3등  " + this.arriveHeroList[2].heroName.ToString() + " 시간 (" + this.arriveHeroList[2].elapsedTime.ToString("N2"+ ")\n"+
                       "4등  " + this.arriveHeroList[3].heroName.ToString() + " 시간 (" + this.arriveHeroList[3].elapsedTime.ToString("N2"+ ")\n"+
                       "5등  " + this.arriveHeroList[4].heroName.ToString() + " 시간 (" + this.arriveHeroList[4].elapsedTime.ToString("N2"+ ")\n";
    }
 
    private void ResultButtonAction()
    {
        this.resultBtn.onClick.AddListener(() =>
        {
            this.cameraMainCamera.enabled = false;
            this.cameraSecondCamera.enabled = true;
            this.SetGradeBoxPosition();
        });
    }
 
    private void SetGradeBoxPosition()
    {
        List<GameObject> heroGradeList = new List<GameObject>();
 
        for(int i=0; i<3; i++)
        {
            heroGradeList.Add(Instantiate(this.arriveHeroList[i].heroGo));
        }
 
        heroGradeList[0].transform.position = new Vector3
            (this.GradeCube1.transform.position.x,this.GradeCube1.transform.position.y+0.5f,this.GradeCube1.transform.position.z);
        heroGradeList[1].transform.position = new Vector3
            (this.GradeCube2.transform.position.x, this.GradeCube2.transform.position.y + 0.5f, this.GradeCube2.transform.position.z);
        heroGradeList[2].transform.position = new Vector3
            (this.GradeCude3.transform.position.x, this.GradeCude3.transform.position.y + 0.35f, this.GradeCude3.transform.position.z);
        this.resultBtn.GetComponent<Button>().interactable = false;
    }
}
 
 

 

 

2. Hero

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
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
 
public class Hero : MonoBehaviour
{
    public GameObject heroGo;
    public string heroName;
    public float speed;
    public Animation anim;
    public Vector3 goal;
    public float distance;
    public bool isRun;
    public int isArriveCount;
    public float elapsedTime;
 
 
    public void Init(GameObject heroGo, string heroName)
    {
        this.heroGo = heroGo;
        this.heroName = heroName;
 
        this.speed = Random.Range(0.5f, 1.5f);
 
        this.anim = this.heroGo.GetComponent<Animation>();
        this.goal = new Vector3(heroGo.transform.position.x, 03);
 
 
    }
 
    private void Update()
    {
        if(this.isRun)
        {
            this.elapsedTime += Time.deltaTime;
            this.distance = Vector3.Distance(this.heroGo.transform.position, this.goal);
 
            if (this.distance <= 0.12f)
            {
                this.Stop();
               
            }
        }
        
    }
 
    private void Stop()
    {
        this.anim.Play("idle@loop");
        this.heroGo.transform.position = this.goal;
        this.isArriveCount = 1;
        this.isRun = false;
        this.speed = 0;
    }
    
}

 

 

 

 

 

Study_003.unitypackage
0.05MB