103 lines
3.2 KiB
C#
103 lines
3.2 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
[DisallowMultipleComponent]
|
|
public class MapTarget : MonoBehaviour
|
|
{
|
|
[Header("Target State (Runtime)")]
|
|
[SerializeField] private TargetConfig config;
|
|
[SerializeField] private float timeUntilAttack;
|
|
[SerializeField] private Vector2 mapCoordinates;
|
|
[SerializeField] private int currentHealth;
|
|
[SerializeField] private MissileType requiredMissileType;
|
|
[SerializeField] private bool isReadyToAttack;
|
|
|
|
private MapCoordinateSystem coordinateSystem;
|
|
private MapPlayer player;
|
|
private bool isConfigured;
|
|
private Renderer[] targetRenderers;
|
|
|
|
public Vector2 MapCoordinates => mapCoordinates;
|
|
public MapCoordinateSystem CoordinateSystem => coordinateSystem;
|
|
public TargetConfig Config => config;
|
|
public MapPlayer Player => player;
|
|
public float TimeUntilAttack => timeUntilAttack;
|
|
public int CurrentHealth => currentHealth;
|
|
public MissileType RequiredMissileType => requiredMissileType;
|
|
public bool IsReadyToAttack => isReadyToAttack;
|
|
|
|
public event Action<MapTarget> AttackReady;
|
|
|
|
public void Configure(
|
|
TargetConfig newConfig,
|
|
MapCoordinateSystem newCoordinateSystem,
|
|
Vector2 startCoordinates,
|
|
MapPlayer newPlayer)
|
|
{
|
|
config = newConfig;
|
|
coordinateSystem = newCoordinateSystem;
|
|
player = newPlayer;
|
|
timeUntilAttack = config != null ? config.TimeBeforeAttack : 0f;
|
|
currentHealth = config != null ? config.Health : 1;
|
|
requiredMissileType = config != null ? config.RequiredMissileType : MissileType.None;
|
|
isReadyToAttack = false;
|
|
isConfigured = coordinateSystem != null && coordinateSystem.HasValidCorners();
|
|
SetMapCoordinates(startCoordinates);
|
|
}
|
|
|
|
protected virtual void Update()
|
|
{
|
|
if (!isConfigured)
|
|
return;
|
|
|
|
TickAttackTimer(Time.deltaTime);
|
|
TickMovement(Time.deltaTime);
|
|
}
|
|
|
|
public void SetMapCoordinates(Vector2 coordinates)
|
|
{
|
|
mapCoordinates = coordinateSystem != null
|
|
? coordinateSystem.ClampCoordinates(coordinates)
|
|
: new Vector2(
|
|
Mathf.Clamp(coordinates.x, 0f, MapCoordinateSystem.CoordinateMaximum),
|
|
Mathf.Clamp(coordinates.y, 0f, MapCoordinateSystem.CoordinateMaximum));
|
|
|
|
if (isConfigured)
|
|
transform.position = coordinateSystem.CoordinatesToWorldPosition(mapCoordinates);
|
|
}
|
|
|
|
public Vector2 GetMapCoordinates()
|
|
{
|
|
return mapCoordinates;
|
|
}
|
|
|
|
public void SetVisualsVisible(bool isVisible)
|
|
{
|
|
if (targetRenderers == null)
|
|
targetRenderers = GetComponentsInChildren<Renderer>(true);
|
|
|
|
foreach (Renderer targetRenderer in targetRenderers)
|
|
{
|
|
if (targetRenderer != null)
|
|
targetRenderer.enabled = isVisible;
|
|
}
|
|
}
|
|
|
|
protected virtual void TickMovement(float deltaTime)
|
|
{
|
|
}
|
|
|
|
private void TickAttackTimer(float deltaTime)
|
|
{
|
|
if (isReadyToAttack)
|
|
return;
|
|
|
|
timeUntilAttack = Mathf.Max(0f, timeUntilAttack - deltaTime);
|
|
if (timeUntilAttack > 0f)
|
|
return;
|
|
|
|
isReadyToAttack = true;
|
|
AttackReady?.Invoke(this);
|
|
}
|
|
}
|