C#/문제 해결

SpaceShooter 게임 구현

HSH12345 2023. 2. 2. 22:09

BulletController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletController : MonoBehaviour
{
    Coroutine bulletMoveRoutine;
    public GameObject explosionFx;    
    public Collider2D scorings;    
    public GameDirector gameDirector;

    public float bulletSpeed;
    // Start is called before the first frame update
    void Start()
    {
        this.bulletMoveRoutine = this.StartCoroutine(this.Move());
    }

    IEnumerator Move()
    {
        while (true)
        {
            this.transform.Translate(0, bulletSpeed, 0);

            if(this.transform.position.y >= 1.5)
            {
                Destroy(this.gameObject);
            }
            yield return null;
        }
    }

    public void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.gameObject.tag == "Enemy")
        {
            var go = Instantiate(explosionFx.gameObject);
            go.transform.position = collision.transform.position;
            this.scorings = collision;

            if (collision.gameObject.name == "enemy_big(Clone)") gameDirector.score += 1000;

            if (collision.gameObject.name == "enemy_medium(Clone)") gameDirector.score += 500;

            if (collision.gameObject.name == "enemy_small(Clone)") gameDirector.score += 100;

            Destroy(this.gameObject);
            Destroy(collision.gameObject);            
        }
    }
}

 

EnemyController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyController : MonoBehaviour
{
    Coroutine enemyMoveRoutine;
    public float moveSpeed;
    // Start is called before the first frame update
    void Start()
    {
        this.enemyMoveRoutine = this.StartCoroutine(this.Move());
    }

    IEnumerator Move()
    {
        while (true)
        {
            this.transform.Translate(0, -this.moveSpeed, 0);

            if (this.transform.position.y <= -1.5)
            {
                Destroy(this.gameObject);
            }
            yield return null;
        }
    }
}

 

EnemyGenerator

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyGanerator : MonoBehaviour
{
    Coroutine enemyGenerating;

    public GameObject[] enemies;

    private float span;
    private float delta;
    private int ratio;

    public float minGenerationgTime;
    public float maxGenerationgTime;
    // Start is called before the first frame update
    void Start()
    {
        this.enemyGenerating = this.StartCoroutine(EnemyGenerating());
    }

    IEnumerator EnemyGenerating()
    {
        while (true)
        {
            this.delta += Time.deltaTime;             

            if (this.span <= this.delta) //랜덤 시간으로 적 생성
            {
                this.delta = 0;
                this.span = Random.Range(minGenerationgTime, maxGenerationgTime);

                GameObject targetPrefab;
                int dice = Random.Range(1, 11);

                if (dice <= 2f)
                {
                    targetPrefab = this.enemies[0];
                }
                else if (dice > 2 && dice <= 5)
                {
                    targetPrefab = this.enemies[1];
                }
                else
                {
                    targetPrefab = this.enemies[2];
                }

                var go = Instantiate(targetPrefab);
                var x = Random.Range(-0.7f, 0.8f);

                go.transform.position = new Vector3(x, 1.8f, 0);
            }

            yield return null;
        }
    }

}

 

Explosion

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Explosion : MonoBehaviour
{
    Coroutine destroyExplosion;

    private float delta;
    // Start is called before the first frame update
    void Start()
    {
        this.destroyExplosion = StartCoroutine(DestroyExplosion());
    }

    IEnumerator DestroyExplosion()
    {
        while (true)
        {
            this.delta += Time.deltaTime;
            if (this.delta >= 0.6f)
            {
                this.delta = 0;
                Destroy(this.gameObject);
            }
            yield return null;
        }
    }
}

 

