129 lines
4.0 KiB
C#
129 lines
4.0 KiB
C#
using GeometryTD.Definition;
|
|
using GeometryTD.Entity.EntityData;
|
|
using GeometryTD;
|
|
using GeometryTD.Entity;
|
|
using UnityEngine;
|
|
|
|
namespace Components
|
|
{
|
|
[DisallowMultipleComponent]
|
|
public class ShooterMuzzleComp : MonoBehaviour
|
|
{
|
|
[SerializeField] [Min(1f)] private int _attackDamage = 100;
|
|
|
|
[SerializeField] private AttackMethodType _attackMethodType = AttackMethodType.NormalBullet;
|
|
|
|
[SerializeField] [Min(1)] private int _bulletTypeId = 501;
|
|
|
|
[SerializeField] private Transform _muzzlePoint;
|
|
|
|
[SerializeField] [Min(0.1f)] private float _bulletSpeed = 12f;
|
|
|
|
[SerializeField] private SpriteRenderer _renderer;
|
|
|
|
private TagRuntimeData[] _tagRuntimes = System.Array.Empty<TagRuntimeData>();
|
|
|
|
public int AttackDamage => _attackDamage;
|
|
public AttackMethodType AttackMethodType => _attackMethodType;
|
|
|
|
public void OnInit(
|
|
int attackDamage,
|
|
AttackMethodType attackMethodType = AttackMethodType.NormalBullet,
|
|
int bulletTypeId = 501,
|
|
TagRuntimeData[] tagRuntimes = null)
|
|
{
|
|
_attackDamage = Mathf.Max(1, attackDamage);
|
|
_attackMethodType = attackMethodType;
|
|
_bulletTypeId = Mathf.Max(1, bulletTypeId);
|
|
_tagRuntimes = CloneTagRuntimes(tagRuntimes);
|
|
}
|
|
|
|
public void OnReset()
|
|
{
|
|
_attackDamage = 1;
|
|
_attackMethodType = AttackMethodType.None;
|
|
_bulletTypeId = 501;
|
|
_tagRuntimes = System.Array.Empty<TagRuntimeData>();
|
|
}
|
|
|
|
public void SetColor(Color color)
|
|
{
|
|
if (_renderer != null)
|
|
{
|
|
_renderer.color = color;
|
|
}
|
|
}
|
|
|
|
public bool Attack(Transform target)
|
|
{
|
|
return Attack(target, AttackPropertyType.None);
|
|
}
|
|
|
|
public bool Attack(Transform target, AttackPropertyType attackPropertyType)
|
|
{
|
|
if (_attackMethodType != AttackMethodType.NormalBullet)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (target == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
Transform spawnPoint = _muzzlePoint != null ? _muzzlePoint : transform;
|
|
int bulletEntityId = GameEntry.Entity.GenerateSerialId();
|
|
AttackPayload attackPayload = new AttackPayload
|
|
{
|
|
BaseDamage = _attackDamage,
|
|
AttackPropertyType = attackPropertyType,
|
|
SourceEntityId = ResolveSourceEntityId(),
|
|
ProjectileEntityId = bulletEntityId,
|
|
OriginPosition = spawnPoint.position,
|
|
TagRuntimes = CloneTagRuntimes(_tagRuntimes)
|
|
};
|
|
BulletData bulletData = new BulletData(
|
|
bulletEntityId,
|
|
_bulletTypeId,
|
|
spawnPoint.position,
|
|
target,
|
|
attackPayload,
|
|
_bulletSpeed);
|
|
GameEntry.Entity.ShowBullet(bulletData);
|
|
return true;
|
|
}
|
|
|
|
private static TagRuntimeData[] CloneTagRuntimes(TagRuntimeData[] tagRuntimes)
|
|
{
|
|
if (tagRuntimes == null || tagRuntimes.Length <= 0)
|
|
{
|
|
return System.Array.Empty<TagRuntimeData>();
|
|
}
|
|
|
|
TagRuntimeData[] cloned = new TagRuntimeData[tagRuntimes.Length];
|
|
for (int i = 0; i < tagRuntimes.Length; i++)
|
|
{
|
|
TagRuntimeData runtime = tagRuntimes[i];
|
|
if (runtime == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
cloned[i] = new TagRuntimeData
|
|
{
|
|
TagType = runtime.TagType,
|
|
TotalStack = runtime.TotalStack
|
|
};
|
|
}
|
|
|
|
return cloned;
|
|
}
|
|
|
|
private int ResolveSourceEntityId()
|
|
{
|
|
TowerEntity towerEntity = GetComponentInParent<TowerEntity>();
|
|
return towerEntity != null ? towerEntity.Id : 0;
|
|
}
|
|
}
|
|
}
|