Map mehanic ready to multiplayer
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
[DisallowMultipleComponent]
|
||||
@@ -6,17 +5,18 @@ public class MapTarget : MonoBehaviour
|
||||
{
|
||||
[Header("Target State (Runtime)")]
|
||||
[SerializeField] private TargetConfig config;
|
||||
[SerializeField] private int targetId = -1;
|
||||
[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;
|
||||
private Collider[] targetColliders;
|
||||
|
||||
public int TargetId => targetId;
|
||||
public Vector2 MapCoordinates => mapCoordinates;
|
||||
public MapCoordinateSystem CoordinateSystem => coordinateSystem;
|
||||
public TargetConfig Config => config;
|
||||
@@ -24,34 +24,26 @@ public class MapTarget : MonoBehaviour
|
||||
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()
|
||||
public void ApplyNetworkState(MapTargetNetworkState state)
|
||||
{
|
||||
if (!isConfigured)
|
||||
return;
|
||||
|
||||
TickAttackTimer(Time.deltaTime);
|
||||
TickMovement(Time.deltaTime);
|
||||
targetId = state.TargetId;
|
||||
timeUntilAttack = state.AttackTimeRemaining;
|
||||
currentHealth = state.Health;
|
||||
requiredMissileType = config != null
|
||||
? config.RequiredMissileType
|
||||
: MissileType.None;
|
||||
SetMapCoordinates(state.Coordinates);
|
||||
}
|
||||
|
||||
public void SetMapCoordinates(Vector2 coordinates)
|
||||
@@ -62,41 +54,34 @@ public class MapTarget : MonoBehaviour
|
||||
Mathf.Clamp(coordinates.x, 0f, MapCoordinateSystem.CoordinateMaximum),
|
||||
Mathf.Clamp(coordinates.y, 0f, MapCoordinateSystem.CoordinateMaximum));
|
||||
|
||||
if (isConfigured)
|
||||
if (coordinateSystem != null && coordinateSystem.HasValidCorners())
|
||||
transform.position = coordinateSystem.CoordinatesToWorldPosition(mapCoordinates);
|
||||
}
|
||||
|
||||
public Vector2 GetMapCoordinates()
|
||||
{
|
||||
return mapCoordinates;
|
||||
}
|
||||
public Vector2 GetMapCoordinates() => mapCoordinates;
|
||||
|
||||
public void SetVisualsVisible(bool isVisible)
|
||||
{
|
||||
if (targetRenderers == null)
|
||||
targetRenderers = GetComponentsInChildren<Renderer>(true);
|
||||
if (targetColliders == null)
|
||||
targetColliders = GetComponentsInChildren<Collider>(true);
|
||||
|
||||
foreach (Renderer targetRenderer in targetRenderers)
|
||||
{
|
||||
if (targetRenderer != null)
|
||||
targetRenderer.enabled = isVisible;
|
||||
}
|
||||
|
||||
// Map targets are presentation objects, never physical world objects.
|
||||
foreach (Collider targetCollider in targetColliders)
|
||||
{
|
||||
if (targetCollider != null)
|
||||
targetCollider.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user