Files
back_cantanBuilding/Assets/Scripts/EventWashing/EventWashingController.cs
2026-05-26 16:15:54 +08:00

304 lines
10 KiB
C#

using asap.core;
using GameCore;
using System.Collections;
using System.Collections.Generic;
using UniRx;
using UnityEngine;
using UnityEngine.AddressableAssets;
using Cinemachine;
using cfg;
namespace game
{
public class EventWashingController : MonoBehaviour
{
[Tooltip("虚拟摄像机")]
[SerializeField] private CinemachineVirtualCamera m_VirtualCamera;
[Tooltip("虚拟摄像机移动管理器")]
public CameraFollowPathMoveController m_VirtualCameraMoveController;
[Tooltip("工具初始位置")]
[SerializeField] private List<Transform> m_ToolsInitTransfomList = new List<Transform>();
private List<OperatingToolsBase> m_Tools;
#region
WashingGameCollection m_GameCollection; // 游戏合集
WashingGameBase m_Game; // 当前游戏
GameObject m_GameNode;
GameObject m_ToolNode;
EventWashingDataManager m_DataManager;
/// <summary>
/// 加载游戏
/// </summary>
/// <param name="gameData"></param>
public async void LoadGame(EventWashingGameData gameData)
{
ILoadResourceService loadResourceService = GContext.container.Resolve<ILoadResourceService>();
await loadResourceService.Load(gameData.resName);
await UIManager.Instance.ShowUI(UITypes.CloudTransitionPanel);
var gameNode = await Addressables.InstantiateAsync(gameData.resName, transform, true).Task;
if (gameNode != null )
{
if (m_GameNode != null)
{
Addressables.ReleaseInstance(m_GameNode);
m_GameNode = null;
}
m_GameNode = gameNode;
m_GameCollection = m_GameNode.GetComponent<WashingGameCollection>();
int count = m_GameCollection.GameList.Count;
for (int i = 0; i < count; i++)
{
var game = m_GameCollection.GameList[i];
if (game != null)
game.InitGame(gameData.resName);
else
continue;
if (game.GameType == gameData.gameType && game.GameID == gameData.gameID)
{
m_Game = game;
}
else
{
game.gameObject.SetActive(false);
}
}
if (m_Game != null)
{
m_Game.ResetAndInitData(m_VirtualCamera, m_VirtualCameraMoveController, GetTool(gameData.toolType, gameData.toolID));
m_Game.Fresh(m_DataManager);
m_Game.EnterGame();
m_Game.gameObject.SetActive(true);
}
}
GContext.Publish(new EndTransition());
}
public OperatingToolsBase GetTool(EventWashingTool toolType, int toolID)
{
OperatingToolsBase toolBase = null;
foreach (var tool in m_Tools)
{
if (tool == null) continue;
if (tool.ToolType == toolType && tool.ToolID == toolID)
{
tool.gameObject.SetActive(true);
toolBase = tool;
}
else
{
tool.gameObject.SetActive(false);
}
}
if (toolBase != null) return toolBase;
return null;
}
public void GameOver(EventWashingGameOverData data)
{
EventWashingStep washingStep = m_DataManager.GetCurrentEventStep();
// 展示奖励界面
List<ItemData> itemDataList = GContext.container.Resolve<PlayerItemData>().AddItemByDrop(washingStep.DropID, true, RewardType.NormalOne);
AddDropStatisticalData(washingStep.DropID, itemDataList);
GContext.Publish(new ShowData());
// 清空当前游戏信息
m_DataManager.WashingData.Percent = 1f;
m_DataManager.WashingData.Energy = 0f;
m_DataManager.WashingData.OmitPercent = 0f;
m_DataManager.WashingData.IsOver = true;
m_DataManager.WashingData.ConsumedToken = 0;
}
private void OnRewardPanelClose(RewardPanelClose rewardPanelClose)
{
EventWashingData washingData = m_DataManager.WashingData;
// 获取最新游戏数据
EventWashingStep washingStep = m_DataManager.GetNextEventStep();
if (washingStep == null)
{
// 活动结束
m_DataManager.WashingData.IsAllGameOver = true;
FishingYachtAct.Publish<EventWashingAllGameOverData>(new EventWashingAllGameOverData());
}
else
{
// 卸载旧游戏
UnloadGame();
// 加载新游戏
SwitchPlayerToGame();
}
}
/// <summary>
/// 卸载游戏
/// </summary>
public void UnloadGame()
{
if (m_Game != null)
{
m_Game.GameOver();
m_Game.gameObject.SetActive(false);
m_Game = null;
}
}
private void InitTools()
{
int count = m_Tools.Count;
for (int i = 0; i < count; i++)
{
var tool = m_Tools[i];
tool.InitTransfom = m_ToolsInitTransfomList[i];
if (tool != null)
{
tool.InitTool();
tool.gameObject.SetActive(false);
}
}
}
private void SwitchPlayerToGame()
{
if (m_DataManager.WashingData == null)
{
EnterFreshGame();
}
else
{
ContinueGame();
}
}
private void EnterFreshGame()
{
EventWashingStep washingStep = m_DataManager.GetFreshEventStep();
if (washingStep == null) return;
EventWashingData washingData = m_DataManager.WashingData;
if (washingData == null) return;
string res = string.Format("GC_{0:D}_{1:D}_{2:D}", washingData.WashingID, washingData.StageID, washingData.StepID);
FishingYachtAct.Publish<EventWashingGameData>(new EventWashingGameData(res, washingStep.GameType, washingStep.GameID, washingStep.ToolType, washingStep.ToolID));
}
private void ContinueGame()
{
EventWashingStep washingStep = m_DataManager.GetCurrentEventStep();
if (washingStep == null)
{
washingStep = m_DataManager.GetFreshEventStep();
if (washingStep == null) return;
}
EventWashingData washingData = m_DataManager.WashingData;
if (washingData == null) return;
string res = string.Format("GC_{0:D}_{1:D}_{2:D}", washingData.WashingID, washingData.StageID, washingData.StepID);
FishingYachtAct.Publish<EventWashingGameData>(new EventWashingGameData(res, washingStep.GameType, washingStep.GameID, washingStep.ToolType, washingStep.ToolID));
}
#endregion
#region
private void ClearGameData()
{
if (m_DataManager == null || m_DataManager.WashingData == null) return;
m_DataManager.WashingData.StepItems.Clear();
m_DataManager.WashingData.Energy = 0f;
m_DataManager.WashingData.Percent = 0f;
m_DataManager.WashingData.OmitPercent = 0f;
m_DataManager.WashingData.IsOver = false;
m_DataManager.WashingData.ConsumedToken = 0;
}
private void AddDropStatisticalData(int dropID, List<ItemData> itemDataList)
{
#if AGG
using (var e = GEvent.GameEvent("event_washing_reward"))
{
e.AddContent("step_id", m_DataManager.WashingData.StepID)
.AddContent("independent_id", m_DataManager.WashingData.ConsumedToken)
.AddContent("reward_id", dropID);
if (itemDataList != null)
{
for (int i = 0; i < itemDataList.Count; i++)
{
if (itemDataList[i].id == 1001)
{
e.AddContent("reward_hook", itemDataList[i].count);
}
//else if (itemDataList[i].id == 1002)
//{
// e.AddContent("reward_cash", itemDataList[i].count);
//}
}
}
}
#endif
}
#endregion
#region
// Start is called before the first frame update
async void Start()
{
m_DataManager = GContext.container.Resolve<EventWashingDataManager>();
// 加载工具
m_ToolNode = await Addressables.InstantiateAsync("EventWashingTools", transform, true).Task;
if (m_ToolNode != null)
{
ToolCollection toolCollection = m_ToolNode.GetComponent<ToolCollection>();
this.m_Tools = toolCollection.Tools;
}
InitTools();
// 加载游戏
FishingYachtAct.Subscribe<EventWashingGameData>(LoadGame);
// 游戏结束
FishingYachtAct.Subscribe<EventWashingGameOverData>(GameOver);
// 监听关闭奖励面板
GContext.OnEvent<RewardPanelClose>().Subscribe(OnRewardPanelClose).AddTo(FishingYachtAct.Disposables);
if (m_VirtualCameraMoveController != null) m_VirtualCameraMoveController.enabled = false;
m_DataManager.FixWashingData();
SwitchPlayerToGame();
//string bgm = m_DataManager.GetBgm();
//if (string.IsNullOrEmpty(bgm) == false) GContext.Publish(new EventBGMSound(bgm));
}
private void OnDisable()
{
if (m_ToolNode != null)
{
Addressables.ReleaseInstance(m_ToolNode);
m_ToolNode = null;
}
if (m_GameNode != null)
{
Addressables.ReleaseInstance(m_GameNode);
m_GameNode = null;
}
}
#endregion
}
}