152 lines
4.6 KiB
C#
152 lines
4.6 KiB
C#
using Lights;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
public class EnemyShip : MonoBehaviour
|
||
{
|
||
public ShipType Type;
|
||
|
||
[SerializeField] private BezierRunner Mover;
|
||
[SerializeField] private float MaxHp;
|
||
public Health HealthComponent;
|
||
[SerializeField] private List<Hitbox> Hitboxes;
|
||
[SerializeField] private EnemyHealthBar HealthBar;
|
||
|
||
[SerializeField] RealisticSinking sinking;
|
||
[SerializeField] private List<MeshRenderer> outlineRenderers; // Теперь это список
|
||
[SerializeField] private float outlineAnimationDuration = 0.3f;
|
||
[SerializeField] private AnimationCurve outlineCurve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
|
||
public EnemyShooter Shooter;
|
||
|
||
[SerializeField, Range(0f, 1f)] float maxOutlineAlpha = 0.4f;
|
||
|
||
public int SpawnCost = 1;
|
||
public int Score = 100;
|
||
|
||
[SerializeField] private float timeToClearTheWay = 5;
|
||
|
||
|
||
public static event Action<EnemyShip> ShipDamaged; //Заспавнен
|
||
public static event Action<EnemyShip> ShipSpawned; //Заспавнен
|
||
public static event Action<EnemyShip> ShipDefeated; //Побежден
|
||
public static event Action<EnemyShip> ShipReleased; //Отпущен
|
||
|
||
public void Initialize()
|
||
{
|
||
foreach (var hitbox in Hitboxes)
|
||
hitbox.Initialize(owner: gameObject);
|
||
HealthComponent.Initialize(MaxHp, Hitboxes);
|
||
HealthComponent.Damaged += HealthComponent_Damaged;
|
||
HealthComponent.Died += Defeat;
|
||
|
||
ShipSpawned?.Invoke(this);
|
||
Mover.OnReachedEnd += Release;
|
||
|
||
HealthBar?.Initialize(HealthComponent);
|
||
|
||
HideOutline(0.001f);
|
||
}
|
||
|
||
private void HealthComponent_Damaged(float obj)
|
||
{
|
||
LightSystem.Ef_Hit();
|
||
ShipDamaged?.Invoke(this);
|
||
}
|
||
|
||
public void AssignToWay(BezierWay way)
|
||
{
|
||
way.SetBusiness(true);
|
||
Mover.SetBezierWay(way);
|
||
}
|
||
|
||
public void Defeat(Vector3 damagePosition)
|
||
{
|
||
Mover.BezierWay.SetBusiness(false, timeToClearTheWay);
|
||
Mover.OnReachedEnd -= Release;
|
||
ShipDefeated?.Invoke(this);
|
||
if (Shooter != null)
|
||
Destroy(Shooter.gameObject);
|
||
|
||
sinking.OnTorpedoHit(damagePosition, Mover.Velocity);
|
||
Mover.enabled = false;
|
||
}
|
||
|
||
public void Release()
|
||
{
|
||
ShipReleased?.Invoke(this);
|
||
Mover.BezierWay.SetBusiness(false);
|
||
Mover.OnReachedEnd -= Release;
|
||
|
||
Destroy(gameObject);
|
||
}
|
||
|
||
public enum ShipType //Типизация кораблей для счетчиков
|
||
{
|
||
CombatBoat,
|
||
Frigate,
|
||
Destroyer,
|
||
Destroyer2,
|
||
Carrier
|
||
}
|
||
|
||
public void ShowOutline(float duration = -1f)
|
||
{
|
||
StopAllCoroutines();
|
||
float animDuration = duration > 0 ? duration : outlineAnimationDuration;
|
||
StartCoroutine(AnimateOutlineAlpha(0f, maxOutlineAlpha, animDuration));
|
||
}
|
||
|
||
public void HideOutline(float duration = -1f)
|
||
{
|
||
StopAllCoroutines();
|
||
float animDuration = duration > 0 ? duration : outlineAnimationDuration;
|
||
StartCoroutine(AnimateOutlineAlpha(maxOutlineAlpha, 0f, animDuration));
|
||
}
|
||
|
||
private System.Collections.IEnumerator AnimateOutlineAlpha(float startAlpha, float targetAlpha, float duration)
|
||
{
|
||
// Собираем все материалы со всех рендереров
|
||
List<Material[]> allMaterials = new List<Material[]>();
|
||
foreach (var renderer in outlineRenderers)
|
||
{
|
||
if (renderer != null)
|
||
allMaterials.Add(renderer.materials);
|
||
}
|
||
|
||
float elapsedTime = 0f;
|
||
|
||
while (elapsedTime < duration)
|
||
{
|
||
elapsedTime += Time.deltaTime;
|
||
float t = elapsedTime / duration;
|
||
|
||
// Используем кривую из инспектора
|
||
float curveValue = outlineCurve.Evaluate(t);
|
||
float currentAlpha = Mathf.Lerp(startAlpha, targetAlpha, curveValue);
|
||
|
||
// Применяем alpha ко всем материалам всех рендереров
|
||
foreach (var materials in allMaterials)
|
||
{
|
||
foreach (var material in materials)
|
||
{
|
||
if (material != null)
|
||
material.SetFloat("_alpha", currentAlpha);
|
||
}
|
||
}
|
||
|
||
yield return null;
|
||
}
|
||
|
||
// Убеждаемся, что достигли целевого значения
|
||
foreach (var materials in allMaterials)
|
||
{
|
||
foreach (var material in materials)
|
||
{
|
||
if (material != null)
|
||
material.SetFloat("_alpha", targetAlpha);
|
||
}
|
||
}
|
||
}
|
||
}
|