using Base; using Data; using Managers; using Prefab; using UnityEngine; using Utils; namespace Entity { public class Bullet : Entity { public float lifeTime = 10; private BulletDef bulletDef; public Entity bulletSource { get; private set; } private void OnTriggerEnter2D(Collider2D other) { var entity = other.GetComponent()?.entity; if (!entity) { Kill(); return; } if (IsDead || entity == bulletSource || entity is not CombatantEntity) return; if (AffiliationManager.Instance.GetRelation(bulletSource.affiliation, entity.affiliation) != Relation.Friendly || Setting.Instance.CurrentSettings.friendlyFire) { entity.OnHit(bulletSource); bulletSource.OnDealHit(entity); if (bulletDef?.hitHediffs != null && entity is LivingEntity livingEntity) foreach (var hediff in bulletDef.hitHediffs) livingEntity.AddHediff(new Hediff(hediff)); } else { return; // 如果是友好关系且不允许友军伤害,则不处理 } OnHit(1, false); } public override void Init(EntityDef entityDefine) { base.Init(entityDefine); bulletDef = entityDefine as BulletDef; } 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 bool SetTarget(Vector3 pos) { base.SetTarget(pos); RotateTool.RotateTransformToDirection(transform, Direction); return true; } protected override void AutoBehave() { Move(); lifeTime -= Time.deltaTime; if (lifeTime <= 0) Kill(); } } }