회원 로그인
정보기억 정보기억에 체크할 경우 다음접속시 아이디와 패스워드를 입력하지 않으셔도 됩니다.
그러나, 개인PC가 아닐 경우 타인이 로그인할 수 있습니다.
PC를 여러사람이 사용하는 공공장소에서는 체크하지 마세요.
소셜네트워크 서비스를 통해서 로그인하시면 별도의 로그인 절차없이 회원서비스를 이용하실 수 있습니다.


최근 게시물

1.노션에서 작성 중

1.노션에서 작성 중

개편하기 전까지 노션에서 작성 중

2024.04.04//read more

2.ChatGPT

2.ChatGPT

OpenAI로 대규모 언어 모델대화형...

2023.03.16//read more

3.노코딩 게임 엔진 - 빌..

3.노코딩 게임 엔진 - 빌..

빌드 지원안드로이드iOS윈도우즈특이사...

2023.03.14//read more

4.(완료) 미접속 회원 정..

4.(완료) 미접속 회원 정..

[완료] 36명의 회원을 정리하였습니...

2023.02.16//read more

5.매뉴얼 플러스 - 전자제..



안정적인 DNS 서비스 DNSEver
DNS Powered by DNSEver.com


interface (인터페이스) (with Unity)

푸딩뱃살 | 2015.11.15 17:32 | 조회 3306
interface (인터페이스) (with Unity)

*충돌 일반 방법, 업캐스팅 방법, 인터페이스 방법(CPlayer.cs)
*플레이어의 HP 게이지를 머리 위에서 따라가기

CBomb.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class CBomb : MonoBehaviour, IPlayerConflict {
    public void Conflict(CPlayer player)
    {
        player.BombDie(99);
    }
}

CCreature.cs
using UnityEngine;
using System.Collections;

public class CCreature : MonoBehaviour {

    public int hp = 100;

    // 충돌했다 메소드를 추상 메소드로 정의함
    // public abstract void Conflict(CPlayer player);

}

CHeart.cs
using UnityEngine;
using System.Collections;

public class CHeart : MonoBehaviour, IPlayerConflict {

    // 아이템을 취함
    public void PickUp(CPlayer player)
    {
        // 플레이어의 체력 증가
        player.HpUp(40);
    }

    //
    public void Conflict(CPlayer player)
    {
        PickUp(player);
    }
}

CMonster.cs
using UnityEngine;
using System.Collections;

public class CMonster : MonoBehaviour, IPlayerConflict {

    // 플레이어를 공격
    // 플레이어로 지정된 객체로 공격만 하나?
    public void Attack(CPlayer player)
    {
        player.Hit(10);
    }

    // 크리쳐의 충돌했음 메소드를 오버라이드 함
    public void Conflict(CPlayer player)
    {
        Attack(player);
    }
}

CPlayer.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class CPlayer : MonoBehaviour {

    public int _hp;
    public Image _hpBar;
    public CTriggerMonster _triggerMonster;

    void Update()
    {
        // 케릭터 이동
        float w = Input.GetAxis("Vertical");
        transform.Translate(Vector3.up * w * 4f * Time.deltaTime);
        float h = Input.GetAxis("Horizontal");
        transform.Translate(Vector3.right * h * 4f * Time.deltaTime);
    }

    void Start()
    {
        // transform.FindChild("자식오브텍트명)"
        // -> 현재 오브젝트의 자식들중 진정한 이름을 가진 오브젝트를 찾아줌
        // -> transform.FindChild("HpBar").GetComponent<Image>();
        // _hpBar = transform.FindChild("Canvas/HpPos/HpBackground/Hp").GetComponent<Image>();
        // -> 직접 경로로 찾음
    }

    // Trigger 충돌 이벤트 함수
    void OnTriggerEnter2D(Collider2D collider)
    {
        // 인터페이스
        IPlayerConflict conflict = collider.GetComponent<IPlayerConflict>();
        conflict.Conflict(this);

        // 업캐스팅
        /*
        CCreature creature = collider.GetComponent<CCreature>();
        creature.Conflict(this);
        */

        // 일반적인 조건
        /*
        // 게임오브젝트가 늘어날 때마다 조건을 해야 하는데 위의 코드로 오버라이드/업캐스팅으로 해결
        // 게임오브젝트에 각 속성을 넣어 플레이어에 적용!
        if (collider.name == "Monster")
        {
            Debug.Log("몬스터에게 공격 당함");
            CMonster monster = collider.GetComponent<CMonster>();
            // this: 현재 객채의 주소(?) - 스스로의 참조 변수
            monster.Attack(this);  // 자기 객체에서 자기 변수 참조(this) CPlayer의 주소
            // monster.Conflict(this);
        }
        else if (collider.name == "Heart")
        {
            Debug.Log("체력을 보충함");
            CHeart heart = collider.GetComponent<CHeart>();
            heart.PickUp(this);
            // heart.Conflict(this);
        }
        else if (collider.name == "Spike")
        {
            Debug.Log("플레이어가 장애물을 밟음");
            CSpike spike = collider.GetComponent<CSpike>();
            spike.Tread(this);
            // spike.Conflict(this);
        }
        else if (collider.name == "Bomb")
        {
            // 3초 후에 죽음
            Debug.Log("시한 폭탄이 동작함");
        }
        */
    }

    // 타격 입음
    public void Hit(int damage)
    {
        // 프로그레스바 감소
        _hp -= damage;
        _hpBar.fillAmount = _hp * 0.01f;

        // _hp가 0이면 사망
        if (_hp < 0)
        {
            Die();
            return;  // return : 함수를 빠져나감
        }

        Debug.Log(damage + "의 데미지를 입어 체력이 " + _hp + "가 됨");
    }

    // 체력 증가
    public void HpUp(int upValue)
    {
        // 프로그레스바 바로 적용
        _hp += upValue;
        if (_hp > 100) _hp = 100;  // 100이상 안되게
        _hpBar.fillAmount = _hp * 0.01f;
    }

    // 사망
    public void Die()
    {
        Destroy(gameObject);
    }

    // 폭탄
    public void BombDie(int value)
    {
        _hp -= value;
        _hpBar.fillAmount = _hp * 0.01f;

        Destroy(gameObject, 4f);
    }
}

