184 lines
6.3 KiB
C#
184 lines
6.3 KiB
C#
using asap.core;
|
|
using UnityEngine;
|
|
using GameCore;
|
|
using game;
|
|
using cfg;
|
|
using UnityEngine.Assertions;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
using UnityEngine.U2D;
|
|
public class EventPartnerAct : AGameAct
|
|
{
|
|
public static Context Ctx;
|
|
public override async System.Threading.Tasks.Task<bool> StartAsync()
|
|
{
|
|
Ctx = new Context();
|
|
// var panelGO = await UIManager.Instance.ShowUI(UITypes.EventPartnerFishbowlPanel);
|
|
// panelGO.GetComponent<EventPartnerPanel>().Init(_context);
|
|
await LoadSprites();
|
|
await LoadAudioClips();
|
|
await Ctx.ShowMainPanel();
|
|
return await base.StartAsync();
|
|
}
|
|
|
|
public override async System.Threading.Tasks.Task StopAsync()
|
|
{
|
|
Ctx.ReleaseAddressableOperation();
|
|
UIManager.Instance.DestroyUI(UITypes.EventPartnerPanel);
|
|
await base.StopAsync();
|
|
}
|
|
private async System.Threading.Tasks.Task LoadSprites()
|
|
{
|
|
Ctx.SpriteDic = new Dictionary<string, Sprite>();
|
|
var op = Addressables.LoadAssetAsync<SpriteAtlas>(Ctx.Data.AtlasUrl);
|
|
Ctx.AddAddressableOperation(op);
|
|
var atlas = await op.Task;
|
|
var spinwheelBgList = Ctx.Tables.TbEventPartnerMain[Ctx.Data.RedirectId].SpinBg;
|
|
foreach (var key in spinwheelBgList.Where(key => !Ctx.SpriteDic.ContainsKey(key)))
|
|
{
|
|
Ctx.SpriteDic.Add(key, atlas.GetSprite(key));
|
|
}
|
|
var componentIdList = Ctx.Tables.TbEventPartnerMain[Ctx.Data.RedirectId].ComponentList;
|
|
var componentTable = Ctx.Tables.TbEventPartnerComponent;
|
|
foreach (var id in componentIdList)
|
|
{
|
|
var resList = componentTable[id].ResourceList;
|
|
foreach (var resId in resList)
|
|
{
|
|
Ctx.SpriteDic.Add(resId, atlas.GetSprite(resId));
|
|
}
|
|
}
|
|
}
|
|
|
|
private async System.Threading.Tasks.Task LoadAudioClips()
|
|
{
|
|
Ctx.AudioDic = new Dictionary<string, AudioClip>();
|
|
var opHandle = Addressables.LoadAssetAsync<AudioClip>(Ctx.Data.MatchBgmUrl);
|
|
Ctx.AddAddressableOperation(opHandle);
|
|
Ctx.AudioDic.Add(Ctx.Data.MatchBgmUrl, await opHandle.Task);
|
|
var opHandle2 = Addressables.LoadAssetAsync<AudioClip>(Ctx.Data.BgmUrl);
|
|
Ctx.AddAddressableOperation(opHandle2);
|
|
Ctx.AudioDic.Add(Ctx.Data.BgmUrl, await opHandle2.Task);
|
|
}
|
|
|
|
protected override void OnDestroy()
|
|
{
|
|
Debug.Log("EventPartnerAct Exit.");
|
|
}
|
|
|
|
public static EPanelState GetPanelState()
|
|
{
|
|
if (Ctx == null)
|
|
return EPanelState.NotOpen;
|
|
return Ctx.State;
|
|
}
|
|
|
|
public enum EPanelState : int
|
|
{
|
|
NotOpen = -2,
|
|
MainPanel = -1,
|
|
BuildPanel0 = 0,
|
|
BuildPanel1 = 1,
|
|
BuildPanel2 = 2,
|
|
BuildPanel3 = 3
|
|
}
|
|
|
|
public class Context
|
|
{
|
|
private readonly IEventAggregator _eventAggregator = new EventAggregator();
|
|
public IEventAggregator EventAggregator => _eventAggregator;
|
|
public int CurrentComponentIdx;
|
|
public Tables Tables { get; } = GContext.container.Resolve<Tables>();
|
|
|
|
private readonly IUserService _userService = GContext.container.Resolve<IUserService>();
|
|
private readonly EventPartnerData _data = GContext.container.Resolve<EventPartnerData>();
|
|
public EventPartnerData Data => _data;
|
|
private EventPartnerPanel _panel;
|
|
public EventPartnerData.Component Component => _data.Components[CurrentComponentIdx];
|
|
public Dictionary<string, Sprite> SpriteDic;
|
|
public Dictionary<string, AudioClip> AudioDic;
|
|
private readonly List<AsyncOperationHandle> _ops = new List<AsyncOperationHandle>();
|
|
private System.Threading.Tasks.Task _taskZoom;
|
|
public EPanelState State { get; private set; } = EPanelState.NotOpen;
|
|
|
|
public async System.Threading.Tasks.Task ShowMainPanel(bool doShowMatchTip = false, string namePartner = "", string iconPartner = "", string nameComponent = "")
|
|
{
|
|
try
|
|
{
|
|
if (_panel == null)
|
|
{
|
|
var panelGo = await UIManager.Instance.ShowUI(UITypes.EventPartnerPanel);
|
|
_panel = panelGo.GetComponent<EventPartnerPanel>();
|
|
}
|
|
|
|
_panel.Init();
|
|
_panel.ShowMainPanel();
|
|
_panel.ZoomOut();
|
|
_panel.PlayUiAnimation();
|
|
_panel.ShowMatchTip(namePartner, iconPartner, nameComponent, doShowMatchTip);
|
|
State = EPanelState.MainPanel;
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Debug.LogError($"<color=#c191ff>[EventPartner] Error in show main panel.</color>");
|
|
Debug.LogError(e);
|
|
}
|
|
}
|
|
|
|
public void ShowBuildPanel(int componentIdx)
|
|
{
|
|
Assert.IsTrue(_panel != null, "Main Panel not instantiated.");
|
|
CurrentComponentIdx = componentIdx;
|
|
_panel.ShowBuildPanel(this);
|
|
_panel.ZoomIn(componentIdx);
|
|
_panel.PlayUiAnimation();
|
|
State = (EPanelState)componentIdx;
|
|
}
|
|
|
|
public Sprite GetSprite(string key)
|
|
{
|
|
// Assert.IsTrue(_context.SpriteDic.ContainsKey(key), $"key \"{key}\" not found in sprite dictionary");
|
|
if (SpriteDic.TryGetValue(key, out var sprite))
|
|
return sprite;
|
|
Debug.LogError($"key \"{key}\" not found in sprite dictionary");
|
|
return null;
|
|
}
|
|
|
|
public string GetUserAvatarUrl()
|
|
{
|
|
string s = _userService.AvatarUrl;
|
|
return s;
|
|
}
|
|
|
|
public void AddAddressableOperation(AsyncOperationHandle op)
|
|
{
|
|
_ops.Add(op);
|
|
}
|
|
|
|
public void ReleaseAddressableOperation()
|
|
{
|
|
foreach (var op in _ops)
|
|
{
|
|
Addressables.Release(op);
|
|
}
|
|
}
|
|
|
|
public void UpgradeComponent(int componentIdx, int targetGrade)
|
|
{
|
|
_panel.SetComponent(componentIdx, targetGrade, isUpgrade: true);
|
|
}
|
|
|
|
public void PlayAddScoreFx(int idx)
|
|
{
|
|
if (_panel == null)
|
|
{
|
|
Debug.LogWarning($"Event Partner Panel not instantiated.");
|
|
return;
|
|
}
|
|
_panel.PlayAddScoreFx(idx);
|
|
}
|
|
|
|
}
|
|
} |