345 lines
10 KiB
C#
345 lines
10 KiB
C#
using Gameplay.Level;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Gameplay.Building;
|
|
using Gameplay.Common;
|
|
using Gameplay.Fight.Protect;
|
|
using Gameplay.Summon;
|
|
using Gameplay.Unit;
|
|
using Gameplay.Unit.Data;
|
|
using Gameplay.Vehicle.Impl;
|
|
|
|
namespace Gameplay.Pause
|
|
{
|
|
using Character;
|
|
using Framework;
|
|
using Gameplay.Area;
|
|
using Gameplay.Effect;
|
|
using Gameplay.Skill;
|
|
using UnityEngine;
|
|
|
|
public class PauseManager
|
|
{
|
|
|
|
private static PauseManager _instance;
|
|
|
|
public static PauseManager instance
|
|
{
|
|
get
|
|
{
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
public static PauseManager CreateInstance()
|
|
{
|
|
if (_instance != null) return _instance;
|
|
_instance = new PauseManager();
|
|
return _instance;
|
|
}
|
|
|
|
private float _globalScale = 1f;
|
|
public float globalScale
|
|
{
|
|
get
|
|
{
|
|
return _globalScale;
|
|
}
|
|
set
|
|
{
|
|
if (Math.Abs(_globalScale - value) > 0.1f)
|
|
{
|
|
_globalScale = value;
|
|
isSpeedChanged = true;
|
|
|
|
// 暂停时强制更新一次
|
|
if (_globalScale == 0)
|
|
{
|
|
LogicUpdate(0);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public bool isSpeedChanged
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
|
|
private List<AnimatorPauser> _animList;
|
|
|
|
private List<ParticalPauser> _paticalList;
|
|
|
|
public void Init()
|
|
{
|
|
_animList = new List<AnimatorPauser>();
|
|
|
|
_paticalList = new List<ParticalPauser>();
|
|
|
|
// 先把场景中的绑定
|
|
var allUnits = GameUnitManager.instance.infightUnits;
|
|
for (int i = 0; i < allUnits.count; i++)
|
|
{
|
|
var unit = allUnits.GetByIndex(i);
|
|
_AddUnit(unit);
|
|
}
|
|
|
|
_RegistEvents();
|
|
}
|
|
|
|
private void _AddUnit(GameUnit unit)
|
|
{
|
|
switch (unit.gameunitType)
|
|
{
|
|
case EGameUnitType.Character:
|
|
var character = unit as Character;
|
|
_AddCharacter(character);
|
|
break;
|
|
case EGameUnitType.Vehicle:
|
|
var vehicle = unit as NormalVehicle;
|
|
_AddVehicle(vehicle);
|
|
break;
|
|
case EGameUnitType.Summon:
|
|
var summon = unit as BaseSummon;
|
|
_AddSummon(summon);
|
|
break;
|
|
case EGameUnitType.Player:
|
|
// 玩家不需要暂停
|
|
break;
|
|
case EGameUnitType.BeProtect:
|
|
var beProtectUnit = unit as BeProtectUnit;
|
|
_AddBeProtect(beProtectUnit);
|
|
break;
|
|
case EGameUnitType.Building:
|
|
var building = unit as UnitBuild;
|
|
_AddBuilding(building);
|
|
break;
|
|
default:
|
|
DebugUtil.LogError("暂未处理的类型:{0}", unit.gameunitType);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void _AddBuilding(UnitBuild building)
|
|
{
|
|
// 建筑有一个动画
|
|
var anims = building.Node.GameObject.GetComponentsInChildren<Animator>();
|
|
for (var i = 0; i < anims.Length; ++i)
|
|
{
|
|
var anim = anims[i];
|
|
_AddAnim(anim, building);
|
|
}
|
|
// 建筑也可能有特效
|
|
var particals = building.Node.GameObject.GetComponentsInChildren<ParticleSystem>();
|
|
for (var i = 0; i < particals.Length; ++i)
|
|
{
|
|
var partical = particals[i];
|
|
_AddPartical(partical, building);
|
|
}
|
|
}
|
|
|
|
private void _AddBeProtect(BeProtectUnit beProtectUnit)
|
|
{
|
|
// 动画
|
|
var anims = beProtectUnit.Node.GameObject.GetComponentsInChildren<Animator>();
|
|
for (var i = 0; i < anims.Length; ++i)
|
|
{
|
|
var anim = anims[i];
|
|
_AddAnim(anim, beProtectUnit);
|
|
}
|
|
// 特效
|
|
var particals = beProtectUnit.Node.GameObject.GetComponentsInChildren<ParticleSystem>();
|
|
for (var i = 0; i < particals.Length; ++i)
|
|
{
|
|
var partical = particals[i];
|
|
_AddPartical(partical, beProtectUnit);
|
|
}
|
|
}
|
|
|
|
private void _AddSummon(BaseSummon summon)
|
|
{
|
|
// 可能有动画
|
|
var anims = summon.Node.GameObject.GetComponentsInChildren<Animator>();
|
|
for (var i = 0; i < anims.Length; ++i)
|
|
{
|
|
var anim = anims[i];
|
|
_AddAnim(anim, summon);
|
|
}
|
|
// 也可能有特效
|
|
var particals = summon.Node.GameObject.GetComponentsInChildren<ParticleSystem>();
|
|
for (var i = 0; i < particals.Length; ++i)
|
|
{
|
|
var partical = particals[i];
|
|
_AddPartical(partical, summon);
|
|
}
|
|
}
|
|
|
|
private void _AddVehicle(NormalVehicle vehicle)
|
|
{
|
|
// 车有两个动画,车本身一个,炮台一个
|
|
var anim1 = vehicle.vAnim.rawAnim;
|
|
var anim2 = vehicle.battery.rawAnim;
|
|
if (anim1 != null)
|
|
{
|
|
_AddAnim(anim1, vehicle);
|
|
}
|
|
if (anim2 != null)
|
|
{
|
|
_AddAnim(anim2, vehicle);
|
|
}
|
|
}
|
|
|
|
private void _AddCharacter(Character c)
|
|
{
|
|
var animator = c.RawAnimator;
|
|
if (animator == null) return;
|
|
_AddAnim(animator, c);
|
|
}
|
|
|
|
private void _AddAnim(Animator anim,
|
|
GameUnit owner)
|
|
{
|
|
var pauser = new AnimatorPauser(anim);
|
|
pauser.owner = owner;
|
|
_animList.Add(pauser);
|
|
}
|
|
|
|
private void _AddPartical(ParticleSystem partical,
|
|
GameUnit owner)
|
|
{
|
|
var pauser = new ParticalPauser(partical);
|
|
pauser.owner = owner;
|
|
_paticalList.Add(pauser);
|
|
}
|
|
|
|
private void _RegistEvents()
|
|
{
|
|
EventManager.Instance.Register(EventManager.EventName.INFIGHT_UNIT_READY, (Action<GameUnit>)_OnUnitReady);
|
|
EventManager.Instance.Register(EventManager.EventName.INFIGHT_UNIT_REMOVE, (Action<GameUnit>)_OnUnitRemove);
|
|
EventManager.Instance.Register(EventManager.EventName.EFFECT_INST_FINISHED, (Action<EffectPlayingData>)_OnEffectInstFinished);
|
|
EventManager.Instance.Register(EventManager.EventName.EFFECT_INST_RETURN, (Action<EffectPlayingData>)_OnEffectInstReturn);
|
|
}
|
|
|
|
private void _UnRegistEvents()
|
|
{
|
|
EventManager.Instance.Unregister(EventManager.EventName.INFIGHT_UNIT_READY, (Action<GameUnit>)_OnUnitReady);
|
|
EventManager.Instance.Unregister(EventManager.EventName.INFIGHT_UNIT_REMOVE, (Action<GameUnit>)_OnUnitRemove);
|
|
EventManager.Instance.Unregister(EventManager.EventName.EFFECT_INST_FINISHED, (Action<EffectPlayingData>)_OnEffectInstFinished);
|
|
EventManager.Instance.Unregister(EventManager.EventName.EFFECT_INST_RETURN, (Action<EffectPlayingData>)_OnEffectInstReturn);
|
|
}
|
|
|
|
private void _OnUnitReady(GameUnit unit)
|
|
{
|
|
_AddUnit(unit);
|
|
}
|
|
|
|
private void _OnUnitRemove(GameUnit unit)
|
|
{
|
|
_RemoveUnit(unit);
|
|
}
|
|
|
|
private void _RemoveUnit(GameUnit unit)
|
|
{
|
|
for (int i = 0; i < _animList.Count; i++)
|
|
{
|
|
var animPauser = _animList[i];
|
|
if (animPauser.isEffect) continue;
|
|
if (animPauser.owner == unit)
|
|
{
|
|
_animList.RemoveAt(i);
|
|
i--;
|
|
}
|
|
}
|
|
// 遍历特效
|
|
for (int i = 0; i < _paticalList.Count; i++)
|
|
{
|
|
var particalPauser = _paticalList[i];
|
|
if (particalPauser.isEffect) continue;
|
|
if (particalPauser.owner == unit)
|
|
{
|
|
_paticalList.RemoveAt(i);
|
|
i--;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void _OnEffectInstFinished(EffectPlayingData effectData)
|
|
{
|
|
if (effectData.pauserData == null)
|
|
{
|
|
effectData.pauserData = new DataEffectPauser(effectData);
|
|
}
|
|
_animList.AddRange(effectData.pauserData.animPausers);
|
|
_paticalList.AddRange(effectData.pauserData.particalPauser);
|
|
}
|
|
|
|
private void _OnEffectInstReturn(EffectPlayingData effectData)
|
|
{
|
|
var animList = effectData?.pauserData?.animPausers;
|
|
if (animList != null)
|
|
{
|
|
for (int i = 0; i < animList.Count; i++)
|
|
{
|
|
var animPauser = animList[i];
|
|
_animList.Remove(animPauser);
|
|
}
|
|
}
|
|
|
|
var particalList = effectData?.pauserData?.particalPauser;
|
|
if (particalList != null)
|
|
{
|
|
for (int i = 0; i < particalList.Count; i++)
|
|
{
|
|
var particalPauser = particalList[i];
|
|
_paticalList.Remove(particalPauser);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void LogicUpdate(float dt)
|
|
{
|
|
if (AreaManager.instance.isInSelecting)
|
|
{
|
|
globalScale = ELevelSpeed.Level_0;
|
|
}
|
|
else if (SkillManager.instance.needSkillPaused)
|
|
{
|
|
globalScale = ELevelSpeed.DEFAULT_SPEED;
|
|
}
|
|
for (var i = 0; i < _animList.Count; ++i)
|
|
{
|
|
var pauser = _animList[i];
|
|
pauser.LogicUpdate(dt);
|
|
}
|
|
for (var i = 0; i < _paticalList.Count; ++i)
|
|
{
|
|
var pauser = _paticalList[i];
|
|
pauser.LogicUpdate(dt);
|
|
}
|
|
|
|
isSpeedChanged = false;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_UnRegistEvents();
|
|
|
|
_animList.Clear();
|
|
_animList = null;
|
|
|
|
_paticalList.Clear();
|
|
_paticalList = null;
|
|
|
|
_instance = null;
|
|
}
|
|
|
|
}
|
|
}
|