178 lines
5.7 KiB
C#
178 lines
5.7 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using SepCore.Event;
|
|
using SepCore.Entity;
|
|
using SepCore.Definition;
|
|
using SepCore.EnemyManager;
|
|
using SepCore.Simulation;
|
|
using SepCore.Timer;
|
|
using UnityEngine;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace SepCore.Procedure
|
|
{
|
|
public class GameStateBattle : GameStateBase
|
|
{
|
|
private const float VacuumMaxDuration = 0.8f;
|
|
private const float VacuumAbsorbSpeed = 30f;
|
|
|
|
public override GameStateType GameStateType => GameStateType.Battle;
|
|
|
|
private EnemyManagerComponent _enemyManager;
|
|
private int _currentLevel;
|
|
private TimerHandle _levelTimerHandle;
|
|
private bool _isWaveEnding;
|
|
private float _vacuumTimer;
|
|
|
|
private Player Player => _procedureGame.Player;
|
|
private ProcedureGame _procedureGame;
|
|
|
|
public void AddBattleDuration(float seconds)
|
|
{
|
|
if (_isWaveEnding || seconds <= 0f) return;
|
|
|
|
float remaining = GameEntry.Timer.GetRemainingTime(_levelTimerHandle);
|
|
if (remaining < 0f) return;
|
|
|
|
GameEntry.Timer.ResetRemainingTime(_levelTimerHandle, remaining + seconds);
|
|
}
|
|
|
|
#region FSM
|
|
|
|
public override void OnInit(ProcedureGame master)
|
|
{
|
|
_enemyManager = GameEntry.EnemyManager;
|
|
_procedureGame = master;
|
|
}
|
|
|
|
public override void OnEnter()
|
|
{
|
|
_currentLevel = _procedureGame.CurrentLevel;
|
|
_isWaveEnding = false;
|
|
_vacuumTimer = 0f;
|
|
|
|
var levelConfig = GameEntry.Luban.Get<LevelConfig>(_currentLevel);
|
|
_levelTimerHandle = GameEntry.Timer.ScheduleOnce(levelConfig.Duration, OnLevelTimeUp, this);
|
|
_enemyManager.OnInit(levelConfig, Player);
|
|
|
|
Player.Enable = true;
|
|
|
|
if (Application.isMobilePlatform)
|
|
{
|
|
GameEntry.UIRouter.OpenUIAsync(UIFormType.JoystickForm).Forget();
|
|
}
|
|
}
|
|
|
|
public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
|
|
{
|
|
if (_isWaveEnding)
|
|
{
|
|
Vector3 currentPos = Player.CachedTransform.position;
|
|
GameEntry.SimulationWorld.Tick(new SimulationTickContext(elapseSeconds, currentPos));
|
|
|
|
_vacuumTimer -= elapseSeconds;
|
|
if (GameEntry.SimulationWorld.PickupCount == 0 || _vacuumTimer <= 0f)
|
|
{
|
|
CollectAllRemainingDrops(Player);
|
|
_isWaveEnding = false;
|
|
_procedureGame.BattleToShopOrLevelUp();
|
|
}
|
|
return;
|
|
}
|
|
|
|
_enemyManager.OnUpdate(elapseSeconds, realElapseSeconds);
|
|
|
|
Vector3 playerPosition = Player.CachedTransform.position;
|
|
GameEntry.SimulationWorld.Tick(new SimulationTickContext(elapseSeconds, playerPosition));
|
|
|
|
int timeLeft = Mathf.Max(0, (int)GameEntry.Timer.GetRemainingTime(_levelTimerHandle));
|
|
GameEntry.Event.Fire(this, LevelProcessEventArgs.Create(timeLeft));
|
|
}
|
|
|
|
private void OnLevelTimeUp()
|
|
{
|
|
if (_isWaveEnding) return;
|
|
|
|
_isWaveEnding = true;
|
|
_vacuumTimer = VacuumMaxDuration;
|
|
|
|
// 1. 停止玩家移动与射击
|
|
Player.Enable = false;
|
|
|
|
// 2. 清退所有敌人并停止刷怪
|
|
_enemyManager.OnReset();
|
|
|
|
// 3. 隐藏所有投射物
|
|
HideEntityGroup(GameEntry.Luban.Global.ProjectileGroupName);
|
|
|
|
// 4. 触发全图磁铁,并提高吸附速度
|
|
GameEntry.SimulationWorld.TriggerFullMapMagnet();
|
|
GameEntry.SimulationWorld.SyncPlayerAbsorbSettings(999f, VacuumAbsorbSpeed, GameEntry.Luban.Global.CollectDistance);
|
|
}
|
|
|
|
public override void OnLeave()
|
|
{
|
|
GameEntry.UIRouter.CloseUIAsync(UIFormType.JoystickForm).Forget();
|
|
|
|
if (_levelTimerHandle.IsValid)
|
|
{
|
|
GameEntry.Timer.Cancel(_levelTimerHandle);
|
|
_levelTimerHandle = TimerHandle.Invalid;
|
|
}
|
|
|
|
_isWaveEnding = false;
|
|
|
|
// 隐藏所有敌人实体
|
|
_enemyManager.OnReset();
|
|
|
|
// 停止玩家逻辑
|
|
Player.Enable = false;
|
|
|
|
// 隐藏所有掉落物与投射物实体
|
|
HideEntityGroup(GameEntry.Luban.Global.DropGroupName);
|
|
HideEntityGroup(GameEntry.Luban.Global.ProjectileGroupName);
|
|
}
|
|
|
|
public override void OnDestroy()
|
|
{
|
|
_enemyManager = null;
|
|
_procedureGame = null;
|
|
}
|
|
|
|
private static void CollectAllRemainingDrops(Player player)
|
|
{
|
|
if (player == null || !GameEntry.Entity.HasEntityGroup(GameEntry.Luban.Global.DropGroupName))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var entityGroup = GameEntry.Entity.GetEntityGroup(GameEntry.Luban.Global.DropGroupName);
|
|
var entities = entityGroup.GetAllEntities();
|
|
foreach (var entity in entities)
|
|
{
|
|
if (entity is UnityGameFramework.Runtime.Entity runtimeEntity && runtimeEntity.Logic is DropEntity drop)
|
|
{
|
|
drop.TryCollect(player);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void HideEntityGroup(string groupName)
|
|
{
|
|
if (!GameEntry.Entity.HasEntityGroup(groupName))
|
|
{
|
|
Log.Error("Entity group '{0}' does not exist.", groupName);
|
|
return;
|
|
}
|
|
|
|
var entityGroup = GameEntry.Entity.GetEntityGroup(groupName);
|
|
var entities = entityGroup.GetAllEntities();
|
|
foreach (var entity in entities)
|
|
{
|
|
GameEntry.Entity.HideEntity(entity.Id);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|