508 lines
18 KiB
C#
508 lines
18 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using GeometryTD.CustomComponent;
|
|
using GeometryTD.CustomUtility;
|
|
using GeometryTD.Map;
|
|
using GeometryTD.Definition;
|
|
using GeometryTD.Entity.EntityData;
|
|
using GeometryTD.UI;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.Tilemaps;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace GeometryTD.Entity
|
|
{
|
|
public class MapEntity : EntityBase
|
|
{
|
|
private const int DefaultTowerTypeId = 401;
|
|
|
|
private static readonly Spawner[] EmptySpawners = Array.Empty<Spawner>();
|
|
|
|
[SerializeField] private bool _enableCombatSelectInput = true;
|
|
|
|
[SerializeField] private int _towerTypeId = DefaultTowerTypeId;
|
|
|
|
[SerializeField] private int[] _buildTowerCosts = { 40, 60, 60, 80 };
|
|
|
|
[SerializeField] private int _upgradeCost = 50;
|
|
|
|
[SerializeField] private int _destroyGain = 40;
|
|
|
|
private MapDataRefs _mapDataRefs;
|
|
private MapData _mapData;
|
|
private MapTopologyService _mapTopologyService;
|
|
private CombatSelectFormUseCase _combatSelectFormUseCase;
|
|
private CombatSelectInputService _combatSelectInputService;
|
|
private TowerPlacementService _towerPlacementService;
|
|
private TowerSelectionPresenter _towerSelectionPresenter;
|
|
|
|
private readonly BuildTowerVisualInfo[] _buildTowerVisualInfos = new BuildTowerVisualInfo[4];
|
|
|
|
public IReadOnlyList<Vector3Int> PathCells => _mapTopologyService != null
|
|
? _mapTopologyService.PathCells
|
|
: Array.Empty<Vector3Int>();
|
|
public IReadOnlyList<Vector3Int> FoundationCells => _mapTopologyService != null
|
|
? _mapTopologyService.FoundationCells
|
|
: Array.Empty<Vector3Int>();
|
|
public Tilemap Tilemap => _mapDataRefs != null ? _mapDataRefs.Tilemap : null;
|
|
public Spawner[] Spawners => _mapDataRefs?.Spawners ?? EmptySpawners;
|
|
public House House => _mapDataRefs?.House;
|
|
|
|
public bool IsPathCell(Vector3Int cellPosition)
|
|
{
|
|
return _mapTopologyService != null && _mapTopologyService.IsPathCell(cellPosition);
|
|
}
|
|
|
|
public bool IsFoundationCell(Vector3Int cellPosition)
|
|
{
|
|
return _mapTopologyService != null && _mapTopologyService.IsFoundationCell(cellPosition);
|
|
}
|
|
|
|
public bool TryGetNearestPathCell(Vector3 worldPosition, out Vector3Int pathCell)
|
|
{
|
|
pathCell = default;
|
|
return _mapTopologyService != null && _mapTopologyService.TryGetNearestPathCell(Tilemap, worldPosition, out pathCell);
|
|
}
|
|
|
|
public Vector3 GetPathCellCenterWorld(Vector3Int pathCell)
|
|
{
|
|
return _mapTopologyService != null
|
|
? _mapTopologyService.GetPathCellCenterWorld(Tilemap, pathCell)
|
|
: Vector3.zero;
|
|
}
|
|
|
|
public bool TryGetDefaultPathCells(Spawner spawner, out IReadOnlyList<Vector3Int> pathCells)
|
|
{
|
|
pathCells = null;
|
|
return _mapTopologyService != null && _mapTopologyService.TryGetDefaultPathCells(spawner, out pathCells);
|
|
}
|
|
|
|
public bool TryFindPathCells(Spawner spawner, IReadOnlyCollection<Vector3Int> blockedCells,
|
|
List<Vector3Int> pathResult)
|
|
{
|
|
return _mapTopologyService != null && _mapTopologyService.TryFindPathCells(spawner, blockedCells, pathResult);
|
|
}
|
|
|
|
public bool TryFindPathWorldPoints(Spawner spawner, IReadOnlyCollection<Vector3Int> blockedCells,
|
|
List<Vector3> worldPathResult)
|
|
{
|
|
return _mapTopologyService != null &&
|
|
_mapTopologyService.TryFindPathWorldPoints(Tilemap, spawner, blockedCells, worldPathResult);
|
|
}
|
|
|
|
protected override void OnInit(object userData)
|
|
{
|
|
base.OnInit(userData);
|
|
|
|
_mapDataRefs = GetComponent<MapDataRefs>();
|
|
if (_mapDataRefs == null)
|
|
{
|
|
Log.Error("MapDataRefs is missing on map entity '{0}'.", name);
|
|
}
|
|
|
|
InitializeCombatSelectUseCase();
|
|
InitializeCombatSelectInputService();
|
|
InitializeMapTopologyService();
|
|
InitializeTowerPlacementService();
|
|
InitializeTowerSelectionPresenter();
|
|
}
|
|
|
|
protected override void OnShow(object userData)
|
|
{
|
|
base.OnShow(userData);
|
|
|
|
_mapData = userData as MapData;
|
|
if (_mapData == null)
|
|
{
|
|
Log.Warning("MapData is invalid for map entity '{0}'.", Id);
|
|
}
|
|
|
|
RefreshTiles();
|
|
ConfigureCombatSelectUseCase();
|
|
HideCombatSelectForm();
|
|
}
|
|
|
|
protected override void OnHide(bool isShutdown, object userData)
|
|
{
|
|
HideCombatSelectForm();
|
|
_towerPlacementService?.HideAndClearAllPlacedTowers();
|
|
ClearRuntimeData();
|
|
base.OnHide(isShutdown, userData);
|
|
}
|
|
|
|
protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
|
|
{
|
|
base.OnUpdate(elapseSeconds, realElapseSeconds);
|
|
HandleCombatSelectInput();
|
|
}
|
|
|
|
private void RefreshTiles()
|
|
{
|
|
ClearRuntimeData();
|
|
|
|
if (_mapDataRefs == null)
|
|
{
|
|
_mapDataRefs = GetComponent<MapDataRefs>();
|
|
if (_mapDataRefs == null)
|
|
{
|
|
Log.Error("MapDataRefs is missing on map entity '{0}'.", name);
|
|
return;
|
|
}
|
|
}
|
|
|
|
Tilemap tilemap = _mapDataRefs.Tilemap;
|
|
if (tilemap == null)
|
|
{
|
|
Log.Error("Tilemap reference is missing in MapDataRefs on '{0}'.", name);
|
|
return;
|
|
}
|
|
|
|
_mapTopologyService?.Refresh(tilemap, Spawners, House, name, _mapData != null ? _mapData.LevelId : 0);
|
|
}
|
|
|
|
private void ClearRuntimeData()
|
|
{
|
|
_mapTopologyService?.Clear();
|
|
_towerPlacementService?.ClearTracking();
|
|
_towerSelectionPresenter?.ClearSelectedObject();
|
|
}
|
|
|
|
private void InitializeCombatSelectUseCase()
|
|
{
|
|
if (_combatSelectFormUseCase == null)
|
|
{
|
|
_combatSelectFormUseCase = new CombatSelectFormUseCase();
|
|
}
|
|
|
|
GameEntry.UIRouter.BindUIUseCase(UIFormType.CombatSelectForm, _combatSelectFormUseCase);
|
|
}
|
|
|
|
private void InitializeTowerSelectionPresenter()
|
|
{
|
|
if (_towerSelectionPresenter == null)
|
|
{
|
|
_towerSelectionPresenter = new TowerSelectionPresenter();
|
|
}
|
|
}
|
|
|
|
private void InitializeTowerPlacementService()
|
|
{
|
|
if (_towerPlacementService == null)
|
|
{
|
|
_towerPlacementService = new TowerPlacementService();
|
|
}
|
|
}
|
|
|
|
private void InitializeCombatSelectInputService()
|
|
{
|
|
if (_combatSelectInputService == null)
|
|
{
|
|
_combatSelectInputService = new CombatSelectInputService();
|
|
}
|
|
}
|
|
|
|
private void InitializeMapTopologyService()
|
|
{
|
|
if (_mapTopologyService == null)
|
|
{
|
|
_mapTopologyService = new MapTopologyService();
|
|
}
|
|
}
|
|
|
|
private void ConfigureCombatSelectUseCase()
|
|
{
|
|
if (_combatSelectFormUseCase == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_combatSelectFormUseCase.SetCoinProvider(GetCurrentCoin);
|
|
BackpackInventoryData inventorySnapshot = GameEntry.PlayerInventory != null
|
|
? GameEntry.PlayerInventory.GetInventorySnapshot()
|
|
: null;
|
|
IReadOnlyList<TowerItemData> participantTowers = GameEntry.PlayerInventory != null
|
|
? GameEntry.PlayerInventory.GetParticipantTowerSnapshot()
|
|
: null;
|
|
Dictionary<long, MuzzleCompItemData> muzzleMap = BuildComponentMap(inventorySnapshot?.MuzzleComponents);
|
|
Dictionary<long, BearingCompItemData> bearingMap = BuildComponentMap(inventorySnapshot?.BearingComponents);
|
|
Dictionary<long, BaseCompItemData> baseMap = BuildComponentMap(inventorySnapshot?.BaseComponents);
|
|
|
|
int currentBuildTowerCount = GetCurrentBuildTowerCount();
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
int buildIndex = i;
|
|
int buildCost = GetBuildTowerCost(buildIndex);
|
|
bool isBuildAvailable = buildIndex < currentBuildTowerCount;
|
|
BuildTowerVisualInfo buildVisual = ResolveBuildTowerVisual(participantTowers, buildIndex, muzzleMap, bearingMap, baseMap);
|
|
_buildTowerVisualInfos[buildIndex] = buildVisual;
|
|
_combatSelectFormUseCase.SetBuildAction(
|
|
buildIndex,
|
|
isBuildAvailable ? () => TryBuildTower(buildIndex) : (Func<bool>)null,
|
|
buildCost,
|
|
null,
|
|
isBuildAvailable,
|
|
buildVisual.BaseColor,
|
|
buildVisual.BearingColor,
|
|
buildVisual.MuzzleColor);
|
|
_combatSelectFormUseCase.SetBuildVisible(buildIndex, isBuildAvailable);
|
|
}
|
|
|
|
_combatSelectFormUseCase.SetUpgradeAction(TryUpgradeTower, Mathf.Max(0, _upgradeCost));
|
|
_combatSelectFormUseCase.SetDestroyAction(TryDestroyTower, Mathf.Max(0, _destroyGain));
|
|
}
|
|
|
|
private void HandleCombatSelectInput()
|
|
{
|
|
if (!_enableCombatSelectInput || !Input.GetMouseButtonDown(0))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject())
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (_combatSelectInputService == null ||
|
|
!_combatSelectInputService.TryBuildUserData(Tilemap, CachedTransform,
|
|
_towerPlacementService != null ? _towerPlacementService.TowerEntityIdByFoundationCell : null,
|
|
IsFoundationCell, IsTowerAtMaxLevel, _upgradeCost, _destroyGain,
|
|
out CombatSelectFormUserData userData))
|
|
{
|
|
userData = new CombatSelectFormUserData
|
|
{
|
|
ClickObjectType = CombatSelectClickObjectType.None
|
|
};
|
|
}
|
|
|
|
ApplySelectedObject(userData);
|
|
GameEntry.UIRouter.OpenUI(UIFormType.CombatSelectForm, userData);
|
|
}
|
|
|
|
private bool IsTowerAtMaxLevel(int towerEntityId)
|
|
{
|
|
return _towerPlacementService != null && _towerPlacementService.IsTowerAtMaxLevel(towerEntityId);
|
|
}
|
|
|
|
private void ApplySelectedObject(CombatSelectFormUserData userData)
|
|
{
|
|
_towerSelectionPresenter?.ApplySelectedObject(userData);
|
|
}
|
|
|
|
private bool TryBuildTower(int buildIndex)
|
|
{
|
|
if (_towerSelectionPresenter == null ||
|
|
_towerPlacementService == null ||
|
|
!_towerSelectionPresenter.TryGetSelectedFoundationCell(out Vector3Int selectedFoundationCell) ||
|
|
!IsFoundationCell(selectedFoundationCell))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
CombatNodeComponent combatNode = GameEntry.CombatNode;
|
|
if (combatNode == null || !combatNode.TryGetBuildTowerStats(buildIndex, out TowerStatsData buildTowerStats))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
BuildTowerVisualInfo buildVisual = buildIndex >= 0 && buildIndex < _buildTowerVisualInfos.Length
|
|
? _buildTowerVisualInfos[buildIndex]
|
|
: BuildTowerVisualInfo.Default;
|
|
|
|
if (!_towerPlacementService.TryBuildTower(selectedFoundationCell, IsFoundationCell, buildIndex, _buildTowerCosts,
|
|
buildTowerStats, buildVisual.MuzzleColor, buildVisual.BearingColor, buildVisual.BaseColor, _towerTypeId, Tilemap, TryConsumeCoin, AddCoin, out int towerEntityId))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
_towerSelectionPresenter.SelectTower(selectedFoundationCell, towerEntityId);
|
|
return true;
|
|
}
|
|
|
|
private bool TryUpgradeTower()
|
|
{
|
|
if (_towerSelectionPresenter == null ||
|
|
_towerPlacementService == null ||
|
|
!_towerSelectionPresenter.TryGetSelectedTower(_towerPlacementService.FoundationCellByTowerEntityId,
|
|
out int towerEntityId,
|
|
out Vector3Int foundationCell))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!_towerPlacementService.TryUpgradeTower(towerEntityId, _upgradeCost, _towerTypeId, Tilemap,
|
|
TryConsumeCoin, AddCoin, out int resultTowerEntityId, out foundationCell))
|
|
{
|
|
if (resultTowerEntityId != 0)
|
|
{
|
|
_towerSelectionPresenter.SelectTower(foundationCell, resultTowerEntityId);
|
|
}
|
|
else
|
|
{
|
|
_towerSelectionPresenter.ClearSelectedObject();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
_towerSelectionPresenter.SelectTower(foundationCell, resultTowerEntityId);
|
|
return true;
|
|
}
|
|
|
|
private bool TryDestroyTower()
|
|
{
|
|
if (_towerSelectionPresenter == null ||
|
|
_towerPlacementService == null ||
|
|
!_towerSelectionPresenter.TryGetSelectedTower(_towerPlacementService.FoundationCellByTowerEntityId,
|
|
out int towerEntityId,
|
|
out Vector3Int foundationCell))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!_towerPlacementService.TryDestroyTower(towerEntityId, _destroyGain, AddCoin, out foundationCell))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
_towerSelectionPresenter.ClearSelectedObject();
|
|
return true;
|
|
}
|
|
|
|
private void HideCombatSelectForm()
|
|
{
|
|
_combatSelectFormUseCase?.Hide();
|
|
GameEntry.UIRouter.CloseUI(UIFormType.CombatSelectForm);
|
|
}
|
|
|
|
private int GetBuildTowerCost(int buildIndex)
|
|
{
|
|
if (_buildTowerCosts == null || buildIndex < 0 || buildIndex >= _buildTowerCosts.Length)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return Mathf.Max(0, _buildTowerCosts[buildIndex]);
|
|
}
|
|
|
|
private static int GetCurrentBuildTowerCount()
|
|
{
|
|
if (GameEntry.CombatNode == null)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return Mathf.Clamp(GameEntry.CombatNode.CurrentBuildTowerCount, 0, 4);
|
|
}
|
|
|
|
|
|
private static BuildTowerVisualInfo ResolveBuildTowerVisual(
|
|
IReadOnlyList<TowerItemData> participantTowers,
|
|
int buildIndex,
|
|
IReadOnlyDictionary<long, MuzzleCompItemData> muzzleMap,
|
|
IReadOnlyDictionary<long, BearingCompItemData> bearingMap,
|
|
IReadOnlyDictionary<long, BaseCompItemData> baseMap)
|
|
{
|
|
if (participantTowers == null || buildIndex < 0 || buildIndex >= participantTowers.Count)
|
|
{
|
|
return BuildTowerVisualInfo.Default;
|
|
}
|
|
|
|
TowerItemData tower = participantTowers[buildIndex];
|
|
if (tower == null)
|
|
{
|
|
return BuildTowerVisualInfo.Default;
|
|
}
|
|
|
|
Color muzzleColor = ResolveComponentColor(muzzleMap, tower.MuzzleComponentInstanceId);
|
|
Color bearingColor = ResolveComponentColor(bearingMap, tower.BearingComponentInstanceId);
|
|
Color baseColor = ResolveComponentColor(baseMap, tower.BaseComponentInstanceId);
|
|
return new BuildTowerVisualInfo(muzzleColor, bearingColor, baseColor);
|
|
}
|
|
|
|
private static Color ResolveComponentColor<TComp>(IReadOnlyDictionary<long, TComp> componentMap, long componentId)
|
|
where TComp : TowerCompItemData
|
|
{
|
|
if (componentMap == null || componentId <= 0)
|
|
{
|
|
return Color.white;
|
|
}
|
|
|
|
return componentMap.TryGetValue(componentId, out TComp component) && component != null
|
|
? IconColorGenerator.GenerateForComponent(component)
|
|
: Color.white;
|
|
}
|
|
|
|
private static Dictionary<long, TComp> BuildComponentMap<TComp>(IReadOnlyList<TComp> items)
|
|
where TComp : TowerCompItemData
|
|
{
|
|
Dictionary<long, TComp> map = new Dictionary<long, TComp>();
|
|
if (items == null)
|
|
{
|
|
return map;
|
|
}
|
|
|
|
for (int i = 0; i < items.Count; i++)
|
|
{
|
|
TComp item = items[i];
|
|
if (item == null || item.InstanceId <= 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
map[item.InstanceId] = item;
|
|
}
|
|
|
|
return map;
|
|
}
|
|
|
|
private static bool TryConsumeCoin(int cost)
|
|
{
|
|
int requiredCoin = Mathf.Max(0, cost);
|
|
if (requiredCoin <= 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (GameEntry.CombatNode == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return GameEntry.CombatNode.TryConsumeCoin(requiredCoin);
|
|
}
|
|
|
|
private static int GetCurrentCoin()
|
|
{
|
|
return GameEntry.CombatNode != null ? Mathf.Max(0, GameEntry.CombatNode.CurrentCoin) : 0;
|
|
}
|
|
|
|
private readonly struct BuildTowerVisualInfo
|
|
{
|
|
public static BuildTowerVisualInfo Default => new BuildTowerVisualInfo(Color.white, Color.white, Color.white);
|
|
|
|
public BuildTowerVisualInfo(Color muzzleColor, Color bearingColor, Color baseColor)
|
|
{
|
|
MuzzleColor = muzzleColor;
|
|
BearingColor = bearingColor;
|
|
BaseColor = baseColor;
|
|
}
|
|
|
|
public Color MuzzleColor { get; }
|
|
public Color BearingColor { get; }
|
|
public Color BaseColor { get; }
|
|
}
|
|
|
|
private static void AddCoin(int coin)
|
|
{
|
|
int amount = Mathf.Max(0, coin);
|
|
if (amount <= 0)
|
|
{
|
|
return;
|
|
}
|
|
GameEntry.CombatNode?.AddCoin(amount);
|
|
}
|
|
}
|
|
}
|
|
|