using System.Collections;
using Cysharp.Threading.Tasks;
using GameLogic;
using TEngine;
using UnityEngine;
using AudioType = TEngine.AudioType;
using Object = UnityEngine.Object;
using Random = UnityEngine.Random;
///
/// 战斗房间
///
[Update]
public class BattleSystem : BehaviourSingleton
{
private enum ESteps
{
None,
Ready,
Spawn,
WaitSpawn,
WaitWave,
GameOver,
}
private GameObject _roomRoot;
// 关卡参数
private const int EnemyCount = 10;
private const int EnemyScore = 10;
private const int AsteroidScore = 1;
private readonly Vector3 _spawnValues = new Vector3(6, 0, 20);
private readonly string[] _entityLocations = new string[]
{
"asteroid01", "asteroid02", "asteroid03", "enemy_ship"
};
private ESteps _steps = ESteps.None;
private int _totalScore = 0;
private int _waveSpawnCount = 0;
//
// private UniTimer _startWaitTimer = UniTimer.CreateOnceTimer(1f);
// private UniTimer _spawnWaitTimer = UniTimer.CreateOnceTimer(0.75f);
// private UniTimer _waveWaitTimer = UniTimer.CreateOnceTimer(4f);
private float _startWaitTimer = 1f;
private float _spawnWaitTimer = 0.75f;
private float _waveWaitTimer = 4f;
///
/// 加载房间
///
public async UniTaskVoid LoadRoom()
{
_startWaitTimer = 1f;
await UniTask.Yield();
// 创建房间根对象
_roomRoot = new GameObject("BattleRoom");
// 加载背景音乐
GameModule.Audio.Play(AudioType.Music, "music_background", true);
// 创建玩家实体对象
var handle = PoolManager.Instance.GetGameObject("player_ship",parent: _roomRoot.transform);
var entity = handle.GetComponent();
// 显示战斗界面
GameModule.UI.ShowUIAsync();
// 监听游戏事件
GameEvent.AddEventListener(ActorEventDefine.PlayerDead, OnPlayerDead);
GameEvent.AddEventListener(ActorEventDefine.EnemyDead, OnEnemyDead);
GameEvent.AddEventListener(ActorEventDefine.AsteroidExplosion, OnAsteroidExplosion);
GameEvent.AddEventListener(ActorEventDefine.PlayerFireBullet, OnPlayerFireBullet);
GameEvent.AddEventListener(ActorEventDefine.EnemyFireBullet, OnEnemyFireBullet);
_steps = ESteps.Ready;
}
///
/// 销毁房间
///
public void DestroyRoom()
{
// 加载背景音乐
GameModule.Audio.Stop(AudioType.Music, true);
// if (_entitySpawner != null)
// {
// _entitySpawner.DestroyAll(true);
// }
if (_roomRoot != null)
Object.Destroy(_roomRoot);
GameModule.UI.CloseUI();
// 监听游戏事件
GameEvent.RemoveEventListener(ActorEventDefine.PlayerDead, OnPlayerDead);
GameEvent.RemoveEventListener(ActorEventDefine.EnemyDead, OnEnemyDead);
GameEvent.RemoveEventListener(ActorEventDefine.AsteroidExplosion, OnAsteroidExplosion);
GameEvent.RemoveEventListener(ActorEventDefine.PlayerFireBullet, OnPlayerFireBullet);
GameEvent.RemoveEventListener(ActorEventDefine.EnemyFireBullet, OnEnemyFireBullet);
}
public override void Update()
{
UpdateRoom();
}
///
/// 更新房间
///
public void UpdateRoom()
{
if (_steps == ESteps.None || _steps == ESteps.GameOver)
return;
if (_steps == ESteps.Ready)
{
_startWaitTimer -= Time.deltaTime;
if (_startWaitTimer <= 0)
{
_steps = ESteps.Spawn;
}
}
if (_steps == ESteps.Spawn)
{
var enemyLocation = _entityLocations[Random.Range(0, 4)];
Vector3 spawnPosition = new Vector3(Random.Range(-_spawnValues.x, _spawnValues.x), _spawnValues.y, _spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
if (enemyLocation == "enemy_ship")
{
// 生成敌人实体
var gameObject = PoolManager.Instance.GetGameObject(enemyLocation,parent: _roomRoot.transform);
gameObject.transform.position = spawnPosition;
gameObject.transform.rotation = spawnRotation;
var entity = gameObject.GetComponent();
entity.InitEntity();
}
else
{
// 生成小行星实体
var gameObject = PoolManager.Instance.GetGameObject(enemyLocation,parent: _roomRoot.transform);
gameObject.transform.position = spawnPosition;
gameObject.transform.rotation = spawnRotation;
var entity = gameObject.GetComponent();
entity.InitEntity();
}
_waveSpawnCount++;
if (_waveSpawnCount >= EnemyCount)
{
_steps = ESteps.WaitWave;
}
else
{
_steps = ESteps.WaitSpawn;
}
}
if (_steps == ESteps.WaitSpawn)
{
_spawnWaitTimer -= Time.deltaTime;
if (_spawnWaitTimer <= 0)
{
_spawnWaitTimer = 0.75f;
_steps = ESteps.Spawn;
}
}
if (_steps == ESteps.WaitWave)
{
_waveWaitTimer -= Time.deltaTime;
if (_waveWaitTimer <= 0)
{
_waveWaitTimer = 4f;
_waveSpawnCount = 0;
_steps = ESteps.Spawn;
}
}
}
#region 接收事件
private void OnPlayerDead(Vector3 position, Quaternion rotation)
{
// 创建爆炸效果
var gameObject = PoolManager.Instance.GetGameObject("explosion_player",parent: _roomRoot.transform);
gameObject.transform.position = position;
gameObject.transform.rotation = rotation;
var entity = gameObject.GetComponent();
entity.InitEntity();
_steps = ESteps.GameOver;
GameEvent.Send(ActorEventDefine.GameOver);
}
private void OnEnemyDead(Vector3 position, Quaternion rotation)
{
// 创建爆炸效果
var gameObject = PoolManager.Instance.GetGameObject("explosion_enemy",parent: _roomRoot.transform);
gameObject.transform.position = position;
gameObject.transform.rotation = rotation;
var entity = gameObject.GetComponent();
entity.InitEntity();
_totalScore += EnemyScore;
GameEvent.Send(ActorEventDefine.ScoreChange, _totalScore);
}
private void OnAsteroidExplosion(Vector3 position, Quaternion rotation)
{
// 创建爆炸效果
var gameObject = PoolManager.Instance.GetGameObject("explosion_asteroid",parent: _roomRoot.transform);
gameObject.transform.position = position;
gameObject.transform.rotation = rotation;
var entity = gameObject.GetComponent();
entity.InitEntity();
_totalScore += AsteroidScore;
GameEvent.Send(ActorEventDefine.ScoreChange, _totalScore);
}
private void OnPlayerFireBullet(Vector3 position, Quaternion rotation)
{
// 创建子弹实体
var gameObject = PoolManager.Instance.GetGameObject("player_bullet",parent: _roomRoot.transform);
gameObject.transform.position = position;
gameObject.transform.rotation = rotation;
var entity = gameObject.GetComponent();
entity.InitEntity();
}
private void OnEnemyFireBullet(Vector3 position, Quaternion rotation)
{
// 创建子弹实体
var gameObject = PoolManager.Instance.GetGameObject("enemy_bullet",parent: _roomRoot.transform);
gameObject.transform.position = position;
gameObject.transform.rotation = rotation;
var entity = gameObject.GetComponent();
entity.InitEntity();
}
#endregion
}