NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Fight/Capture/CaptureInst.cs

131 lines
4.1 KiB
C#
Raw Normal View History

2024-05-30 14:11:19 +08:00
using System.Collections.Generic;
using Framework;
2024-05-30 14:11:19 +08:00
using Gameplay.Area;
using Gameplay.Common;
2024-05-30 14:11:19 +08:00
using Gameplay.Unit;
2024-11-25 14:09:50 +08:00
using Gameplay.Utils;
using PhxhSDK;
using UnityEngine;
using Constants = Framework.Constants;
2024-05-30 14:11:19 +08:00
namespace Gameplay.Fight.Capture
2024-05-30 13:07:39 +08:00
{
public class CaptureInst
{
2024-11-25 14:09:50 +08:00
private static readonly int _Prop_Color1 = Shader.PropertyToID("_BaseColor");
2024-05-30 13:07:39 +08:00
public int cellIndex;
2024-05-30 14:11:19 +08:00
public int maxCaptureProgress = 10;
2024-05-30 14:11:19 +08:00
public List<GameUnit> inAreaUnits;
public float captureProgress;
2024-06-26 14:13:13 +08:00
private bool _lastIsInArea;
2024-05-30 14:11:19 +08:00
public bool isCaptured { get; private set; }
private GameObject _displayObj;
2024-11-25 14:09:50 +08:00
private Material _displayMat;
private Color _startColor;
private Color _endColor;
2024-05-30 19:03:47 +08:00
public CaptureInst(int cellIndex, int captureValue)
2024-05-30 14:11:19 +08:00
{
2024-05-30 19:03:47 +08:00
maxCaptureProgress = captureValue;
2024-05-30 14:11:19 +08:00
this.cellIndex = cellIndex;
inAreaUnits = new List<GameUnit>();
captureProgress = 0f;
isCaptured = false;
var sampleObj = AssetManager.Instance.GetPreLoadResult<GameObject>(PathDefine.CapturePoint.CAPTURE_POINT_PATH);
_displayObj = Object.Instantiate(sampleObj);
2024-11-15 14:32:41 +08:00
var worldPos = AreaManager.instance.GetWorldPosByIndex(cellIndex);
_displayObj.transform.position = worldPos;
2024-06-26 14:13:13 +08:00
_lastIsInArea = false;
2024-11-25 14:09:50 +08:00
_startColor = ColorEx.FromStr("#FFFFFF");
_endColor = ColorEx.FromStr("#D4FF7E");
2024-11-25 14:34:18 +08:00
_SearchDisplayMat();
2024-11-25 14:09:50 +08:00
}
private void _SearchDisplayMat()
{
var findTrans = _displayObj.transform.GetChild(0);
var meshRender = findTrans.GetComponent<MeshRenderer>();
_displayMat = meshRender.material;
_displayMat.SetColor(_Prop_Color1, _startColor);
2024-05-30 14:11:19 +08:00
}
public void UpdateInAreaUnits(List<GameUnit> checkUnits)
{
if (isCaptured) return;
2024-05-30 14:11:19 +08:00
inAreaUnits.Clear();
for (int i = 0; i < checkUnits.Count; i++)
{
var unit = checkUnits[i];
var unitCellIndex = unit.transData.cellIndex;
2024-11-25 14:09:50 +08:00
if (unitCellIndex != cellIndex) continue;
inAreaUnits.Add(unit);
2024-05-30 14:11:19 +08:00
}
}
2024-05-30 13:07:39 +08:00
public void LogicUpdate(float dt)
{
if (isCaptured) return;
if (inAreaUnits.Count > 0)
{
captureProgress += dt * inAreaUnits.Count;
if (captureProgress >= maxCaptureProgress)
{
isCaptured = true;
EventManager.Instance.Send(EventManager.EventName.INFIGHT_CAPTURE_FINSIHED, this);
2024-06-12 18:14:56 +08:00
_CreateNoticeOnGround();
}
2024-11-25 14:09:50 +08:00
else
{
var lerpValue = captureProgress / maxCaptureProgress;
var lerpColor = Color.Lerp(_startColor, _endColor, lerpValue);
_displayMat.SetColor(_Prop_Color1, lerpColor);
}
2024-06-26 14:13:13 +08:00
if (!_lastIsInArea)
{
_lastIsInArea = true;
EventManager.Instance.Send(EventManager.EventName.INFIGHT_CAPTURE_START, this);
}
}
else
{
if (_lastIsInArea)
{
2024-06-26 14:13:13 +08:00
_lastIsInArea = false;
EventManager.Instance.Send(EventManager.EventName.INFIGHT_CAPTURE_STOP, this);
}
}
}
2024-06-12 18:14:56 +08:00
private void _CreateNoticeOnGround()
{
var sampleObj = AssetManager.Instance.GetPreLoadResult<GameObject>(PathDefine.CapturePoint.CAPTURE_FINISHED_POINT_PATH);
2024-06-12 18:14:56 +08:00
var displayObj = Object.Instantiate(sampleObj);
var worldPos = _displayObj.transform.position;
displayObj.transform.position = worldPos;
_displayObj.Destroy();
_displayObj = displayObj;
}
public void Dispose()
{
if (_displayObj != null)
{
_displayObj.Destroy();
_displayObj = null;
}
2024-05-30 13:07:39 +08:00
}
}
}