BCA/4. Unity

23일 : 체력바 추가 및 보스 강화

Beabletoet 2018. 3. 20. 15:16
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
//GameManagerScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ProgressBar; // 에셋 받은거 이용
using UnityEngine.UI;
 
public static class CanvasExt
{
    public static Vector2 WorldToCanvas(this Canvas canvas, Vector3 world_pos, Camera cam = null)
    {//프로그래스바는 2d이므로 어느 각도에서 보던 보이게 하는 코드라는데..
        if (cam == null)
            cam = Camera.main;
        var viewport_pos = cam.WorldToViewportPoint(world_pos);
        var canvas_rect = canvas.GetComponent<RectTransform>();
        return new Vector2(viewport_pos.x * canvas_rect.sizeDelta.x, viewport_pos.y * canvas_rect.sizeDelta.y);
    }
}
 
public class GameManagerScript : MonoBehaviour
{
    public GameObject enemy = null;
    public GameObject enemyCounter = null;
    public GameObject bullets = null;
    public GameObject playerShip = null;
    public GameObject player = null;
    public GameObject mBullet = null;
    public GameObject effects = null;
    public GameObject boss = null;
    public GameObject nowBoss = null;
    public GameObject playerHpProgress = null;
    public GameObject BossHpProgress = null;
    public float shootTimer;
    public float waitingTime;
    public float gameSetTimer;
    public float gameSetWaitTime;
    public bool playerDie = false;
    public bool gameOver = false;
    public bool bossGen = false;
    public int bossLife;
    public int playerLife;
    public const int bossFullLife = 200;
    public const int playerFullLife = 100;
    public bool bossAngry = false;
 
    public ProgressBarBehaviour mPlayerHpBar = null;
    public ProgressBarBehaviour mBossHpBar = null;
    public Canvas myCanvas = null;
 
    public int GetBossFullLife()
    {
        return bossFullLife;
    }
 
    // Use this for initialization
    void Start()
    {
 
 
        //===========
        bossLife = bossFullLife;
        playerLife = playerFullLife;
        effects = GameObject.FindWithTag("EffectManager");
        shootTimer = 0;
        waitingTime = 0.3f;
        gameSetTimer = 0;
        gameSetWaitTime = 2.0f;
        makeEnemy();
        makePlayer();
    }
 
