게임 개발
[Unity] The Love 게임 - (3)
by chobbo
2024. 6. 7.
Boss.cs 구현 완료
using UnityEngine;
using System.Collections;
using UnityEngine.AI;
public enum BossSkill
{
Attack,
HeartStrike
}
public class Boss : Enemy
{
[Header("Boss")]
[SerializeField] private BossSkill _bossSkill;
[SerializeField] private float _bossSkillDistance;
[Header("HeartStrike Skill")]
[SerializeField] private GameObject _heartBullet;
[SerializeField] private Transform _bulletPos;
[SerializeField] private float _bulletNum;
[SerializeField] private float _heartStrikeDistance;
[SerializeField] private float _heartStrikeCoolTime;
private bool _isAttacking = false;
protected override void AttackPlayer()
{
if (!_isAttacking)
{
_bossSkill = SetSkill();
_isAttacking = true;
}
if (_playerToEnemyDistance > _bossSkillDistance || !IsPlayerInFieldOfView())
{
agent.isStopped = false;
NavMeshPath path = new NavMeshPath();
if (agent.CalculatePath(CharacterManager.Instance.Player.transform.position, path))
{
agent.SetDestination(CharacterManager.Instance.Player.transform.position);
}
}
else if (_playerToEnemyDistance < _bossSkillDistance)
{
if (Time.time - lastAttackTime > _attackCooltime)
{
lastAttackTime = Time.time;
UseSkill(_bossSkill);
}
}
}
BossSkill SetSkill()
{
int _id = Random.Range(0, System.Enum.GetValues(typeof(BossSkill)).Length);
switch (_id)
{
case 0:
_bossSkillDistance = _attackDistance;
return BossSkill.Attack;
case 1:
_bossSkillDistance = _heartStrikeDistance;
return BossSkill.HeartStrike;
}
return BossSkill.Attack;
}
void UseSkill(BossSkill skill)
{
switch(skill)
{
case BossSkill.Attack:
Attack();
break;
case BossSkill.HeartStrike:
HeartStrike();
break;
default:
break;
}
}
void Attack()
{
// TODO :: 플레이어 구현되면 칼에 플레이어 충돌 시 HP 달게 연동
animator.speed = 1;
animationController.AttackAnim();
agent.isStopped = true;
_isAttacking = false;
}
void HeartStrike()
{
agent.isStopped = true;
StartCoroutine(HeartStrikeCoroutine());
}
IEnumerator HeartStrikeCoroutine()
{
float _nowBullet = _bulletNum;
while (_nowBullet > 0)
{
ShootHeartBullet();
_nowBullet--;
yield return new WaitForSeconds(_heartStrikeCoolTime);
}
_isAttacking = false;
}
void ShootHeartBullet()
{
Instantiate(_heartBullet, _bulletPos.position, Quaternion.identity);
}
}
날아오는 사랑의 총알들
ObjectPool.cs 구현 완료
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ObjectPoolManager : Singleton<ObjectPoolManager>
{
[Header("Enemy")]
[SerializeField] private List<GameObject> _boss;
[SerializeField] private List<GameObject> _enemys;
[SerializeField] private int _enemyCount;
[Header("Object Pool")]
public List<GameObject> pool = new List<GameObject>();
void Start()
{
if (pool.Count == 0) InitializePool();
}
public void SpawnEnemy()
{
for (int i = 0; i < pool.Count; i++)
{
if (pool[i].activeInHierarchy)
{
pool[i].transform.position = GetRandomPosition();
pool[i].SetActive(true);
}
}
}
void InitializePool()
{
for (int i = 0; i < _enemyCount; i++)
{
SpawnRandomEnemy();
pool[i].SetActive(true);
}
}
void SpawnRandomEnemy()
{
int _id = Random.Range(0, _enemys.Count);
Vector3 _spawnPos = GetRandomPosition();
GameObject _obj = Instantiate(_enemys[_id],_spawnPos,Quaternion.identity);
_obj.transform.SetParent(transform);
_obj.SetActive(false);
pool.Add(_obj);
}
// TODO :: 랜덤 위치 경고문
Vector3 GetRandomPosition()
{
NavMeshHit hit;
float x = Random.Range(-30f,120f);
float z = Random.Range(0, 120f);
Vector3 randomPosition = new Vector3(x, 0, z);
if (NavMesh.SamplePosition(randomPosition, out hit, 1f, NavMesh.GetAreaFromName("Walkable")))
{
randomPosition = hit.position;
}
return randomPosition;
}
}