80 lines
2.9 KiB
C#
80 lines
2.9 KiB
C#
using UnityEngine;
|
|
|
|
namespace BumpCombat.Defense
|
|
{
|
|
[DisallowMultipleComponent]
|
|
public sealed class DefenseSummonUnit : MonoBehaviour
|
|
{
|
|
private DefenseTower owner;
|
|
private DefensePrototypeController controller;
|
|
private Vector2 anchorLocalPosition;
|
|
private SpriteRenderer spriteRenderer;
|
|
private float attackCooldown;
|
|
|
|
public void Configure(
|
|
DefenseTower tower,
|
|
DefensePrototypeController prototypeController,
|
|
Vector2 localAnchor)
|
|
{
|
|
owner = tower;
|
|
controller = prototypeController;
|
|
anchorLocalPosition = localAnchor;
|
|
spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
|
|
Sprite[] frames = Resources.LoadAll<Sprite>("Tutorial/Swordsman_Idle");
|
|
if (frames != null && frames.Length > 0)
|
|
{
|
|
spriteRenderer.sprite = frames[0];
|
|
}
|
|
spriteRenderer.color = new Color32(255, 234, 137, 255);
|
|
spriteRenderer.sortingOrder = 11;
|
|
transform.localScale = new Vector3(0.9f, 0.9f, 1f);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (owner == null
|
|
|| !owner.isActiveAndEnabled
|
|
|| controller == null
|
|
|| !controller.CanOperateTowers)
|
|
{
|
|
return;
|
|
}
|
|
|
|
attackCooldown = Mathf.Max(0f, attackCooldown - Time.deltaTime);
|
|
DefenseRouteAgent target = controller.FindNearestTarget(
|
|
transform.position,
|
|
controller.Tuning.SummonAreaRadius,
|
|
DefenseTowerKind.Summon);
|
|
Vector2 anchor = owner.transform.TransformPoint(anchorLocalPosition);
|
|
if (target == null)
|
|
{
|
|
transform.position = Vector2.MoveTowards(
|
|
transform.position,
|
|
anchor,
|
|
controller.Tuning.SummonUnitMoveSpeed * Time.deltaTime);
|
|
return;
|
|
}
|
|
|
|
Vector2 targetPosition = target.transform.position;
|
|
Vector2 fromAnchor = targetPosition - anchor;
|
|
if (fromAnchor.magnitude > controller.Tuning.SummonAreaRadius)
|
|
{
|
|
targetPosition = anchor + fromAnchor.normalized * controller.Tuning.SummonAreaRadius;
|
|
}
|
|
|
|
transform.position = Vector2.MoveTowards(
|
|
transform.position,
|
|
targetPosition,
|
|
controller.Tuning.SummonUnitMoveSpeed * Time.deltaTime);
|
|
if (attackCooldown <= 0f
|
|
&& (target.transform.position - transform.position).sqrMagnitude <= controller.Tuning.SummonAttackRange * controller.Tuning.SummonAttackRange)
|
|
{
|
|
if (owner.TryAttack(target))
|
|
{
|
|
attackCooldown = controller.Tuning.SummonAttackInterval;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|