GameDirector

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameDirector : MonoBehaviour
{
    Coroutine showUI;

    public PlayerController player;
    public Text textScore;

    public int maxLife = 3;
    public int recentLife;
    public int score = 0;


    // Start is called before the first frame update
    void Start()
    {
        this.recentLife = this.maxLife;
        this.showUI = StartCoroutine(ShowUI());
        player.score = 0;
        this.textScore.text = string.Format("{0:#,###}", this.score);        
    }

    IEnumerator ShowUI()
    {
        while (true)
        {
            this.textScore.text = string.Format("{0:#,###}", this.score);
            Debug.Log(this.score);
            yield return null;
        }
    }

    IEnumerator MoveScene()
    {
        while (true)
        {

            yield return null;
        }
    }

}

 

 

PlayerController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    Coroutine playerMoveRoutine;
    Coroutine playerShootRoutine;
    Coroutine regenPlayer;

    public GameObject explosionFx;
    public GameObject bullet;
    private SpriteRenderer playerColor;
    public GameDirector gameDirector;
    private float maxAttackSpeed;
    public float attackSpeed = 10f;
    private bool isDying;
    private float regenDelta;
    private bool isIvincibility;
    private float invincibilityDelta;
    public int score = 0;    

    // Start is called before the first frame update
    void Start()
    {
        this.playerMoveRoutine = this.StartCoroutine(this.Move());
        this.playerShootRoutine = this.StartCoroutine(this.Shoot());
        this.regenPlayer = this.StartCoroutine(this.RegenPlayer());
        this.playerColor = this.gameObject.GetComponent<SpriteRenderer>();        
    }

    IEnumerator Move()
    {
        while (true)
        {
            if(isDying == false)
            {
                float xPos = Mathf.Clamp(this.transform.position.x, -0.7f, 0.7f);
                float yPos = Mathf.Clamp(this.transform.position.y, -1.3f, 1.3f);
                transform.position = new Vector3(xPos, yPos, 0.0f);

                var dirX = Input.GetAxisRaw("Horizontal");
                var dirY = Input.GetAxisRaw("Vertical");
                var dir = new Vector3(dirX, dirY, 0);

                this.transform.Translate(dir.normalized * 2.0f * Time.deltaTime);
            }

            yield return null;
        }
    }

    IEnumerator Shoot()
    {
        while (true)
        {
            if (Input.GetKey(KeyCode.Space) && isDying == false)
            {
                this.maxAttackSpeed++;
                if(this.maxAttackSpeed >= this.attackSpeed) //공격속도
                {
                    this.maxAttackSpeed = 0;
                    var go = Instantiate(bullet);
                    go.transform.position = this.transform.position + new Vector3(0, 0.15f, 0);
                }                
            }

            yield return null;
        }        
    }

    IEnumerator RegenPlayer()
    {
        while (true)
        {
            if(this.isDying == true) this.regenDelta += Time.deltaTime;
            if (this.isIvincibility == true) this.invincibilityDelta += Time.deltaTime;

            if (this.isDying == true && this.transform.position.y < -1.0f)
            {
                this.transform.Translate(new Vector3(0, 0.007f, 0), Space.World);                
            }
            else if(this.transform.position.y >= -1.0f && this.regenDelta >= 1.0f)
            {
                this.regenDelta = 0;
                this.isDying = false;
            }

            if(this.invincibilityDelta >= 2.5f)
            {                
                this.invincibilityDelta = 0;                
                this.isIvincibility = false;
                playerColor.color = new Color(255 / 255, 255 / 255, 255 / 255);                
            }


            yield return null;
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Enemy" && this.isIvincibility == false)
        {
            var go = Instantiate(explosionFx.gameObject);
            go.transform.position = this.transform.position;

            gameDirector.recentLife--;
            Destroy(collision.gameObject);

            this.isDying = true;
            this.isIvincibility = true;
            
            this.playerColor.color = new Color(150 / 255, 150 / 255, 150 / 255);

            this.gameObject.transform.position = new Vector3(0, -2.5f, 0);
        }
    }
}

 

 

 게임 오브젝트들을 통한 기능 구현은 가능하나 전체적인 구조를 만들지 못해서 오브젝트 간의 값을 주고받지 못했다. 예를들어 점수를 저장하거나 Life를 저장해서 씬을 변경하는 기능을 구현하는 것이 불가능헀다.