CSpike.cs
using UnityEngine;
using System.Collections;

public class CSpike : MonoBehaviour, IPlayerConflict {

    public void Tread(CPlayer player) { 
        player.Die();
    }

    public void Conflict(CPlayer player)
    {
        Tread(player);
    }
}

IPlayerConflict.cs
using UnityEngine;
using System.Collections;

// 인터페이스
//플레이어와의 충돌에 대한 관계 타입
// 게임오브젝트(객체) 간의 독립된 관계 / 타입
// 
interface IPlayerConflict
{
    void Conflict(CPlayer player);
}


285개(6/15페이지)
프로그래밍
번호 제목 글쓴이 조회 날짜
185 [PHP] 회원가입 + 로그인 스크립트 (with Unity) [2+1] 푸딩뱃살 12240 2015.12.06 17:31
184 [C#] C# 추천 서적 푸딩뱃살 1321 2015.12.06 17:16
183 [C/C++] C++ 참고 사이트 푸딩뱃살 1182 2015.12.04 16:13
182 [PHP] 클래스 푸딩뱃살 3580 2015.12.04 14:40
181 [PHP] CodeIgniter(코드이그나이트) 첨부파일 푸딩뱃살 2931 2015.12.04 14:40
180 [C#] Delegate (델리게이트) (with Unity) 푸딩뱃살 6267 2015.12.01 10:44
179 [PHP] php 함수 푸딩뱃살 2219 2015.11.30 15:33
178 [PHP] Dictionary (딕셔너리) 첨부파일 푸딩뱃살 2868 2015.11.27 12:37
177 [PHP] 배열 푸딩뱃살 2170 2015.11.27 12:37
176 [PHP] 변수 선언 / 산술 연산 푸딩뱃살 2183 2015.11.27 11:14
175 [C#] Dictionary (딕셔너리) (with Unity) 첨부파일 [3+3] 푸딩뱃살 14483 2015.11.25 10:29
174 [C#] List (리스트) (with Unity) 첨부파일 푸딩뱃살 12087 2015.11.24 10:22
173 [C#] Generic (제네릭) (with Unity) 푸딩뱃살 4888 2015.11.22 12:32
172 [C/C++] Templete (템플릿) 푸딩뱃살 2045 2015.11.22 12:15
171 [C#] 키보드 입력 푸딩뱃살 3570 2015.11.21 18:00
170 [C#] 예외와 예외 처리 푸딩뱃살 4124 2015.11.21 17:39
169 [C#] Struct (구조체) (with Unity) 푸딩뱃살 5123 2015.11.18 22:08
168 [C#] Property (프로퍼티) (with Unity) 푸딩뱃살 2443 2015.11.18 10:50
>> [C#] interface (인터페이스) (with Unity) 첨부파일 푸딩뱃살 3307 2015.11.15 17:32
166 [C#] 스트림 (stream) - 문자, 바이너리 읽기/쓰기 푸딩뱃살 12780 2015.11.14 18:24