회원 로그인
|
interface (인터페이스) (with Unity)
interface (인터페이스) (with Unity)
*충돌 일반 방법, 업캐스팅 방법, 인터페이스 방법(CPlayer.cs)
*플레이어의 HP 게이지를 머리 위에서 따라가기
CBomb.cs
CCreature.cs
CHeart.cs
CMonster.cs
CPlayer.cs
CSpike.cs
IPlayerConflict.cs
*충돌 일반 방법, 업캐스팅 방법, 인터페이스 방법(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); }
- interface_sample.unitypackage (3.3MB) (0)
댓글 0개
| 엮인글 0개
285개(1/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
285 | [Python] 동적 import - 모듈을 변수로 받아오기 | 푸딩뱃살 | 417 | 2022.10.27 10:45 |
284 | [Python] 파이썬 3.7.7과 3.9.7의 os.path.expanduser() 차이 | 푸딩뱃살 | 461 | 2022.08.18 12:22 |
283 | [Python] error: Microsoft Visual C++ 9.0 is required. | 푸딩뱃살 | 691 | 2022.08.03 13:35 |
282 | [Python] pyscript | 푸딩뱃살 | 460 | 2022.06.09 11:21 |
281 | [Python] float is / float not is | 푸딩뱃살 | 598 | 2022.03.02 15:03 |
280 | [Python] 이터널 문자열 f | 푸딩뱃살 | 839 | 2022.01.27 16:35 |
279 | [Python] is와 ==의 차이 | 푸딩뱃살 | 488 | 2021.11.25 15:54 |
278 |
[Python] Error: ImportError: file |
푸딩뱃살 | 926 | 2021.11.16 11:24 |
277 | [Python] 파이썬 디컴파일 - uncompyle6 | 푸딩뱃살 | 778 | 2021.11.10 14:46 |
276 | [Python] 파이썬 확장자 설명 | 푸딩뱃살 | 553 | 2021.11.03 14:38 |
275 | [참고] 웹 fbx 뷰어 | 푸딩뱃살 | 473 | 2021.10.19 15:46 |
274 | [Python] enumerate() | 푸딩뱃살 | 504 | 2021.10.13 14:44 |
273 | [Python] 아나콘다에서 가상 환경 | 푸딩뱃살 | 729 | 2020.11.21 00:26 |
272 | [Python] pip로 설치 때 퍼미션 에러 | 푸딩뱃살 | 1273 | 2020.06.06 17:13 |
271 | [Python] OpenCV 10-3. 이미지 Thresholding - Otsu's Binarizatio | 푸딩뱃살 | 677 | 2020.06.05 14:01 |
270 | [Python] OpenCV 10-2. 이미지 Thresholding - Adaptive Threshold | 푸딩뱃살 | 699 | 2020.06.05 13:58 |
269 | [Python] OpenCV 10-1. 이미지 Thresholding | 푸딩뱃살 | 580 | 2020.06.05 13:56 |
268 | [Python] OpenCV 9-2. 색 추적 | 푸딩뱃살 | 777 | 2020.06.02 23:29 |
267 | [Python] OpenCV 9-1. 색공간 바꾸기 | 푸딩뱃살 | 643 | 2020.06.02 23:27 |
266 | [Python] OpenCV 8-3. 이미지 비트 연산 | 푸딩뱃살 | 533 | 2020.06.02 23:21 |