using System; using System.Linq; using UnityEngine; using game; using asap.core; using UnityEngine.UI; using cfg; using DG.Tweening; using UniRx; using System.Collections.Generic; using Castle.Core.Internal; using GameCore; public class EventPartnerPanel : MonoBehaviour { [SerializeField] private Button btnClose, btnConfirm; [SerializeField] private EventPartnerMainPanel mainPanel; [SerializeField] private EventPartnerBuildPanel buildPanel; [SerializeField] private EPanel curPanel; [SerializeField] private GameObject background; [SerializeField] private float zoomTime; [SerializeField] private float zoomScale; [SerializeField] private EventPartnerComponent[] components; [SerializeField] private CanvasGroup mask; [SerializeField] private Animation uiAnimation; [SerializeField] private EventPartnerTips tips; private IEventAggregator _eventAggregator; private EventPartnerData _data; private Tables _tables; private EventPartnerAct.Context _context; private IDisposable _robotAddScoreSubscription; private List _tweenList; private List _posList; private void Awake() { _posList = new List(); foreach (var component in components) { _posList.Add(component.transform.localPosition); // Debug.Log($"init pos: {_posList[^1]}"); } btnClose.onClick.AddListener(OnClickClose); btnConfirm.onClick.AddListener(OnClickClose); } private void Start() { GContext.container.Resolve().InspectTriggerGuide("EventPartnerJurassicbuildingPanel", curPanelName: gameObject.name); } private void OnDestroy() { _robotAddScoreSubscription?.Dispose(); } public void Init() { _context = EventPartnerAct.Ctx; _eventAggregator = _context.EventAggregator; _tables = _context.Tables; _data = _context.Data; // Debug.Log($"[EventPartner] Data in panel: {_data.GetHashCode()}"); for (int i = 0; i < _data.Components.Count; i++) { var c = _data.Components[i]; SetComponent(i, c.GetProgressIdx(c.ScoreDisplay), isInit: true); var now = ZZTimeHelper.UtcNow(); if (!c.IsMatched || !c.IsPartnerRobot || c.BotActionQueue.IsNullOrEmpty() || now < c.BotActionQueue.Peek().Time) continue; if (c.Score >= c.MaxScore) { c.BotActionQueue.Clear(); continue; } while (!c.BotActionQueue.IsNullOrEmpty() && now >= c.BotActionQueue.Peek().Time) { c.AddRobotScore(); } c.SyncRobotScore(); } mask.alpha = 0; // TODO: Optimize to timed task instead of constant looking up. _robotAddScoreSubscription ??= Observable.Interval(System.TimeSpan.FromSeconds(2.0f)) .Subscribe(x => { // Debug.Log($"{x} minute:"); foreach (var c in _data.Components.Where(c => c.IsMatched && c.IsPartnerRobot)) { if (!c.BotActionQueue.TryPeek(out var action) || ZZTimeHelper.UtcNow() < action.Time) continue; if (c.Score >= c.MaxScore) { c.BotActionQueue.Clear(); continue; } c.AddRobotScore(); c.SyncRobotScore(); } }); } public void ShowMainPanel() { curPanel = EPanel.Main; mainPanel.gameObject.SetActive(true); buildPanel.gameObject.SetActive(false); mainPanel.Init(); } public void ShowBuildPanel(EventPartnerAct.Context ctx) { curPanel = EPanel.Build; mainPanel.gameObject.SetActive(false); buildPanel.gameObject.SetActive(true); buildPanel.Init(); } private void OnClickClose() { // Debug.Log("Click click click!"); if (curPanel == EPanel.Main) GContext.Publish(new UnloadActToNextAct()); else { buildPanel.SetStateInactive(); _ = _context.ShowMainPanel(); } } public void SetComponent(int componentIdx, int targetGrade, bool isUpgrade = false, bool isInit = false) { var component = components[componentIdx]; var componentId = _data.Components[componentIdx].ComponentId; var resourceList = _tables.TbEventPartnerComponent[componentId].ResourceList; var resource = resourceList[targetGrade]; // Debug.Log($"Id: {componentId}, Index: {componentIdx}, resource: {resource}."); if (isInit) component.Init(_context, targetGrade, resource); else if (isUpgrade) component.UpgradeComponent(targetGrade, resource); else component.SetComponent(targetGrade, resource); } public void PlayAddScoreFx(int componentIdx) { components[componentIdx].PlayAddScoreFx(); } public void ZoomIn(int componentIdx) { for (int i = 0; i < _data.Components.Count; i++) { components[i].gameObject.SetActive(i == componentIdx); } var pos = _posList[componentIdx]; // Debug.Log($"before pos: {pos}"); // Debug.Log($"before l : {_posList[componentIdx]}"); pos *= zoomScale; // Debug.Log($"after pos: {pos}"); // Debug.Log($"after l : {_posList[componentIdx]}"); if (_tweenList is { Count: > 0 }) foreach (var tween in _tweenList) tween.Kill(); _tweenList = new List { background.transform.DOLocalMove(-pos, zoomTime), background.transform.DOScale(zoomScale, zoomTime), mask.DOFadeAlpha(1.0f, zoomTime) }; } public void ZoomOut() { for (int i = 0; i < _data.Components.Count; i++) { components[i].gameObject.SetActive(true); } // Debug.Log($"Before ZoomOut{transform.position}"); if (_tweenList is { Count: > 0 }) foreach (var tween in _tweenList) tween.Kill(); _tweenList = new List { background.transform.DOLocalMove(Vector3.zero, zoomTime), background.transform.DOScale(1, zoomTime), mask.DOFadeAlpha(0.0f, zoomTime) }; } public void PlayUiAnimation() { uiAnimation.Play("fishbowl_show"); } public void ShowMatchTip(string namePartner, string iconPartner, string nameComponent, bool doShowPanelTips) { mainPanel.SetMatchTip(namePartner, iconPartner, nameComponent, doShowPanelTips); } } public enum EPanel { Main, Build, Info, Match, MatchPopup } /*public class EventPartnerSwitchPanel { public EPanel panelType; public int ComponentIndex; public EventPartnerSwitchPanel (EPanel e, int c = 0) { panelType = e; ComponentIndex = c; } }*/ public class EventPartnerComponentUpgrade { public int ComponentIndex, Grade; public EventPartnerComponentUpgrade(int componentIndex, int grade) { ComponentIndex = componentIndex; Grade = grade; } }