김김김의 게임개발
  • 유니티 게임개발 입문 #5
    2023년 09월 08일 21시 56분 28초에 업로드 된 글입니다.
    작성자: noun06

    [팀프로젝트 벽돌깨기]

    게임 기획 & 설계

    • 팀원들과 회의를 통해 다음과 같이 전반적인 초기 게임 시스템 구상과 클래스 설계를 진행하였음.

     

    운석(벽돌) 충돌

    • Meteor 클래스는 운석의 강도, 색상, 파티클 효과의 정보와 충돌처리, 파괴 등의 메서드를 가지고 있음. 각 운석 타입을 자식 클래스로 상속하고 있으며 자식 클래스에는 운석의 이름과 강도를 지정할 수 있음.
    • Meteor 클래스 생성자에서 이름, 강도와 함께 색상의 배열을 초기화하여 각 강도의 값일 때의 색상을 지정하려고 함.
    • OnCollision 메서드 내에서는 충돌될 때마다 강도를 1씩 감소시키며 색상 값을 강도에 따라 업데이트 해줌.
    • DestroyMeteor는 코루틴에 의해 파티클이 나오고 일정 시간 후에 호출되며 운석을 파괴하는 역할을 수행함. 시간 격차에 의해 나타난 충돌 버그로 인해 spriteRender을 비활성화함. 
    //Meteor.cs
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Meteor : MonoBehaviour
    {
        public string Name { get; }
        public int Hardness { get; set; }
    
        private SpriteRenderer spriteRenderer;
    
        private Color[] hardnessColors;
    
        private ParticleSystem particle;
    
        public Meteor(string name, int hardness)
        {
            Name = name;
            Hardness = hardness;
    
            hardnessColors = new Color[]
            {
                Color.white,
                Color.gray,
                Color.black
            };
        }
    
        private void Awake()
        {
            spriteRenderer = GetComponent<SpriteRenderer>();
            spriteRenderer.color = hardnessColors[Hardness - 1];
    
            particle = GetComponentInChildren<ParticleSystem>();
        }
    
        private void OnCollisionEnter2D(Collision2D collision)
        {
            if (collision.gameObject.CompareTag("Ball"))
            {
                Hardness--;
                if (Hardness <= 0)
                {
                    StartCoroutine(DestroyMeteor());
                }
                else
                {
                    spriteRenderer.color = hardnessColors[Hardness - 1];
                }
            }
        }
    
        private IEnumerator DestroyMeteor()
        {
            particle.Play();
    
            spriteRenderer.enabled = false;
            yield return new WaitForSeconds(1);
            gameObject.SetActive(false);
        }
    }
    //EasyMeteor.cs
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class EasyMeteor : Meteor
    {
        public EasyMeteor() : base("EM", 1)
        {
        }
    }

     

    결과

    댓글