Files
Gen_Hack-and-Slash-Roguelit…/Client/Assets/Scripts/Entity/Bullet.cs

54 lines
1.4 KiB
C#

using Base;
using Data;
using UnityEngine;
namespace Entity
{
public class Bullet : Entity
{
public Entity bulletSource { get; private set; }
public float lifeTime = 10;
public void SetBulletSource(Entity source)
{
bulletSource = source;
attributes.attack = source.attributes.attack;
}
public override void SetTarget(Vector3 pos)
{
base.SetTarget(pos);
Utils.RotateTool.RotateTransformToDirection(transform, direction);
}
protected override void AutoBehave()
{
TryMove();
lifeTime -= Time.deltaTime;
if (lifeTime <= 0)
{
Kill();
}
}
private void OnTriggerEnter2D(Collider2D other)
{
var entity = other.GetComponent<Entity>();
if (!entity || entity == bulletSource || entity is Pickup) return;
if (Managers.AffiliationManager.Instance.GetRelation(bulletSource.affiliation, entity.affiliation) != Relation.Friendly || Setting.Instance.CurrentSettings.friendlyFire)
{
entity.OnHit(this);
}
else
{
return; // 如果是友好关系且不允许友军伤害,则不处理
}
attributes.health -= 1;
}
}
}