슈팅게임 선생님 코드 복습

===[ IDamage(interface) ] =================================================


using UnityEngine;
using System.Collections;
 
public interface IDamage {
    int GetDamage();    
}


===[ CShip ] ==========================================================

using UnityEngine;
using System.Collections;
 
public class CShip : MonoBehaviour
{
 
    //체력설정
    public int _hp;
    //스피드 설정
    public float _speed;
    //적및 아이템 이동방향 설정
    public Vector2 _moveDirection;
    //충돌 이펙트 프리팹 등록
    public GameObject _hiEffectPrefab;
    // 게임 메니져 등록(UI및 점수 관리)
    public GameObject _gmaeManager;
    //타격시 오브젝트 색변경을 위한 SpriteRenderer 형 변수 생성 
    //protected는 상속된 클래스와 자신 클래스에서만 접근 하는 접근자
    protected SpriteRenderer _spriteRendere;
 
    //상속받은 자식 클래스에서 오버로드 할 수 있도록 virtual 키워드 사용
    protected virtual void Start()
    {
        // 게임메니저 오브젝트 변수 대입
        _gmaeManager = GameObject.Find("");
        //_spriteRendere 변수에 GetComponent<SpriteRenderer>() 대입.
        _spriteRendere = GetComponent<SpriteRenderer>();
    }
 
    protected virtual void Update()
    {
        // 이동 메소드 호출
        Move();
    }
 
    //자동 이동 메소드
    public virtual void Move()
    {
        // vecta2 자료형의 _moveDirection 변수를 써서 방향을 설정한다.
        transform.Translate(_moveDirection * _speed * Time.deltaTime);
    }
    //사망시 게임 오브젝트 삭제
    public virtual void Die()
    {
        Destroy(gameObject);
    }
    // 충돌시 설정 메소드 매개변수로 Collider2D 형 collider변수를 받는다.
    public virtual void Hit(Collider2D collider)
    {
        //충돌시 이펙트 프리팹 화면 표시
        Instantiate(_hiEffectPrefab, transform.position, Quaternion.identity);
        Destroy(collider.gameObject);
 
        //IDamage 형 damageUnit 객체 변수 생성
        IDamage damageUnit = collider.GetComponent<IDamage>();
 
        // 인터페이스화 되어 있는 모든 클래스에서 동일하게 실행
        _hp -= damageUnit.GetDamage();
 
        if (_hp <= 0)
        {
            Die();
            return;
        }
        // 컬러 체인지 코루틴 실행
        StartCoroutine(HitColorChangeCorutine());
    }
 
    //IEnumerator 는 반드시 반복문을 사용하지 않아도 된다...
    public virtual IEnumerator HitColorChangeCorutine()
    {
        _spriteRendere.material.color = new Color(0.3f, 0f, 0f, 0.6f);
        yield return new WaitForSeconds(0.1f);
        _spriteRendere.material.color = new Color(1f, 1f, 1f, 1f);
    }
}

===[ CEnemyShip ] =======================================================
using UnityEngine;
using System.Collections;
 
public class CEnemyShip : CShip, IDamage
{
 
    //게임오브젝트 별로 다른 데미지 값 설정
    public int _damage;
    //게임오브젝트 별로 다른 포인트 값 설정
    public int _dieScorePoint;
 
    protected override void Start()
    {
        //부모의 Start 메소드 그대로 실행
        base.Start();
    }
 
    void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.tag == "PlayerBullet")
        {
            //부모 클래스에서 Hit메소드를 실행 인자로 
            //부딪힌 게임 오브젝트를 넘겨준다.
            Hit(collider);
        }
    }
 
    //인터페이스 활용
    public int GetDamage()
    {
        // 설정한 데미지 값을 돌려준다.
        return _damage;
    }
    // 부모의 Die 메소드 오버라이드
    public override void Die()
    {
        // 게임매니저에 스코어 전달
        _gmaeManager.SendMessage("ScoreUP", _dieScorePoint);
        Destroy(gameObject);
    }
 
}
===[ CEnemyShip(추상클래스) ] ===============================================
using UnityEngine;
using System.Collections;
//무조건 오버라이드 되야 하는 추상 함수
//클래스로서 사용불가함.
public abstract class CFireShip : CEnemyShip
{
    // 총알 프리팹 등록
    public GameObject _bulletPrefab;
    // 총알이 나가는(생성되는)위치 등록
    // (비행체의 몸통에 빈 게임오브젝트 생성 후 하이어라키 시킴)
    public Transform _shotPos;
 
    protected override void Start()
    {
        base.Start();
    }
    // 자식 클래스에서 반드시 가지고 있어야 할 메소드
    public abstract IEnumerator ShotCoroutine();
    public abstract void Shot();
}
===[ CEnemyShip(추상클래스) ] ===============================================
using UnityEngine;
using System.Collections;
//무조건 오버라이드 되야 하는 추상 클래스
//클래스로서 사용불가함.
public abstract class CFireShip : CEnemyShip
{
    // 총알 프리팹 등록
    public GameObject _bulletPrefab;
    // 총알이 나가는(생성되는)위치 등록
    // (비행체의 몸통에 빈 게임오브젝트 생성 후 하이어라키 시킴)
    public Transform _shotPos;
 
    protected override void Start()
    {
        base.Start();
    }
    // 자식 클래스에서 반드시 가지고 있어야 할 메소드
    public abstract IEnumerator ShotCoroutine();
    public abstract void Shot();
}
===[ CFireEnemyShip ] ===============================================

using UnityEngine;
using System.Collections;
 
public class CFireEnemyShip : CFireShip
{
 
    public float shotDelayTime;
 
    public override IEnumerator ShotCoroutine()
    {
        while (true)
        {
            yield return new WaitForSeconds(shotDelayTime);
            //Shot();
        }
    }
 
    public override void Shot()
    {
        //부모 클래스에서 선언했던 _bulletPrefab 프리팹을 _shotPos 위치에서 생성한다.
        Instantiate(_bulletPrefab, _shotPos.position, Quaternion.identity);
 
    }
}

댓글

이 블로그의 인기 게시물

날짜 시간 시간차 시간 계산 하기

코루틴에서 CallBack 함수 적용하기

C++ 언어 퍼센트 구하는 방법 / 기본 언어 퍼센트 구하는 방법