sea-battle/Assets/Scripts/CannonBattery.cs
2026-01-09 08:45:22 +03:00

71 lines
2.1 KiB
C#

using UnityEngine;
public class CannonBattery : MonoBehaviour
{
public Cannon[] cannons;
private int nextIndex = 0;
public void FireAtTransform(Transform targetTransform, System.Action onImpact)
{
for (int i = 0; i < cannons.Length; i++)
{
int index = (nextIndex + i) % cannons.Length;
if (cannons[index].IsReady)
{
cannons[index].ShootAtTransform(targetTransform, onImpact);
nextIndex = (index + 1) % cannons.Length;
break;
}
}
}
public void FireShot(GamePole targetPole, int x, int y, System.Action<bool, bool> onShotComplete)
{
Transform targetTransform = targetPole.GetCell(x, y).transform;
FireAtTransform(targetTransform, () =>
{
bool wasHit = targetPole.Shot(x, y);
bool wasShipDestroyed = false;
if (wasHit)
{
var cell = targetPole.Pole[x, y].GetComponent<Cell>();
var ship = cell?.deck?.GetComponentInParent<Ship>();
if (ship != null && ship.IsDestroyed())
wasShipDestroyed = true;
}
onShotComplete?.Invoke(wasHit, wasShipDestroyed);
});
}
// =========================
// ÑÁÐÎÑ ÂÑÅÕ ÏÓØÅÊ
// =========================
/// <summary>
/// Ïðèíóäèòåëüíî ñíèìàåò ñîñòîÿíèå ïåðåçàðÿäêè ó âñåõ ïóøåê è âîçâðàùàåò ïîçó â äåôîëò.
/// instant=true — ìîìåíòàëüíî âåðíóòü ãåîìåòðèþ/ýôôåêòû; false — äàòü êîðóòèíàì çàâåðøèòüñÿ ïëàâíî.
/// </summary>
public void ResetAllCannons(bool instant = true)
{
if (cannons == null) return;
foreach (var c in cannons)
{
if (c == null) continue;
c.ForceReloadReset(instant);
}
// ïî æåëàíèþ: îáíóëèì èíäåêñ, ÷òîáû ïîðÿäîê ñòðåëüáû íà÷èíàëñÿ ñ ïåðâîé
nextIndex = 0;
}
[ContextMenu("Reset All Cannons (instant)")]
private void _ctxResetAllInstant() => ResetAllCannons(true);
[ContextMenu("Reset All Cannons (smooth)")]
private void _ctxResetAllSmooth() => ResetAllCannons(false);
}