BCA/4. Unity

25일 : 연습용 슈팅게임 완성

Beabletoet 2018. 3. 22. 17:51
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//GameManagerScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Net.Json;
 
 
 
public class GameManagerScript : MonoBehaviour
{
 //   public static GameManagerScript instance = null;
 
    public GameObject background = null;
    public GameObject backGroundManager = null;
 
    public GameObject EffectManager = null;
 
    public GameObject MenuGameStartButton = null;
    public GameObject MenuGameExitButton = null;
    public GameObject MenuGameShopButton = null;
 
    public GameObject playerShip = null;
    public GameObject player = null;
 
    public GameObject StoneManager = null;
 
    public float makeStoneCoolTime = 5.0f;
    public bool boolInGame = false;
    public bool firstScore = true;
    public bool playerFullHp = true;
    public int score;
    public float makeStoneTime;
    public int stageLv;
    public int playerLife;
    public float gameOverTime;
    public int highScore;
    public int gold;
    public bool thisRoundHighScore;
    public int playerDam;
    public bool gameoverDelay = false;
 
    public int[] ShopGoldPrice = new int[10] { 1050100200300500700100015003000};
 
    public Image[] NowPlayerLifeSpirte = null;
 
    //점수용
    public Image[] HighNum = null;
    public Image[] num = null;
    public Sprite[] numSprites = null;
 
    //스톤 프리팹풀링 용
    public int[] stoneKeyValue = new int[8];
    private Dictionary<int, Queue<GameObject>> stonePool = new Dictionary<int, Queue<GameObject>>();
    public GameObject[] stones = new GameObject[8];
 
    //이펙트 프리팹 풀링용
    private Dictionary<int, Queue<GameObject>> effectPool = new Dictionary<int, Queue<GameObject>>();
    public GameObject[] effects = new GameObject[2];
 
    //금전수수용
    public Image[] goldImage = null;
 
    //상점용
    public Image[] priceImage = null;
    public Image[] lvImage = null;
 
    void Awake()
    {
        //instance = this;
        CreateStonePool();
        CreateEffectPool();
        
    }
    void Start()
    {
        MenuGameStartButton.transform.position = new Vector3(030);
        MenuGameShopButton.transform.position = new Vector3(000);
        MenuGameExitButton.transform.position = new Vector3(0-30);
        getGameData();
        SetFirstHighScore();
        ChangeGold();
        MakeBackground();
        MakeMenuLayer();
    }
 
    // Update is called once per frame
    void Update()
    {
        if (boolInGame)
        {
            makeStoneTime += Time.deltaTime;
            if (makeStoneTime >= makeStoneCoolTime)
            {
                int stoneType;
                if (stageLv < 5)
                    stoneType = Random.Range(04);
                else
                {
                    int now = Random.Range(05);
                    if (now == 4)
                        stoneType = Random.Range(48);
                    else
                        stoneType = Random.Range(04);
                }
                PopStoneByPool(stoneType);
                makeStoneTime -= makeStoneCoolTime;
            }
        }
        else if(gameoverDelay)
        {
            gameOverTime += Time.deltaTime;
            if (gameOverTime >= 5.0f)
            { 
                MakeMenuLayer();
            }
        }
    }
 
    void MakeBackground()
    {
        for (int i = 0; i < 9++i)
            for (int j = 0; j < 7++j)
            {
                GameObject nowBG = GameObject.Instantiate(background, new Vector3((i * 2.5f) - 10.0f, 6.0f - (j * 2.5f), 0), Quaternion.identity);
                nowBG.transform.parent = backGroundManager.transform;
            }
    }
 
