12강 복습

== [ 12강 복습 인터페이스 없는 예 ]=============================================




[player]==============================================================


using UnityEngine;
using System.Collections;

public class CPlayer : MonoBehaviour {

    public float speed = 5;
    public int _hp;
    
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {

        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        transform.Translate(Vector2.right * h * speed * Time.deltaTime);
        transform.Translate(Vector2.up * v *speed * Time.deltaTime);      
 
 }

    public void Hit(int damage)
    {
        HpDown(damage);
        if (damage <= 0)
        {
            Die();
        }
        
        Debug.Log("플레이어의 현재 체력은 : " + _hp);
    }

    public void Die()
    {
        Debug.Log("플레이어가 사망 했습니다,");
        _hp = 0;
        Destroy(gameObject,0.5f);
    }

    public void HpUp(int hp)
    {
        Debug.Log("플레이어의 체력이" + hp + "만큼 채워집니다");
        _hp += hp;
    }

    public void HpDown(int damage)
    {
        Debug.Log("플레이어가 " + damage + " 만큼 타격받았습니다.");
        _hp -= damage;
    }


    void OnTriggerEnter2D(Collider2D col)
    {

        if (col.name == "Monster")
        {   // 부딪힌 몬스터의 클래스 객체타입을 찾아서 변수로 만들어 주고 변수로 접근하여 해당 클래스의 메소드를 불러온다
            CMonster monster = col.GetComponent<CMonster>();
            monster.Attack(this);
        }
        else if (col.name == "Hert")
        {
            CHeart heart = col.GetComponent<CHeart>();
            heart.Hpplus(this);
        }

        else if (col.name == "bomb")
        {
            CBomb boom = col.GetComponent<CBomb>();
            boom.Boomb(this);
        }
        else if (col.name == "spikes")
        {
            CTrap trap = col.GetComponent<CTrap>();
            trap.Death(this);
        }

    }


}

[monster]==============================================================

using UnityEngine;
using System.Collections;

public class CMonster : MonoBehaviour {


    int _damage = 10;
 
    // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }

    public void Attack(CPlayer player)
    {
        Debug.Log("몬스터가 공격 합니다.");
        player.Hit(_damage);
    }
}
[heart]==============================================================
using UnityEngine; using System.Collections; public class CHeart : MonoBehaviour { int _hpup = 10; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void Hpplus(CPlayer player) { Debug.Log("플레이어의 체력이 회복됩니다."); player.HpUp(_hpup); } }

[trap]==============================================================
using UnityEngine; using System.Collections; public class CTrap : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void Death(CPlayer player) { Debug.Log("플레이어가 사망 합니다."); player.Die(); } }

[bomb]==============================================================
using UnityEngine; using System.Collections; public class CBomb : MonoBehaviour { float time = 1f; int _damage = 30; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void Boomb(CPlayer player) { Debug.Log("폭탄이 폭발합니다."); StartCoroutine(Timer(player)); } public IEnumerator Timer(CPlayer player) { int i = 3; while (i >= 1) { Debug.Log("폭탄 폭발 " + i + " 초전"); yield return new WaitForSeconds(time); i--; } player.Hit(_damage); } }

댓글

이 블로그의 인기 게시물

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

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

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