VioletaBabel
27일 : 간단한 슈팅게임 본문
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 | //TimeManager.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class TimeManager : MonoBehaviour { static public float GameTime = 0; static public bool bTimeStop = false; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (bTimeStop) GameTime = 0; else GameTime = Time.deltaTime; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //Singleton.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class Singleton<T> : MonoBehaviour where T : MonoBehaviour { private static T _instance; public static T Instance { get { if(_instance == null) { _instance = FindObjectOfType(typeof(T)) as T; } return _instance; } } } | 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 | //Shield.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class Shield : MonoBehaviour { public GameObject[] shield = new GameObject[3]; public GameObject[] playerShield = new GameObject[3]; private int nowShield = -1; // Use this for initialization void Start() { for (int i = 0; i < 3; ++i) { playerShield[i] = Instantiate(shield[i], transform); } changeShield(); } // Update is called once per frame void Update() { } public void minusShield() { if (nowShield > -1) --nowShield; changeShield(); } public int getShield() { return nowShield; } private void changeShield() { for (int i = 0; i < 3; ++i) playerShield[i].SetActive(false); if (nowShield > -1) playerShield[nowShield].SetActive(true); } public void fullShield() { nowShield = 2; changeShield(); } } | 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 | //PlayerShip.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerShip : MonoBehaviour { private GameManager TheGameManager; // Use this for initialization void Start () { TheGameManager = GameManager.Instance; } // Update is called once per frame void Update () { //Vector2 mousePos = Input.mousePosition; //mousePos = Camera.main.ScreenToWorldPoint(mousePos); //Vector2 myPos = transform.position; //Vector2 dir = mousePos - myPos; //dir.Normalize(); //float angle = Vector2.SignedAngle(Vector2.up, dir); //Quaternion q = (new Quaternion()); //q.eulerAngles = new Vector3(0, 0, angle); //transform.rotation = q; //이건 마우스 따라가는 코드이니 지우지말고 나중에 써먹자 if (TheGameManager.nonDeadTime > 0) return;//부활하면 2초간 움직임 불가 if(Input.GetKeyDown(KeyCode.LeftControl)) { GetComponent<PlayerLaser>().shoot(true); } else if (Input.GetKeyUp(KeyCode.LeftControl)) { GetComponent<PlayerLaser>().shoot(false); } if (Input.GetKey(KeyCode.LeftArrow)) { if (transform.position.x > -5.0f) transform.Translate(-TimeManager.GameTime * 10.0f, 0, 0); } else if (Input.GetKey(KeyCode.RightArrow)) { if (transform.position.x < 5.0f) transform.Translate(TimeManager.GameTime * 10.0f, 0, 0); } if (Input.GetKey(KeyCode.DownArrow)) { if (transform.position.y > -5.0f) transform.Translate(0, -TimeManager.GameTime * 10.0f, 0); } else if (Input.GetKey(KeyCode.UpArrow)) { if (transform.position.y < 5.0f) transform.Translate(0, TimeManager.GameTime * 10.0f, 0); } } } | 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 | //PlayerRocket.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerRocket : MonoBehaviour { private const int rocketDam = 20; public GameObject closeEnemy = null; public Vector2 dir = Vector2.zero; // Use this for initialization void Start() { GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 30.0f)); } // Update is called once per frame void Update() { if (closeEnemy == null) { transform.Translate(Vector2.up * 10.0f * TimeManager.GameTime); } else { Quaternion q = new Quaternion(); if (closeEnemy.activeInHierarchy) { dir = closeEnemy.transform.position - transform.position; dir.Normalize(); Vector3 a = new Vector3(0, 0, Vector3.Angle(Vector3.up, dir)); if (dir.x > 0) a *= -1; q.eulerAngles = a; transform.rotation = q; } transform.Translate(dir * 10.0f * TimeManager.GameTime); if (dir.Equals(Vector2.zero)) gameObject.SetActive(false); } Vector2 nowPos = transform.position; if (nowPos.x < -6.0f || nowPos.x > 6.0f || nowPos.y < -6.0f || nowPos.y > 6.0f) gameObject.SetActive(false); } private void OnTriggerEnter2D(Collider2D collision) { if (collision.name.Contains("enemyShip")) { gameObject.SetActive(false); var col = collision.GetComponent<UFO>(); col.PlayerHit(rocketDam); } } } | 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 | //MoveItem.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoveItem : MonoBehaviour { public Vector2 nowArrow; public int count = 0; // Use this for initialization void Start () { nowArrow = new Vector2(Random.Range(0, 360.0f), Random.Range(0, 360.0f)); nowArrow.Normalize(); } // Update is called once per frame void Update () { if (count != 3) { if (transform.position.x > 5.0f || transform.position.x < -5.0f) { float x = (transform.position.x > 0) ? 4.99f : -4.99f; transform.position = new Vector2(x, transform.position.y); nowArrow.x *= -1; ++count; nowArrow.Normalize(); } else if (transform.position.y > 5.0f || transform.position.y < -5.0f) { float y = (transform.position.y > 0) ? 4.99f : -4.99f; transform.position = new Vector2(transform.position.x, y); nowArrow.y *= -1; ++count; nowArrow.Normalize(); } transform.Translate(nowArrow * TimeManager.GameTime); } else { transform.Translate(nowArrow * TimeManager.GameTime); if (transform.position.x > 6.0f || transform.position.x < -6.0f || transform.position.y > 6.0f || transform.position.y < -6.0f) gameObject.SetActive(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 | //ItemManager.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class ItemManager : Singleton<ItemManager> { private Dictionary<int, Queue<GameObject>> itemPool = new Dictionary<int, Queue<GameObject>>(); public GameObject[] items = null;//0 = powerUp, 1 = shield public int[] itemKeyValue = null; // Use this for initialization void Start () { CreateItemPool(); } // Update is called once per frame void Update() { } public void CreateItemPool() { int tags = 0; for(int i = 0; i < 2; ++i) { if (items[i].tag.Contains("0")) tags = 0; if (items[i].tag.Contains("1")) tags = 1; itemKeyValue[i] = tags; if(!itemPool.ContainsKey(itemKeyValue[i])) { itemPool.Add(itemKeyValue[i], new Queue<GameObject>()); } for(int j = 0; j < 20; ++j) { GameObject thisObj = Instantiate(items[i], new Vector3(100, 100, 0), Quaternion.identity); thisObj.SetActive(false); thisObj.transform.parent = this.transform; itemPool[itemKeyValue[i]].Enqueue(thisObj); } } } public GameObject PopItemByPool(int itemType, Vector3 position) { GameObject thisItem = itemPool[itemType].Dequeue(); thisItem.transform.position = position; thisItem.SetActive(true); itemPool[itemType].Enqueue(thisItem); return thisItem; } } | 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 | //HPBar.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class HPBar : MonoBehaviour { public GameObject[] hpBar = null; public GameObject[] hp = null; public float HP = 0; public float MaxHP = 0; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void MakeHPBar() {// 체력바부분 hp = new GameObject[3]; for (int i = 0; i < 3; ++i) hp[i] = Instantiate(hpBar[i], transform); InitHPBarSize(); } public void InitHPBarSize() { hp[1].transform.DOScaleX(5.0f, 0); hp[1].transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f); hp[0].transform.position = new Vector3(transform.position.x - 0.099f * 5, transform.position.y + 0.5f); hp[2].transform.position = new Vector3(transform.position.x + 0.099f * 5, transform.position.y + 0.5f); } public void InitHPBarSize(float scale, float size) {//나중에 오버로드해서 만들자 hp[1].transform.DOScaleX(scale, 0); hp[1].transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f); hp[0].transform.position = new Vector3(transform.position.x - 0.099f * 5, transform.position.y + 0.5f); hp[2].transform.position = new Vector3(transform.position.x + 0.099f * 5, transform.position.y + 0.5f); } public void setHpBarSize(float _hp, float _maxHp) { HP = _hp; MaxHP = _maxHp; float hpPercent = HP / MaxHP; if (hpPercent < 0) hpPercent = 0; hp[1].transform.DOScaleX(hpPercent * 5.0f, 0); Vector2 middleBarPos = transform.position; middleBarPos.x -= (1.0f - hpPercent) * 5.0f * 0.099f; middleBarPos.y = hp[1].transform.position.y; hp[1].transform.position = middleBarPos; Vector2 RightBarPos = transform.position; RightBarPos.x = transform.position.x + (0.099f * 5.0f); RightBarPos.y = transform.position.y + 0.5f; RightBarPos.x -= (1.0f - hpPercent) * 5.0f * 0.198f; hp[2].transform.position = RightBarPos; } } | 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 118 119 | //GaugeManager.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class GaugeManager : Singleton<GaugeManager> { public GameObject player = null; public PlayerLaser pL = null; public int maxGauge = 0; public int nowGauge = 0; public GameObject[] gauge = new GameObject[3]; public GameObject[] playerGauge = new GameObject[3]; public bool BoolIsGaugeLive = false; public float gaugePlusTime = 0; public float gaugeScale = 1.0f; public int gaugeScaleLevel = 0; //public float barSize = 0.0450f; // 이건 고쳐야할것. // Use this for initialization void Start() { pL = player.GetComponent<PlayerLaser>(); for(int i = 0; i < 3; ++i) { playerGauge[i] = Instantiate(gauge[i], transform); playerGauge[i].SetActive(false); playerGauge[i].transform.position = new Vector2(-4.95f + (0.1575f * i), -4.85f); } } // Update is called once per frame void Update() { if(BoolIsGaugeLive) { gaugePlusTime += TimeManager.GameTime; if(gaugePlusTime > 1.0f) { if (nowGauge < maxGauge) { gaugePlusTime -= 1.0f; ++nowGauge; SetGaugeBarSize(); } else gaugePlusTime = 0; } } } public int GetNowGauge() { return nowGauge; } public void GaugeMaxUp() { if (maxGauge == 0) OnGauge(); if (maxGauge < 50) { maxGauge += 5; //gaugeScale += 0.5f; ++gaugeScaleLevel; } if (nowGauge < 46) nowGauge += 5; else if (nowGauge < 50) nowGauge = 50; SetGaugeBarSize(); } public void MinusGauge() { --nowGauge; gaugePlusTime = 0; SetGaugeBarSize(); } public void OnGauge() { maxGauge = 0; nowGauge = 0; gaugeScale = 1.0f; gaugeScaleLevel = 0; gaugePlusTime = 0; for (int i = 0; i < 3; ++i) playerGauge[i].SetActive(true); BoolIsGaugeLive = true; } public void OffGauge() { maxGauge = 0; nowGauge = 0; gaugeScale = 1.0f; gaugeScaleLevel = 0; gaugePlusTime = 0; BoolIsGaugeLive = false; for (int i = 0; i < 3; ++i) playerGauge[i].SetActive(false); } public void SetGaugeBarSize() { gaugeScale = (gaugeScaleLevel + 1) * 0.5f * ((float)nowGauge / (float)maxGauge); playerGauge[1].transform.DOScaleX(gaugeScale, 0); Vector2 middleBarPos = playerGauge[1].transform.position; //middleBarPos.x = -4.8375f + 0.0450f * gaugeScaleLevel; middleBarPos.x = -4.95f + 0.090f * gaugeScale+0.045f; playerGauge[1].transform.position = middleBarPos; Vector2 rightBarPos = playerGauge[2].transform.position; rightBarPos.x = -4.95f + 0.090f*2 * gaugeScale+0.045f*2; playerGauge[2].transform.position = rightBarPos; } } | 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 | //GameManager.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class GameManager : Singleton<GameManager> { public bool boolInGame = true; public GameObject player = null; float gameoverDelayTime = 0; public float nonDeadTime = 0; public GameObject uiPlayerLifeImage = null; public GameObject[] uiPlayerLife = null; public int nowLife = 3; // Use this for initialization void Start() { for (int i = 0; i < 3; ++i) { uiPlayerLife[i] = Instantiate(uiPlayerLifeImage); uiPlayerLife[i].transform.position = new Vector2(-4.8f + 0.35f * i, 4.85f); } MakePlayer(); } // Update is called once per frame void Update() { if (!boolInGame) { gameoverDelayTime += TimeManager.GameTime; if (gameoverDelayTime >= 3.0f) MakePlayer(); } else if (nonDeadTime > 0) { nonDeadTime -= TimeManager.GameTime; } } public void setPlayerLife() { for (int i = 0; i < 3; ++i) { if (i < nowLife) uiPlayerLife[i].SetActive(true); else uiPlayerLife[i].SetActive(false); } } public void MakePlayer() { setPlayerLife(); nonDeadTime = 2.0f; player.transform.position = new Vector2(0, -5.5f); player.transform.DOMoveY(-4.5f, 1.0f); //깜빡이는거든 뭐든 나중에 여기다 넣으셈 player.SetActive(true); boolInGame = true; gameoverDelayTime = 0; --nowLife; // } } | 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 | //UFO.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class UFO : MonoBehaviour { private GameManager TheGameManager; private BulletManager TheBulletManager; private ItemManager TheItemManager; public GameObject player = null; float preShootTime = 0.3f; float shootTime = 1.0f; public float MaxHP = 100.0f; public float HP = 100.0f; private Vector2[] movePos = new Vector2[6]; // 패턴을 가지고 움직이게 할 것. private int nowState = 0; void Start() { TheGameManager = GameManager.Instance; TheBulletManager = BulletManager.Instance; TheItemManager = ItemManager.Instance; Transform ts = GetComponent<Transform>(); for (int i = 0; i < 5; ++i) { float rx = Random.Range(-4.0f, 4.0f); float ry = Random.Range(0, 5.0f) - (float)i; movePos[i] = new Vector2(rx, ry); } movePos[5] = new Vector2(movePos[4].x, 4.0f); transform.DOMove(movePos[0], 2.0f); } void Update() { if (TheGameManager.boolInGame) { //체력바관련 if (TheGameManager.nonDeadTime <= 0) { preShootTime += TimeManager.GameTime; if (preShootTime >= shootTime) { TheBulletManager.PopBulletByPool(0, transform.position); preShootTime -= shootTime; } } } if (Vector2.Distance(movePos[nowState], transform.position) < 0.1f) { if (nowState < 5) { ++nowState; transform.DOMove(movePos[nowState], 2.0f); } else { //Destroy(transform.gameObject); } } } public void PlayerHit(int dam) { HP -= dam; this.GetComponent<HPBar>().setHpBarSize(HP, MaxHP); if (HP <= 0 && gameObject.transform.position.x != 100) { HP = 0; gameObject.SetActive(false); this.GetComponent<HPBar>().InitHPBarSize(); int a = Random.Range(0, 1);//무조건 나오게 함. 나중에 고쳐라. if (a.Equals(0)) { a = Random.Range(0, 2); // 아이템 지금은 2개. 나중에 수정 TheItemManager.PopItemByPool(a, this.transform.position); } gameObject.transform.position = new Vector2(100, 100); } } private void OnTriggerEnter2D(Collider2D collision) { if (collision.name.Contains("playerShip")) { gameObject.SetActive(false); if (collision.GetComponent<Shield>().getShield().Equals(-1)) { collision.gameObject.SetActive(false); TheGameManager.boolInGame = false; collision.GetComponent<PlayerLaser>().InitPower(); } else collision.GetComponent<Shield>().minusShield(); } } } | 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 | //EnemyManager.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyManager : Singleton<EnemyManager> { private Dictionary<int, Queue<GameObject>> enemyPool = new Dictionary<int, Queue<GameObject>>(); public GameObject[] enemys = null; //0번 : 일반 깜장이 public int[] enemyKeyValue = null; public float respawnTime = 0; // Use this for initialization void Start() { CreateEnemyPool(); makeEnemy(); } // Update is called once per frame void Update() { UFO[] enemys = GetComponentsInChildren<UFO>(); GameObject closeE = null; float fCloseDistance = 1000.0f; GameObject playerShip = GameObject.Find("playerShip"); if (playerShip != null) { respawnTime += TimeManager.GameTime; if (respawnTime >= 6.0f) { respawnTime -= 6.0f; makeEnemy(); } foreach (UFO e in enemys) { float distance = Vector2.Distance(e.transform.position, playerShip.transform.position); if (distance < fCloseDistance) { closeE = e.gameObject; fCloseDistance = distance; } } playerShip.GetComponent<PlayerLaser>().closeEnemy = closeE; } } void makeEnemy() { int r = Random.Range(0, 5); int sideR = Random.Range(-1, 1); if (sideR == 0) ++sideR; for (int i = 0; i < 1; ++i) { GameObject e = PopEnemyByPool(r, new Vector2(sideR*5, 4)); } } public void CreateEnemyPool() { int tags = 0; for (int i = 0; i < 5; ++i) { if (enemys[i].tag.Contains("0")) tags = 0; else if (enemys[i].tag.Contains("1")) tags = 1; else if (enemys[i].tag.Contains("2")) tags = 2; else if (enemys[i].tag.Contains("3")) tags = 3; else if (enemys[i].tag.Contains("4")) tags = 4; enemyKeyValue[i] = tags; if (!enemyPool.ContainsKey(enemyKeyValue[i])) { enemyPool.Add(enemyKeyValue[i], new Queue<GameObject>()); } for (int j = 0; j < 50; ++j) { GameObject thisObj = Instantiate(enemys[i], new Vector3(100, 100, 0), Quaternion.identity); thisObj.SetActive(false); thisObj.transform.parent = this.transform; enemyPool[enemyKeyValue[i]].Enqueue(thisObj); thisObj.GetComponent<HPBar>().MakeHPBar(); thisObj.GetComponent<HPBar>().HP = 100; thisObj.GetComponent<HPBar>().MaxHP = 100; } } } public GameObject PopEnemyByPool(int enemyType, Vector3 position) { GameObject thisEnemy = enemyPool[enemyType].Dequeue(); thisEnemy.transform.position = position; thisEnemy.SetActive(true); enemyPool[enemyType].Enqueue(thisEnemy); return thisEnemy; } } | 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 | //PowerUpItem.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class PowerUpItem : MonoBehaviour { // Use this for initialization void Start() { } // Update is called once per frame void Update() { } private void OnTriggerEnter2D(Collider2D collision) { if (collision.name.Contains("playerShip")) { this.gameObject.SetActive(false); collision.GetComponent<PlayerLaser>().PowerUp(); } } } | 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 | //ShieldItem.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class ShieldItem : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } private void OnTriggerEnter2D(Collider2D collision) { if (collision.name.Contains("playerShip")) { this.gameObject.SetActive(false); collision.GetComponent<Shield>().fullShield(); } } } | 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 | //BulletManager.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class BulletManager : Singleton<BulletManager> { private Dictionary<int, Queue<GameObject>> bulletPool = new Dictionary<int, Queue<GameObject>>(); public GameObject[] bullets = new GameObject[4]; //0번 = 적 레이저, 1번 = 플레이어레이저1, 2번 = 플레이어레이저2, 3번 = 플레이어유도탄 public int[] bulletKeyValue = new int[4]; // 현재 탄환 4개라 4. 나중에 탄환 추가시 더 넣어라. // Use this for initialization void Start() { } // Update is called once per frame void Update() { } public void CreateBulletPool() { int tags = 0; for (int i = 0; i < 4; ++i) { if (bullets[i].tag.Contains("0")) tags = 0; else if (bullets[i].tag.Contains("1")) tags = 1; else if (bullets[i].tag.Contains("2")) tags = 2; else if (bullets[i].tag.Contains("3")) tags = 3; bulletKeyValue[i] = tags; if (!bulletPool.ContainsKey(bulletKeyValue[i])) { bulletPool.Add(bulletKeyValue[i], new Queue<GameObject>()); } for (int j = 0; j < 200; ++j) { GameObject thisObj = Instantiate(bullets[i], new Vector3(100, 100, 0), Quaternion.identity); thisObj.SetActive(false); thisObj.transform.parent = this.transform; bulletPool[bulletKeyValue[i]].Enqueue(thisObj); } } } public GameObject PopBulletByPool(int bulletType, Vector3 position) { GameObject thisBullet = bulletPool[bulletType].Dequeue(); thisBullet.transform.position = position; thisBullet.SetActive(true); bulletPool[bulletType].Enqueue(thisBullet); return thisBullet; } } | 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 | //PlayerLaser.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerLaser : MonoBehaviour { public GameObject laser1 = null; public GameObject laser2 = null; public GameObject rocket = null; public GameObject closeEnemy = null; private bool bShoot = false; private int nowPower = 2;//레이저의 파워 private float shootTime = 0.15f;//몇 초에 한 발 나가는가 private float preShootTime = 0.15f; // 이전에 쐈던 시간 private int countShotForRocket = 0; private BulletManager TheBulletManager; private GaugeManager TheGaugeManager; void Start() { TheBulletManager = BulletManager.Instance; TheBulletManager.CreateBulletPool(); TheGaugeManager = GaugeManager.Instance; } void Update() { if (bShoot) { ShootingType(); } } public int GetNowPower() { return nowPower; } public void shoot(bool bShootButton) { bShoot = bShootButton; if (!bShoot) preShootTime = shootTime; } public void PowerUp() { if (nowPower < 3) { ++nowPower; //shootTime = (float)(4 - nowPower) * 0.075f; // 파워가 오를 때마다 쏘는 속도가 조금씩 감소 } //else // TheGaugeManager.GaugeMaxUp(); if (nowPower.Equals(3)) { TheGaugeManager.GaugeMaxUp(); } else { TheGaugeManager.OffGauge(); } } public void InitPower() { nowPower = 1; TheGaugeManager.OffGauge(); } public void ShootingType() { preShootTime += TimeManager.GameTime; if (preShootTime >= shootTime) { TheBulletManager.PopBulletByPool(1, transform.position); preShootTime -= shootTime; if (nowPower > 1) { Vector2 laser1Pos = transform.position; Vector2 laser2Pos = transform.position; laser1Pos.x -= 0.2f; laser2Pos.x += 0.2f; TheBulletManager.PopBulletByPool(2, laser1Pos); TheBulletManager.PopBulletByPool(2, laser2Pos); countShotForRocket = (countShotForRocket + 1) % 2; } if (nowPower > 2 && countShotForRocket.Equals(0)&&(TheGaugeManager.GetNowGauge() > 0)) { TheGaugeManager.MinusGauge(); GameObject rockets = TheBulletManager.PopBulletByPool(3, transform.position); PlayerRocket roc = rockets.GetComponent<PlayerRocket>(); roc.closeEnemy = closeEnemy; preShootTime = 0; } } } } | 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 | //Laser.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class Laser : MonoBehaviour { private const int laserDam = 10; // Use this for initialization void Start() { } // Update is called once per frame void Update() { transform.Translate(new Vector2(0, 1.0f) * TimeManager.GameTime * 5.0f); Vector2 nowPos = transform.position; if (nowPos.x < -6.0f || nowPos.x > 6.0f || nowPos.y < -6.0f || nowPos.y > 6.0f) gameObject.SetActive(false); } private void OnTriggerEnter2D(Collider2D collision) { if (collision.name.Contains("enemyShip")) { gameObject.SetActive(false); //collision.gameObject.SetActive(false); var col = collision.GetComponent<UFO>(); col.PlayerHit(laserDam); } } } | 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 | //EnemyLazer.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyLazer : MonoBehaviour { private GameObject player = null; private Vector2 mDir = new Vector2(0, -1.0f); private GameManager TheGameManager; private bool firstShoot = false; // Use this for initialization void Start() { player = GameObject.FindGameObjectWithTag("Player"); TheGameManager = GameManager.Instance; } // Update is called once per frame void Update() { if (TheGameManager.nonDeadTime <= 0) { if (!firstShoot) { mDir = (player.transform.position - transform.position).normalized; Quaternion q = new Quaternion(); float _r = Mathf.Atan2(Vector2.up.x - mDir.x, Vector2.up.y - mDir.y); float angle = (_r / Mathf.PI) * 180; Debug.Log(angle); q.eulerAngles = new Vector3(0, 0, -angle * 2); transform.rotation = q; firstShoot = true; } Vector2 pos = transform.position; pos += mDir * 0.1f; transform.position = pos; Vector2 nowPos = transform.position; if (nowPos.x < -6.0f || nowPos.x > 6.0f || nowPos.y < -6.0f || nowPos.y > 6.0f) { gameObject.SetActive(false); firstShoot = false; } } } private void OnTriggerEnter2D(Collider2D collision) { if (TheGameManager.nonDeadTime <= 0) if (collision.name.Contains("playerShip")) { if (collision.GetComponent<Shield>().getShield().Equals(-1)) { collision.gameObject.SetActive(false); TheGameManager.boolInGame = false; firstShoot = false; collision.GetComponent<PlayerLaser>().InitPower(); } else collision.GetComponent<Shield>().minusShield(); gameObject.SetActive(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 | //BackGroundMoveBack.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class BackGroundMoveBack : MonoBehaviour { private const float MOVE_SPEED = 1.0f; private const float BG_Y_SIZE = 10.24f; // Use this for initialization void Start () { } // Update is called once per frame void Update () { Transform tr = GetComponent<Transform>(); tr.Translate(new Vector2(0, -MOVE_SPEED * TimeManager.GameTime)); if (tr.position.y <= -BG_Y_SIZE) { float fLeftY = tr.position.y + BG_Y_SIZE * 2.0f; tr.position = new Vector2(0, fLeftY); } } } | cs |
'BCA > 4. Unity' 카테고리의 다른 글
29. 테트리스 (0) | 2018.04.04 |
---|---|
28. 슈팅게임 일단은 완성 (0) | 2018.03.30 |
26일 : 풀링과 싱글톤 & 연습용 슈팅게임 발전 (0) | 2018.03.23 |
25일 : 연습용 슈팅게임 완성 (0) | 2018.03.22 |
24일 : dotween 이용, 2D슈팅게임 (0) | 2018.03.21 |
Comments