Mappp
This commit is contained in:
@@ -1,43 +1,57 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
[DisallowMultipleComponent]
|
||||
public sealed class MapTarget : MonoBehaviour
|
||||
public class MapTarget : MonoBehaviour
|
||||
{
|
||||
[Header("Map Position (Runtime)")]
|
||||
[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 Vector2 movementDirection;
|
||||
private float movementSpeed;
|
||||
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,
|
||||
float newMovementSpeed,
|
||||
Vector2 initialDirection)
|
||||
MapPlayer newPlayer)
|
||||
{
|
||||
config = newConfig;
|
||||
coordinateSystem = newCoordinateSystem;
|
||||
movementSpeed = Mathf.Max(0f, newMovementSpeed);
|
||||
movementDirection = initialDirection.sqrMagnitude > Mathf.Epsilon
|
||||
? initialDirection.normalized
|
||||
: Vector2.right;
|
||||
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);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
protected virtual void Update()
|
||||
{
|
||||
if (!isConfigured || movementSpeed <= 0f)
|
||||
if (!isConfigured)
|
||||
return;
|
||||
|
||||
Vector2 nextCoordinates = mapCoordinates + movementDirection * (movementSpeed * Time.deltaTime);
|
||||
ReflectAtMapBounds(ref nextCoordinates);
|
||||
SetMapCoordinates(nextCoordinates);
|
||||
TickAttackTimer(Time.deltaTime);
|
||||
TickMovement(Time.deltaTime);
|
||||
}
|
||||
|
||||
public void SetMapCoordinates(Vector2 coordinates)
|
||||
@@ -69,20 +83,20 @@ public sealed class MapTarget : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private void ReflectAtMapBounds(ref Vector2 coordinates)
|
||||
protected virtual void TickMovement(float deltaTime)
|
||||
{
|
||||
float maximum = MapCoordinateSystem.CoordinateMaximum;
|
||||
}
|
||||
|
||||
if (coordinates.x < 0f || coordinates.x > maximum)
|
||||
{
|
||||
movementDirection.x = -movementDirection.x;
|
||||
coordinates.x = Mathf.Clamp(coordinates.x, 0f, maximum);
|
||||
}
|
||||
private void TickAttackTimer(float deltaTime)
|
||||
{
|
||||
if (isReadyToAttack)
|
||||
return;
|
||||
|
||||
if (coordinates.y < 0f || coordinates.y > maximum)
|
||||
{
|
||||
movementDirection.y = -movementDirection.y;
|
||||
coordinates.y = Mathf.Clamp(coordinates.y, 0f, maximum);
|
||||
}
|
||||
timeUntilAttack = Mathf.Max(0f, timeUntilAttack - deltaTime);
|
||||
if (timeUntilAttack > 0f)
|
||||
return;
|
||||
|
||||
isReadyToAttack = true;
|
||||
AttackReady?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user