194 lines
6.5 KiB
C#
194 lines
6.5 KiB
C#
using System;
|
||
using Animancer;
|
||
using Logic;
|
||
using Logic.Action;
|
||
using Logic.CrashSight;
|
||
using Logic.Multilingual;
|
||
using RuntimeData;
|
||
using TH1_Core.Events;
|
||
using TH1_Core.Managers;
|
||
using TH1_Logic.Core;
|
||
using TH1_Renderer.Prefab;
|
||
using TH1Renderer;
|
||
using TH1Resource;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using Object = UnityEngine.Object;
|
||
|
||
namespace TH1_Renderer
|
||
{
|
||
public enum BubbleState
|
||
{
|
||
Prepared,
|
||
Playing,
|
||
Finished
|
||
}
|
||
|
||
public class BubbleRenderer
|
||
{
|
||
private uint _id;
|
||
public uint Id => _id;
|
||
private BubbleMono _bubbleMono;
|
||
private BubbleData _bubbleData;
|
||
|
||
public BubbleData GetBubbleData() => _bubbleData;
|
||
|
||
public GameObject ROBubble; // 实例化的 Hint GameObject
|
||
//uint参数是id,结束的时候告诉manager 该id可以重新加回池子
|
||
//注意 close 和 click 是完全不同的两个事情,不能混在一起 .click是玩家触发 close 是被动逻辑触发
|
||
private Action<uint,uint> _onClick; // rendererId, gid
|
||
|
||
|
||
|
||
public BubbleRenderer(uint id,GameObject prefab, Transform father)
|
||
{
|
||
_id = id;
|
||
ROBubble = Object.Instantiate(prefab, father);
|
||
ROBubble.SetActive(false);
|
||
}
|
||
|
||
public bool SetInfo(BubbleData data,Action<uint,uint> onClick)
|
||
{
|
||
//保存data
|
||
_bubbleData = data;
|
||
|
||
//设置click action
|
||
_onClick = onClick;
|
||
|
||
//设置position
|
||
Vector3 tpos = Table.Instance.GridToWorld(data.Grid);
|
||
ROBubble.transform.position = tpos;
|
||
ROBubble.transform.localRotation = Quaternion.identity;
|
||
ROBubble.transform.localScale = Vector3.one;
|
||
|
||
//设置内容(交给mono完成)
|
||
var mono = ROBubble.GetComponent<BubbleMono>();
|
||
Action onClickAction = data.BubbleType switch
|
||
{
|
||
BubbleType.HeroUpgrade => OnClickHeroUpgrade,
|
||
BubbleType.Upgrade => OnClickUpgrade,
|
||
BubbleType.Capture => OnClickCapture,
|
||
BubbleType.Examine => OnClickExamine,
|
||
BubbleType.Gather => OnClickGather,
|
||
BubbleType.HeroSelect => OnClickHeroSelect,
|
||
_ => OnClickCommon
|
||
};
|
||
if (mono != null)
|
||
mono.SetContent(data.BubbleType, onClickAction, data.CustomSprite);
|
||
ROBubble.SetActive(true);
|
||
return true;
|
||
}
|
||
|
||
private void OnClickUpgrade()
|
||
{
|
||
var upgradeAction = ActionLogicFactory.GetActionLogic(new CommonActionId()
|
||
{ ActionType = CommonActionType.UnitAction, UnitActionType = UnitActionType.Upgrade });
|
||
var actionParam = new CommonActionParams(mapData: Main.MapData, unitData: _bubbleData.Unit,
|
||
playerData: Main.MapData.PlayerMap.SelfPlayerData);
|
||
|
||
if(upgradeAction.CheckCan(actionParam))
|
||
upgradeAction.CompleteExecute(actionParam);
|
||
|
||
OnClickCommon();
|
||
}
|
||
|
||
private void OnClickHeroUpgrade()
|
||
{
|
||
var heroUpgradeAction = ActionLogicFactory.GetActionLogic(new CommonActionId()
|
||
{ ActionType = CommonActionType.UnitAction, UnitActionType = UnitActionType.HeroUpgrade });
|
||
|
||
var actionParam = new CommonActionParams(mapData: Main.MapData,
|
||
playerData: Main.MapData.PlayerMap.SelfPlayerData, unitData: _bubbleData.Unit);
|
||
|
||
if(heroUpgradeAction.CheckCan(actionParam))
|
||
heroUpgradeAction.CompleteExecute(actionParam);
|
||
OnClickCommon();
|
||
}
|
||
|
||
private void OnClickCapture()
|
||
{
|
||
//Debug.Log("Capture Once");
|
||
var action = ActionLogicFactory.GetActionLogic(new CommonActionId()
|
||
{ ActionType = CommonActionType.UnitAction, UnitActionType = UnitActionType.Capture });
|
||
|
||
var actionParam = new CommonActionParams(mapData: Main.MapData,
|
||
playerData: Main.MapData.PlayerMap.SelfPlayerData, unitData: _bubbleData.Unit,
|
||
gridData: _bubbleData.Unit.Grid(Main.MapData));
|
||
|
||
if(action.CheckCan(actionParam))
|
||
action.CompleteExecute(actionParam);
|
||
OnClickCommon();
|
||
}
|
||
|
||
|
||
private void OnClickExamine()
|
||
{
|
||
var action = ActionLogicFactory.GetActionLogic(new CommonActionId()
|
||
{ ActionType = CommonActionType.UnitAction, UnitActionType = UnitActionType.Examine });
|
||
|
||
var actionParam = new CommonActionParams(mapData: Main.MapData,
|
||
playerData: Main.MapData.PlayerMap.SelfPlayerData, unitData: _bubbleData.Unit,
|
||
gridData: _bubbleData.Unit.Grid(Main.MapData));
|
||
|
||
if(action.CheckCan(actionParam))
|
||
action.CompleteExecute(actionParam);
|
||
OnClickCommon();
|
||
}
|
||
|
||
|
||
private void OnClickGather()
|
||
{
|
||
var action = ActionLogicFactory.GetActionLogic(new CommonActionId()
|
||
{ ActionType = CommonActionType.UnitAction, UnitActionType = UnitActionType.Gather });
|
||
|
||
var actionParam = new CommonActionParams(mapData: Main.MapData,
|
||
playerData: Main.MapData.PlayerMap.SelfPlayerData, unitData: _bubbleData.Unit,
|
||
gridData: _bubbleData.Unit.Grid(Main.MapData));
|
||
|
||
if(action.CheckCan(actionParam))
|
||
action.CompleteExecute(actionParam);
|
||
OnClickCommon();
|
||
}
|
||
|
||
private void OnClickHeroSelect()
|
||
{
|
||
// 纯UI行为:打开英雄选择界面
|
||
EventManager.Publish(new ShowUIInfoHero());
|
||
OnClickCommon();
|
||
}
|
||
|
||
|
||
private void OnClickCommon()
|
||
{
|
||
_onClick.Invoke(_id, _bubbleData.Grid?.Id ?? 0);
|
||
OnCloseCommon();
|
||
|
||
}
|
||
|
||
private void OnCloseCommon()
|
||
{
|
||
ROBubble.SetActive(false);
|
||
}
|
||
|
||
public void ForceClose()
|
||
{
|
||
OnCloseCommon();
|
||
}
|
||
|
||
public void Destroy()
|
||
{
|
||
if (ROBubble != null)
|
||
{
|
||
// 销毁GameObject,其所有子对象和组件也会被一并销毁。
|
||
Object.Destroy(ROBubble);
|
||
}
|
||
|
||
// 清理所有C#引用,以帮助GC回收并防止内存泄漏。
|
||
ROBubble = null;
|
||
_onClick = null;
|
||
_bubbleMono = null;
|
||
}
|
||
|
||
}
|
||
}
|