geometry-tower-defense/Assets/GameMain/Scripts/Scene/Map/TowerSelectionPresenter.cs

135 lines
4.2 KiB
C#

using System.Collections.Generic;
using GeometryTD.Entity;
using GeometryTD.UI;
using UnityEngine;
namespace GeometryTD.Map
{
public sealed class TowerSelectionPresenter
{
private bool _hasSelectedFoundationCell;
private Vector3Int _selectedFoundationCell;
private int _selectedTowerEntityId;
private int _attackRangeVisibleTowerEntityId;
public void ApplySelectedObject(CombatSelectFormUserData userData)
{
if (userData == null)
{
ClearSelectedObject();
return;
}
switch (userData.ClickObjectType)
{
case CombatSelectClickObjectType.Foundation:
_hasSelectedFoundationCell = true;
_selectedFoundationCell = userData.CellPosition;
_selectedTowerEntityId = 0;
break;
case CombatSelectClickObjectType.Tower:
_hasSelectedFoundationCell = true;
_selectedFoundationCell = userData.CellPosition;
_selectedTowerEntityId = userData.TowerEntityId;
break;
default:
ClearSelectedObject();
return;
}
UpdateTowerAttackRangeDisplay(_selectedTowerEntityId);
}
public void SelectTower(Vector3Int foundationCell, int towerEntityId)
{
_hasSelectedFoundationCell = true;
_selectedFoundationCell = foundationCell;
_selectedTowerEntityId = towerEntityId;
UpdateTowerAttackRangeDisplay(_selectedTowerEntityId);
}
public bool TryGetSelectedFoundationCell(out Vector3Int foundationCell)
{
foundationCell = default;
if (!_hasSelectedFoundationCell)
{
return false;
}
foundationCell = _selectedFoundationCell;
return true;
}
public bool TryGetSelectedTower(IReadOnlyDictionary<int, Vector3Int> foundationCellByTowerEntityId,
out int towerEntityId, out Vector3Int foundationCell)
{
towerEntityId = 0;
foundationCell = default;
if (_selectedTowerEntityId == 0 || foundationCellByTowerEntityId == null)
{
return false;
}
if (!foundationCellByTowerEntityId.TryGetValue(_selectedTowerEntityId, out foundationCell))
{
return false;
}
towerEntityId = _selectedTowerEntityId;
return true;
}
public void ClearSelectedObject()
{
UpdateTowerAttackRangeDisplay(0);
_hasSelectedFoundationCell = false;
_selectedFoundationCell = default;
_selectedTowerEntityId = 0;
}
private void UpdateTowerAttackRangeDisplay(int towerEntityId)
{
if (_attackRangeVisibleTowerEntityId != 0 && _attackRangeVisibleTowerEntityId != towerEntityId)
{
SetTowerAttackRangeVisible(_attackRangeVisibleTowerEntityId, false);
_attackRangeVisibleTowerEntityId = 0;
}
if (towerEntityId == 0)
{
if (_attackRangeVisibleTowerEntityId != 0)
{
SetTowerAttackRangeVisible(_attackRangeVisibleTowerEntityId, false);
_attackRangeVisibleTowerEntityId = 0;
}
return;
}
if (SetTowerAttackRangeVisible(towerEntityId, true))
{
_attackRangeVisibleTowerEntityId = towerEntityId;
}
}
private static bool SetTowerAttackRangeVisible(int towerEntityId, bool visible)
{
if (towerEntityId == 0 || GameEntry.Entity == null)
{
return false;
}
EntityBase gameEntity = GameEntry.Entity.GetGameEntity(towerEntityId);
if (gameEntity is not TowerEntity towerEntity)
{
return false;
}
towerEntity.SetAttackRangeVisible(visible);
return true;
}
}
}