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

566 lines
19 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using asap.core;
using GameCore;
using cfg;
using UniRx;
namespace game
{
public class WashingGameRemove : WashingGameBase
{
[Tooltip("弹跳距离。如:清理藤壶游戏中,清理藤壶后,藤壶向上方弹跳的距离,然后消失。安装座椅游戏中,新座椅从上方弹跳距离移动到椅子的位置。")]
public float m_BouncingDistance = 0.2f;
[Tooltip("弹跳时间。")]
public float m_BouncingTime = 0.1f;
[Tooltip("被操作的物品。如:清理藤壶游戏中的藤壶等。")]
public List<LongPressStuffBase> m_OperatedItems = new List<LongPressStuffBase>();
[Tooltip("操作区。如:清理藤壶游戏,铲子铲藤壶时,需要移动到的位置。")]
public List<Transform> m_OperationAreas = new List<Transform>();
#region
private EventWashingPanel panel;
private void InitPanel()
{
panel.SetDataManager(m_DataManager);
if (m_DataManager.WashingData.IsAllGameOver == true)
{
panel.ShowForAllGameOver();
// 设置关闭按钮
panel.SetBtnCloseClickListener(OnBtnCloseClickListener);
return;
}
panel.ShowForRemove();
// 设置预览按钮
panel.SetBtnPreviewClickListener(null);
// 设置关闭按钮
panel.SetBtnCloseClickListener(OnBtnCloseClickListener);
// 关闭笔刷填充进度
panel.ResetBrushChannelCounterFillAndInverse(false, false);
m_IsGuiding = panel.InitGuidance();
}
private void HideElectricityConsumption()
{
if (panel == null) return;
panel.SetElectricityConsumptionVisible(false);
}
private void ShowElectricityConsumption()
{
if (panel == null) return;
panel.SetElectricityConsumptionVisible(true);
}
private void OnBtnCloseClickListener()
{
// 关闭当前Act
GContext.Publish(new UnloadActToNextAct());
}
private void UpdatePanelProgress()
{
if (panel != null)
{
float progress = OperatedItemProgress;
if (progress == 0f)
{
panel.UpdateSingleProgress(progress);
return;
}
if (m_DataManager != null)
{
m_DataManager.WashingData.Percent = progress;
}
panel.UpdateSingleProgress(progress);
}
}
private void SetGestureFollowPosition(Vector2 position)
{
if (panel == null) return;
ShowElectricityConsumption();
//Vector3 worldPosition = Vector3.zero;
RectTransformUtility.ScreenPointToWorldPointInRectangle(
panel.gestureFollow.transform.GetComponent<RectTransform>(),
position,
UIManager.Instance.UICanvas.worldCamera,
out Vector3 worldPosition);
panel.SetGestureFollowPosition(worldPosition);
}
#endregion
#region
public List<LongPressStuffBase> OperatedItems
{
get { return m_OperatedItems; }
}
public List<Transform> OperationAreas
{
get { return m_OperationAreas; }
}
protected virtual void SyncGameData()
{
if (m_DataManager == null) return;
EventWashingData washingData = m_DataManager.WashingData;
bool syncData = false;
int count = m_OperatedItems.Count;
if (washingData.StepItems.Count == 0 && washingData.Percent == 0f && washingData.IsOver == false)
{
for (int i = 0; i < count; i++)
{
washingData.StepItems.Add(0f);
}
syncData = true;
}
else
{
for (int i = 0;i < count; i++)
{
if (washingData.StepItems.Count == i)
{
washingData.StepItems.Add(0f);
m_OperatedItems[i].SetHaveExecutedTime(0f);
m_OperatedItems[i].gameObject.SetActive(true);
syncData = true;
}
else if (washingData.StepItems[i] >= 1f)
{
m_OperatedItems[i].SetHaveExecutedTime(m_OperatedItems[i].ConsumeTime);
//if (!washingData.IsOver)
//{
// m_OperatedItems[i].gameObject.SetActive(false);
//}
m_OperatedItems[i].gameObject.SetActive(false);
}
else
{
m_OperatedItems[i].SetHaveExecutedTime(washingData.StepItems[i] * m_OperatedItems[i].ConsumeTime);
m_OperatedItems[i].gameObject.SetActive(true);
}
}
}
if (syncData) m_DataManager.SyncData();
}
protected override void SetEnergyEnough()
{
base.SetEnergyEnough();
if (panel != null) panel.SetEnergyEnough();
}
protected override void SetEnergyShortage()
{
base.SetEnergyShortage();
if (panel != null) panel.SetEnergyShortage();
}
protected void CheckIsOver()
{
if (OperatedItemProgress >= 0.999f)
{
if (m_DataManager != null)
{
m_DataManager.WashingData.Percent = 1f;
m_DataManager.WashingData.IsOver = true;
m_DataManager.SyncData();
}
FishingYachtAct.Publish<EventWashingConclusionData>(new EventWashingConclusionData());
}
}
#endregion
#region
/// <summary>
/// 工具准备工作结束回调
/// </summary>
/// <param name="operatingToolsBase"></param>
protected void OperatingToolsReadyAction(OperatingToolsBase operatingToolsBase)
{
if (m_Tool == null || m_CurOperatedItem == null) return;
if (operatingToolsBase.Status == EventWashingActionStatus.Preparing)
{
if (!IsGameOver() && (HaveEnergy() || ConsumeTokens(1)))
{
m_Tool.StartExecuting();
m_CurOperatedItem.StartAction(OperatedItemExecuteOverAction);
m_Status = EventWashingActionStatus.Executing;
}
}
}
/// <summary>
/// 杂物操作完成回调
/// </summary>
/// <param name="stuffBase"></param>
protected virtual void OperatedItemExecuteOverAction(LongPressStuffBase stuffBase)
{
// 道具飞回展示区
m_Tool.MoveToExhibition();
m_CurOperatedItem.StopAction();
int count = m_OperatedItems.Count;
for (int i = 0; i < count; i++)
{
if (stuffBase == m_OperatedItems[i])
{
m_DataManager.WashingData.StepItems[i] = 1f;
break;
}
}
m_DataManager.SyncData();
var tmp = m_CurOperatedItem;
m_CurOperatedItem.transform.DOMoveY(m_CurOperatedItem.transform.position.y + m_BouncingDistance, m_BouncingTime).SetEase(Ease.OutQuad).OnComplete(() =>
{
tmp.gameObject.SetActive(false);
tmp = null;
m_Status = EventWashingActionStatus.Ending;
if (OperatedItemProgress >= 0.999f)
{
if (m_DataManager != null)
{
m_DataManager.WashingData.Percent = 1f;
m_DataManager.WashingData.IsOver = true;
m_DataManager.SyncData();
}
FishingYachtAct.Publish<EventWashingConclusionData>(new EventWashingConclusionData());
}
});
}
protected void ChangeState2InvalidFromExecuting()
{
// 停止消耗电量
// 工具停止播放操作动画
// 道具飞回展示区
m_Tool.StopExecuting();
m_Tool.MoveToExhibition();
if (m_CurOperatedItem != null)
m_CurOperatedItem.StopAction();
m_Status = EventWashingActionStatus.Invalid;
}
protected void ChangeState2InvalidFromPreparing()
{
// 工具停止播放准备动画
// 道具飞回展示区
m_Tool.StopExecuting();
m_Tool.MoveToExhibition();
if (m_CurOperatedItem != null)
m_CurOperatedItem.StopAction();
m_Status = EventWashingActionStatus.Invalid;
}
protected void ChangeState2Invalid()
{
if (m_Tool != null && m_Tool.Status != EventWashingActionStatus.Invalid)
{
m_Tool.StopExecuting();
m_Tool.MoveToExhibition();
}
if (m_CurOperatedItem != null)
m_CurOperatedItem.StopAction();
m_Status = EventWashingActionStatus.Invalid;
}
#endregion
#region
protected LongPressStuffBase m_CurOperatedItem = null;
/// <summary>
/// 设置被操作物品已消耗时间
/// </summary>
/// <param name="time"></param>
public void SetOperatedItemHaveExecutedTime(int index, float time)
{
int count = m_OperatedItems.Count;
if (index < 0 || index >= count) return;
for (int i = 0; i < index; i++)
{
m_OperatedItems[i].SetOver();
}
m_OperatedItems[index].SetHaveExecutedTime(time);
for (int i = 0; i < count; i++)
{
m_OperatedItems[i].SetHaveExecutedTime(0f);
}
}
/// <summary>
/// 当前被操作物品进度
/// </summary>
public float OperatedItemProgress
{
get
{
float total = 0f;
float cumulativeTime = 0f;
int count = m_OperatedItems.Count;
EventWashingData washingData = null;
if (m_DataManager != null) washingData = m_DataManager.WashingData;
for (int i = 0; i < count; ++i)
{
var item = m_OperatedItems[i];
total += item.ConsumeTime;
cumulativeTime += item.CumulativeConsumeTime;
if (washingData != null) washingData.StepItems[i] = item.CumulativeConsumeTime / item.ConsumeTime;
}
return cumulativeTime / total;
}
}
protected void StopAllItemAction()
{
int count = m_OperatedItems.Count;
for (int i = 0; i < count; i++)
{
m_OperatedItems[i].StopAction();
}
}
#endregion
#region
private void InitEvent()
{
this.Subscribe<EventWashingConclusionData>(StartConclusion);
this.Subscribe<EventWashingStepOverData>(StepOver);
this.Subscribe<EventWashingAllGameOverData>(AllGameOver);
this.Subscribe<EventWashingTokenUpdateData>(UpdateBatteryInfo);
this.Subscribe<EventWashingToolEnter1stStageData>(ToolEnter1stStage);
this.Subscribe<EventWashingToolExit1stStageData>(ToolExit1stStage);
this.Subscribe<EventWashingToolEnter2ndStageData>(ToolEnter2ndStage);
this.Subscribe<EventWashingToolExit2ndStageData>(ToolExit2ndStage);
GContext.OnEvent<AddIndexEvent>().Subscribe(GuideListener).AddTo(this.disposables);
}
private void GuideListener(AddIndexEvent addIndexEvent)
{
if (addIndexEvent.CompleteGuide == true && panel != null)
{
panel.CompleteGuidance();
m_IsGuiding = false;
}
}
private void ToolEnter1stStage(EventWashingToolEnter1stStageData data)
{
if (m_Tool == null) return;
m_Tool.Enter1stStage();
}
private void ToolExit1stStage(EventWashingToolExit1stStageData data)
{
if (m_Tool == null) return;
m_Tool.Exit1stStage();
}
private void ToolEnter2ndStage(EventWashingToolEnter2ndStageData data)
{
if (m_Tool == null) return;
m_Tool.Enter2ndStage();
}
private void ToolExit2ndStage(EventWashingToolExit2ndStageData data)
{
if (m_Tool == null) return;
m_Tool.Exit2ndStage();
}
private bool m_IsUpdateBatteryInfo = true;
private void UpdateBatteryInfo(EventWashingTokenUpdateData data)
{
if (panel != null && m_IsUpdateBatteryInfo == true)
{
panel.SetBatteryInfo();
}
}
protected virtual void StartConclusion(EventWashingConclusionData data)
{
panel.StartConclusion();
}
private void StepOver(EventWashingStepOverData data)
{
m_IsUpdateBatteryInfo = false;
GameOver();
FishingYachtAct.Publish<EventWashingGameOverData>(new EventWashingGameOverData());
}
private void AllGameOver(EventWashingAllGameOverData data)
{
if (panel != null)
{
panel.ShowForAllGameOver();
}
}
#endregion
#region
public override void InitGame(string name)
{
base.InitGame(name);
int count = m_OperatedItems.Count;
for (int i = 0; i < count; i++)
{
m_OperatedItems[i].InitStuff();
}
m_IsGuiding = true;
}
public async override void EnterGame()
{
base.EnterGame();
m_IsUpdateBatteryInfo = true;
// 打开页面(UI)
GameObject pannelObj = await UIManager.Instance.ShowUI(UITypes.EventWashingPanel);
panel = pannelObj.transform.GetComponent<EventWashingPanel>();
InitPanel();
InitEvent();
AfterEnterGame();
CheckIsOver();
}
protected override void Update()
{
base.Update();
UpdatePanelProgress();
if (m_IsGuiding == true) return;
//bool showPowerConsumption = false;
Vector2 screenPosition = Vector2.zero;
if (m_OperatedItems != null && m_Tool != null)
{
if (Input.GetMouseButtonDown(0) || Input.GetMouseButton(0))
{
screenPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
bool needChange = false;
if (m_CurOperatedItem != null)
{
if (hit.transform != m_CurOperatedItem.transform)
needChange = true;
}
else
{
needChange = true;
}
if (needChange)
{
if (m_Status == EventWashingActionStatus.Executing)
{
ChangeState2InvalidFromExecuting();
}
else if (m_Status == EventWashingActionStatus.Preparing)
{
ChangeState2InvalidFromPreparing();
}
m_CurOperatedItem = null;
int count = m_OperatedItems.Count;
for (int i = 0; i < count; i++)
{
if (hit.transform == m_OperatedItems[i].transform)
{
m_CurOperatedItem = m_OperatedItems[i];
if (!m_CurOperatedItem.IsOver())
{
m_Tool.PrepareAction(m_OperationAreas[i].transform, OperatingToolsReadyAction);
m_Status = EventWashingActionStatus.Preparing;
}
}
}
}
//if (m_CurOperatedItem != null)
//{
// if (hit.transform == m_CurOperatedItem.transform)
// showPowerConsumption = true;
//}
}
}
else
{
if (m_Status == EventWashingActionStatus.Executing)
{
ChangeState2InvalidFromExecuting();
}
else if (m_Status == EventWashingActionStatus.Preparing)
{
ChangeState2InvalidFromPreparing();
}
else
{
ChangeState2Invalid();
}
m_CurOperatedItem = null;
}
}
//if (showPowerConsumption)
//{
// SetGestureFollowPosition(screenPosition);
//}
//else
//{
// // 隐藏电量消耗
// HideElectricityConsumption();
//}
}
protected override void PauseGame()
{
base.PauseGame();
StopAllItemAction();
}
public override void SyncData()
{
SyncGameData();
base.SyncData();
}
#endregion
}
}