拆分 MapEntity 职责
- TowerSelectionPresenter:选中态 + 攻击范围展示 - CombatSelectInputService:点击事件逻辑(位置计算、点击对象识别、组装 CombatSelectFormUserData) - TowerPlacementService:建造/升级/销毁与塔映射管理 - MapTopologyService:维护地图拓扑结构与寻路缓存
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using GeometryTD.UI;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
namespace GeometryTD.Entity
|
||||
{
|
||||
public sealed class CombatSelectInputService
|
||||
{
|
||||
public bool TryBuildUserData(Tilemap tilemap, Transform mapTransform,
|
||||
IReadOnlyDictionary<Vector3Int, int> towerEntityIdByFoundationCell, Func<Vector3Int, bool> isFoundationCell,
|
||||
int upgradeCost, int destroyGain, out CombatSelectFormUserData userData)
|
||||
{
|
||||
userData = null;
|
||||
if (tilemap == null || !TryGetPointerWorldPosition(tilemap, mapTransform, out Vector3 worldPosition,
|
||||
out Vector2 contentPosition))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector3Int clickedCell = tilemap.WorldToCell(worldPosition);
|
||||
CombatSelectClickObjectType clickObjectType = CombatSelectClickObjectType.None;
|
||||
int towerEntityId = 0;
|
||||
Vector2 resolvedContentPosition = contentPosition;
|
||||
|
||||
if (towerEntityIdByFoundationCell != null &&
|
||||
towerEntityIdByFoundationCell.TryGetValue(clickedCell, out int occupiedTowerEntityId))
|
||||
{
|
||||
clickObjectType = CombatSelectClickObjectType.Tower;
|
||||
towerEntityId = occupiedTowerEntityId;
|
||||
resolvedContentPosition = BuildContentPositionFromCell(tilemap, clickedCell, contentPosition);
|
||||
}
|
||||
else if (isFoundationCell != null && isFoundationCell(clickedCell))
|
||||
{
|
||||
clickObjectType = CombatSelectClickObjectType.Foundation;
|
||||
resolvedContentPosition = BuildContentPositionFromCell(tilemap, clickedCell, contentPosition);
|
||||
}
|
||||
|
||||
userData = new CombatSelectFormUserData
|
||||
{
|
||||
ClickObjectType = clickObjectType,
|
||||
ContentPosition = resolvedContentPosition,
|
||||
WorldPosition = worldPosition,
|
||||
CellPosition = clickedCell,
|
||||
TowerEntityId = towerEntityId,
|
||||
UpgradeCost = Mathf.Max(0, upgradeCost),
|
||||
DestroyGain = Mathf.Max(0, destroyGain)
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool TryGetPointerWorldPosition(Tilemap tilemap, Transform mapTransform, out Vector3 worldPosition,
|
||||
out Vector2 contentPosition)
|
||||
{
|
||||
worldPosition = Vector3.zero;
|
||||
contentPosition = Vector2.zero;
|
||||
|
||||
Camera mainCamera = GameEntry.Scene != null ? GameEntry.Scene.MainCamera : Camera.main;
|
||||
if (mainCamera == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
|
||||
float mapPlaneZ = tilemap != null ? tilemap.transform.position.z : (mapTransform != null ? mapTransform.position.z : 0f);
|
||||
Vector3 planeNormal = mainCamera.transform.forward.sqrMagnitude > Mathf.Epsilon
|
||||
? -mainCamera.transform.forward
|
||||
: Vector3.forward;
|
||||
Plane mapPlane = new Plane(planeNormal, new Vector3(0f, 0f, mapPlaneZ));
|
||||
if (!mapPlane.Raycast(ray, out float enterDistance))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
worldPosition = ray.GetPoint(enterDistance);
|
||||
contentPosition = BuildContentPosition(Input.mousePosition);
|
||||
contentPosition = new Vector2((int)contentPosition.x, (int)contentPosition.y);
|
||||
contentPosition = new Vector2(contentPosition.x + 0.5f, contentPosition.y + 0.5f);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Vector2 BuildContentPosition(Vector3 pointerScreenPosition)
|
||||
{
|
||||
return new Vector2(
|
||||
pointerScreenPosition.x - Screen.width * 0.5f,
|
||||
pointerScreenPosition.y - Screen.height * 0.5f);
|
||||
}
|
||||
|
||||
private static Vector2 BuildContentPositionFromCell(Tilemap tilemap, Vector3Int cellPosition,
|
||||
Vector2 fallbackContentPosition)
|
||||
{
|
||||
if (tilemap == null)
|
||||
{
|
||||
return fallbackContentPosition;
|
||||
}
|
||||
|
||||
Camera mainCamera = GameEntry.Scene != null ? GameEntry.Scene.MainCamera : Camera.main;
|
||||
if (mainCamera == null)
|
||||
{
|
||||
return fallbackContentPosition;
|
||||
}
|
||||
|
||||
Vector3 cellCenterWorld = tilemap.GetCellCenterWorld(cellPosition);
|
||||
Vector3 cellScreenPosition = mainCamera.WorldToScreenPoint(cellCenterWorld);
|
||||
Vector2 contentPosition = BuildContentPosition(cellScreenPosition);
|
||||
contentPosition = new Vector2((int)contentPosition.x, (int)contentPosition.y);
|
||||
return new Vector2(contentPosition.x + 0.5f, contentPosition.y + 0.5f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4fb7517aceac0814685c7260ac02ae6f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,9 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using GeometryTD;
|
||||
using GeometryTD.Map;
|
||||
using GeometryTD.Definition;
|
||||
using GeometryTD.Entity.EntityData;
|
||||
using GeometryTD.Pathfinding;
|
||||
using GeometryTD.UI;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
@@ -14,24 +13,9 @@ namespace GeometryTD.Entity
|
||||
{
|
||||
public class MapEntity : EntityBase
|
||||
{
|
||||
private const string PathTileName = "Path";
|
||||
private const string FoundationTileName = "Foundation";
|
||||
private const int DefaultTowerTypeId = 401;
|
||||
private static readonly Spawner[] EmptySpawners = Array.Empty<Spawner>();
|
||||
|
||||
private readonly List<Vector3Int> _pathCells = new();
|
||||
private readonly List<Vector3Int> _foundationCells = new();
|
||||
private readonly HashSet<Vector3Int> _pathCellSet = new();
|
||||
private readonly HashSet<Vector3Int> _foundationCellSet = new();
|
||||
private readonly IMapPathfinder _mapPathfinder = new GridMapPathfinder();
|
||||
private readonly List<Vector3Int> _pathCellBuffer = new();
|
||||
private readonly Dictionary<Spawner, Vector3Int> _spawnerPathStartByRef = new();
|
||||
private readonly Dictionary<Spawner, List<Vector3Int>> _defaultPathCellsBySpawner = new();
|
||||
private readonly Dictionary<Vector3Int, int> _towerEntityIdByFoundationCell = new();
|
||||
private readonly Dictionary<int, Vector3Int> _foundationCellByTowerEntityId = new();
|
||||
private readonly Dictionary<int, DefenseTowerStatsData> _towerStatsByEntityId = new();
|
||||
private readonly List<int> _towerEntityIdBuffer = new();
|
||||
|
||||
[SerializeField] private bool _enableCombatSelectInput = true;
|
||||
[SerializeField] private int _towerTypeId = DefaultTowerTypeId;
|
||||
[SerializeField] private int[] _buildTowerCosts = { 80, 120, 160, 220 };
|
||||
@@ -40,131 +24,62 @@ namespace GeometryTD.Entity
|
||||
|
||||
private MapDataRefs _mapDataRefs;
|
||||
private MapData _mapData;
|
||||
private bool _hasHousePathCell;
|
||||
private Vector3Int _housePathCell;
|
||||
private MapTopologyService _mapTopologyService;
|
||||
private CombatSelectFormUseCase _combatSelectFormUseCase;
|
||||
private bool _hasSelectedFoundationCell;
|
||||
private Vector3Int _selectedFoundationCell;
|
||||
private int _selectedTowerEntityId;
|
||||
private int _attackRangeVisibleTowerEntityId;
|
||||
private CombatSelectInputService _combatSelectInputService;
|
||||
private TowerPlacementService _towerPlacementService;
|
||||
private TowerSelectionPresenter _towerSelectionPresenter;
|
||||
|
||||
public IReadOnlyList<Vector3Int> PathCells => _pathCells;
|
||||
public IReadOnlyList<Vector3Int> FoundationCells => _foundationCells;
|
||||
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 _pathCellSet.Contains(cellPosition);
|
||||
return _mapTopologyService != null && _mapTopologyService.IsPathCell(cellPosition);
|
||||
}
|
||||
|
||||
public bool IsFoundationCell(Vector3Int cellPosition)
|
||||
{
|
||||
return _foundationCellSet.Contains(cellPosition);
|
||||
return _mapTopologyService != null && _mapTopologyService.IsFoundationCell(cellPosition);
|
||||
}
|
||||
|
||||
public bool TryGetNearestPathCell(Vector3 worldPosition, out Vector3Int pathCell)
|
||||
{
|
||||
pathCell = default;
|
||||
if (_pathCells.Count <= 0 || Tilemap == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector3Int directCell = Tilemap.WorldToCell(worldPosition);
|
||||
if (_pathCellSet.Contains(directCell))
|
||||
{
|
||||
pathCell = directCell;
|
||||
return true;
|
||||
}
|
||||
|
||||
float minDistance = float.MaxValue;
|
||||
for (int i = 0; i < _pathCells.Count; i++)
|
||||
{
|
||||
Vector3Int candidate = _pathCells[i];
|
||||
float distance = (Tilemap.GetCellCenterWorld(candidate) - worldPosition).sqrMagnitude;
|
||||
if (distance >= minDistance)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
minDistance = distance;
|
||||
pathCell = candidate;
|
||||
}
|
||||
|
||||
return minDistance < float.MaxValue;
|
||||
return _mapTopologyService != null && _mapTopologyService.TryGetNearestPathCell(Tilemap, worldPosition, out pathCell);
|
||||
}
|
||||
|
||||
public Vector3 GetPathCellCenterWorld(Vector3Int pathCell)
|
||||
{
|
||||
return Tilemap != null ? Tilemap.GetCellCenterWorld(pathCell) : Vector3.zero;
|
||||
return _mapTopologyService != null
|
||||
? _mapTopologyService.GetPathCellCenterWorld(Tilemap, pathCell)
|
||||
: Vector3.zero;
|
||||
}
|
||||
|
||||
public bool TryGetDefaultPathCells(Spawner spawner, out IReadOnlyList<Vector3Int> pathCells)
|
||||
{
|
||||
pathCells = null;
|
||||
if (spawner == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!_defaultPathCellsBySpawner.TryGetValue(spawner, out List<Vector3Int> cachedPathCells))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
pathCells = cachedPathCells;
|
||||
return true;
|
||||
return _mapTopologyService != null && _mapTopologyService.TryGetDefaultPathCells(spawner, out pathCells);
|
||||
}
|
||||
|
||||
public bool TryFindPathCells(Spawner spawner, IReadOnlyCollection<Vector3Int> blockedCells,
|
||||
List<Vector3Int> pathResult)
|
||||
{
|
||||
if (pathResult == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
pathResult.Clear();
|
||||
if (spawner == null || !_hasHousePathCell)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!_spawnerPathStartByRef.TryGetValue(spawner, out Vector3Int startCell))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _mapPathfinder.TryFindPath(_pathCells, startCell, _housePathCell, blockedCells, pathResult);
|
||||
return _mapTopologyService != null && _mapTopologyService.TryFindPathCells(spawner, blockedCells, pathResult);
|
||||
}
|
||||
|
||||
public bool TryFindPathWorldPoints(Spawner spawner, IReadOnlyCollection<Vector3Int> blockedCells,
|
||||
List<Vector3> worldPathResult)
|
||||
{
|
||||
if (worldPathResult == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
worldPathResult.Clear();
|
||||
if (Tilemap == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!TryFindPathCells(spawner, blockedCells, _pathCellBuffer))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var pos in _pathCellBuffer)
|
||||
{
|
||||
worldPathResult.Add(GetPathCellCenterWorld(pos));
|
||||
}
|
||||
|
||||
return true;
|
||||
return _mapTopologyService != null &&
|
||||
_mapTopologyService.TryFindPathWorldPoints(Tilemap, spawner, blockedCells, worldPathResult);
|
||||
}
|
||||
|
||||
protected override void OnInit(object userData)
|
||||
@@ -178,6 +93,10 @@ namespace GeometryTD.Entity
|
||||
}
|
||||
|
||||
InitializeCombatSelectUseCase();
|
||||
InitializeCombatSelectInputService();
|
||||
InitializeMapTopologyService();
|
||||
InitializeTowerPlacementService();
|
||||
InitializeTowerSelectionPresenter();
|
||||
}
|
||||
|
||||
protected override void OnShow(object userData)
|
||||
@@ -198,7 +117,7 @@ namespace GeometryTD.Entity
|
||||
protected override void OnHide(bool isShutdown, object userData)
|
||||
{
|
||||
HideCombatSelectForm();
|
||||
ClearPlacedTowers();
|
||||
_towerPlacementService?.HideAndClearAllPlacedTowers();
|
||||
ClearRuntimeData();
|
||||
base.OnHide(isShutdown, userData);
|
||||
}
|
||||
@@ -230,108 +149,14 @@ namespace GeometryTD.Entity
|
||||
return;
|
||||
}
|
||||
|
||||
BoundsInt bounds = tilemap.cellBounds;
|
||||
foreach (Vector3Int cellPosition in bounds.allPositionsWithin)
|
||||
{
|
||||
TileBase tile = tilemap.GetTile(cellPosition);
|
||||
if (tile == null || string.IsNullOrEmpty(tile.name))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (string.Equals(tile.name, PathTileName, StringComparison.Ordinal))
|
||||
{
|
||||
_pathCells.Add(cellPosition);
|
||||
_pathCellSet.Add(cellPosition);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (string.Equals(tile.name, FoundationTileName, StringComparison.Ordinal))
|
||||
{
|
||||
_foundationCells.Add(cellPosition);
|
||||
_foundationCellSet.Add(cellPosition);
|
||||
}
|
||||
}
|
||||
|
||||
RefreshPathCache();
|
||||
Log.Info(
|
||||
"Map '{0}' initialized. LevelId={1}, PathCells={2}, FoundationCells={3}, Spawners={4}, House={5}, Routes={6}.",
|
||||
name,
|
||||
_mapData != null ? _mapData.LevelId : 0,
|
||||
_pathCells.Count,
|
||||
_foundationCells.Count,
|
||||
Spawners.Length,
|
||||
House != null ? House.name : "None",
|
||||
_defaultPathCellsBySpawner.Count);
|
||||
}
|
||||
|
||||
private void RefreshPathCache()
|
||||
{
|
||||
_hasHousePathCell = false;
|
||||
_housePathCell = default;
|
||||
_spawnerPathStartByRef.Clear();
|
||||
_defaultPathCellsBySpawner.Clear();
|
||||
|
||||
if (House == null)
|
||||
{
|
||||
Log.Warning("Map '{0}' has no house reference, path cache skipped.", name);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!TryGetNearestPathCell(House.Position, out _housePathCell))
|
||||
{
|
||||
Log.Warning("Map '{0}' house position can not map to a valid path cell.", name);
|
||||
return;
|
||||
}
|
||||
|
||||
_hasHousePathCell = true;
|
||||
Spawner[] spawners = Spawners;
|
||||
foreach (var spawner in spawners)
|
||||
{
|
||||
if (spawner == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!TryGetNearestPathCell(spawner.Position, out Vector3Int startCell))
|
||||
{
|
||||
Log.Warning("Map '{0}' spawner '{1}' can not map to a valid path cell.", name, spawner.name);
|
||||
continue;
|
||||
}
|
||||
|
||||
_spawnerPathStartByRef[spawner] = startCell;
|
||||
|
||||
List<Vector3Int> defaultPathCells = new List<Vector3Int>();
|
||||
if (!_mapPathfinder.TryFindPath(_pathCells, startCell, _housePathCell, null, defaultPathCells))
|
||||
{
|
||||
Log.Warning(
|
||||
"Map '{0}' spawner '{1}' has no path to house cell {2}.",
|
||||
name,
|
||||
spawner.name,
|
||||
_housePathCell);
|
||||
continue;
|
||||
}
|
||||
|
||||
_defaultPathCellsBySpawner[spawner] = defaultPathCells;
|
||||
}
|
||||
_mapTopologyService?.Refresh(tilemap, Spawners, House, name, _mapData != null ? _mapData.LevelId : 0);
|
||||
}
|
||||
|
||||
private void ClearRuntimeData()
|
||||
{
|
||||
_pathCells.Clear();
|
||||
_foundationCells.Clear();
|
||||
_pathCellSet.Clear();
|
||||
_foundationCellSet.Clear();
|
||||
_pathCellBuffer.Clear();
|
||||
_hasHousePathCell = false;
|
||||
_housePathCell = default;
|
||||
_spawnerPathStartByRef.Clear();
|
||||
_defaultPathCellsBySpawner.Clear();
|
||||
_towerEntityIdByFoundationCell.Clear();
|
||||
_foundationCellByTowerEntityId.Clear();
|
||||
_towerStatsByEntityId.Clear();
|
||||
_towerEntityIdBuffer.Clear();
|
||||
ClearSelectedObject();
|
||||
_mapTopologyService?.Clear();
|
||||
_towerPlacementService?.ClearTracking();
|
||||
_towerSelectionPresenter?.ClearSelectedObject();
|
||||
}
|
||||
|
||||
private void InitializeCombatSelectUseCase()
|
||||
@@ -344,6 +169,38 @@ namespace GeometryTD.Entity
|
||||
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)
|
||||
@@ -382,7 +239,10 @@ namespace GeometryTD.Entity
|
||||
return;
|
||||
}
|
||||
|
||||
if (!TryBuildCombatSelectUserData(out CombatSelectFormUserData userData))
|
||||
if (_combatSelectInputService == null ||
|
||||
!_combatSelectInputService.TryBuildUserData(Tilemap, CachedTransform,
|
||||
_towerPlacementService != null ? _towerPlacementService.TowerEntityIdByFoundationCell : null,
|
||||
IsFoundationCell, _upgradeCost, _destroyGain, out CombatSelectFormUserData userData))
|
||||
{
|
||||
userData = new CombatSelectFormUserData
|
||||
{
|
||||
@@ -394,334 +254,86 @@ namespace GeometryTD.Entity
|
||||
GameEntry.UIRouter.OpenUI(UIFormType.CombatSelectForm, userData);
|
||||
}
|
||||
|
||||
private bool TryBuildCombatSelectUserData(out CombatSelectFormUserData userData)
|
||||
{
|
||||
userData = null;
|
||||
if (Tilemap == null || !TryGetPointerWorldPosition(out Vector3 worldPosition, out Vector2 contentPosition))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector3Int clickedCell = Tilemap.WorldToCell(worldPosition);
|
||||
CombatSelectClickObjectType clickObjectType = CombatSelectClickObjectType.None;
|
||||
int towerEntityId = 0;
|
||||
|
||||
if (_towerEntityIdByFoundationCell.TryGetValue(clickedCell, out int occupiedTowerEntityId))
|
||||
{
|
||||
clickObjectType = CombatSelectClickObjectType.Tower;
|
||||
towerEntityId = occupiedTowerEntityId;
|
||||
}
|
||||
else if (IsFoundationCell(clickedCell))
|
||||
{
|
||||
clickObjectType = CombatSelectClickObjectType.Foundation;
|
||||
}
|
||||
|
||||
userData = new CombatSelectFormUserData
|
||||
{
|
||||
ClickObjectType = clickObjectType,
|
||||
ContentPosition = contentPosition,
|
||||
WorldPosition = worldPosition,
|
||||
CellPosition = clickedCell,
|
||||
TowerEntityId = towerEntityId,
|
||||
UpgradeCost = Mathf.Max(0, _upgradeCost),
|
||||
DestroyGain = Mathf.Max(0, _destroyGain)
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TryGetPointerWorldPosition(out Vector3 worldPosition, out Vector2 contentPosition)
|
||||
{
|
||||
worldPosition = Vector3.zero;
|
||||
contentPosition = Vector2.zero;
|
||||
|
||||
Camera mainCamera = GameEntry.Scene != null ? GameEntry.Scene.MainCamera : Camera.main;
|
||||
if (mainCamera == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
|
||||
float mapPlaneZ = Tilemap != null ? Tilemap.transform.position.z : CachedTransform.position.z;
|
||||
Vector3 planeNormal = mainCamera.transform.forward.sqrMagnitude > Mathf.Epsilon
|
||||
? -mainCamera.transform.forward
|
||||
: Vector3.forward;
|
||||
Plane mapPlane = new Plane(planeNormal, new Vector3(0f, 0f, mapPlaneZ));
|
||||
if (!mapPlane.Raycast(ray, out float enterDistance))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
worldPosition = ray.GetPoint(enterDistance);
|
||||
contentPosition = BuildContentPosition(Input.mousePosition);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Vector2 BuildContentPosition(Vector3 pointerScreenPosition)
|
||||
{
|
||||
return new Vector2(
|
||||
pointerScreenPosition.x - Screen.width * 0.5f,
|
||||
pointerScreenPosition.y - Screen.height * 0.5f);
|
||||
}
|
||||
|
||||
private 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();
|
||||
break;
|
||||
}
|
||||
|
||||
UpdateTowerAttackRangeDisplay(_selectedTowerEntityId);
|
||||
_towerSelectionPresenter?.ApplySelectedObject(userData);
|
||||
}
|
||||
|
||||
private bool TryBuildTower(int buildIndex)
|
||||
{
|
||||
if (!_hasSelectedFoundationCell || !IsFoundationCell(_selectedFoundationCell))
|
||||
if (_towerSelectionPresenter == null ||
|
||||
_towerPlacementService == null ||
|
||||
!_towerSelectionPresenter.TryGetSelectedFoundationCell(out Vector3Int selectedFoundationCell) ||
|
||||
!IsFoundationCell(selectedFoundationCell))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_towerEntityIdByFoundationCell.ContainsKey(_selectedFoundationCell))
|
||||
if (!_towerPlacementService.TryBuildTower(selectedFoundationCell, IsFoundationCell, buildIndex, _buildTowerCosts,
|
||||
_towerTypeId, Tilemap, TryConsumeCoin, AddCoin, out int towerEntityId))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int buildCost = GetBuildTowerCost(buildIndex);
|
||||
if (!TryConsumeCoin(buildCost))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
DefenseTowerStatsData towerStats = BuildTowerStats(buildIndex);
|
||||
if (!TryShowTowerEntity(_selectedFoundationCell, towerStats, out int towerEntityId))
|
||||
{
|
||||
GameEntry.CombatNode?.AddCoin(buildCost);
|
||||
return false;
|
||||
}
|
||||
|
||||
_towerEntityIdByFoundationCell[_selectedFoundationCell] = towerEntityId;
|
||||
_foundationCellByTowerEntityId[towerEntityId] = _selectedFoundationCell;
|
||||
_towerStatsByEntityId[towerEntityId] = CloneTowerStats(towerStats);
|
||||
_selectedTowerEntityId = towerEntityId;
|
||||
UpdateTowerAttackRangeDisplay(_selectedTowerEntityId);
|
||||
_towerSelectionPresenter.SelectTower(selectedFoundationCell, towerEntityId);
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TryUpgradeTower()
|
||||
{
|
||||
if (!TryGetSelectedTower(out int towerEntityId, out Vector3Int foundationCell))
|
||||
if (_towerSelectionPresenter == null ||
|
||||
_towerPlacementService == null ||
|
||||
!_towerSelectionPresenter.TryGetSelectedTower(_towerPlacementService.FoundationCellByTowerEntityId,
|
||||
out int towerEntityId,
|
||||
out Vector3Int foundationCell))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int upgradeCost = Mathf.Max(0, _upgradeCost);
|
||||
if (!TryConsumeCoin(upgradeCost))
|
||||
if (!_towerPlacementService.TryUpgradeTower(towerEntityId, _upgradeCost, _towerTypeId, Tilemap,
|
||||
TryConsumeCoin, AddCoin, out int resultTowerEntityId, out foundationCell))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
DefenseTowerStatsData oldStats = _towerStatsByEntityId.TryGetValue(towerEntityId, out DefenseTowerStatsData cachedStats)
|
||||
? CloneTowerStats(cachedStats)
|
||||
: BuildTowerStats(0);
|
||||
DefenseTowerStatsData upgradedStats = CloneTowerStats(oldStats);
|
||||
ApplyUpgradeToStats(upgradedStats);
|
||||
|
||||
HideTowerEntity(towerEntityId);
|
||||
_towerEntityIdByFoundationCell.Remove(foundationCell);
|
||||
_foundationCellByTowerEntityId.Remove(towerEntityId);
|
||||
_towerStatsByEntityId.Remove(towerEntityId);
|
||||
|
||||
if (!TryShowTowerEntity(foundationCell, upgradedStats, out int newTowerEntityId))
|
||||
{
|
||||
if (TryShowTowerEntity(foundationCell, oldStats, out int fallbackTowerEntityId))
|
||||
if (resultTowerEntityId != 0)
|
||||
{
|
||||
_towerEntityIdByFoundationCell[foundationCell] = fallbackTowerEntityId;
|
||||
_foundationCellByTowerEntityId[fallbackTowerEntityId] = foundationCell;
|
||||
_towerStatsByEntityId[fallbackTowerEntityId] = CloneTowerStats(oldStats);
|
||||
_selectedTowerEntityId = fallbackTowerEntityId;
|
||||
UpdateTowerAttackRangeDisplay(_selectedTowerEntityId);
|
||||
_towerSelectionPresenter.SelectTower(foundationCell, resultTowerEntityId);
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateTowerAttackRangeDisplay(0);
|
||||
_towerSelectionPresenter.ClearSelectedObject();
|
||||
}
|
||||
|
||||
GameEntry.CombatNode?.AddCoin(upgradeCost);
|
||||
return false;
|
||||
}
|
||||
|
||||
_towerEntityIdByFoundationCell[foundationCell] = newTowerEntityId;
|
||||
_foundationCellByTowerEntityId[newTowerEntityId] = foundationCell;
|
||||
_towerStatsByEntityId[newTowerEntityId] = CloneTowerStats(upgradedStats);
|
||||
_hasSelectedFoundationCell = true;
|
||||
_selectedFoundationCell = foundationCell;
|
||||
_selectedTowerEntityId = newTowerEntityId;
|
||||
UpdateTowerAttackRangeDisplay(_selectedTowerEntityId);
|
||||
_towerSelectionPresenter.SelectTower(foundationCell, resultTowerEntityId);
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TryDestroyTower()
|
||||
{
|
||||
if (!TryGetSelectedTower(out int towerEntityId, out Vector3Int foundationCell))
|
||||
if (_towerSelectionPresenter == null ||
|
||||
_towerPlacementService == null ||
|
||||
!_towerSelectionPresenter.TryGetSelectedTower(_towerPlacementService.FoundationCellByTowerEntityId,
|
||||
out int towerEntityId,
|
||||
out Vector3Int foundationCell))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
HideTowerEntity(towerEntityId);
|
||||
_towerEntityIdByFoundationCell.Remove(foundationCell);
|
||||
_foundationCellByTowerEntityId.Remove(towerEntityId);
|
||||
_towerStatsByEntityId.Remove(towerEntityId);
|
||||
GameEntry.CombatNode?.AddCoin(Mathf.Max(0, _destroyGain));
|
||||
if (!_towerPlacementService.TryDestroyTower(towerEntityId, _destroyGain, AddCoin, out foundationCell))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
ClearSelectedObject();
|
||||
_towerSelectionPresenter.ClearSelectedObject();
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TryGetSelectedTower(out int towerEntityId, out Vector3Int foundationCell)
|
||||
{
|
||||
towerEntityId = 0;
|
||||
foundationCell = default;
|
||||
|
||||
if (_selectedTowerEntityId == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!_foundationCellByTowerEntityId.TryGetValue(_selectedTowerEntityId, out foundationCell))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
towerEntityId = _selectedTowerEntityId;
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TryShowTowerEntity(Vector3Int foundationCell, DefenseTowerStatsData towerStats, out int towerEntityId)
|
||||
{
|
||||
towerEntityId = 0;
|
||||
if (GameEntry.Entity == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int entityId = GameEntry.Entity.GenerateSerialId();
|
||||
int typeId = _towerTypeId > 0 ? _towerTypeId : DefaultTowerTypeId;
|
||||
Vector3 towerPosition = Tilemap != null ? Tilemap.GetCellCenterWorld(foundationCell) : foundationCell;
|
||||
towerPosition.z = 0f;
|
||||
var towerData = new DefenseTowerData(entityId, typeId, towerPosition, Quaternion.identity, towerStats);
|
||||
GameEntry.Entity.ShowDefenseTower(towerData);
|
||||
|
||||
towerEntityId = entityId;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void HideTowerEntity(int towerEntityId)
|
||||
{
|
||||
if (towerEntityId == 0 || GameEntry.Entity == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UnityGameFramework.Runtime.Entity towerEntity = GameEntry.Entity.GetEntity(towerEntityId);
|
||||
if (towerEntity != null)
|
||||
{
|
||||
GameEntry.Entity.HideEntity(towerEntity);
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearPlacedTowers()
|
||||
{
|
||||
_towerEntityIdBuffer.Clear();
|
||||
foreach (KeyValuePair<int, Vector3Int> pair in _foundationCellByTowerEntityId)
|
||||
{
|
||||
_towerEntityIdBuffer.Add(pair.Key);
|
||||
}
|
||||
|
||||
for (int i = 0; i < _towerEntityIdBuffer.Count; i++)
|
||||
{
|
||||
HideTowerEntity(_towerEntityIdBuffer[i]);
|
||||
}
|
||||
|
||||
_towerEntityIdByFoundationCell.Clear();
|
||||
_foundationCellByTowerEntityId.Clear();
|
||||
_towerStatsByEntityId.Clear();
|
||||
_towerEntityIdBuffer.Clear();
|
||||
ClearSelectedObject();
|
||||
}
|
||||
|
||||
private void HideCombatSelectForm()
|
||||
{
|
||||
_combatSelectFormUseCase?.Hide();
|
||||
GameEntry.UIRouter.CloseUI(UIFormType.CombatSelectForm);
|
||||
}
|
||||
|
||||
private 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 DefenseTowerEntity towerEntity)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
towerEntity.SetAttackRangeVisible(visible);
|
||||
return true;
|
||||
}
|
||||
|
||||
private int GetBuildTowerCost(int buildIndex)
|
||||
{
|
||||
if (_buildTowerCosts == null || buildIndex < 0 || buildIndex >= _buildTowerCosts.Length)
|
||||
@@ -753,105 +365,14 @@ namespace GeometryTD.Entity
|
||||
return GameEntry.CombatNode != null ? Mathf.Max(0, GameEntry.CombatNode.CurrentCoin) : 0;
|
||||
}
|
||||
|
||||
private static DefenseTowerStatsData BuildTowerStats(int buildIndex)
|
||||
private static void AddCoin(int coin)
|
||||
{
|
||||
switch (buildIndex)
|
||||
{
|
||||
case 0:
|
||||
return new DefenseTowerStatsData
|
||||
{
|
||||
AttackDamage = 100,
|
||||
DamageRandomRate = 0f,
|
||||
RotateSpeed = 200f,
|
||||
AttackRange = 4.5f,
|
||||
AttackSpeed = 1.5f,
|
||||
AttackMethodType = AttackMethodType.NormalBullet,
|
||||
AttackPropertyType = AttackPropertyType.Physics,
|
||||
Tags = Array.Empty<TagType>()
|
||||
};
|
||||
case 1:
|
||||
return new DefenseTowerStatsData
|
||||
{
|
||||
AttackDamage = 13,
|
||||
DamageRandomRate = 0f,
|
||||
RotateSpeed = 160f,
|
||||
AttackRange = 5f,
|
||||
AttackSpeed = 1.2f,
|
||||
AttackMethodType = AttackMethodType.NormalBullet,
|
||||
AttackPropertyType = AttackPropertyType.Fire,
|
||||
Tags = Array.Empty<TagType>()
|
||||
};
|
||||
case 2:
|
||||
return new DefenseTowerStatsData
|
||||
{
|
||||
AttackDamage = 17,
|
||||
DamageRandomRate = 0f,
|
||||
RotateSpeed = 140f,
|
||||
AttackRange = 5.5f,
|
||||
AttackSpeed = 0.95f,
|
||||
AttackMethodType = AttackMethodType.NormalBullet,
|
||||
AttackPropertyType = AttackPropertyType.Ice,
|
||||
Tags = Array.Empty<TagType>()
|
||||
};
|
||||
case 3:
|
||||
return new DefenseTowerStatsData
|
||||
{
|
||||
AttackDamage = 22,
|
||||
DamageRandomRate = 0f,
|
||||
RotateSpeed = 120f,
|
||||
AttackRange = 6f,
|
||||
AttackSpeed = 0.75f,
|
||||
AttackMethodType = AttackMethodType.NormalBullet,
|
||||
AttackPropertyType = AttackPropertyType.Poison,
|
||||
Tags = Array.Empty<TagType>()
|
||||
};
|
||||
default:
|
||||
return new DefenseTowerStatsData
|
||||
{
|
||||
AttackDamage = 100,
|
||||
DamageRandomRate = 0f,
|
||||
RotateSpeed = 180f,
|
||||
AttackRange = 5f,
|
||||
AttackSpeed = 1f,
|
||||
AttackMethodType = AttackMethodType.NormalBullet,
|
||||
AttackPropertyType = AttackPropertyType.Physics,
|
||||
Tags = Array.Empty<TagType>()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private static DefenseTowerStatsData CloneTowerStats(DefenseTowerStatsData source)
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
return BuildTowerStats(0);
|
||||
}
|
||||
|
||||
TagType[] copiedTags = source.Tags != null ? (TagType[])source.Tags.Clone() : Array.Empty<TagType>();
|
||||
return new DefenseTowerStatsData
|
||||
{
|
||||
AttackDamage = source.AttackDamage,
|
||||
DamageRandomRate = source.DamageRandomRate,
|
||||
RotateSpeed = source.RotateSpeed,
|
||||
AttackRange = source.AttackRange,
|
||||
AttackSpeed = source.AttackSpeed,
|
||||
AttackMethodType = source.AttackMethodType,
|
||||
AttackPropertyType = source.AttackPropertyType,
|
||||
Tags = copiedTags
|
||||
};
|
||||
}
|
||||
|
||||
private static void ApplyUpgradeToStats(DefenseTowerStatsData stats)
|
||||
{
|
||||
if (stats == null)
|
||||
int amount = Mathf.Max(0, coin);
|
||||
if (amount <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
stats.AttackDamage = Mathf.Max(1, stats.AttackDamage + 3);
|
||||
stats.AttackSpeed = Mathf.Max(0.01f, stats.AttackSpeed + 0.2f);
|
||||
stats.AttackRange = Mathf.Max(0.1f, stats.AttackRange + 0.4f);
|
||||
stats.RotateSpeed = Mathf.Max(1f, stats.RotateSpeed + 10f);
|
||||
GameEntry.CombatNode?.AddCoin(amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user