    void MakeMenuLayer()
    {
        //init
        gameoverDelay = false;
        thisRoundHighScore = false;
        gameOverTime = 0;
        makeStoneCoolTime = 3.0f;
        score = 0;
        makeStoneTime = makeStoneCoolTime - 1.0f;
        firstScore = true;
        stageLv = 1;
        playerLife = 3;
        playerFullHp = true;
        //----------
        boolInGame = false;
        MenuGameExitButton.SetActive(true);
        MenuGameShopButton.SetActive(true);
        MenuGameStartButton.SetActive(true);
        for (int i = 0; i < 6++i)
        {
            num[i].gameObject.SetActive(false);
            if (i < 3)
                NowPlayerLifeSpirte[i].gameObject.SetActive(false);
        }
        SetPriceAndLv();
    }
 
    public void MakeGameLayer()
    {
        boolInGame = true;
        MenuGameExitButton.SetActive(false);
        MenuGameShopButton.SetActive(false);
        MenuGameStartButton.SetActive(false);
        MakePlayer();
        ChangeScore();
        SeeLife();
        player.transform.position = new Vector3(0-40);
    }
 
    public void SetPriceAndLv()
    {
        int nowLv = playerDam - 10;
        int nowPrice = ShopGoldPrice[nowLv];
        int[] nowPrices = new int[4];
        int[] nowLvs = new int[2];
        for (int i = 1000, j = nowPrice, k = 0, l = nowLv; i > 0; i /= 10++k)
        {
            nowPrices[k] = j / i;
            j %= i;
            if(i<100)
            {
                nowLvs[k - 2= l / i;
                l %= i;
            }
        }
        for (int i = 0; i < 4++i)
        {
            priceImage[i].sprite = numSprites[nowPrices[i]];
            if (i < 2)
                lvImage[i].sprite = numSprites[nowLvs[i]];
        }
    }
 
    public void AtkLvUp()
    {
        int nowLv = playerDam - 10;
        if(nowLv >= 10)
        {
            //다 샀음
        }
        else
        {
            int nowPrice = ShopGoldPrice[nowLv];
            if(nowPrice <= gold)
            {//구매가능
                gold -= nowPrice;
                ++playerDam;
                SetPriceAndLv();
                setGameData();
            }
            else
            {//돈 부족
 
            }
        }
    }
 
    void MakePlayer()
    {
        if (player == null)
        {
            player = GameObject.Instantiate(playerShip, new Vector3(0-40), Quaternion.identity);
            player.transform.parent = this.transform;
        }
        else
            player.SetActive(true);
    }
 
    public void ChangeGold()
    {
        int[] nowGold = new int[4];
        for(int i = 1000, j = gold, k = 0; i > 0; i/=10++k)
        {
            nowGold[k] = j / i;
            j %= i;
        }
        for (int i = 0; i < 4++i)
            goldImage[i].sprite = numSprites[nowGold[i]];
    }
 
    public void ChangeScore()
    {
        if (!num[0].gameObject.activeInHierarchy)
            for (int i = 0; i < 6++i)
                num[i].gameObject.SetActive(true);
        int[] nowScore = new int[6];
        for (int i = 100000, j = score, k = 0; i > 0; i /= 10++k)
        {
            nowScore[k] = j / i;
            j %= i;
        }
        for (int i = 0; i < 6++i)
            num[i].sprite = numSprites[nowScore[i]];
 
        if (score / 3000 + 1 > stageLv)
        {
            if (makeStoneCoolTime > 0.5f)
            {
                makeStoneCoolTime -= 0.3f;
                ++stageLv;
            }
        }
        if (highScore < score)
        {
            highScore = score;
            for (int i = 0; i < 6++i)
                HighNum[i].sprite = numSprites[nowScore[i]];
            if (!thisRoundHighScore)
                thisRoundHighScore = true;
        }
    }
 
    public void SetFirstHighScore()
    {
        int[] nowScore = new int[6];
        for (int i = 100000, j = highScore, k = 0; i > 0; i /= 10++k)
        {
            nowScore[k] = j / i;
            j %= i;
        }
        for (int i = 0; i < 6++i)
            HighNum[i].sprite = numSprites[nowScore[i]];
    }
 
    public void SeeLife()
    {
        if (playerLife == 3)
        {
            for (int i = 0; i < 3++i)
                NowPlayerLifeSpirte[i].gameObject.SetActive(true);
        }
        else if (playerLife > -1 && playerLife < 3)
            NowPlayerLifeSpirte[playerLife].gameObject.SetActive(false);
    }
 
    void CreateStonePool()
    {
        for (int i = 0; i < 8++i)
        {
            stoneKeyValue[i] = stones[i].GetInstanceID();
            if (!stonePool.ContainsKey(stoneKeyValue[i]))
                stonePool.Add(stoneKeyValue[i], new Queue<GameObject>());
            for (int j = 0; j < 50++j)
            {
                GameObject thisObj = Instantiate(stones[i], new Vector3(1001000), Quaternion.identity);
                if(i < 4)
                    thisObj.GetComponent<StoneScript>().hp = Random.Range(2050);
                else
                    thisObj.GetComponent<StoneScript>().hp = Random.Range(5090);
                thisObj.SetActive(false);
                thisObj.transform.parent = StoneManager.transform;
                stonePool[stoneKeyValue[i]].Enqueue(thisObj);
            }
        }
    }
 
    void CreateEffectPool()
    {
        EffectManager = GameObject.FindWithTag("EffectManager");
        for (int i = 0; i < 2++i)
        {
            string a = i.ToString();
            if (!effectPool.ContainsKey(20 + i))//20과 21이 각각 데미지, 디스트로이 이펙트
                effectPool.Add(20 + i, new Queue<GameObject>());
            for(int j = 0; j < 50++j)
            {
                GameObject thisObj = Instantiate(effects[i], new Vector3(1001000), Quaternion.identity);
                thisObj.SetActive(false);
                thisObj.transform.parent = EffectManager.transform;
                effectPool[(20 + i)].Enqueue(thisObj);
            }
        }
    }
 
    void PopStoneByPool(int stoneType)
    {
        float stoneXPos = Random.Range(-10.0f, 10.0f);
        GameObject thisStone = stonePool[stoneKeyValue[stoneType]].Dequeue();
        thisStone.transform.position = new Vector3(stoneXPos, 70);
        thisStone.SetActive(true);
        stonePool[stoneKeyValue[stoneType]].Enqueue(thisStone);
    }
 
    public void popEffectByPool(int effectType, Vector3 position)
    {
        GameObject thisEffect = effectPool[effectType].Dequeue();
        thisEffect.transform.position = position;
        thisEffect.SetActive(true);
        thisEffect.GetComponent<EffectScript>().livingEffect = true;
        effectPool[effectType].Enqueue(thisEffect);
    }
 
    public void hitPlayer()
    {
        --playerLife;
        SeeLife();
        popEffectByPool(20, player.transform.position);
        if (playerLife < 1)
        {
            GameOver();
            popEffectByPool(21, player.transform.position);
        }
    }
    public void GameOver()
    {
        gold += (score / 1000);
        if (thisRoundHighScore)
            gold += 10;
        ChangeGold();
        player.SetActive(false);
        boolInGame = false;
        setGameData();
        gameoverDelay = true;
    }
 
    public void getGameData()
    {
        string s;
        try
        {
            s = System.IO.File.ReadAllText("GameData.json");
        }
        catch
        {
            playerDam = 10;
            highScore = 0;
            gold = 0;
            setGameData();
            s = System.IO.File.ReadAllText("GameData.json");
        }
        JsonTextParser textParser = new JsonTextParser();
        JsonObjectCollection root = textParser.Parse(s) as JsonObjectCollection;
        JsonArrayCollection arr = root["UI"as JsonArrayCollection;
        foreach(JsonObjectCollection obj in arr)
        {
            highScore = (int)(obj["HighScore"as JsonNumericValue).Value;
            gold = (int)(obj["Gold"as JsonNumericValue).Value;
            playerDam = (int)(obj["Damage"as JsonNumericValue).Value;
        }
        Debug.Log(highScore);
        Debug.Log(gold);
    }
    public void setGameData()
    {
        JsonObjectCollection root = new JsonObjectCollection();
        JsonArrayCollection arr = new JsonArrayCollection("UI");
        JsonObjectCollection ui = new JsonObjectCollection();
        ui.Add(new JsonNumericValue("HighScore", highScore));
        ui.Add(new JsonNumericValue("Gold", gold));
        ui.Add(new JsonNumericValue("Damage", playerDam));
        arr.Add(ui);
        root.Add(arr);
        string s = root.ToString();
        System.IO.File.WriteAllText("GameData.json", s);
    }
}
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
//ButtonSizeUpScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
 
public class ButtonSizeUpScript : MonoBehaviour
{
    public GameObject GameManager = null;
    public int buttonType; //1 = start, 2 = exit, 3 = Shop
    void OnMouseEnter()
    {
        gameObject.transform.DOScale(new Vector3(666), 0.2f);
    }
 
    void OnMouseExit()
    {
        gameObject.transform.DOScale(new Vector3(555), 0.2f);
    }
 
    void OnMouseDown()
    {
        gameObject.transform.DOScale(new Vector3(5.5f, 5.5f, 5.5f), 0.05f);
    }
 
    void OnMouseUp()
    {
        gameObject.transform.DOScale(new Vector3(666), 0.05f);
        if (buttonType == 1)
            GameManager.GetComponent<GameManagerScript>().MakeGameLayer();
        else if (buttonType == 3)
            GameManager.GetComponent<GameManagerScript>().AtkLvUp();
        else
            quit();
    }
 
    void quit()
    {
        #if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
        #elif UNITY_WEBPLAYER
            Application.OpenURL("http://google.com");
        #else
            Application.Quit();
        #endif
    }
}
 
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
//PlayerScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class PlayerScript : MonoBehaviour
{
    public GameObject GameManager = null;
    public GameObject EffectManager = null;
    public GameObject PlayerLaser = null;
    public float shootTime = 0;
    public bool boolShoot = false;
    public GameObject damEffect = null;
    public GameObject bombEffect = null;
    public GameObject nowLaser = null;
    
    // Use this for initialization
    void Start()
    {
        GameManager = GameObject.FindWithTag("GameManager");
        EffectManager = GameObject.FindWithTag("EffectManager");
 
        Vector3 laserPos = transform.position + new Vector3(06.0f, 0);
        nowLaser = Instantiate(PlayerLaser, laserPos, Quaternion.identity);
        nowLaser.transform.parent = transform;
        nowLaser.gameObject.SetActive(false);
    }
 
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
            if (transform.position.x > -10)
                transform.Translate(-Time.deltaTime * 10.0f, 00);
        if (Input.GetKey(KeyCode.RightArrow))
            if (transform.position.x < 10)
                transform.Translate(Time.deltaTime * 10.0f, 00);
        if (Input.GetKey(KeyCode.UpArrow))
            if (transform.position.y < 6.5)
                transform.Translate(0, Time.deltaTime * 10.0f, 0);
        if (Input.GetKey(KeyCode.DownArrow))
            if (transform.position.y > -4.5)
                transform.Translate(0-Time.deltaTime * 10.0f, 0);
 
        if (Input.GetKey(KeyCode.Space))
        {
            //hit.collider.gameObject.name.Contains("BigIron")
            Vector3 laserPos = transform.position + new Vector3(06.0f, 0);
            if (!boolShoot)
            {
                nowLaser.transform.position = laserPos;
                boolShoot = true;
                nowLaser.gameObject.SetActive(true);
            }
            //RaycastHit[] objectHit = Physics.RaycastAll(transform.position, Vector3.up);
            RaycastHit2D[] objectHit = Physics2D.RaycastAll(transform.position, Vector2.up);
            foreach (RaycastHit2D hit in objectHit)
            {
                if (hit.collider.gameObject != null)
                {
                    StoneScript hitStone = hit.collider.gameObject.GetComponent<StoneScript>();
                    if (hitStone)
                    {
                        if (hitStone.dam < hitStone.hp)
                        {
                            hitStone.damTime += Time.deltaTime;
                            if (hitStone.damTime >= 0.5f)
                            {
                                hitStone.damTime -= 0.5f;
                                //++hitStone.dam;
                                hitStone.dam += GameManager.GetComponent<GameManagerScript>().playerDam;
                                GameManager.GetComponent<GameManagerScript>().score += (hitStone.dam/10* 100;
                                GameManager.GetComponent<GameManagerScript>().popEffectByPool(20, hit.collider.gameObject.transform.position);
                            }
                        }
                        else if(hitStone.dam >= hitStone.hp)
                        {
                            hit.collider.gameObject.GetComponent<StoneScript>().dam = 0;
                            hit.collider.gameObject.GetComponent<StoneScript>().damTime = 0.5f;
                            hit.collider.gameObject.SetActive(false);
                            GameManager.GetComponent<GameManagerScript>().score += (Random.Range(06* 100);
                            GameManager.GetComponent<GameManagerScript>().popEffectByPool(21, hit.collider.gameObject.transform.position);
                        }
                        GameManager.GetComponent<GameManagerScript>().ChangeScore();
                    }
                }
            }
        }
        else
        {
            if (boolShoot)
            {
                nowLaser.gameObject.SetActive(false);
                boolShoot = false;
            }
        }
    }
}
 
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
//StoneScript.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class StoneScript : MonoBehaviour
{
    public GameObject GameManager = null;
    public GameObject EffectManager = null;
    public Vector3 mDir;
    public float speed;
    public float damTime;
    public int hp;
    public int dam;
 
    public bool damaging = false;
    public float attackTime;
 
    public float damTimeResetTime = 0;
    public bool INeedResetTime = false;
    // Use this for initialization
    void Start()
    {
        GameManager = GameObject.FindWithTag("GameManager");
        EffectManager = GameObject.FindWithTag("EffectManager");
        speed = UnityEngine.Random.Range(0.5f, 2.0f);
        damTime = 0.5f;
        dam = 0;
        mDir = new Vector3(0-10);
        mDir.Normalize();
        attackTime = 1.0f;
    }
 
    // Update is called once per frame
    void Update()
    {
        transform.Translate(mDir * Time.deltaTime * speed);
        if (transform.position.y <= -7)
        {
            initStone();
        }
        if(damaging)
        {
            attackTime += Time.deltaTime;
            if(attackTime >= 1.0f)
            {
                attackTime -= 1.0f;
                GameManager.GetComponent<GameManagerScript>().hitPlayer();
            }
        }
        else
        {
            attackTime = 1.0f;
        }
        if(!GameManager.GetComponent<GameManagerScript>().boolInGame)
        {
            GameManager.GetComponent<GameManagerScript>().popEffectByPool(21this.transform.position);
            initStone();
        }
    }
    void initStone()
    {
        this.dam = 0;
        this.damTime = 0.5f;
        this.damaging = false;
        this.gameObject.SetActive(false);
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.name == "playerShip(Clone)")
            damaging = true;
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.name == "playerShip(Clone)")
            damaging = false;
    }
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class EffectScript : MonoBehaviour
{
    public GameObject GameManager = null;
    public bool livingEffect;
    public float effecttime;
    public float duration;
    // Use this for initialization
    void Start ()
    {
        GameManager = GameObject.FindWithTag("GameManager");
    }
    
    // Update is called once per frame
    void Update ()
    {
        if(livingEffect)
        {
            effecttime += Time.deltaTime;
            if(effecttime >= 5.0f)
            {
                effecttime -= 5.0f;
                initEffect();
            }
        }
    }
 
    void initEffect()
    {
        livingEffect = false;
        effecttime = 0;
        this.gameObject.SetActive(false);
    }
}
 
cs