using System.Collections; using System.Collections.Generic; using MoleMole.Config; using UniRx; using UnityEngine; namespace MoleMole { public class Avatar3dModelContext : BaseWidgetContext { private Dictionary _avatarModelDict; private Dictionary _avatarDefaultYDict; private Coroutine _createAvatarCoroutine; private bool _setAvatarPosFlag = true; public Avatar3dModelContext(GameObject view = null) { config = new ContextPattern { contextName = "Avatar3dModelContext", viewPrefabPath = "UI/Menus/Widget/AvatarContainer", cacheType = ViewCacheType.DontCache, dontDestroyView = true }; _avatarModelDict = new Dictionary(); _avatarDefaultYDict = new Dictionary(); base.view = view; } public Transform GetAvatarById(int avatarID) { return _avatarModelDict[avatarID]; } public bool ContainUIAvatar(int avatarID) { return _avatarModelDict.ContainsKey(avatarID); } public List GetAllAvatars() { List list = new List(); foreach (Transform value in _avatarModelDict.Values) { list.Add(value); } return list; } public void SetStandOnSpaceship(int avatarID) { Transform transform = _avatarModelDict[avatarID]; transform.GetComponent().standOnSpaceshipInGameEntry = true; } public void TriggerAvatarTurnAround(int avatarID) { Transform transform = _avatarModelDict[avatarID]; transform.GetComponent().SetTrigger("TriggerTurnAround"); Singleton.Instance.playerData.uiTempSaveData.hasShowAvatarTurnAroundAnim = true; } public override bool OnNotify(Notify ntf) { if (ntf.type == NotifyTypes.CreateAvatarUIModels) { return OnRecvCreateAvatarNotify((List)ntf.body); } if (ntf.type == NotifyTypes.SetSpaceShipActive) { return OnSetSpaceShipActive(((Tuple)ntf.body).Item1, ((Tuple)ntf.body).Item2); } if (ntf.type == NotifyTypes.PlayAvtarChangeEffect) { return PlayAvtarChangeEffect(); } return false; } protected override bool SetupView() { base.view.name = "AvatarContainer"; SetLockViewActive(false); base.view.transform.Find("AvatarChangeEffect").gameObject.SetActive(false); return false; } public void TriggerStartGalTouch(int avatarID) { Transform transform = _avatarModelDict[avatarID]; BaseMonoUIAvatar component = transform.GetComponent(); if (component != null) { component.EnterGalTouch(); } } public void TriggerStartGalTouch() { List allAvatars = GetAllAvatars(); int i = 0; for (int count = allAvatars.Count; i < count; i++) { BaseMonoUIAvatar component = allAvatars[i].GetComponent(); if (component != null) { component.EnterGalTouch(); } } } public void TriggerStopGalTouch() { foreach (Transform value in _avatarModelDict.Values) { BaseMonoUIAvatar component = value.GetComponent(); if (component != null) { component.ExitGalTouch(); } } } private bool OnSetSpaceShipActive(bool active, bool setCameraComponentOnly = false) { base.view.SetActive(active); return false; } private bool OnRecvCreateAvatarNotify(List avatarDataList) { if (_createAvatarCoroutine != null) { Singleton.Instance.StopCoroutine(_createAvatarCoroutine); } _createAvatarCoroutine = Singleton.Instance.StartCoroutine(DoCreateAvatarUIModels(avatarDataList)); return false; } private bool PlayAvtarChangeEffect() { base.view.transform.Find("AvatarChangeEffect").gameObject.SetActive(true); ParticleSystem component = base.view.transform.Find("AvatarChangeEffect").GetComponent(); component.Play(); return false; } private string GetAvatarPrefaPath(string avatarRegistryKey) { return string.Format("Entities/Avatar/{0}/Avatar_{0}_UI", avatarRegistryKey); } private IEnumerator DoCreateAvatarUIModels(List avatarDataList) { List removeKeys = new List(); foreach (int avatarId in _avatarModelDict.Keys) { if (avatarDataList.Find((Avatar3dModelDataItem x) => x.avatar.avatarID == avatarId) == null) { removeKeys.Add(avatarId); } } foreach (int key in removeKeys) { if (_avatarModelDict[key] != null) { Object.Destroy(_avatarModelDict[key].gameObject); } _avatarModelDict.Remove(key); _avatarDefaultYDict.Remove(key); } foreach (Avatar3dModelDataItem data in avatarDataList) { Transform modelTrans = null; bool needSetTriggerFlag = true; MonoGameEntry monoGameEntry = Singleton.Instance.SceneCanvas as MonoGameEntry; if (_avatarModelDict.ContainsKey(data.avatar.avatarID)) { modelTrans = _avatarModelDict[data.avatar.avatarID]; needSetTriggerFlag = false; } else { BaseMonoUIAvatar monoAvatar = base.view.GetComponentInChildren(); bool loadFlag = true; if (monoAvatar != null) { if (monoAvatar.avatarID == data.avatar.avatarID) { modelTrans = monoAvatar.gameObject.transform; _setAvatarPosFlag = false; loadFlag = false; } else { Object.DestroyImmediate(monoAvatar.gameObject); _setAvatarPosFlag = true; } } if (loadFlag) { modelTrans = Object.Instantiate(Miscs.LoadResource(GetAvatarPrefaPath(data.avatar.AvatarRegistryKey))).transform; modelTrans.SetParent(base.view.transform); monoAvatar = modelTrans.GetComponent(); if (monoGameEntry != null) { Object.DontDestroyOnLoad(base.view); } } monoAvatar.avatarData = data.avatar; monoAvatar.tattooVisible = false; monoAvatar.SetTattooVisible(0); monoAvatar.Init(data.avatar.avatarID); _avatarModelDict.Add(data.avatar.avatarID, modelTrans); _avatarDefaultYDict.Add(data.avatar.avatarID, modelTrans.localPosition.y); if (monoGameEntry == null) { modelTrans.GetComponent().standOnSpaceshipInGameEntry = false; } } SetAvatarModelView(modelTrans, data.avatar, data.pos, data.eulerAngles, data.showLockViewIfLock); if (needSetTriggerFlag) { if (monoGameEntry != null) { modelTrans.GetComponent().SetTrigger("TriggerStandByBack"); } else { modelTrans.GetComponent().SetTrigger("TriggerStandBy"); } if (monoGameEntry == null && !Singleton.Instance.playerData.uiTempSaveData.hasShowAvatarTurnAroundAnim) { modelTrans.GetComponent().SetTrigger("TriggerTurnAround"); Singleton.Instance.playerData.uiTempSaveData.hasShowAvatarTurnAroundAnim = true; } } } Singleton.Instance.FireNotify(new Notify(NotifyTypes.FinishCreateAvatarUIModels)); yield return null; } private void SetAvatarModelView(Transform modelTrans, AvatarDataItem avatarData, Vector3 pos, Vector3 eulerAngles, bool showLockViewIfLock) { if (_setAvatarPosFlag) { modelTrans.SetLocalPositionX(pos.x); modelTrans.SetLocalPositionZ(pos.z); modelTrans.SetLocalPositionY(_avatarDefaultYDict[avatarData.avatarID] + pos.y); modelTrans.GetComponent().SetOriginPos(modelTrans.position); MonoGameEntry monoGameEntry = Singleton.Instance.SceneCanvas as MonoGameEntry; if (monoGameEntry != null) { modelTrans.Translate(0f, 0f, 20f); } } modelTrans.eulerAngles = eulerAngles; bool lockViewActive = showLockViewIfLock && !avatarData.UnLocked; SetLockViewActive(lockViewActive); Transform transform = base.view.transform.Find("Lock"); transform.position = new Vector3(modelTrans.position.x, transform.position.y, modelTrans.position.z); SetAvatarAttachWeaponView(modelTrans, avatarData); } private void SetLockViewActive(bool active) { base.view.transform.Find("Lock").gameObject.SetActive(active); } private void SetAvatarAttachWeaponView(Transform modelTrans, AvatarDataItem avatarData) { WeaponDataItem weapon = avatarData.GetWeapon(); if (weapon != null) { BaseMonoUIAvatar component = modelTrans.GetComponent(); if (component.WeaponMetaID != weapon.ID) { component.AttachWeapon(weapon.ID, avatarData.AvatarRegistryKey); } } } } }