    // Update is called once per frame
    void Update()
    {
        if (!playerDie)
        {
            Vector3 hpBarPos = CanvasExt.WorldToCanvas(myCanvas, player.transform.position);
            if (mPlayerHpBar != null)
            {
                hpBarPos.y += 50.0f;
                mPlayerHpBar.transform.position = hpBarPos;
                mPlayerHpBar.transform.localScale = new Vector3(0.5f, 0.5f);
                //mPlayerHpBar.SetFillerSizeAsPercentage((float)playerLife);
                mPlayerHpBar.SetFillerSizeAsPercentage((float)(((float)playerLife / (float)playerFullLife) * 100));
            }
        }
 
 
 
        shootTimer += Time.deltaTime;
        if (Input.GetKey(KeyCode.Space) == true)
        {
            if (shootTimer > waitingTime)
            {
                shootTimer = 0;
                Vector3 bulletStartPos = player.transform.position + new Vector3(0800);
                var bullet = Instantiate(mBullet, bulletStartPos, Quaternion.identity);
                var bul = bullet.GetComponent<Bullet>();
                bul.mDir = new Vector3(010);
                bul.playersBullet = true;
                bul.transform.parent = bullets.transform;
            }
        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            if (player.transform.position.x > -955)
                player.transform.Translate(-Time.deltaTime * 600.0f, 00);
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            if (player.transform.position.x < 955)
                player.transform.Translate(Time.deltaTime * 600.0f, 00);
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            if (player.transform.position.y < 100)
                player.transform.Translate(00, Time.deltaTime * 600.0f);
        }
        else if (Input.GetKey(KeyCode.DownArrow))
        {
            if (player.transform.position.y > -400)
                player.transform.Translate(00-Time.deltaTime * 600.0f);
        }
 
        if (enemyCounter.transform.childCount <= 0 && !bossGen)
        {
            makeBoss();
            bossGen = true;
        }
        if (playerDie || (enemyCounter.transform.childCount <= 0 && bossGen))
            gameOver = true;
 
 
        if (gameOver)
        {
            gameSetTimer += Time.deltaTime;
            if (gameSetWaitTime < gameSetTimer)
            {
                gameSetTimer = 0;
                if (enemyCounter.transform.childCount <= 0)
                {
                    foreach (Transform c in effects.transform)
                        GameObject.Destroy(c.gameObject);
                    foreach (Transform c in bullets.transform)
                        GameObject.Destroy(c.gameObject);
                    if (bossGen)
                        GameObject.Destroy(BossHpProgress);
                    //GameObject.Destroy(playerHpProgress);
                    makeEnemy();
                    bossGen = false;
                }
                if (playerDie)
                {
                    foreach (Transform c in effects.transform)
                        GameObject.Destroy(c.gameObject);
                    foreach (Transform c in bullets.transform)
                        GameObject.Destroy(c.gameObject);
                    foreach (Transform c in enemyCounter.transform)
                        GameObject.Destroy(c.gameObject);
                    GameObject.Destroy(playerHpProgress);
                    if (bossGen)
                        GameObject.Destroy(BossHpProgress);
                    playerDie = false;
                    makeEnemy();
                    makePlayer();
                    bossGen = false;
                }
                bossLife = bossFullLife;
                playerLife = playerFullLife;
                gameOver = false;
            }
        }
        if (bossGen)
        {
            Vector3 bossHpBarPos = CanvasExt.WorldToCanvas(myCanvas, boss.transform.position);
            if (mBossHpBar != null)
            {
                bossHpBarPos.y += 200.0f;
                mBossHpBar.transform.position = bossHpBarPos;
                mBossHpBar.transform.localScale = new Vector3(0.7f, 0.7f);
                mBossHpBar.SetFillerSizeAsPercentage((float)(((float)bossLife / (float)bossFullLife) * 100));
                if ((((float)bossLife / (float)bossFullLife) * 100.0f) <= 50.0f)
                {
                    bossAngry = true;
                }
            }
        }
    }
    void makeEnemy()
    {
        for (int i = 0; i < 7++i)
        {
            GameObject nowEnemy = GameObject.Instantiate(enemy, new Vector3(-450 + (150 * i), 300100), Quaternion.Euler(9000));
            nowEnemy.transform.parent = enemyCounter.transform;
        }
    }
    void makePlayer()
    {
        player = GameObject.Instantiate(playerShip, new Vector3(0-320100), Quaternion.Euler(27000));
        player.transform.parent = this.transform;
        makePlayerHpProgress();
    }
    void makeBoss()
    {
        bossAngry = false;
        Vector3 canvasPos = CanvasExt.WorldToCanvas(myCanvas, transform.position);
        var progress = Resources.Load("ProgressBarLabelInside"as GameObject;
        BossHpProgress = Instantiate(progress, canvasPos, Quaternion.identity);
        mBossHpBar = BossHpProgress.GetComponent<ProgressBarBehaviour>();
        mBossHpBar.transform.SetParent(myCanvas.transform, false);
        //mBossHpBar.SetFillerSizeAsPercentage(100.0f);
 
        GameObject nowBoss = GameObject.Instantiate(boss, new Vector3(0300100), Quaternion.Euler(9000));
        nowBoss.transform.parent = enemyCounter.transform;
 
        HpProgressTransparent(mBossHpBar);
    }
    void makePlayerHpProgress()
    {
        Vector3 canvasPos = CanvasExt.WorldToCanvas(myCanvas, transform.position);
        var progress = Resources.Load("ProgressBarLabelInside"as GameObject;
        playerHpProgress = Instantiate(progress, canvasPos, Quaternion.identity);
        mPlayerHpBar = playerHpProgress.GetComponent<ProgressBarBehaviour>();
        mPlayerHpBar.transform.SetParent(myCanvas.transform, false);
        HpProgressTransparent(mPlayerHpBar);
    }
 
    void HpProgressTransparent(ProgressBarBehaviour a)
    {
        Transform[] ts = a.transform.GetComponentsInChildren<Transform>();
        foreach (Transform t in ts)
        {
            if (t.gameObject.name == "FillerBg")
            {
                var mat = t.gameObject.GetComponent<Image>();
                mat.color = new Color(1110.5f);
            }
            if (t.gameObject.name == "Filler")
            {
                var mat = t.gameObject.GetComponent<Image>();
                mat.color = new Color(1110.5f);
            }
        }
    }
}
 
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
//BossShipScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class BossShipScript : MonoBehaviour
{
    public float shootTimer;
    public float waitingTime;
    public GameObject GameManager = null;
    public GameObject bossBullet = null;
    public GameObject bullets = null;
    public GameObject effects = null;
    // Use this for initialization
    void Start()
    {
        GameManager = GameObject.FindWithTag("GameManager");
        bullets = GameObject.FindWithTag("BulletManager");
        effects = GameObject.FindWithTag("EffectManager");
        shootTimer = 10.0f;
        waitingTime = 10.0f;
    }
 
    // Update is called once per frame
    void Update()
    {
        shootTimer += Time.deltaTime;
        if (shootTimer > waitingTime)
        {
            shootTimer -= waitingTime;
            Vector3 bulletStartPos = transform.position + new Vector3(0-800);
            var bullet = Instantiate(bossBullet, bulletStartPos, Quaternion.identity);
            var bul = bullet.GetComponent<BossBullet>();
            bul.mDir = new Vector3(0-10);
            bul.transform.parent = bullets.transform;
            
            if (GameManager.GetComponent<GameManagerScript>().bossAngry)
            {
                var bullet1 = Instantiate(bossBullet, bulletStartPos, Quaternion.identity);
                var bul1 = bullet1.GetComponent<BossBullet>();
                bul1.mDir = new Vector3(-1-10);
                bul1.mDir.Normalize();
                bul1.transform.parent = bullets.transform;
 
                var bullet2 = Instantiate(bossBullet, bulletStartPos, Quaternion.identity);
                var bul2 = bullet2.GetComponent<BossBullet>();
                bul2.mDir = new Vector3(1-10);
                bul2.mDir.Normalize();
                bul2.transform.parent = bullets.transform;
            }
 
            foreach (Transform c in effects.transform)
                GameObject.Destroy(c.gameObject);
        }
    }
}
 
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
//BossBullet.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class BossBullet : MonoBehaviour
{
    public Vector3 mDir = new Vector3(000);
    public GameObject GameManager = null;
    public bool playersBullet = false;
    public GameObject explosion = null;
    public GameObject effects = null;
    public float slow;
    public bool bomb = false;
    public float bombTime;
    public float bombSetTime;
    public float ang;
    public int count;
    public float alpha;
    public GameObject mBullet = null;
    public GameObject bullets = null;
    // Use this for initialization
    void Start ()
    {
        bullets = GameObject.FindWithTag("BulletManager");
        count = 0;
        alpha = 1.0f;
        ang = Random.Range(0359.9f);
        slow = 250.0f;
        bombSetTime = 0.05f;
        GameManager = GameObject.FindWithTag("GameManager");
        effects = GameObject.FindWithTag("EffectManager");
    }
    
    // Update is called once per frame
    void Update ()
    {
        if (!bomb)
        {
            transform.Translate(mDir * Time.deltaTime * slow);
            if (slow >= 0)
                slow -= 1.0f;
            else
            {
                slow = 0;
                bomb = true;
            }
        }
        else
        {
            bombTime += Time.deltaTime;
            if(count>=75)
            {
                Destroy(gameObject);
            }
            else if(count >= 50)
            {
                GameObject ef = Instantiate(explosion, transform.position, Quaternion.identity);
                ef.transform.parent = effects.transform;
                ++count;
            }
            else if(bombTime >= bombSetTime)
            {
                bombTime -= bombSetTime;
 
                Vector3 bulletStartPos = transform.position;
                var bullet = Instantiate(mBullet, bulletStartPos, Quaternion.identity);
                var bul = bullet.GetComponent<Bullet>();
                bul.mDir = new Vector3(Mathf.Sin(ang/2), Mathf.Cos(ang/2),0);
                bul.mDir.Normalize();
                bul.transform.parent = bullets.transform;
 
                ang += Random.Range(26.0f,40.0f);
                if (ang >= 360.0f)
                    ang -= 360.0f;
                ++count;
            }
        }
    }
}
 
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
//Bullet.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Bullet : MonoBehaviour
{
    public Vector3 mDir = new Vector3(000);
    public GameObject GameManager = null;
    public bool playersBullet = false;
    public GameObject explosion = null;
    public GameObject effects = null;
    public GameObject bossDamEffect = null;
    // Use this for initialization
    void Start()
    {
        GameManager = GameObject.FindWithTag("GameManager");
        effects = GameObject.FindWithTag("EffectManager");
}
 
    // Update is called once per frame
    void Update()
    {
        transform.Translate(mDir * Time.deltaTime * 250.0f);
        if (transform.position.x < -1000 || transform.position.y > 600 || transform.position.x > 1000 || transform.position.y < -600)
            Destroy(gameObject);
        
    }
    void OnTriggerEnter(Collider other)
    {
        if (playersBullet)
        {
            if (other.gameObject.name != "PlayerShip(Clone)")
            {
                if (other.gameObject.name == "EnemyShip(Clone)")
                {
                    GameObject ef = Instantiate(explosion, transform.position, Quaternion.identity);
                    ef.transform.parent = effects.transform;
                    Destroy(other.gameObject);
                    Destroy(gameObject);
                }
                else if(other.gameObject.name == "Sphere(Clone)")
                {
                    Destroy(other.gameObject);
                    Destroy(gameObject);
                }
                else if(other.gameObject.name == "Boss(Clone)")
                {
                    if (--(GameManager.GetComponent<GameManagerScript>().bossLife) <= 0
                    {
                        GameObject ef = Instantiate(explosion, transform.position, Quaternion.identity);
                        ef.transform.parent = effects.transform;
                        Destroy(other.gameObject);
                    }
                    else
                    {
                        GameObject ef = Instantiate(bossDamEffect, transform.position+ new Vector3(0,100,-100), Quaternion.identity);
                        ef.transform.parent = effects.transform;
                    }
                    Destroy(gameObject);
                }
            }
        }
        else
        {
            if(other.gameObject.name == "PlayerShip(Clone)")
            {
                int a = Random.Range(1034);
                if (GameManager.GetComponent<GameManagerScript>().playerLife <= a)
                {
                    GameManager.GetComponent<GameManagerScript>().playerLife = 0;
                    GameManager.GetComponent<GameManagerScript>().mPlayerHpBar.SetFillerSizeAsPercentage(0);
                    GameObject ef = Instantiate(explosion, transform.position, Quaternion.identity);
                    ef.transform.parent = effects.transform;
                    GameManager.GetComponent<GameManagerScript>().playerDie = true;
                    Destroy(other.gameObject);
                }
                else
                {
                    GameManager.GetComponent<GameManagerScript>().playerLife -= a;
                    GameObject ef = Instantiate(bossDamEffect, transform.position, Quaternion.identity);
                    ef.transform.parent = effects.transform;
                }
                Destroy(gameObject);
            }
        }
    }
 
}
 
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
//EnemyShipScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class EnemyShipScript : MonoBehaviour
{
    public float shootTimer;
    public float waitingTime;
    public float moveTimer;
    public float reverseTime;
    public bool left = true;
    public GameObject mBullet = null;
    public GameObject bullets = null;
    // Use this for initialization
    void Start()
    {
        bullets = GameObject.FindWithTag("BulletManager");
        shootTimer = 0;
        waitingTime = 0.5f;
        moveTimer = 3.0f;
        reverseTime = 6.0f;
    }
 
    // Update is called once per frame
    void Update()
    {
        shootTimer += Time.deltaTime;
        moveTimer += Time.deltaTime;
        if (shootTimer > waitingTime)
        {
            shootTimer -= waitingTime;
            Vector3 bulletStartPos = transform.position + new Vector3(0-800);
            var bullet = Instantiate(mBullet, bulletStartPos, Quaternion.identity);
            var bul = bullet.GetComponent<Bullet>();
            bul.mDir = new Vector3(0-10);
            bul.transform.parent = bullets.transform;
        }
        if(moveTimer > reverseTime)
        {
            moveTimer -= reverseTime;
            if (left)
                left = false;
            else
                left = true;
        }
        if(left)
            transform.Translate(-Time.deltaTime * 150.0f, 00);
        else
            transform.Translate(Time.deltaTime * 150.0f, 00);
        
    }
}
 
cs