
캡틴 죽이기
--------------------------------------------------------------
애니메이션 컨트롤
정지상태에서 Idle 애니메이션 실행
마우스 버튼을 클릭하면 Hit 애니메이션 실행
2번 클릭하면 Dead 애니메이션 실행
죽은 후에는 클릭해도 변화 없음
---------------------------------------------------------------
HP 표시
화면에 캡틴의 HP 표시를 해줌
마우스 버튼을 클릭하면 HP 1 감소
HP 가 0이되면 클릭해도 변화 없음
--------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyController : MonoBehaviour
{
public enum State //애니메이션
{
Idle, Hit, Dead
}
[SerializeField] private Animator anim;
[SerializeField] private BombGameDirector gameDirector;
private float hitAnimLength = 0.133f; // 애니메이션 길이
private float dieAnimLength = 0.133f;
private State state;
private float delta = 0; // 시간 간격
private int maxHp = 2;
private int hp;
void Start()
{
this.hp = this.maxHp;
Debug.LogFormat("{0}/{1}", this.hp, this.maxHp);
this.gameDirector.UpdateHpText(this.hp, this.maxHp);
this.SetState(State.Idle); // 시작 동작
}
void Update()
{
//마우스를 클릭하면 hp -1 소모, 텍스트 표시, 애니 Hit 전환
if(Input.GetMouseButtonDown(0))
{
this.hp -= 1;
if (this.hp <= 0)
{
this.hp = 0;
}
this.gameDirector.UpdateHpText(this. hp, this.maxHp);
this.SetState(State.Hit);
}
switch (this.state)
{
case State.Idle:
break;
case State.Hit:
this.delta += Time.deltaTime;
if (this.delta > this.hitAnimLength)
{
this.delta = 0;
if (this.hp <= 0) //hp가 0보다 작으면
{
this.SetState(State.Dead);
}
else
{
this.SetState(State.Idle);
}
}
break;
case State.Dead:
this.delta += Time.deltaTime;
if (this.delta > this.dieAnimLength)
{
this.delta = 0;
Destroy(this.gameObject);
}
break;
}
}
private void SetState(State state)
{
if(this.state!= state)
{
this.state = state;
this.anim.SetInteger("State",(int)this.state);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BombGameDirector : MonoBehaviour
{
[SerializeField] private Text hpText;
private int maxHp=2;
private int hp;
public void UpdateHpText(int hp,int maxHp)
{
this.maxHp = maxHp;
this.hp= this.maxHp;
this.hpText.text="HP "+hp.ToString() +"/"+maxHp.ToString();
}
}

'게임개발 > 게임 클라이언트 프로그래밍' 카테고리의 다른 글
ShootingSpace(1) 우주선 총알 발사 (0) | 2024.02.03 |
---|---|
3D 밤송이를 날려라 (0) | 2024.02.03 |
PirateBomb(1) BombGuy (0) | 2024.02.01 |
Cat Climb Cloud (1) | 2024.01.31 |
c# 대리자 및 람다 (0) | 2024.01.29 |