using cfg.LevelCfg; using System.Collections.Generic; using Code.Scripts.Gameplay.Enviorment; using Code.Scripts.Gameplay.Utils; using Cysharp.Threading.Tasks; using Framework; using Gameplay.AI; using Gameplay.Buff; using Gameplay.Character; using Gameplay.Common; using Gameplay.Effect; using Gameplay.Emoji; using Gameplay.Pool; using Gameplay.Skill; using Gameplay.Vehicle; using PhxhSDK; using UnityEngine; using Constants = Framework.Constants; namespace Gameplay.Level { public partial class Level : IUpdatable, ILoadable { private int _id; private LevelData _levelData; private DataLevel _cfg; private Map _map; private CharacterManager _characterManager = new CharacterManager(); private StateMachine _stateMachine = new StateMachine(); public bool isInBattle = false; public int ID => _id; public Map Map => _map; public CharacterManager CharacterManager => _characterManager; public float LevelTime { set; get; } public Level(int leveId) { _id = leveId; Init(); } private Dictionary _configLevelDic = new Dictionary(); public Dictionary ConfigLevelDic => _configLevelDic; private void LoadConfigLevel() { _cfg = TableManager.Instance.Tables.Level.DataMap.GetValueOrDefault(_id); } private async UniTask LoadData() { _levelData = await LevelUtils.LoadLevelData(_id); } private async UniTask LoadMap() { if (_levelData != null) { if (_map != null) { MapUtils.RemoveCameraController(CameraManager.Instance.MainCamera); MapUtils.UnLoadMap(_map); } _map = await MapUtils.LoadMap(_levelData.mapName); MapUtils.BindCameraController(CameraManager.Instance.MainCamera, _map); } } private async UniTask LoadNpc() { if (_levelData != null && _map != null) { for (var i = 0; i < _levelData.npcInfos.Count; i++) { var npcInfo = _levelData.npcInfos[i]; // TODO: LT:临时处理,后续需要根据配置来 var teamId = ETroopsId.Enemy1; var pos = _map.BlockPosition2WorldPosition(npcInfo.position); var yaw = LevelUtils.SetCharaterToward(npcInfo.npcToward); var character = await _characterManager.CreateNPC(npcInfo.id, teamId, npcInfo.level, pos, yaw); character.transData.pos = pos; character.Node.SetPosition(pos); character.runtimeData.characterLevelData = new CharacterLevelData() { AttachAIName = npcInfo.attachAIType, AIName = npcInfo.aIType, BornPosition = npcInfo.position, NpcToward = npcInfo.npcToward, PatrolInfos = npcInfo.patrolInfos }; character.PrepareReady(); } } } public void BindMap(Map map) { if (map != null) { _map = map; } if (_map.MapData.name.Equals(_levelData.mapName)) { DebugUtil.LogError("{0}.{1} Error:map name not match", GetType(), System.Reflection.MethodBase.GetCurrentMethod()?.Name); } } public void Update(float deltaTime) { if (!_isPause) { _stateMachine.Update(deltaTime * _speed); _characterManager.Update(deltaTime * _speed); AIManager.Instance.Update(deltaTime * _speed); SelecterView.Instance.Update(deltaTime * _speed); } } /// /// 这里是 level system的初始化,不是 level 场景的初始化 /// public void Init() { AIManager.Instance.Init(); _characterManager = new CharacterManager(); _characterManager.Init(); VehicleManager.CreateInstance(); GameUnitMapper.CreateInstance(); SkillManager.CreateInstance(); BuffManager.CreateInstance(); EmojiManager.CreateInstance(); } public void Release() { EmojiManager.inst.Dispose(); VehicleManager.Inst.Dispose(); GameUnitMapper.Inst.Dispose(); SkillManager.instance.Dispose(); BuffManager.instance.Dispose(); _stateMachine?.ChangeState(null); _characterManager.Dispose(); _characterManager = null; AIManager.Instance.Release(); EffectManager.instance.ClearPool(); ObjPoolManager.instance.Clear(); } public async UniTask Load() { LoadConfigLevel(); await LoadData(); await LoadMap(); await _InitAfterLoadMap(); await LoadNpc(); await _LoadOthers(); _SetShadows(); RegisterAllEvent(); InitRules(); _stateMachine.ChangeState(new PrepareState(this)); SelecterView.Instance.Init(); } private void _SetShadows() { var setDistance = _cfg.ShadowDistance; ShadowEx.SetShadowDistance(setDistance); } private async UniTask _LoadOthers() { AssetManager.Instance.PostPreload(Constants.TIREMARK_UNIT_PATH); AssetManager.Instance.PostPreload(Constants.TIREMARK_MAT_PATH); AssetManager.Instance.PostPreload(Constants.CHARACTER_SELECT_GROUND_GFX); AssetManager.Instance.PostPreload(Constants.EMOJI_HEAD_MAT); await AssetManager.Instance.WaitAllPreloads(); } private async UniTask _InitAfterLoadMap() { // 加载后处理 await _LoadPostProcess(); // 初始化GameUnitMapper GameUnitMapper.Inst.Init(); // 表情相关 AssetManager.Instance.PostPreload(Constants.EMOJI_PREFAB_PATH); await AssetManager.Instance.WaitAllPreloads(); } private async UniTask _LoadPostProcess() { // 加载shader AssetManager.Instance.PostPreload(Constants.SHADER_OUTLINE_PATH1); AssetManager.Instance.PostPreload(Constants.SHADER_OUTLINE_PATH1_1); AssetManager.Instance.PostPreload(Constants.SHADER_OUTLINE_PATH2); AssetManager.Instance.PostPreload(Constants.SHADER_OUTLINE_PATH3); await AssetManager.Instance.WaitAllPreloads(); var loadObj = await AssetManager.Instance.LoadAssetAsync(Constants.OUTLINE_POSTPROCESS_PATH); if (loadObj != null) { Object.Instantiate(loadObj); } } public void BeginBattle() { _stateMachine.ChangeState(new BattleState(this)); } public bool IsPreparing() { return _stateMachine.GetCurrentState() is PrepareState; } public bool IsInBattle() { return isInBattle; } public bool IsEnd() { return _stateMachine.GetCurrentState() is EndState; } public UniTask Unload() { MapUtils.RemoveCameraController(CameraManager.Instance.MainCamera); MapUtils.UnLoadMap(_map); UnRegisterAllEvent(); ReleaseRules(); SelecterView.Instance.Release(); return UniTask.NextFrame(); } public LevelData GetLevelData() { return _levelData; } private const string DescKey = "LevelDesc{0}"; private const string NameKey = "LevelName{0}"; public string GetLevelName() { return StringManager.Instance.GetLocalizeTextByKey(string.Format(NameKey, _id)); } public string GetLevelDesc(LevelData levelData) { return StringManager.Instance.GetLocalizeTextByKey(string.Format(DescKey, _id)); } } }