NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/LegionBattle/View/FightLineControl2.cs

124 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using TGS;
using UnityEngine;
namespace Gameplay.LegionBattle.View
{
public class FightLineControl2 : MonoBehaviour
{
public Transform m_unitsParent;
public TerrainGridSystem m_tgs;
public GameObject m_samplePoint;
public Camera m_mainCamera;
private List<FightLineStandUnit2> _totalUnits;
private FightLineMap2 _fightLineMap;
public static FightLineControl2 instance;
private void Start()
{
instance = this;
_totalUnits = new List<FightLineStandUnit2>();
for (int i = 0; i < m_unitsParent.childCount; i++)
{
var unit = m_unitsParent.GetChild(i).GetComponent<FightLineStandUnit2>();
if (unit != null)
{
_totalUnits.Add(unit);
}
}
_fightLineMap = new FightLineMap2(m_tgs);
_fightLineMap.CreateView(m_samplePoint);
for (int i = 0; i < _totalUnits.Count; i++)
{
var unit = _totalUnits[i];
unit.Init();
}
_fightLineMap.Init(_totalUnits);
_fightLineMap.UpdateColor();
}
private void OnDestroy()
{
_fightLineMap.DestroyView();
}
private Vector3 _GetClickWorldPoint(Vector2 mousePos)
{
var ray = m_mainCamera.ScreenPointToRay(mousePos);
Physics.Raycast(ray, out RaycastHit hit);
var worldPos = hit.point;
return worldPos;
}
private FightLineStandUnit2 _selectUnit;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
var mousePos = Input.mousePosition;
var worldPos = _GetClickWorldPoint(mousePos);
var getCell = m_tgs.CellGetAtPosition(worldPos, true);
if (getCell != null)
{
var cellIndex = getCell.index;
_HandleClickCellIndex(cellIndex);
}
}
var isMoved = false;
for (int i = 0; i < _totalUnits.Count; i++)
{
var unit = _totalUnits[i];
unit.ViewUpdate(Time.deltaTime);
isMoved = unit.isPosChanged || isMoved;
unit.isPosChanged = false;
}
if (isMoved)
{
_fightLineMap.UpdateStandMap(_totalUnits);
_fightLineMap.UpdateColor();
_fightLineMap.UpdateAreas();
_fightLineMap.ReverseAloneArea();
}
}
private void _HandleClickCellIndex(int cellIndex)
{
if (_selectUnit == null)
{
_selectUnit = _GetStandUnit(cellIndex);
}
else
{
if (cellIndex == _selectUnit.standCellIndex) return;
var worldPos = m_tgs.CellGetPosition(cellIndex);
_selectUnit.MoveTo(worldPos);
_selectUnit = null;
}
}
private FightLineStandUnit2 _GetStandUnit(int cellIndex)
{
for (int i = 0; i < _totalUnits.Count; i++)
{
var unit = _totalUnits[i];
if (unit.isBuild) continue;
if (unit.standCellIndex == cellIndex) return unit;
}
return null;
}
}
}