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

208 lines
6.6 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;
using Gameplay.Effect;
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
{
private static readonly int _Prop_BaseMap = Shader.PropertyToID("_BaseMap");
2024-05-30 13:07:39 +08:00
public int cellIndex;
private Vector3 _worldPos;
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 Material _displayMat;
private GameObject _displayObj;
private GameObject _objGroundPoint;
private GameObject _objIcon;
private bool _isEntered;
2024-11-25 14:09:50 +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;
_isEntered = false;
2024-06-26 14:13:13 +08:00
_lastIsInArea = false;
2024-11-25 14:09:50 +08:00
_CreateObjs();
}
private void _CreateProgressObj(Vector3 worldPos)
{
var sampleObj = AssetManager.Instance.GetPreLoadResult<GameObject>(PathDefine.CapturePoint.CAPTURE_POINT_PROGRESS_PATH);
_displayObj = Object.Instantiate(sampleObj);
_displayObj.transform.position = worldPos;
2024-11-25 14:34:18 +08:00
_SearchDisplayMat();
2024-11-25 14:09:50 +08:00
}
private void _CreateIcon()
{
// var sampleObj = AssetManager.Instance.GetPreLoadResult<GameObject>(PathDefine.CapturePoint.CAPTURE_EFFECT_ICON_PATH);
// _objIcon = Object.Instantiate(sampleObj);
// _objIcon.transform.position = _worldPos;
//UI创建Icon
EventManager.Instance.Send(EventManager.EventName.INFIGHT_UI_ADD_GRID_OBJ,
new UIGridTypeData(cellIndex, UIGridTypeNode.GridType.Capture, this));
}
private void _CreateObjs()
{
_worldPos = AreaManager.instance.GetWorldPosByIndex(cellIndex);
{
var sampleObj = AssetManager.Instance.GetPreLoadResult<GameObject>(PathDefine.CapturePoint.CAPTURE_POINT_GROUND_POINT_PATH);
_objGroundPoint = Object.Instantiate(sampleObj);
_objGroundPoint.transform.position = _worldPos;
}
_CreateIcon();
}
private void _DestroyObjs()
{
if (_objGroundPoint != null)
{
_objGroundPoint.Destroy();
_objGroundPoint = null;
}
_DestroyProgressObj();
_DestroyIcon();
}
private void _DestroyProgressObj()
{
if (_displayObj != null)
{
_displayObj.Destroy();
_displayObj = null;
}
}
private void _DestroyIcon()
{
// if (_objIcon != null)
// {
// _objIcon.Destroy();
// _objIcon = null;
// }
EventManager.Instance.Send(EventManager.EventName.INFIGHT_UI_REMOVE_GRID_OBJ,
new UIGridTypeData(cellIndex, UIGridTypeNode.GridType.Capture, this));
}
2024-11-25 14:09:50 +08:00
private void _SearchDisplayMat()
{
var findTrans = _displayObj.transform.Find("lizi/liubian_dutiao");
var particleSystem = findTrans.GetComponent<ParticleSystemRenderer>();
_displayMat = particleSystem.material;
_displayMat.SetTextureOffset(_Prop_BaseMap, new Vector2(0, 0));
2024-05-30 14:11:19 +08:00
}
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)
{
if (!_isEntered)
{
_isEntered = true;
_DestroyIcon();
_CreateProgressObj(_worldPos);
}
if (!_lastIsInArea)
{
_lastIsInArea = true;
EventManager.Instance.Send(EventManager.EventName.INFIGHT_CAPTURE_START, this);
EffectManager.instance.PlayEffectById(CodeDefine.Fight.CapturePoint.EFFECT_ID_EXIT, _worldPos);
}
captureProgress += dt;
if (captureProgress >= maxCaptureProgress)
{
isCaptured = true;
EventManager.Instance.Send(EventManager.EventName.INFIGHT_CAPTURE_FINSIHED, this);
EffectManager.instance.PlayEffectById(CodeDefine.Fight.CapturePoint.EFFECT_ID_FINISH, _worldPos);
_DestroyObjs();
return;
2024-11-25 14:09:50 +08:00
}
var lerpValue = captureProgress / maxCaptureProgress;
_displayMat.SetTextureOffset(_Prop_BaseMap, new Vector2(lerpValue, 0));
2024-06-26 14:13:13 +08:00
}
else
{
if (_lastIsInArea)
{
2024-06-26 14:13:13 +08:00
_lastIsInArea = false;
EventManager.Instance.Send(EventManager.EventName.INFIGHT_CAPTURE_STOP, this);
EffectManager.instance.PlayEffectById(CodeDefine.Fight.CapturePoint.EFFECT_ID_EXIT, _worldPos);
}
captureProgress -= dt;
if (captureProgress <= 0)
{
captureProgress = 0;
if (_isEntered)
{
_isEntered = false;
_CreateIcon();
_DestroyProgressObj();
}
}
else
{
var lerpValue = captureProgress / maxCaptureProgress;
_displayMat.SetTextureOffset(_Prop_BaseMap, new Vector2(lerpValue, 0));
}
}
2024-06-12 18:14:56 +08:00
}
public void Dispose()
{
_DestroyObjs();
2024-05-30 13:07:39 +08:00
}
}
}