根据提供的code differences信息,由于没有具体的代码变更内容,我将生成一个通用的commit message模板:

```
docs(readme): 更新文档说明

- 添加项目使用指南
- 完善API接口说明
- 修正错误的配置示例
```

注意:由于您提供的code differences部分为空,以上是一个示例模板。当您有具体的代码变更时,请提供详细的变更内容以便生成更准确的commit message。
This commit is contained in:
huadexuan committed 2026-10-04 22:58:39 +08:00
1 parent 11f1af357d
commit 7d9d0f52d1
83 files changed
+3510 -1493

No files matched your search

+113
View File
@@ -0,0 +1,113 @@
using UnityEngine;
/// <summary>
/// 部署格位高亮:拖动卡牌时在目标格位显示一块发光地板。
/// 绿色 = 可以放置;红色 = 该格已被占用 / 不可放置。
/// 全场只需要一个,由 DefenseGameFlowController 在布阵阶段创建。
/// </summary>
public class DeployHighlight : MonoBehaviour
{
public static DeployHighlight Instance { get; private set; }
private static readonly Color FreeColor = new Color(0.25f, 1f, 0.45f, 0.55f);
private static readonly Color BlockedColor = new Color(1f, 0.28f, 0.28f, 0.55f);
private GameObject plate;
private Material plateMaterial;
private int shownCell = -1;
/// <summary>高亮当前是否可见。</summary>
public bool IsVisible => plate != null && plate.activeSelf;
/// <summary>当前显示的是否是"不可放置"(红色)。</summary>
public bool IsBlocked { get; private set; }
/// <summary>当前高亮所在的格位,-1 表示没有。</summary>
public int ShownCell => shownCell;
public static DeployHighlight Ensure()
{
if (Instance != null) return Instance;
var go = new GameObject("DeployHighlight");
Instance = go.AddComponent<DeployHighlight>();
Instance.Build();
return Instance;
}
private void Awake()
{
if (Instance == null)
{
Instance = this;
Build();
}
}
private void Build()
{
if (plate != null) return;
plate = GameObject.CreatePrimitive(PrimitiveType.Cube);
plate.name = "CellPlate";
plate.transform.SetParent(transform, false);
plate.transform.localRotation = Quaternion.identity;
plate.transform.localScale = new Vector3(DeployGrid.CellWidth * 0.96f, 0.02f, DeployGrid.CellDepth * 0.96f);
// 高亮只是视觉提示,不需要碰撞
var collider = plate.GetComponent<Collider>();
if (collider != null)
{
Destroy(collider);
}
var shader = Shader.Find("Sprites/Default") ?? Shader.Find("Unlit/Color");
plateMaterial = new Material(shader);
plateMaterial.name = "DeployHighlightMat";
plateMaterial.color = FreeColor;
var renderer = plate.GetComponent<Renderer>();
renderer.sharedMaterial = plateMaterial;
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
renderer.receiveShadows = false;
plate.SetActive(false);
}
/// <summary>在指定格位显示高亮。blocked = true 显示红色。</summary>
public void Show(int cellIndex, bool blocked)
{
if (plate == null) Build();
if (!DeployGrid.IsValidIndex(cellIndex))
{
Hide();
return;
}
var pos = DeployGrid.CellPosition(cellIndex);
plate.transform.position = new Vector3(pos.x, DeployGrid.GridY + 0.05f, pos.z);
plateMaterial.color = blocked ? BlockedColor : FreeColor;
IsBlocked = blocked;
if (shownCell != cellIndex || !plate.activeSelf)
{
plate.SetActive(true);
shownCell = cellIndex;
}
}
public void Hide()
{
if (plate != null && plate.activeSelf)
{
plate.SetActive(false);
}
shownCell = -1;
IsBlocked = false;
}
private void OnDestroy()
{
if (Instance == this) Instance = null;
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: W3JKtSP/Ai5L068yM/KfmfMqMzxNuuwNH7682DCQ2pexDdqL0sqt1zY=
guid: XHkY4HmuWipU3pQUHG3woFO67ulNfp7xd8NigFzHi9m1Ba1ZVxVUNLM=
MonoImporter:
externalObjects: {}
serializedVersion: 2
+291
View File
@@ -0,0 +1,291 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
/// <summary>
/// 英雄卡片拖动组件(布阵阶段使用)。
/// 拖动时在场景里生成一个半透明的英雄"幽灵"跟随指针,并吸附到最近的部署格位;
/// 松手后把该卡对应的英雄登记到格位上,卡片进入"已使用"状态。
/// </summary>
public class HeroCardDrag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
[Header("配置(由 DefenseGameFlowController 注入)")]
public int cardIndex; // 卡片序号,0 起
public GameObject heroPrefab; // 对应英雄 Prefab
public CharacterData characterData; // 角色数据
[Header("幽灵外观")]
[Range(0.1f, 1f)] public float ghostAlpha = 0.55f;
private Canvas canvas;
private CanvasGroup canvasGroup;
private GameObject ghost;
private Material ghostHiddenMaterial;
private bool dragging;
private int currentCell = -1;
private const float PlacedCardAlpha = 0.35f;
private void Awake()
{
canvas = GetComponentInParent<Canvas>();
canvasGroup = GetComponent<CanvasGroup>();
if (canvasGroup == null)
{
canvasGroup = gameObject.AddComponent<CanvasGroup>();
}
}
/// <summary>由流程控制器注入卡片配置。</summary>
public void Setup(int index, GameObject prefab, CharacterData data)
{
cardIndex = index;
heroPrefab = prefab;
characterData = data;
SetPlaced(false);
}
/// <summary>标记该卡是否已经放下去了(已放置的卡片变暗且不能再拖)。</summary>
public void SetPlaced(bool placed)
{
if (canvasGroup != null)
{
canvasGroup.alpha = placed ? PlacedCardAlpha : 1f;
}
}
public void OnBeginDrag(PointerEventData eventData)
{
var flow = DefenseGameFlowController.Instance;
if (flow == null || !flow.IsDeployPhase) return;
if (flow.IsCardPlaced(cardIndex)) return; // 已经放置过了
if (heroPrefab == null) return;
dragging = true;
currentCell = -1;
if (canvasGroup != null)
{
canvasGroup.alpha = 0.5f;
canvasGroup.blocksRaycasts = false;
}
CreateGhost();
FollowPointer(eventData.position);
}
public void OnDrag(PointerEventData eventData)
{
if (!dragging) return;
FollowPointer(eventData.position);
}
public void OnEndDrag(PointerEventData eventData)
{
if (!dragging) return;
dragging = false;
if (canvasGroup != null)
{
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
}
if (DeployHighlight.Instance != null) DeployHighlight.Instance.Hide();
int cell = currentCell;
currentCell = -1;
DestroyGhost();
if (cell >= 0)
{
var flow = DefenseGameFlowController.Instance;
if (flow != null && flow.PlaceHeroFromCard(cardIndex, cell))
{
SetPlaced(true);
}
}
}
// ── 幽灵 ────────────────────────────────────────────────
private void CreateGhost()
{
ghost = Instantiate(heroPrefab);
ghost.name = "Ghost_" + heroPrefab.name;
// 只保留外观:关掉所有逻辑组件与碰撞体
foreach (var behaviour in ghost.GetComponentsInChildren<Behaviour>(true))
{
if (behaviour is Animator) continue; // 保留 Animator 让它做待机动作
behaviour.enabled = false;
}
foreach (var collider in ghost.GetComponentsInChildren<Collider>(true))
{
collider.enabled = false;
}
foreach (var rb in ghost.GetComponentsInChildren<Rigidbody>(true))
{
rb.isKinematic = true;
rb.detectCollisions = false;
}
// Animator 不能带动根位移,否则幽灵会自己漂走
foreach (var animator in ghost.GetComponentsInChildren<Animator>(true))
{
animator.applyRootMotion = false;
}
// 血条 UI 是 WorldHealthBar.Awake 生成的,幽灵不需要
var healthBar = ghost.transform.Find("WorldHealthBar");
if (healthBar != null)
{
Destroy(healthBar.gameObject);
}
BuildGhostMaterials();
}
private void BuildGhostMaterials()
{
if (ghostHiddenMaterial == null)
{
var hiddenShader = Shader.Find("Sprites/Default") ?? Shader.Find("Unlit/Color");
ghostHiddenMaterial = new Material(hiddenShader);
ghostHiddenMaterial.name = "GhostHidden";
ghostHiddenMaterial.color = new Color(1f, 1f, 1f, 0f);
}
var bodyShader = Shader.Find("Standard");
foreach (var renderer in ghost.GetComponentsInChildren<Renderer>(true))
{
var source = renderer.sharedMaterials;
var result = new Material[source.Length];
for (int i = 0; i < source.Length; i++)
{
// 第 0 个材质是本体,做成半透明;其余(描边等附加材质)隐藏掉
if (i == 0 && source[i] != null && bodyShader != null)
{
result[i] = MakeTransparent(source[i], ghostAlpha, bodyShader);
}
else
{
result[i] = ghostHiddenMaterial;
}
}
renderer.sharedMaterials = result;
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
}
}
private static Material MakeTransparent(Material source, float alpha, Shader standard)
{
var mat = new Material(source);
mat.name = source.name + "_Ghost";
// 统一用 Standard 的透明混合模式,保证任何贴图都能变成半透明
if (mat.shader != standard)
{
var mainTex = mat.HasProperty("_MainTex") ? mat.GetTexture("_MainTex") : null;
var color = mat.HasProperty("_Color") ? mat.GetColor("_Color") : Color.white;
mat.shader = standard;
if (mainTex != null) mat.SetTexture("_MainTex", mainTex);
mat.SetColor("_Color", color);
}
if (mat.HasProperty("_Mode"))
{
mat.SetFloat("_Mode", 3f); // Transparent
mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
mat.SetInt("_ZWrite", 0);
mat.DisableKeyword("_ALPHATEST_ON");
mat.EnableKeyword("_ALPHABLEND_ON");
mat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
mat.renderQueue = 3000;
}
if (mat.HasProperty("_Color"))
{
var c = mat.GetColor("_Color");
c.a = alpha;
mat.SetColor("_Color", c);
}
return mat;
}
private void FollowPointer(Vector2 screenPosition)
{
if (ghost == null) return;
var cam = Camera.main;
if (cam == null) return;
var ray = cam.ScreenPointToRay(screenPosition);
var ground = new Plane(Vector3.up, new Vector3(0f, DeployGrid.UnitY, 0f));
if (!ground.Raycast(ray, out float distance))
{
return;
}
Vector3 world = ray.GetPoint(distance);
int cell = DeployGrid.SnapToCellClamped(world);
if (cell == currentCell) return;
currentCell = cell;
var flow = DefenseGameFlowController.Instance;
bool blocked = cell < 0 || (flow != null && flow.IsCellOccupied(cell));
if (cell >= 0)
{
ghost.transform.position = DeployGrid.CellPosition(cell);
if (DeployHighlight.Instance != null) DeployHighlight.Instance.Show(cell, blocked);
}
else
{
// 没吸附到格位:幽灵跟着指针但压暗提示不可放置
ghost.transform.position = new Vector3(world.x, DeployGrid.UnitY, world.z);
if (DeployHighlight.Instance != null) DeployHighlight.Instance.Hide();
}
}
private void DestroyGhost()
{
if (ghost == null) return;
foreach (var renderer in ghost.GetComponentsInChildren<Renderer>(true))
{
if (renderer.sharedMaterials == null) continue;
foreach (var mat in renderer.sharedMaterials)
{
if (mat != null && mat != ghostHiddenMaterial) Destroy(mat);
}
}
Destroy(ghost);
ghost = null;
}
private void OnDisable()
{
if (dragging)
{
dragging = false;
if (canvasGroup != null)
{
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
}
if (DeployHighlight.Instance != null) DeployHighlight.Instance.Hide();
}
DestroyGhost();
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: CXkWsyr5V3PqLCVXf9kh9UDgagA4PNu+m8m3e+26o8Vi3MvAgRRmtzA=
guid: BnlMty2vUiow3BhqaXGezjZxgLO0vMhqB1RBSlrYGt9jhcgal1OINJI=
MonoImporter:
externalObjects: {}
serializedVersion: 2
+85 -2
View File
@@ -9,6 +9,11 @@ public class HeroCardUI : MonoBehaviour
{
[Header("引用")]
public Image iconImage; // 英雄头像
public Image rarityFrameImage; // 稀有度边框
public Sprite blueRarityFrame;
public Sprite orangeRarityFrame;
public Sprite purpleRarityFrame;
public Sprite redRarityFrame;
public Image hpFillImage; // 血条填充
public GameObject deathPanel; // 死亡遮罩
public Text reviveTimerText; // 复活倒计时文本
@@ -86,21 +91,28 @@ public class HeroCardUI : MonoBehaviour
/// 设置英雄单位
/// </summary>
public void SetHero(UnitBase unit, Sprite icon, Vector3 spawnPos)
{
SetHero(unit, icon, spawnPos, null);
}
public void SetHero(UnitBase unit, Sprite icon, Vector3 spawnPos, CharacterData characterData)
{
heroUnit = unit;
heroSpawnPosition = spawnPos;
if (unit != null)
{
heroHealthBar = unit.GetComponent<WorldHealthBar>();
}
// 设置头像
if (iconImage != null && icon != null)
{
iconImage.sprite = icon;
}
UpdateRarityFrame(characterData);
UpdateCharacterName(characterData);
isDead = false;
if (deathPanel != null)
{
@@ -108,6 +120,77 @@ public class HeroCardUI : MonoBehaviour
}
}
/// <summary>
/// 只刷新卡面(头像 / 名字 / 稀有度边框),不绑定单位。
/// 布阵阶段按玩家出战阵容初始化卡片时用,避免"卡片显示 A、拖出来却是 B"。
/// </summary>
public void SetCharacterInfo(CharacterData characterData)
{
if (characterData == null) return;
if (iconImage != null && characterData.cardIcon != null)
{
iconImage.sprite = characterData.cardIcon;
}
UpdateRarityFrame(characterData);
UpdateCharacterName(characterData);
}
private void UpdateRarityFrame(CharacterData characterData)
{
if (rarityFrameImage == null)
{
Transform frame = transform.Find("RarityFrame") ?? transform.Find("稀有度边框");
if (frame != null)
{
rarityFrameImage = frame.GetComponent<Image>();
}
}
if (rarityFrameImage == null || characterData == null)
{
return;
}
Sprite frameSprite = characterData.rarity switch
{
CharacterRarity.Blue => blueRarityFrame,
CharacterRarity.Orange => orangeRarityFrame,
CharacterRarity.Purple => purpleRarityFrame,
CharacterRarity.Red => redRarityFrame,
_ => blueRarityFrame
};
if (frameSprite != null)
{
rarityFrameImage.sprite = frameSprite;
}
}
private void UpdateCharacterName(CharacterData characterData)
{
if (characterData == null)
{
return;
}
// 查找 NameLabel/Text
Transform nameLabel = transform.Find("NameLabel");
if (nameLabel != null)
{
Transform textTransform = nameLabel.Find("Text");
if (textTransform != null)
{
Text nameText = textTransform.GetComponent<Text>();
if (nameText != null)
{
nameText.text = characterData.characterName;
}
}
}
}
/// <summary>
/// 更新血条显示
/// </summary>
@@ -245,7 +245,7 @@ public class BattleFormationSpawner : MonoBehaviour
UnityEngine.Sprite icon = characterData != null ? characterData.cardIcon : null;
// 绑定英雄到卡片
heroCardUI.SetHero(unitBase, icon, spawnPos);
heroCardUI.SetHero(unitBase, icon, spawnPos, characterData);
spawnedHeroCount++;
@@ -1,117 +0,0 @@
using UnityEngine;
/// <summary>
/// 混沌之门:被占领后按固定间隔从所属阵营卡组随机出兵。
/// </summary>
[RequireComponent(typeof(CrystalMineCapture))]
public class ChaosGateSpawner : MonoBehaviour
{
[Header("出兵配置")]
[Min(1f)] public float spawnInterval = 12f;
[Min(0f)] public float spawnRadius = 1.5f;
public bool spawnImmediatelyOnCapture = false;
[Header("蓝方卡组")]
[SerializeField] private string[] blueDeckDataNames =
{
"人类士兵Data", "弓箭手Data", "盾卫Data", "精锐士兵Data",
"法师Data", "狂暴狼人Data", "石巨人Data", "弓箭猎人Data"
};
private CrystalMineCapture capture;
private UnitTeam unitTeam;
private float spawnTimer;
private int previousTeamId = 2;
private void Awake()
{
capture = GetComponent<CrystalMineCapture>();
unitTeam = GetComponent<UnitTeam>();
spawnTimer = spawnInterval;
}
private void Update()
{
if (unitTeam == null)
{
unitTeam = GetComponent<UnitTeam>();
if (unitTeam == null) return;
}
int currentTeamId = unitTeam.teamId;
if (currentTeamId != previousTeamId)
{
previousTeamId = currentTeamId;
spawnTimer = spawnImmediatelyOnCapture ? 0f : spawnInterval;
}
if (currentTeamId != 0 && currentTeamId != 1)
{
spawnTimer = spawnInterval;
return;
}
spawnTimer -= Time.deltaTime;
if (spawnTimer > 0f) return;
if (TrySpawn(currentTeamId))
spawnTimer = spawnInterval;
else
spawnTimer = 1f;
}
private bool TrySpawn(int teamId)
{
Vector3 position = transform.position;
position.x += Random.Range(-spawnRadius, spawnRadius);
position.z += Random.Range(-spawnRadius, spawnRadius);
if (teamId == 1)
{
EnemyCastleAI enemyAI = FindObjectOfType<EnemyCastleAI>();
return enemyAI != null && enemyAI.TrySpawnFromPortal(position, transform.rotation);
}
CharacterData card = ChooseBlueCard();
if (card == null || CastleSpawnManager.Instance == null)
return false;
return SpawnBlueUnit(card, position, transform.rotation);
}
private CharacterData ChooseBlueCard()
{
CharacterData[] cards = new CharacterData[blueDeckDataNames.Length];
int validCount = 0;
foreach (string dataName in blueDeckDataNames)
{
CharacterData data = Resources.Load<CharacterData>("Data/" + dataName);
if (data != null && data.unitPrefab != null)
cards[validCount++] = data;
}
if (validCount == 0) return null;
return cards[Random.Range(0, validCount)];
}
private bool SpawnBlueUnit(CharacterData data, Vector3 position, Quaternion rotation)
{
GameObject unit = Instantiate(data.unitPrefab, position, rotation);
UnitTeam team = unit.GetComponent<UnitTeam>();
if (team != null)
{
team.teamId = 0;
unit.tag = "Friend";
}
UnitBase unitBase = unit.GetComponent<UnitBase>();
if (unitBase != null)
{
unitBase.InitializeFromPlayerData(data.GetMaxHealth(1), data.GetAttackPower(1), data.GetMoveSpeed(1));
}
return true;
}
}
@@ -0,0 +1,599 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// 塔防模式游戏流程控制器
/// 1. 开局进入布阵阶段:显示部署网格,玩家把底部英雄卡拖到格位上
/// 2. 点击"开始游戏"后收起布阵 UI、显示血条并按波次出兵
/// 3. 每波结束后英雄回到自己的格位(锚点)
/// </summary>
public class DefenseGameFlowController : MonoBehaviour
{
public static DefenseGameFlowController Instance { get; private set; }
[Header("引用")]
public DefenseWaveLevelController waveLevelController;
public Button startBattleButton;
public GameObject deploymentUI; // 布阵 UI(英雄卡片面板)
[Header("英雄配置")]
public HeroRoster roster;
public Transform heroParent; // 生成英雄的父节点(战场/英雄)
public int playerTeamId = 0;
[Header("布阵配置")]
public int maxHeroCount = 5;
public string deployGridObjectName = "部署网格"; // 战场 下的可见网格对象名
[Header("阵位守护")]
[Tooltip("英雄只在「离自己格位不超过寻敌视野 detectionRange」的范围内接敌,避免一路推出去。")]
public bool heroGuardPost = true;
// 布阵登记
private readonly Dictionary<int, int> cardToCell = new Dictionary<int, int>(); // 卡片序号 -> 格位
private readonly Dictionary<int, GameObject> cellToHero = new Dictionary<int, GameObject>(); // 格位 -> 英雄
private readonly Dictionary<int, HeroCardDrag> cardDrags = new Dictionary<int, HeroCardDrag>();
// 已生成英雄
private readonly List<GameObject> spawnedHeroes = new List<GameObject>();
private readonly Dictionary<GameObject, Vector3> heroInitialPositions = new Dictionary<GameObject, Vector3>();
private readonly List<GameObject> hiddenHeroBars = new List<GameObject>(); // 布阵阶段藏起来的血条
private GameObject deployGridRoot;
private bool battleStarted;
/// <summary>true = 还在布阵阶段(可以拖动卡牌)。</summary>
public bool IsDeployPhase => !battleStarted;
private void Awake()
{
Instance = this;
// 场上不应该有预先摆好的英雄
HideExistingHeroes();
}
private void Start()
{
if (waveLevelController != null)
{
waveLevelController.autoStart = false;
}
if (startBattleButton != null)
{
startBattleButton.onClick.AddListener(OnStartBattleClicked);
startBattleButton.interactable = true;
}
if (deploymentUI != null)
{
deploymentUI.SetActive(true);
}
SetupDeployGrid();
DeployHighlight.Ensure();
InitializeCardDrag();
Debug.Log("DefenseGameFlowController: 布阵阶段,把英雄卡拖到网格上。");
}
private void Update()
{
if (battleStarted) return;
// 布阵阶段:点击已经放好的英雄可以把他撤下来
if (!TryGetTap(out Vector2 screenPosition)) return;
if (IsPointerOverUI()) return;
var camera = Camera.main;
if (camera == null) return;
var ray = camera.ScreenPointToRay(screenPosition);
if (!Physics.Raycast(ray, out RaycastHit hit, 300f, ~0, QueryTriggerInteraction.Collide)) return;
var unit = hit.collider.GetComponentInParent<UnitBase>();
if (unit != null && spawnedHeroes.Contains(unit.gameObject))
{
RemoveHero(unit.gameObject);
}
}
private void OnDestroy()
{
if (Instance == this) Instance = null;
}
// ── 布阵阶段 ────────────────────────────────────────────
/// <summary>隐藏场景里预先摆好的英雄。</summary>
private void HideExistingHeroes()
{
foreach (var unit in FindObjectsOfType<UnitBase>(true))
{
unit.gameObject.SetActive(false);
}
}
/// <summary>找到可见的部署网格,摆正到 DeployGrid 定义的位置并显示。</summary>
private void SetupDeployGrid()
{
var battlefield = GameObject.Find("战场");
if (battlefield != null)
{
var child = battlefield.transform.Find(deployGridObjectName);
if (child != null) deployGridRoot = child.gameObject;
}
if (deployGridRoot == null)
{
deployGridRoot = GameObject.Find(deployGridObjectName);
}
if (deployGridRoot == null)
{
Debug.LogWarning($"DefenseGameFlowController: 没找到部署网格对象「{deployGridObjectName}」。");
return;
}
AlignGridCells(deployGridRoot.transform);
deployGridRoot.SetActive(true);
}
/// <summary>把网格下的格位按 DeployGrid 的坐标摆好,保证视觉与吸附完全一致。</summary>
private static void AlignGridCells(Transform gridRoot)
{
gridRoot.localPosition = new Vector3(0f, DeployGrid.GridY, 0f);
gridRoot.localRotation = Quaternion.identity;
int count = gridRoot.childCount;
if (count == 0) return;
for (int i = 0; i < count; i++)
{
var cell = gridRoot.GetChild(i);
int index = ParseCellIndex(cell.name, i);
if (!DeployGrid.IsValidIndex(index)) continue;
var pos = DeployGrid.CellPosition(index);
cell.localPosition = new Vector3(pos.x, 0f, pos.z);
}
}
/// <summary>格位命名形如「格位_3_4」(行_列,从 1 开始);解析失败时退回顺序索引。</summary>
private static int ParseCellIndex(string name, int fallback)
{
var parts = name.Split('_');
if (parts.Length >= 3
&& int.TryParse(parts[parts.Length - 2], out int row)
&& int.TryParse(parts[parts.Length - 1], out int col))
{
int r = row - 1;
int c = col - 1;
if (r >= 0 && r < DeployGrid.Rows && c >= 0 && c < DeployGrid.Cols)
{
return DeployGrid.ToIndex(r, c);
}
}
return fallback;
}
/// <summary>
/// 按玩家出战阵容(PlayerData)给底部每张英雄卡挂上拖动组件,并刷新卡面。
/// 站位不再从 PlayerData 读取(改成开局手动布阵),但「用哪几个角色」仍然读 PlayerData。
/// </summary>
private void InitializeCardDrag()
{
var canvasObj = GameObject.Find("TowerDefenseCanvas");
if (canvasObj == null)
{
Debug.LogWarning("DefenseGameFlowController: 没找到 TowerDefenseCanvas。");
return;
}
var heroPanel = canvasObj.transform.Find("BottomUI/HeroCardsPanel");
if (heroPanel == null)
{
Debug.LogWarning("DefenseGameFlowController: 没找到 BottomUI/HeroCardsPanel。");
return;
}
var deck = GetDeckCharacters();
for (int i = 0; i < maxHeroCount; i++)
{
var cardObj = heroPanel.Find($"HeroCard{i + 1}");
if (cardObj == null) continue;
var drag = cardObj.GetComponent<HeroCardDrag>();
if (drag == null)
{
drag = cardObj.gameObject.AddComponent<HeroCardDrag>();
}
var data = i < deck.Length ? deck[i] : null;
drag.Setup(i, data != null ? data.unitPrefab : null, data);
cardDrags[i] = drag;
// 卡面(头像 / 名字 / 稀有度)也和出战阵容保持一致
var cardUI = cardObj.GetComponent<HeroCardUI>();
if (cardUI != null && data != null)
{
cardUI.SetCharacterInfo(data);
}
}
Debug.Log($"DefenseGameFlowController: 已按出战阵容开启 {cardDrags.Count} 张卡片的拖动。");
}
/// <summary>
/// 读取玩家出战阵容(PlayerData.json 的 currentDeck),按槽位返回角色数据。
/// PlayerData 不可用时回退到场景里配置的 HeroRoster。
/// </summary>
private CharacterData[] GetDeckCharacters()
{
var deck = new CharacterData[maxHeroCount];
var allData = Resources.LoadAll<CharacterData>("Data");
if (PlayerDataManager.Instance != null)
{
for (int i = 0; i < maxHeroCount; i++)
{
var owned = PlayerDataManager.Instance.GetDeckCharacter(i);
if (owned == null || string.IsNullOrEmpty(owned.characterId)) continue;
foreach (var data in allData)
{
if (data == null || data.characterId != owned.characterId) continue;
if (data.unitPrefab == null) break; // 只有数据的角色(如荒野之王)不能出战
deck[i] = data;
break;
}
}
}
// 兜底:PlayerData 没给出阵容时用 HeroRoster
bool anyDeck = false;
foreach (var d in deck) if (d != null) { anyDeck = true; break; }
if (!anyDeck && roster != null && roster.heroes != null)
{
for (int i = 0; i < roster.heroes.Length && i < maxHeroCount; i++)
{
var entry = roster.heroes[i];
if (entry == null || entry.prefab == null) continue;
deck[i] = FindCharacterDataForPrefab(entry.prefab);
}
}
return deck;
}
/// <summary>把某张卡的英雄放到格位上。成功返回 true。</summary>
public bool PlaceHeroFromCard(int cardIndex, int cellIndex)
{
if (battleStarted) return false;
if (cardIndex < 0 || cardIndex >= maxHeroCount) return false;
if (!DeployGrid.IsValidIndex(cellIndex)) return false;
if (IsCardPlaced(cardIndex)) return false;
if (IsCellOccupied(cellIndex)) return false;
if (!cardDrags.TryGetValue(cardIndex, out var drag) || drag == null || drag.heroPrefab == null)
{
return false;
}
var hero = SpawnHero(drag.heroPrefab, cellIndex, cardIndex);
if (hero == null) return false;
cardToCell[cardIndex] = cellIndex;
cellToHero[cellIndex] = hero;
Debug.Log($"布阵: 第 {cardIndex + 1} 张卡 → 格位 {cellIndex} ({hero.name})");
return true;
}
/// <summary>撤销某个格位上的英雄,并让对应卡片恢复可用。</summary>
public void RemoveHeroAt(int cellIndex)
{
if (cellToHero.TryGetValue(cellIndex, out var hero) && hero != null)
{
RemoveHero(hero);
}
}
private void RemoveHero(GameObject hero)
{
int cardIndex = -1;
foreach (var kv in cardToCell)
{
if (cellToHero.TryGetValue(kv.Value, out var h) && h == hero)
{
cardIndex = kv.Key;
break;
}
}
if (cardIndex >= 0)
{
cardToCell.Remove(cardIndex);
if (cardDrags.TryGetValue(cardIndex, out var drag) && drag != null)
{
drag.SetPlaced(false);
}
}
foreach (var kv in new List<KeyValuePair<int, GameObject>>(cellToHero))
{
if (kv.Value == hero) cellToHero.Remove(kv.Key);
}
spawnedHeroes.Remove(hero);
heroInitialPositions.Remove(hero);
Debug.Log($"撤回英雄: {hero.name}");
Destroy(hero);
}
public bool IsCellOccupied(int cellIndex)
{
return cellToHero.TryGetValue(cellIndex, out var hero) && hero != null;
}
public bool IsCardPlaced(int cardIndex)
{
return cardToCell.ContainsKey(cardIndex);
}
public int GetCellOfCard(int cardIndex)
{
return cardToCell.TryGetValue(cardIndex, out int cell) ? cell : -1;
}
// ── 开战 ────────────────────────────────────────────────
private void OnStartBattleClicked()
{
if (battleStarted) return;
battleStarted = true;
if (deploymentUI != null) deploymentUI.SetActive(false);
if (startBattleButton != null) startBattleButton.gameObject.SetActive(false);
if (deployGridRoot != null) deployGridRoot.SetActive(false);
if (DeployHighlight.Instance != null) DeployHighlight.Instance.Hide();
// 卡片从"待放置"切换成"英雄状态显示":恢复满不透明度
foreach (var drag in cardDrags.Values)
{
if (drag != null) drag.SetPlaced(false);
}
// 一个都没布置时用默认阵型兜底
if (cellToHero.Count == 0)
{
SpawnDefaultFormation();
}
// 布阵阶段藏起来的血条,开战后显示
foreach (var barObject in hiddenHeroBars)
{
if (barObject != null) barObject.SetActive(true);
}
Debug.Log("DefenseGameFlowController: 开始游戏,出兵!");
StartCoroutine(StartBattleSequence());
}
private IEnumerator StartBattleSequence()
{
yield return new WaitForSeconds(0.5f);
if (waveLevelController != null)
{
StartCoroutine(waveLevelController.StartLevel());
}
}
// ── 生成英雄 ────────────────────────────────────────────
private GameObject SpawnHero(GameObject prefab, int cell, int cardIndex)
{
var characterData = FindCharacterDataForPrefab(prefab);
var spawnPos = DeployGrid.CellPosition(cell);
var unit = Instantiate(prefab, spawnPos, Quaternion.identity);
unit.name = "英雄_" + prefab.name;
if (heroParent != null)
{
unit.transform.SetParent(heroParent, true);
}
var team = unit.GetComponent<UnitTeam>();
if (team != null) team.teamId = playerTeamId;
var unitBase = unit.GetComponent<UnitBase>();
if (unitBase != null)
{
unitBase.returnToAnchorWhenIdle = true;
unitBase.anchorArrivalDistance = 0.35f;
// 阵位守护:接敌范围直接用单位自己的寻敌视野 detectionRange
unitBase.useAnchorLeash = heroGuardPost;
unitBase.ResetAnchorPosition();
}
if (characterData != null && unitBase != null)
{
var playerData = PlayerDataManager.Instance != null
? PlayerDataManager.Instance.GetCharacterData(characterData.characterId)
: null;
int level = playerData != null ? playerData.level : 1;
unitBase.InitializeFromPlayerData(
characterData.GetMaxHealth(level),
characterData.GetAttackPower(level),
characterData.GetMoveSpeed(level));
}
var healthBar = unit.GetComponent<WorldHealthBar>();
if (healthBar != null)
{
if (playerTeamId == 0) healthBar.canRevive = true;
// 布阵阶段把血条收起来,开战时再显示(血条是 WorldHealthBar.Awake 生成的子物体)
var barObject = unit.transform.Find("WorldHealthBar");
if (barObject != null && !battleStarted)
{
barObject.gameObject.SetActive(false);
hiddenHeroBars.Add(barObject.gameObject);
}
}
spawnedHeroes.Add(unit);
heroInitialPositions[unit] = spawnPos;
BindHeroToCard(unit, characterData, spawnPos, cardIndex);
return unit;
}
/// <summary>一张都没摆时用出战阵容按"近战前排 / 远程后排"兜底。</summary>
private void SpawnDefaultFormation()
{
var deck = GetDeckCharacters();
int meleeTotal = 0, rangedTotal = 0;
for (int i = 0; i < deck.Length; i++)
{
if (deck[i] == null) continue;
if (IsMelee(deck[i])) meleeTotal++; else rangedTotal++;
}
var used = new HashSet<int>();
int meleeIndex = 0, rangedIndex = 0, spawned = 0;
for (int i = 0; i < deck.Length && spawned < maxHeroCount; i++)
{
var data = deck[i];
if (data == null || data.unitPrefab == null) continue;
int cell = IsMelee(data)
? DeployGrid.DefaultCell(true, meleeIndex++, meleeTotal)
: DeployGrid.DefaultCell(false, rangedIndex++, rangedTotal);
if (used.Contains(cell)) cell = NextFreeCell(cell, used);
if (cell < 0) continue;
used.Add(cell);
if (SpawnHero(data.unitPrefab, cell, i) == null) continue;
cellToHero[cell] = spawnedHeroes[spawnedHeroes.Count - 1];
spawned++;
}
}
/// <summary>判定角色是否近战(远程单位 attackType 为 RangedProjectile)。</summary>
private static bool IsMelee(CharacterData data)
{
// 用"是否配置了投射物"来判定更可靠:RangedProjectile 一定有 projectilePrefab
return data.unitPrefab == null || data.unitPrefab.GetComponent<UnitBase>()?.attackType != UnitBase.AttackType.RangedProjectile;
}
private static int NextFreeCell(int from, HashSet<int> used)
{
for (int i = 0; i < DeployGrid.CellCount; i++)
{
int candidate = (from + i) % DeployGrid.CellCount;
if (!used.Contains(candidate)) return candidate;
}
return -1;
}
/// <summary>把英雄绑到它来源的那张卡片上(显示名字 / 稀有度 / 血条)。</summary>
private void BindHeroToCard(GameObject heroUnit, CharacterData characterData, Vector3 spawnPos, int cardIndex)
{
if (cardIndex < 0 || cardIndex >= maxHeroCount) return;
if (!cardDrags.TryGetValue(cardIndex, out var drag) || drag == null) return;
var cardUI = drag.GetComponent<HeroCardUI>();
var unitBase = heroUnit.GetComponent<UnitBase>();
if (cardUI == null || unitBase == null) return;
var icon = characterData != null ? characterData.cardIcon : null;
cardUI.SetHero(unitBase, icon, spawnPos, characterData);
}
/// <summary>
/// 一波清完后让英雄回格位。
/// 正常情况放开目标,让 UnitBase 自己走回格位;只有寻路真的不可用(卡住)时才瞬移兜底。
/// </summary>
public void ResetHeroPositions()
{
foreach (var hero in spawnedHeroes)
{
if (hero == null || !hero.activeSelf) continue;
var unitBase = hero.GetComponent<UnitBase>();
var navAgent = hero.GetComponent<UnityEngine.AI.NavMeshAgent>();
bool canWalk = UnityEngine.AI.NavMesh.SamplePosition(
hero.transform.position, out _, 2f, UnityEngine.AI.NavMesh.AllAreas);
if (navAgent != null && navAgent.enabled && canWalk)
{
if (unitBase != null) unitBase.target = null; // 放开目标,进入回位
navAgent.isStopped = false;
navAgent.ResetPath();
continue;
}
// 卡住了(比如被挤出 NavMesh)才直接摆回格位
if (heroInitialPositions.TryGetValue(hero, out var initialPos))
{
Debug.LogWarning($"DefenseGameFlowController: {hero.name} 寻路不可用,瞬移回格位");
hero.transform.position = initialPos;
if (navAgent != null) navAgent.nextPosition = initialPos;
if (unitBase != null) unitBase.ResetAnchorPosition();
}
}
}
// ── 工具 ────────────────────────────────────────────────
private CharacterData FindCharacterDataForPrefab(GameObject prefab)
{
if (prefab == null) return null;
foreach (var data in Resources.LoadAll<CharacterData>("Data"))
{
if (data != null && data.unitPrefab == prefab) return data;
}
return null;
}
private static bool TryGetTap(out Vector2 screenPosition)
{
if (Input.GetMouseButtonDown(0))
{
screenPosition = Input.mousePosition;
return true;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
screenPosition = Input.GetTouch(0).position;
return true;
}
screenPosition = default;
return false;
}
private static bool IsPointerOverUI()
{
if (EventSystem.current == null) return false;
if (Input.touchCount > 0)
{
return EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId);
}
return EventSystem.current.IsPointerOverGameObject();
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: C31KvXn8U39bDHLWfJ9jSgBhWvoLhWw7Xvmn2WmpmrFDtWho+cg1kak=
guid: B38YtCOpVSouc7G4rbHoBIMoJtLgT79fWI15sJ401Ny0ANjUOgZHriM=
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -59,6 +59,19 @@ public class DefenseWaveLevelController : MonoBehaviour
if (autoStart) StartCoroutine(RunLevel());
}
/// <summary>
/// 外部调用:开始关卡(用于游戏流程控制)
/// </summary>
public IEnumerator StartLevel()
{
if (level == null || level.waves == null || level.waves.Length == 0)
{
Debug.LogError("DefenseWaveLevelController: 无法启动关卡,波次数据未加载");
yield break;
}
yield return RunLevel();
}
private bool LoadLevel()
{
if (levelJson == null)
@@ -88,12 +101,29 @@ public class DefenseWaveLevelController : MonoBehaviour
yield return new WaitForSeconds(Mathf.Max(0f, wave.restBeforeWave));
yield return SpawnWave(wave);
yield return WaitForWaveCleared();
// 每波结束后,让英雄回到初始位置
ResetHeroPositionsAfterWave();
yield return new WaitForSeconds(Mathf.Max(0f, waveCompleteDelay));
}
UpdateProgress(level.waves.Length, level.waves.Length, "通关");
Debug.Log("DefenseWaveLevelController: 第一关波次全部完成。");
}
/// <summary>
/// 每波结束后让英雄回到初始位置
/// </summary>
private void ResetHeroPositionsAfterWave()
{
DefenseGameFlowController flowController = FindObjectOfType<DefenseGameFlowController>();
if (flowController != null)
{
flowController.ResetHeroPositions();
Debug.Log($"第 {currentWaveIndex + 1} 波结束,英雄回到初始位置");
}
}
private IEnumerator SpawnWave(WaveEntry wave)
{
-8
View File
@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: Xn9Nt3upASmCnjf71jScieVdxk5dt59IaBF5ly41RuZmaEtbnQfCoRA=
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
-127
View File
@@ -1,127 +0,0 @@
using UnityEngine;
using UnityEngine.UI;
public class Barracks : MonoBehaviour
{
[Header("角色数据")]
public CharacterData characterData;
[Header("阵营")]
public string unitTag = "Friend";
[Header("生产设置")]
public Transform spawnPoint; // 单位生成位置
[Header("UI显示")]
public Image progressBar; // 生产进度条
public Image characterIcon; // 角色头像
private float spawnTimer = 0f;
private float spawnInterval = 5f;
private bool isProducing = true;
private BuildingHealth buildingHealth;
private void Start()
{
buildingHealth = GetComponent<BuildingHealth>();
if (characterData != null)
{
spawnInterval = 5f;
// 设置角色头像
if (characterIcon != null && characterData.cardIcon != null)
{
characterIcon.sprite = characterData.cardIcon;
}
Debug.Log($"兵营初始化:{characterData.characterName},生产间隔{spawnInterval}秒");
}
else
{
Debug.LogError("兵营缺少角色数据!");
}
// 如果没有spawnPoint,使用兵营自己的位置
if (spawnPoint == null)
{
spawnPoint = transform;
}
}
private void Update()
{
if (!isProducing || characterData == null) return;
spawnTimer += Time.deltaTime;
// 更新进度条
if (buildingHealth != null)
{
buildingHealth.SetProductionProgress(spawnTimer / spawnInterval);
}
else if (progressBar != null)
{
progressBar.fillAmount = spawnTimer / spawnInterval;
}
// 时间到,生产单位
if (spawnTimer >= spawnInterval)
{
SpawnUnit();
spawnTimer = 0f;
}
}
private void SpawnUnit()
{
if (characterData == null || characterData.unitPrefab == null)
{
Debug.LogError("无法生产单位:缺少unitPrefab");
return;
}
// 在spawnPoint位置生成单位
Vector3 spawnPosition = spawnPoint.position;
GameObject unit = Instantiate(characterData.unitPrefab, spawnPosition, Quaternion.identity);
unit.name = characterData.characterName;
unit.tag = unitTag;
// 设置阵营 ID(根据建筑的阵营)
var buildingTeam = GetComponent<UnitTeam>();
var unitTeam = unit.GetComponent<UnitTeam>();
if (buildingTeam != null && unitTeam != null)
{
unitTeam.teamId = buildingTeam.teamId;
}
else if (unitTeam != null)
{
// 兜底:根据 tag 判断阵营
unitTeam.teamId = unitTag == "Friend" ? 0 : 1;
}
// 如果单位有UnitBase组件,使用1级属性初始化
var unitBase = unit.GetComponent<UnitBase>();
if (unitBase != null)
{
float health = characterData.GetMaxHealth(1);
float attack = characterData.GetAttackPower(1);
float moveSpeed = characterData.GetMoveSpeed(1);
unitBase.InitializeFromPlayerData(health, attack, moveSpeed);
}
Debug.Log($"兵营生产了单位:{characterData.characterName}");
}
public void StopProduction()
{
isProducing = false;
}
public void StartProduction()
{
isProducing = true;
}
}
@@ -1,42 +0,0 @@
using UnityEngine;
[RequireComponent(typeof(WorldHealthBar))]
public class BuildingHealth : MonoBehaviour
{
[Header("建筑生命")]
[Min(1f)] public float maxHealth = 500f;
private WorldHealthBar healthBar;
public float ProductionProgress => healthBar != null
? Mathf.Clamp01(healthBar.currentEnergy / healthBar.maxEnergy)
: 0f;
private void Awake()
{
healthBar = GetComponent<WorldHealthBar>();
healthBar.maxHealth = maxHealth;
healthBar.currentHealth = maxHealth;
healthBar.maxEnergy = 1f;
healthBar.currentEnergy = 0f;
}
public void SetProductionProgress(float normalizedProgress)
{
if (healthBar != null)
{
healthBar.SetEnergy(Mathf.Clamp01(normalizedProgress) * healthBar.maxEnergy);
}
}
public bool ReceiveDamage(float damage, GameObject attacker = null)
{
if (damage <= 0f || healthBar == null)
{
return false;
}
healthBar.TakeDamage(damage);
return true;
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: BihN5y6lVy1x4afDhOwE5/fM5e8mWQXxroZfuMUij3yrzemt9JIHQDA=
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,242 +0,0 @@
using System.Collections.Generic;
using UnityEngine;
public class BuildingPlacementManager : MonoBehaviour
{
public static BuildingPlacementManager Instance { get; private set; }
[Header("建筑预制体")]
public GameObject barracksBasePrefab;
public GameObject goldMinePrefab;
[Header("虚拟网格")]
[Min(0.1f)] public float gridSize = 1f;
[Min(0f)] public float placementHeight;
public Vector2 xLimits = new Vector2(-7f, 7f);
public Vector2 zLimits = new Vector2(-10f, 20f);
[Header("放置检测")]
public LayerMask groundLayer;
[Min(0.1f)] public float buildingRadius = 1.25f;
public Material previewMaterialValid;
public Material previewMaterialInvalid;
private readonly List<GameObject> placedBuildings = new List<GameObject>();
private CharacterData currentCharacterData;
private GameObject currentPrefab;
private GameObject previewObject;
private Camera mainCamera;
private bool isPlacing;
private bool previewPositionValid;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
}
private void Start()
{
mainCamera = Camera.main;
RegisterExistingBuilding(GameObject.Find("主要基地"));
}
private void Update()
{
if (!isPlacing)
{
return;
}
UpdatePreview();
if (Input.GetMouseButtonDown(0))
{
TryPlaceBuilding();
}
else if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
{
CancelPlacement();
}
}
public void StartPlacingBuilding(CharacterData data)
{
if (data == null)
{
return;
}
StartPlacingBarracks(data);
}
public void StartPlacingBarracks(CharacterData data)
{
if (data == null || barracksBasePrefab == null)
{
Debug.LogError("兵营预制体或角色数据为空");
return;
}
BeginPlacement(barracksBasePrefab, data);
}
public void StartPlacingGoldMine()
{
if (goldMinePrefab == null)
{
Debug.LogError("金矿预制体为空");
return;
}
BeginPlacement(goldMinePrefab, null);
}
private void BeginPlacement(GameObject prefab, CharacterData data)
{
CancelPlacement();
currentPrefab = prefab;
currentCharacterData = data;
previewObject = Instantiate(prefab);
previewObject.name = "建筑预览";
SetCollidersEnabled(previewObject, false);
isPlacing = true;
mainCamera = Camera.main;
}
private void UpdatePreview()
{
if (previewObject == null || mainCamera == null)
{
return;
}
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
int mask = groundLayer.value == 0 ? Physics.DefaultRaycastLayers : groundLayer.value;
if (!Physics.Raycast(ray, out RaycastHit hit, 1000f, mask))
{
previewPositionValid = false;
return;
}
Vector3 position = hit.point;
position.x = Mathf.Round(position.x / gridSize) * gridSize;
position.y = placementHeight;
position.z = Mathf.Round(position.z / gridSize) * gridSize;
previewObject.transform.position = position;
previewPositionValid = IsValidPosition(position);
UpdatePreviewMaterial(previewPositionValid);
}
private bool IsValidPosition(Vector3 position)
{
if (position.x < xLimits.x || position.x > xLimits.y ||
position.z < zLimits.x || position.z > zLimits.y)
{
return false;
}
foreach (GameObject building in placedBuildings)
{
if (building != null && Vector3.Distance(position, building.transform.position) < buildingRadius * 2f)
{
return false;
}
}
return true;
}
private void TryPlaceBuilding()
{
if (!previewPositionValid || previewObject == null || currentPrefab == null)
{
return;
}
int cost = 100;
if (GoldSystem.Instance == null || !GoldSystem.Instance.TrySpend(cost))
{
Debug.Log($"金币不足,需要{cost}");
return;
}
Vector3 position = previewObject.transform.position;
GameObject building = Instantiate(currentPrefab, position, Quaternion.identity);
building.name = currentCharacterData != null
? "兵营_" + currentCharacterData.characterName
: "金矿";
building.tag = "Friend";
if (building.GetComponent<BuildingHealth>() == null)
{
building.AddComponent<BuildingHealth>();
}
if (currentCharacterData != null)
{
Barracks barracks = building.GetComponent<Barracks>();
if (barracks == null)
{
barracks = building.AddComponent<Barracks>();
}
barracks.characterData = currentCharacterData;
}
else if (building.GetComponent<GoldMine>() == null)
{
building.AddComponent<GoldMine>();
}
placedBuildings.Add(building);
CancelPlacement();
}
private void UpdatePreviewMaterial(bool valid)
{
Material material = valid ? previewMaterialValid : previewMaterialInvalid;
if (material == null || previewObject == null)
{
return;
}
foreach (Renderer renderer in previewObject.GetComponentsInChildren<Renderer>())
{
renderer.sharedMaterial = material;
}
}
private void SetCollidersEnabled(GameObject target, bool enabled)
{
foreach (Collider collider in target.GetComponentsInChildren<Collider>())
{
collider.enabled = enabled;
}
}
public void RegisterExistingBuilding(GameObject building)
{
if (building != null && !placedBuildings.Contains(building))
{
placedBuildings.Add(building);
}
}
public void CancelPlacement()
{
if (previewObject != null)
{
Destroy(previewObject);
}
previewObject = null;
currentPrefab = null;
currentCharacterData = null;
isPlacing = false;
previewPositionValid = false;
}
}
@@ -1,75 +0,0 @@
using UnityEngine;
/// <summary>
/// 拆家版出兵管理:从城堡前方出兵点生成单位,消耗能量。
/// 替代旧的召唤师身边召唤逻辑。
/// </summary>
public class CastleSpawnManager : MonoBehaviour
{
public static CastleSpawnManager Instance { get; private set; }
[Header("出兵点(城堡前方)")]
public Transform blueSpawnPoint;
public Transform redSpawnPoint;
private void Awake()
{
if (Instance != null && Instance != this) { Destroy(gameObject); return; }
Instance = this;
}
/// <summary>从指定阵营城堡出兵,消耗能量。teamId: 0=蓝, 1=红。</summary>
public bool TrySpawn(CharacterData data, int teamId)
{
if (data == null || data.unitPrefab == null) return false;
Transform spawnPoint = teamId == 0 ? blueSpawnPoint : redSpawnPoint;
if (spawnPoint == null)
{
Debug.LogError($"找不到 teamId={teamId} 的出兵点");
return false;
}
Vector3 direction = spawnPoint.forward;
direction.y = 0f;
if (direction.sqrMagnitude < 0.001f) direction = Vector3.forward;
direction.Normalize();
GameObject unit = Instantiate(
data.unitPrefab,
spawnPoint.position,
Quaternion.LookRotation(direction, Vector3.up));
// 设置阵营
UnitTeam unitTeam = unit.GetComponent<UnitTeam>();
if (unitTeam != null)
{
unitTeam.teamId = teamId;
unit.tag = teamId == 0 ? "Friend" : "Enemy";
}
// 初始化属性(默认 1 级)
UnitBase unitBase = unit.GetComponent<UnitBase>();
if (unitBase != null)
{
PlayerCharacterData playerData = new PlayerCharacterData("castle_spawn", 1, 0);
float hp = playerData.GetMaxHealth(data);
float atk = playerData.GetAttackPower(data);
float spd = playerData.GetMoveSpeed(data);
unitBase.InitializeFromPlayerData(hp, atk, spd);
// 如果是敌人(teamId != 0),增大寻敌距离
if (teamId != 0)
{
unitBase.detectionRange = 100f; // 从默认30增加到100
}
}
Debug.Log($"城堡出兵: {data.characterName} teamId={teamId} pos={spawnPoint.position}");
return true;
}
public bool CanAfford(CharacterData data)
{
return data != null && data.unitPrefab != null;
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: Wysc5i6rUC+IV5FZ0/o3UMuAsbu+kExxLM4sa7XnkVGb+y85mKtxqMU=
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,277 +0,0 @@
using UnityEngine;
using UnityEngine.UI;
public class CrystalMineCapture : MonoBehaviour
{
[Header("争夺进度")]
[Range(0f, 100f)] public float blueProgress = 50f;
[Min(0f)] public float capturePerDamage = 1f;
[Tooltip("是否自动添加中立阵营(teamId=2)。拆家版关闭以避免单位自动锁敌水晶矿")]
public bool autoSetNeutralTeam = true;
[Header("资源产出")]
[Tooltip("占领后是否按固定间隔给所属阵营增加能量")]
public bool producesMana = true;
[Min(1f)] public float manaInterval = 8f;
[Min(1)] public int manaAmount = 1;
// [SerializeField] private AdjustableGlowOrb glowOrb; // 已删除,视觉效果脚本未使用
// [Tooltip("混沌裂缝传送门(随机出兵点),随争夺进度同步变色,可留空")]
// [SerializeField] private RiftPortalVisual riftVisual; // 已删除,视觉效果脚本未使用
[SerializeField] private Color neutralColor = Color.white;
[SerializeField] private Color blueColor = new Color(0.1f, 0.65f, 1f, 1f);
[SerializeField] private Color redColor = new Color(1f, 0.12f, 0.08f, 1f);
[Header("世界血条")]
[Tooltip("血条相对模型渲染包围盒顶部的额外高度")]
[Min(0f)] public float heightOffset = 1.1f;
[Min(0.1f)] public float barWidth = 5f;
private float? barHeightCache;
private Canvas canvas;
private RectTransform barRect;
private Image blueFill;
private Image redFill;
private RectTransform marker;
private UnitTeam unitTeam;
private float manaTimer;
private static Sprite roundedSprite;
private static readonly int BaseColorId = Shader.PropertyToID("_Color");
private void Awake()
{
if (autoSetNeutralTeam)
{
unitTeam = GetComponent<UnitTeam>();
if (unitTeam == null)
{
unitTeam = gameObject.AddComponent<UnitTeam>();
}
unitTeam.teamId = 2;
}
else
{
unitTeam = GetComponent<UnitTeam>();
}
// if (glowOrb == null)
// {
// glowOrb = GetComponentInChildren<AdjustableGlowOrb>(true);
// }
// if (riftVisual == null)
// {
// riftVisual = GetComponentInChildren<RiftPortalVisual>(true);
// }
CreateCaptureBar();
RefreshVisuals();
}
private void Start()
{
// 强制重置光球颜色为初始状态
// if (glowOrb != null)
// {
// glowOrb.SetGlowColor(neutralColor);
// }
RefreshVisuals();
}
private void LateUpdate()
{
UpdateManaProduction();
if (barRect == null) return;
barRect.position = transform.position + Vector3.up * GetBarHeight();
barRect.localScale = Vector3.one * (barWidth / 100f);
Camera camera = Camera.main;
if (camera != null)
{
barRect.rotation = Quaternion.LookRotation(camera.transform.position - barRect.position, camera.transform.up);
}
}
// 血条高度 = 所有子 Renderer 包围盒顶部 + heightOffset。
// 水晶矿模型根节点是 100 倍缩放,高度必须按世界包围盒算,否则血条会埋进模型内部被遮挡。
private float GetBarHeight()
{
if (barHeightCache.HasValue) return barHeightCache.Value;
float top = 0f;
Renderer[] renderers = GetComponentsInChildren<Renderer>();
if (renderers.Length > 0)
{
Bounds bounds = renderers[0].bounds;
for (int i = 1; i < renderers.Length; i++)
{
bounds.Encapsulate(renderers[i].bounds);
}
top = bounds.max.y;
}
barHeightCache = top - transform.position.y + heightOffset;
return barHeightCache.Value;
}
private void UpdateManaProduction()
{
if (!producesMana || unitTeam == null || (unitTeam.teamId != 0 && unitTeam.teamId != 1))
{
manaTimer = 0f;
return;
}
manaTimer += Time.deltaTime;
while (manaTimer >= manaInterval)
{
manaTimer -= manaInterval;
if (unitTeam.teamId == 0)
{
ManaSystem.Instance?.AddMana(manaAmount);
}
else
{
EnemyCastleAI enemyAI = FindObjectOfType<EnemyCastleAI>();
enemyAI?.AddMana(manaAmount);
}
}
}
public bool ReceiveCaptureDamage(float damage, GameObject attacker)
{
if (damage <= 0f || attacker == null) return false;
UnitTeam attackerTeam = attacker.GetComponentInParent<UnitTeam>();
if (attackerTeam == null) return false;
float direction = attackerTeam.teamId == 0 ? 1f : -1f;
blueProgress = Mathf.Clamp(blueProgress + direction * damage * capturePerDamage, 0f, 100f);
RefreshVisuals();
return true;
}
public void SetCaptureProgress(float value)
{
blueProgress = Mathf.Clamp(value, 0f, 100f);
RefreshVisuals();
}
private void RefreshVisuals()
{
if (blueFill == null || redFill == null) return;
float blue = blueProgress / 100f;
blueFill.fillAmount = blue;
redFill.fillAmount = 1f - blue;
marker.anchorMin = new Vector2(blue, 0f);
marker.anchorMax = new Vector2(blue, 1f);
marker.anchoredPosition = Vector2.zero;
marker.gameObject.SetActive(blueProgress > 0f && blueProgress < 100f);
if (unitTeam != null)
{
unitTeam.teamId = blueProgress >= 100f ? 0 : blueProgress <= 0f ? 1 : 2;
}
// if (glowOrb != null)
// {
// Color color = blueProgress >= 100f ? blueColor : blueProgress <= 0f ? redColor : neutralColor;
// glowOrb.SetGlowColor(color);
// }
// if (riftVisual != null)
// {
// Color riftColor = blueProgress >= 100f ? blueColor : blueProgress <= 0f ? redColor : neutralColor;
// riftVisual.SetRiftColor(riftColor);
// }
}
private void CreateCaptureBar()
{
GameObject root = new GameObject("CrystalCaptureBar", typeof(RectTransform), typeof(Canvas), typeof(CanvasScaler));
// 血条 Canvas 不要作为水晶矿的子对象,直接放在场景根目录避免被 100 倍缩放影响
root.transform.parent = null;
canvas = root.GetComponent<Canvas>();
canvas.renderMode = RenderMode.WorldSpace;
canvas.sortingOrder = 110;
root.GetComponent<CanvasScaler>().dynamicPixelsPerUnit = 10f;
barRect = root.GetComponent<RectTransform>();
barRect.sizeDelta = new Vector2(100f, 16f);
barRect.localScale = Vector3.one * (barWidth / 100f);
RectTransform row = CreateRow(root.transform, "CaptureBar", 100f, 14f);
Image background = CreateImage(row, "Background", new Color(0.03f, 0.03f, 0.03f, 0.95f));
SetFullRect(background.rectTransform);
blueFill = CreateImage(row, "BlueFill", blueColor);
blueFill.type = Image.Type.Filled;
blueFill.fillMethod = Image.FillMethod.Horizontal;
blueFill.fillOrigin = 0;
blueFill.rectTransform.anchorMin = new Vector2(0f, 0f);
blueFill.rectTransform.anchorMax = new Vector2(1f, 1f);
blueFill.rectTransform.offsetMin = new Vector2(2f, 2f);
blueFill.rectTransform.offsetMax = new Vector2(-2f, -2f);
redFill = CreateImage(row, "RedFill", redColor);
redFill.type = Image.Type.Filled;
redFill.fillMethod = Image.FillMethod.Horizontal;
redFill.fillOrigin = 1;
redFill.rectTransform.anchorMin = new Vector2(0f, 0f);
redFill.rectTransform.anchorMax = new Vector2(1f, 1f);
redFill.rectTransform.offsetMin = new Vector2(2f, 2f);
redFill.rectTransform.offsetMax = new Vector2(-2f, -2f);
GameObject markerObject = new GameObject("CenterMarker", typeof(RectTransform), typeof(Image));
markerObject.transform.SetParent(row, false);
marker = markerObject.GetComponent<RectTransform>();
marker.sizeDelta = new Vector2(2f, 14f);
marker.anchorMin = new Vector2(0.5f, 0f);
marker.anchorMax = new Vector2(0.5f, 1f);
marker.GetComponent<Image>().color = Color.white;
}
private RectTransform CreateRow(Transform parent, string name, float width, float height)
{
GameObject rowObject = new GameObject(name, typeof(RectTransform));
rowObject.transform.SetParent(parent, false);
RectTransform rect = rowObject.GetComponent<RectTransform>();
rect.anchorMin = new Vector2(0.5f, 0.5f);
rect.anchorMax = new Vector2(0.5f, 0.5f);
rect.pivot = new Vector2(0.5f, 0.5f);
rect.sizeDelta = new Vector2(width, height);
return rect;
}
private Image CreateImage(Transform parent, string name, Color color)
{
GameObject imageObject = new GameObject(name, typeof(RectTransform), typeof(Image));
imageObject.transform.SetParent(parent, false);
Image image = imageObject.GetComponent<Image>();
image.sprite = RoundedSprite();
image.color = color;
return image;
}
private void SetFullRect(RectTransform rect)
{
rect.anchorMin = Vector2.zero;
rect.anchorMax = Vector2.one;
rect.offsetMin = Vector2.zero;
rect.offsetMax = Vector2.zero;
}
private static Sprite RoundedSprite()
{
if (roundedSprite != null) return roundedSprite;
Texture2D texture = new Texture2D(1, 1, TextureFormat.RGBA32, false);
texture.SetPixel(0, 0, Color.white);
texture.Apply();
roundedSprite = Sprite.Create(texture, new Rect(0f, 0f, 1f, 1f), new Vector2(0.5f, 0.5f));
roundedSprite.name = "CrystalCaptureBarSprite";
return roundedSprite;
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: Ci8f4Sz/WnrXjU/p2xyMiRtWkh/2zF5+zBmBE7FvT1PHEvOb80JVbxc=
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,284 +0,0 @@
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 红方城堡自动出兵 AI:独立能量、三路目标评估、低费试探与高费推进。
/// </summary>
public class EnemyCastleAI : MonoBehaviour
{
[Header("AI 阵营")]
[SerializeField] private int teamId = 1;
[SerializeField] private int maxMana = 10;
[SerializeField] private float manaRegenInterval = 3f;
[SerializeField] private float decisionInterval = 1.2f;
[SerializeField] private float minimumSpawnInterval = 1.4f;
[Header("三路目标")]
[SerializeField] private float leftLaneX = -10f;
[SerializeField] private float centerLaneX = 0f;
[SerializeField] private float rightLaneX = 10f;
[SerializeField] private float laneHalfWidth = 4f;
[SerializeField] private float resourceThreatWeight = 2.5f;
[SerializeField] private float enemyUnitWeight = 1.5f;
[SerializeField] private float laneRotationWeight = 0.7f;
[Header("卡组轮转")]
[SerializeField] private int visibleSlotCount = 4;
[SerializeField] private string[] deckDataNames =
{
"人类士兵Data", "弓箭手Data", "盾卫Data", "精锐士兵Data",
"法师Data", "狂暴狼人Data", "石巨人Data", "弓箭猎人Data"
};
[SerializeField] private CharacterData[] deck;
private readonly List<CharacterData> hand = new List<CharacterData>();
private readonly Queue<CharacterData> drawQueue = new Queue<CharacterData>();
private Transform redSpawnPoint;
private AttackTargetController targetController;
private float currentMana;
private float manaTimer;
private float decisionTimer;
private float spawnTimer;
private int lastLane = 1;
private int consecutiveLaneSpawns;
private bool initialized;
private void Start()
{
redSpawnPoint = FindRedSpawnPoint();
targetController = AttackTargetController.Instance;
currentMana = maxMana;
BuildDeck();
BuildHand();
initialized = redSpawnPoint != null && hand.Count > 0;
if (!initialized)
Debug.LogWarning("EnemyCastleAI 初始化失败:缺少红方出兵点或角色卡组。");
}
private void Update()
{
if (!initialized) return;
manaTimer += Time.deltaTime;
while (manaTimer >= manaRegenInterval)
{
manaTimer -= manaRegenInterval;
currentMana = Mathf.Min(maxMana, currentMana + 1f);
}
decisionTimer -= Time.deltaTime;
spawnTimer -= Time.deltaTime;
if (decisionTimer > 0f || spawnTimer > 0f) return;
decisionTimer = decisionInterval;
int lane = ChooseLane();
CharacterData card = ChooseCard(lane);
if (card == null) return;
if (Spawn(card, lane))
{
currentMana -= 1f;
RotatePlayedCard(card);
spawnTimer = minimumSpawnInterval;
if (lane == lastLane) consecutiveLaneSpawns++;
else consecutiveLaneSpawns = 1;
lastLane = lane;
}
}
public void AddMana(int amount)
{
if (amount <= 0) return;
currentMana = Mathf.Min(maxMana, currentMana + amount);
}
public int CurrentMana => Mathf.FloorToInt(currentMana);
public bool TrySpawnFromPortal(Vector3 spawnPosition, Quaternion spawnRotation)
{
if (!initialized) return false;
List<CharacterData> affordable = new List<CharacterData>();
foreach (CharacterData card in hand)
{
if (card != null && card.unitPrefab != null)
affordable.Add(card);
}
if (affordable.Count == 0) return false;
CharacterData selected = affordable[Random.Range(0, affordable.Count)];
if (!SpawnAt(selected, spawnPosition, spawnRotation, teamId)) return false;
currentMana -= 1f;
RotatePlayedCard(selected);
return true;
}
private CharacterData SelectWeightedCard(List<CharacterData> cards)
{
return cards == null || cards.Count == 0 ? null : cards[Random.Range(0, cards.Count)];
}
private int ChooseLane()
{
float left = ScoreLane(leftLaneX, 0);
float center = ScoreLane(centerLaneX, 1);
float right = ScoreLane(rightLaneX, 2);
if (consecutiveLaneSpawns >= 2)
{
if (lastLane == 0) left -= 3f;
if (lastLane == 1) center -= 3f;
if (lastLane == 2) right -= 3f;
}
float best = Mathf.Max(left, Mathf.Max(center, right));
if (Mathf.Approximately(best, left)) return 0;
if (Mathf.Approximately(best, right)) return 2;
return 1;
}
private float ScoreLane(float laneX, int lane)
{
float score = 1f;
score += lane == 1 ? 0.8f : 0f;
score += lane == lastLane ? laneRotationWeight : 0f;
foreach (var unit in FindObjectsOfType<UnitTeam>())
{
if (unit == null || unit.teamId != 0) continue;
Vector3 p = unit.transform.position;
if (Mathf.Abs(p.x - laneX) <= laneHalfWidth)
score += enemyUnitWeight;
}
foreach (var mine in FindObjectsOfType<CrystalMineCapture>())
{
if (mine == null) continue;
Vector3 p = mine.transform.position;
if (Mathf.Abs(p.x - laneX) > laneHalfWidth * 1.8f) continue;
UnitTeam team = mine.GetComponent<UnitTeam>();
if (team == null || team.teamId != teamId)
score += resourceThreatWeight;
}
return score;
}
private CharacterData ChooseCard(int lane)
{
CharacterData best = null;
float bestScore = float.NegativeInfinity;
foreach (var card in hand)
{
if (card == null || card.unitPrefab == null) continue;
float score = Random.Range(0f, 1f);
if (score > bestScore)
{
bestScore = score;
best = card;
}
}
return best;
}
private void BuildDeck()
{
if (deck != null && deck.Length > 0) return;
var loadedDeck = new List<CharacterData>();
foreach (string dataName in deckDataNames)
{
CharacterData data = Resources.Load<CharacterData>("Data/" + dataName);
if (data == null)
{
Debug.LogWarning($"敌方卡组缺少角色数据: Resources/Data/{dataName}.asset");
continue;
}
loadedDeck.Add(data);
}
deck = loadedDeck.ToArray();
}
private void BuildHand()
{
hand.Clear();
drawQueue.Clear();
if (deck == null) return;
int handCount = Mathf.Min(visibleSlotCount, deck.Length);
for (int i = 0; i < handCount; i++)
hand.Add(deck[i]);
for (int i = handCount; i < deck.Length; i++)
drawQueue.Enqueue(deck[i]);
}
private void RotatePlayedCard(CharacterData playedCard)
{
int handIndex = hand.IndexOf(playedCard);
if (handIndex < 0 || drawQueue.Count == 0) return;
hand[handIndex] = drawQueue.Dequeue();
drawQueue.Enqueue(playedCard);
}
private bool SpawnAt(CharacterData data, Vector3 position, Quaternion rotation, int spawnTeamId)
{
GameObject unit = Instantiate(data.unitPrefab, position, rotation);
UnitTeam team = unit.GetComponent<UnitTeam>();
if (team != null)
{
team.teamId = spawnTeamId;
unit.tag = spawnTeamId == 0 ? "Friend" : "Enemy";
}
UnitBase unitBase = unit.GetComponent<UnitBase>();
if (unitBase != null)
{
unitBase.InitializeFromPlayerData(data.GetMaxHealth(1), data.GetAttackPower(1), data.GetMoveSpeed(1));
// 如果是敌人(teamId != 0),增大寻敌距离
if (spawnTeamId != 0)
{
unitBase.detectionRange = 100f; // 从默认30增加到100
}
}
return true;
}
private bool Spawn(CharacterData data, int lane)
{
if (!SpawnAt(data, redSpawnPoint.position, redSpawnPoint.rotation, teamId)) return false;
if (targetController != null)
{
targetController.redTarget = FindLaneTarget(lane);
}
return true;
}
private GameObject FindLaneTarget(int lane)
{
float x = lane == 0 ? leftLaneX : lane == 2 ? rightLaneX : centerLaneX;
GameObject best = null;
float bestDistance = float.PositiveInfinity;
foreach (var mine in FindObjectsOfType<CrystalMineCapture>())
{
if (Mathf.Abs(mine.transform.position.x - x) < laneHalfWidth * 2f)
{
float d = Mathf.Abs(mine.transform.position.z - redSpawnPoint.position.z);
if (d < bestDistance) { bestDistance = d; best = mine.gameObject; }
}
}
return best != null ? best : targetController?.GetEnemyCastleOfTeam(teamId);
}
private Transform FindRedSpawnPoint()
{
var manager = GetComponent<CastleSpawnManager>();
if (manager != null && manager.redSpawnPoint != null) return manager.redSpawnPoint;
var castles = GameObject.Find("红色城堡");
return castles != null ? castles.transform.Find("出兵点") : null;
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: Di4e5COsW3MaOIKjAZpkNB3DV96stk4sKbaXXg7lgL4M9IOQyMJld6Y=
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
-78
View File
@@ -1,78 +0,0 @@
using UnityEngine;
/// <summary>
/// 金矿建筑,定时产出金币
/// </summary>
public class GoldMine : MonoBehaviour
{
[Header("金币产出")]
[Tooltip("每次产出的金币数量")]
[SerializeField] private int goldPerProduction = 50;
[Tooltip("产出间隔(秒)")]
[SerializeField] private float productionInterval = 5f;
[Header("可选:血条和进度条")]
[SerializeField] private WorldHealthBar healthBar;
private float productionTimer = 0f;
private void Awake()
{
// 如果没有手动指定,尝试自动获取
if (healthBar == null)
{
healthBar = GetComponent<WorldHealthBar>();
}
}
private void Start()
{
// 初始化进度条(使用能量条显示产出进度)
if (healthBar != null)
{
healthBar.maxEnergy = 100f;
healthBar.currentEnergy = 0f;
}
}
private void Update()
{
if (GoldManager.Instance == null) return;
productionTimer += Time.deltaTime;
// 更新进度条
if (healthBar != null)
{
float progress = Mathf.Clamp01(productionTimer / productionInterval);
healthBar.currentEnergy = progress * 100f;
}
// 达到产出时间
if (productionTimer >= productionInterval)
{
ProduceGold();
productionTimer = 0f;
}
}
private void ProduceGold()
{
if (GoldManager.Instance != null)
{
GoldManager.Instance.AddGold(goldPerProduction);
Debug.Log($"{gameObject.name} 产出金币 +{goldPerProduction}");
// 可以在这里播放特效和音效
// TODO: 添加金币产出特效
}
}
// 在编辑器中显示信息
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position + Vector3.up * 2f, 0.5f);
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: DykfvC7+VnysnFVe0RuuhqYblOX8b3TqKpWkys/vBAvo9QxqFltLb0g=
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -13,6 +13,13 @@ public class EliteSoldierSkill : UnitSkill
[Min(0f)]
public float skillAnimationDuration = 2.22f;
[Header("特效")]
public GameObject groundCrackEffectPrefab;
[Min(0f)]
public float groundCrackForwardDistance = 1.2f;
[Min(0f)]
public float groundCrackHeightOffset = 0.05f;
private bool isSkillReady;
public override void ReleaseSkill()
@@ -35,6 +42,24 @@ public class EliteSoldierSkill : UnitSkill
return;
}
if (groundCrackEffectPrefab != null)
{
Vector3 effectPosition = unit.transform.position
+ unit.transform.forward * groundCrackForwardDistance;
effectPosition.y += groundCrackHeightOffset;
GameObject effectObject = Instantiate(
groundCrackEffectPrefab,
effectPosition,
groundCrackEffectPrefab.transform.rotation);
ShockwaveRing shockwave = effectObject.GetComponent<ShockwaveRing>();
if (shockwave != null)
{
shockwave.Play();
}
}
unit.ApplyAreaSkillDamage(skillRadius, skillDamage, false);
isSkillReady = false;
}
@@ -46,14 +46,10 @@ public class StoneGolemSkill : UnitSkill
{
Vector3 effectPosition = unit.transform.position;
var effectObj = Instantiate(shockwaveEffectPrefab, effectPosition, shockwaveEffectPrefab.transform.rotation);
// 可选:动态配置特效参数(不配置则使用Prefab默认值)
var effect = effectObj.GetComponent<ShockwaveEffect>();
if (effect != null)
ShockwaveRing shockwave = effectObj.GetComponent<ShockwaveRing>();
if (shockwave != null)
{
// 根据技能范围动态设置扩散半径
effect.overrideRadius = skillRadius;
// 可以设置颜色,例如:effect.overrideColor = new Color(1f, 0.8f, 0.2f, 1f);
shockwave.Play();
}
}
@@ -122,11 +122,9 @@ public class Projectile : MonoBehaviour
}
UnitBase targetUnit = hitObject.GetComponentInParent<UnitBase>();
CrystalMineCapture crystalMine = hitObject.GetComponentInParent<CrystalMineCapture>();
BuildingHealth targetBuilding = hitObject.GetComponentInParent<BuildingHealth>();
WorldHealthBar targetHealthBar = hitObject.GetComponentInParent<WorldHealthBar>();
if (targetUnit == null && crystalMine == null && targetBuilding == null && targetHealthBar == null)
if (targetUnit == null && targetHealthBar == null)
{
return;
}
@@ -137,14 +135,6 @@ public class Projectile : MonoBehaviour
{
targetUnit.ReceiveDamage(damage, owner);
}
else if (crystalMine != null)
{
crystalMine.ReceiveCaptureDamage(damage, owner);
}
else if (targetBuilding != null)
{
targetBuilding.ReceiveDamage(damage, owner);
}
else if (targetHealthBar != null)
{
// 召唤师等特殊单位(有 WorldHealthBar 但没有 UnitBase)
@@ -1,11 +1,20 @@
using UnityEngine;
public enum CharacterRarity
{
Blue = 0,
Orange = 1,
Purple = 2,
Red = 3
}
[CreateAssetMenu(fileName = "NewCharacterData", menuName = "召唤之战/角色数据")]
public class CharacterData : ScriptableObject
{
[Header("基础信息")]
public string characterId = "unknown";
public string characterName = "未命名角色";
public CharacterRarity rarity = CharacterRarity.Blue;
[TextArea(2, 4)]
public string description = "";
+53 -12
View File
@@ -2,20 +2,22 @@ using UnityEngine;
/// <summary>
/// 部署网格的几何参数与坐标换算。
/// 阵型编排场景和战斗场景共用同一份参数,保证两边的格位完全对齐。
/// 网格与场景里用于标记布阵区域的那个 Plane 完全重合:
/// Plane 世界范围 X -5.75..5.75、Z -15.6..-4.4,中心 (0, ?, -10)。
/// 4 列 × 4 行,正好铺满这块区域;整体位于 NavMesh 覆盖范围内。
/// </summary>
public static class DeployGrid
{
public const int Cols = 6;
public const int Rows = 5;
public const float ColStep = 2.4f; // 列间距改为2.4,让网格更紧凑,避免最边缘格子太靠外
public const float RowStep = 2.8f; // 行间距保持2.8
public const float BackZ = -15.0f; // 最靠后一排的中心 Z(调整到靠近摄像机的底部区域)
public const float CellWidth = 2.5f;
public const float CellDepth = 2.5f; // 格子深度改为2.5,与宽度相同,形成正方形
public const int Cols = 4;
public const int Rows = 4;
public const float ColStep = 2.875f; // 11.5 / 4
public const float RowStep = 2.8f; // 11.2 / 4
public const float BackZ = -14.2f; // 第 0 排(最靠近镜头)中心 Z = -10 - 1.5*2.8
public const float CellWidth = 2.815f; // 比间距略小,留出缝,看得出是格子
public const float CellDepth = 2.74f;
public const float CellHeight = 0.03f;
public const float GridY = 0.12f; // 格位可视块的中心高度
public const float UnitY = 0.16f; // 单位落点高度(与 NavMesh 高度一致)
public const float GridY = 0.115f; // 格位可视块中心高度(贴在布阵平面之上)
public const float UnitY = 0.10f; // 单位落点高度(与烘焙后的 NavMesh 高度一致)
public const int CellCount = Cols * Rows;
@@ -34,7 +36,23 @@ public static class DeployGrid
return new Vector3(x, UnitY, z);
}
/// <summary>把地面上的世界坐标吸附到最近格位;不在网格范围内返回 -1。</summary>
/// <summary>整个网格的中心(可视块高度)。</summary>
public static Vector3 GridCenter
{
get
{
float z = BackZ + (Rows - 1) * RowStep * 0.5f;
return new Vector3(0f, GridY, z);
}
}
/// <summary>网格在 X 方向的宽度。</summary>
public static float GridWidth => Cols * ColStep;
/// <summary>网格在 Z 方向的深度。</summary>
public static float GridDepth => Rows * RowStep;
/// <summary>把地面坐标吸附到最近格位;不在网格范围内返回 -1。</summary>
public static int SnapToCell(Vector3 world)
{
int c = Mathf.RoundToInt(world.x / ColStep + (Cols - 1) * 0.5f);
@@ -50,6 +68,29 @@ public static class DeployGrid
return ToIndex(r, c);
}
/// <summary>
/// 宽松吸附:超出网格时夹到最近格位,只要离网格边缘不超过 tolerance 个格子就算命中。
/// 让玩家在网格边缘附近松手也能放下单位。
/// </summary>
public static int SnapToCellClamped(Vector3 world, float tolerance = 0.9f)
{
int c = Mathf.RoundToInt(world.x / ColStep + (Cols - 1) * 0.5f);
int r = Mathf.RoundToInt((world.z - BackZ) / RowStep);
int clampedC = Mathf.Clamp(c, 0, Cols - 1);
int clampedR = Mathf.Clamp(r, 0, Rows - 1);
// 超出网格多少(以格为单位)
int overC = c < 0 ? -c : (c >= Cols ? c - (Cols - 1) : 0);
int overR = r < 0 ? -r : (r >= Rows ? r - (Rows - 1) : 0);
if (overC > tolerance || overR > tolerance)
{
return -1;
}
return ToIndex(clampedR, clampedC);
}
/// <summary>把一组单位在网格宽度内均匀铺开,返回列号。</summary>
public static int SpreadColumn(int indexInGroup, int groupCount)
{
@@ -60,7 +101,7 @@ public static class DeployGrid
return Mathf.RoundToInt(step * (indexInGroup + 1));
}
/// <summary>默认阵型格位:近战在最前排(靠近敌人),远程在最后排。</summary>
/// <summary>默认阵型格位:近战在最靠敌方的一排,远程在最后排。</summary>
public static int DefaultCell(bool isMelee, int indexInGroup, int groupCount)
{
int row = isMelee ? Rows - 1 : 0;
@@ -81,13 +81,28 @@ public class StatusEffectController : MonoBehaviour
Bounds bounds = new Bounds(transform.position, Vector3.zero);
Renderer[] renderers = GetComponentsInChildren<Renderer>(true);
if (renderers.Length > 0)
bool hasRenderer = false;
foreach (Renderer renderer in renderers)
{
bounds = renderers[0].bounds;
for (int i = 1; i < renderers.Length; i++)
if (stunIndicator != null && renderer.transform.IsChildOf(stunIndicator.transform))
{
bounds.Encapsulate(renderers[i].bounds);
continue;
}
if (!hasRenderer)
{
bounds = renderer.bounds;
hasRenderer = true;
}
else
{
bounds.Encapsulate(renderer.bounds);
}
}
if (!hasRenderer)
{
bounds = new Bounds(transform.position, Vector3.zero);
}
indicatorTransform.position = new Vector3(
@@ -126,11 +126,6 @@ public class MainCharacterDebugMovement : MonoBehaviour
continue;
}
if (candidate.GetComponentInParent<BuildingHealth>() == null)
{
continue;
}
// 主建筑按名称优先匹配;找不到时退回第一个己方建筑
if (candidate.name.Contains("主要") || candidate.name.Contains("基地"))
{
+88 -70
View File
@@ -76,13 +76,17 @@ public class UnitBase : MonoBehaviour
private bool summonerAttackEnabled = true;
[Header("无目标回位")]
[Tooltip("开启后,当前没有敌方单位时返回生成时的初始格位;不限制正常追击距离。")]
[Tooltip("开启后,当前没有敌方单位时返回生成时的初始格位。")]
public bool returnToAnchorWhenIdle = false;
[Min(0.1f)]
public float anchorArrivalDistance = 0.35f;
private Vector3 anchorPosition;
private bool anchorInitialized;
[Header("阵位守护")]
[Tooltip("开启后,只打「离自己阵位不超过 寻敌视野 detectionRange」的敌人,避免一路推到很远的地方。")]
public bool useAnchorLeash = false;
//角色状态
private State state = State.Alert;
@@ -112,6 +116,8 @@ public class UnitBase : MonoBehaviour
private float attackSpeedMultiplier = 1f;
private UnitSkill unitSkill;
private System.Action skillFinishedCallback;
private float skillElapsedTime;
private float skillTimeout;
//状态枚举
@@ -183,7 +189,7 @@ public class UnitBase : MonoBehaviour
navAgent.speed = speed;
navAgent.angularSpeed = 120f;
navAgent.acceleration = 8f;
navAgent.stoppingDistance = attackRange * 0.95f; // 接近攻击范围边缘时停止
navAgent.stoppingDistance = NormalStoppingDistance; // 接近攻击范围边缘时停止
navAgent.autoBraking = true;
// NavMesh 使用烘焙平面的高度,不能根据动画帧中的 Renderer 包围盒推算 baseOffset。
@@ -287,6 +293,13 @@ public class UnitBase : MonoBehaviour
// Attack 状态在上方单独处理,因此不会被这里打断或切换目标。
target = FindTarget();
// 阵位守护:目标离自己阵位超过寻敌视野(detectionRange)就不追了,留在阵位附近
if (target != null && useAnchorLeash && anchorInitialized
&& IsBeyondLeash(target.transform.position, detectionRange))
{
target = null;
}
if (target != null)
{
// 召唤师模式:不主动追击,站定且攻击开关开启时才攻击射程内目标。
@@ -374,6 +387,11 @@ public class UnitBase : MonoBehaviour
public void ApplyStun(float duration)
{
if (duration <= 0f)
{
return;
}
EnsureStatusEffectController().ApplyStun(duration);
}
@@ -455,6 +473,20 @@ public class UnitBase : MonoBehaviour
return Vector3.Distance(current, anchor) <= anchorArrivalDistance;
}
/// <summary>某个世界坐标是否离锚点超过了指定距离(只算水平距离)。</summary>
private bool IsBeyondLeash(Vector3 worldPosition, float maxDistance)
{
if (!anchorInitialized) return false;
Vector3 anchor = anchorPosition;
anchor.y = 0f;
Vector3 point = worldPosition;
point.y = 0f;
return Vector3.Distance(anchor, point) > maxDistance;
}
/// <summary>追击/接敌时用的停靠距离(贴着攻击范围边缘停下)。</summary>
private float NormalStoppingDistance => Mathf.Max(0f, attackRange * 0.95f);
/// <summary>
/// 重置锚点位置到当前位置(用于复活等场景)
/// </summary>
@@ -468,11 +500,7 @@ public class UnitBase : MonoBehaviour
{
if (navAgent == null || !anchorInitialized)
{
if (!summonerMode)
{
animator.SetInteger("speed", 0);
animator.speed = 1f; // 重置动画播放速度
}
RestoreIdleSpeed();
return;
}
@@ -483,11 +511,7 @@ public class UnitBase : MonoBehaviour
if (!navAgent.isOnNavMesh)
{
if (!summonerMode)
{
animator.SetInteger("speed", 0);
animator.speed = 1f; // 重置动画播放速度
}
RestoreIdleSpeed();
return;
}
@@ -497,22 +521,41 @@ public class UnitBase : MonoBehaviour
navAgent.ResetPath();
transform.position = new Vector3(anchorPosition.x, transform.position.y, anchorPosition.z);
navAgent.nextPosition = transform.position;
if (!summonerMode)
{
animator.SetInteger("speed", 0);
animator.speed = 1f; // 重置动画播放速度
}
RestoreIdleSpeed();
return;
}
navAgent.isStopped = false;
// 回位必须贴到锚点:攻击用的停靠距离会把 agent 挡在半路(远程角色尤其明显)
navAgent.stoppingDistance = 0f;
if (Time.time >= nextPathUpdateTime)
{
navAgent.SetDestination(anchorPosition);
nextPathUpdateTime = Time.time + PathUpdateInterval;
}
animator.SetInteger("speed", navAgent.velocity.sqrMagnitude > 0.01f ? 1 : 0);
animator.speed = 1f; // 确保移动时动画速度正常
// 用正常速度走回格位
navAgent.speed = speed;
if (!summonerMode)
{
animator.SetInteger("speed", navAgent.velocity.sqrMagnitude > 0.01f ? 1 : 0);
animator.speed = 1f; // 确保走路动画速度正常
}
}
/// <summary>回位结束或无法寻路时,把速度和动画恢复成正常站定状态。</summary>
private void RestoreIdleSpeed()
{
if (navAgent != null && navAgent.enabled)
{
navAgent.speed = speed;
navAgent.stoppingDistance = NormalStoppingDistance;
}
if (!summonerMode && animator != null)
{
animator.SetInteger("speed", 0);
animator.speed = 1f;
}
}
//目标决策:只寻找 detectionRange 内的敌方战斗单位;范围内没有敌人时原地待机或回到锚点。
@@ -837,21 +880,6 @@ public class UnitBase : MonoBehaviour
return;
}
CrystalMineCapture targetCrystalMine = opponent.GetComponentInParent<CrystalMineCapture>();
if (targetCrystalMine != null && targetCrystalMine.ReceiveCaptureDamage(attackPower, gameObject))
{
RegisterAttackHit();
return;
}
BuildingHealth targetBuilding = opponent.GetComponentInParent<BuildingHealth>();
if (targetBuilding != null && targetBuilding.ReceiveDamage(attackPower, gameObject))
{
RegisterAttackHit();
return;
}
// 召唤师等特殊单位(有 WorldHealthBar 但没有 UnitBase)
WorldHealthBar targetHealthBar = opponent.GetComponentInParent<WorldHealthBar>();
if (targetHealthBar != null)
{
@@ -871,18 +899,6 @@ public class UnitBase : MonoBehaviour
UnitBase targetUnit = target.GetComponentInParent<UnitBase>();
bool hit = targetUnit != null && targetUnit.ReceiveDamage(damage, gameObject);
if (!hit)
{
CrystalMineCapture targetCrystalMine = target.GetComponentInParent<CrystalMineCapture>();
hit = targetCrystalMine != null && targetCrystalMine.ReceiveCaptureDamage(damage, gameObject);
}
if (!hit)
{
BuildingHealth targetBuilding = target.GetComponentInParent<BuildingHealth>();
hit = targetBuilding != null && targetBuilding.ReceiveDamage(damage, gameObject);
}
if (!hit)
{
return;
@@ -937,22 +953,9 @@ public class UnitBase : MonoBehaviour
}
UnitBase targetUnit = opponent.GetComponentInParent<UnitBase>();
BuildingHealth targetBuilding = opponent.GetComponentInParent<BuildingHealth>();
WorldHealthBar healthBar = null;
if (targetUnit != null)
{
healthBar = targetUnit.GetComponent<WorldHealthBar>();
}
else if (targetBuilding != null)
{
healthBar = targetBuilding.GetComponent<WorldHealthBar>();
}
else
{
// 召唤师等特殊单位(有 WorldHealthBar 但没有 UnitBase)
healthBar = opponent.GetComponentInParent<WorldHealthBar>();
}
WorldHealthBar healthBar = targetUnit != null
? targetUnit.GetComponent<WorldHealthBar>()
: opponent.GetComponentInParent<WorldHealthBar>();
if (healthBar == null || !hitHealthBars.Add(healthBar))
{
@@ -963,13 +966,8 @@ public class UnitBase : MonoBehaviour
{
targetUnit.ReceiveDamage(damage, gameObject);
}
else if (targetBuilding != null)
else
{
targetBuilding.ReceiveDamage(damage, gameObject);
}
else if (healthBar != null)
{
// 召唤师等特殊单位
healthBar.TakeDamage(damage, gameObject);
}
}
@@ -999,6 +997,7 @@ public class UnitBase : MonoBehaviour
// 查找所有带 UnitTeam 的对象(包括单位和召唤师)
UnitTeam[] allTeams = FindObjectsOfType<UnitTeam>();
HashSet<UnitBase> hitUnits = new HashSet<UnitBase>();
HashSet<StatusEffectController> hitStatusEffects = new HashSet<StatusEffectController>();
foreach (UnitTeam targetTeam in allTeams)
{
@@ -1031,7 +1030,7 @@ public class UnitBase : MonoBehaviour
// 召唤师等特殊单位(有 StatusEffectController 但没有 UnitBase)
StatusEffectController statusEffect = opponent.GetComponentInParent<StatusEffectController>();
if (statusEffect != null)
if (statusEffect != null && hitStatusEffects.Add(statusEffect))
{
statusEffect.ApplyStun(duration);
}
@@ -1129,6 +1128,11 @@ public class UnitBase : MonoBehaviour
skillFinishedCallback = onFinished;
isPlayingSkill = true;
skillElapsedTime = 0f;
float animationDuration = GetSkillAnimationDuration();
skillTimeout = targetDuration > 0f
? targetDuration + 0.5f
: Mathf.Max(animationDuration + 0.5f, 2f);
state = State.Skill;
animator.speed = GetSkillAnimationSpeed(targetDuration);
animator.SetInteger("speed", 0);
@@ -1145,13 +1149,23 @@ public class UnitBase : MonoBehaviour
private void CheckSkillAnimationFinished()
{
if (!isPlayingSkill)
{
return;
}
skillElapsedTime += Time.deltaTime;
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
if (!stateInfo.IsName("skill") || stateInfo.normalizedTime < 1f)
bool animationFinished = stateInfo.IsName("skill") && stateInfo.normalizedTime >= 1f;
bool timedOut = skillElapsedTime >= skillTimeout;
if (!animationFinished && !timedOut)
{
return;
}
isPlayingSkill = false;
skillElapsedTime = 0f;
skillTimeout = 0f;
state = State.Alert;
animator.speed = attackSpeedMultiplier;
skillFinishedCallback?.Invoke();
@@ -1232,6 +1246,10 @@ public class UnitBase : MonoBehaviour
if (!navAgent.isOnNavMesh) return;
// 追击时用正常速度和正常停靠距离(可能刚从回位状态切回来)
navAgent.speed = speed;
navAgent.stoppingDistance = NormalStoppingDistance;
// 确保没有被停止
if (navAgent.isStopped)
{