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; AttributesNow.attack = source.AttributesNow.attack; var weapon = source.GetCurrentWeapon(); if (weapon != null) { lifeTime = weapon.Attributes.attackRange / AttributesNow.moveSpeed; } affiliation = source.affiliation; } public override void SetTarget(Vector3 pos) { base.SetTarget(pos); Utils.RotateTool.RotateTransformToDirection(transform, Direction); } protected override void AutoBehave() { Move(); lifeTime -= Time.deltaTime; if (lifeTime <= 0) { Kill(); } } private void OnTriggerEnter2D(Collider2D other) { var entity = other.GetComponent(); if (!entity) { Kill(); return; } if (IsDead || 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; // 如果是友好关系且不允许友军伤害,则不处理 } AttributesNow.health -= 1; } } }