2025-08-19 20:22:10 +08:00
|
|
|
using Base;
|
|
|
|
|
using Data;
|
2025-10-10 14:08:23 +08:00
|
|
|
using Managers;
|
|
|
|
|
using Prefab;
|
2025-08-19 20:22:10 +08:00
|
|
|
using UnityEngine;
|
2025-10-10 14:08:23 +08:00
|
|
|
using Utils;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
namespace Entity
|
|
|
|
|
{
|
|
|
|
|
public class Bullet : Entity
|
|
|
|
|
{
|
|
|
|
|
public float lifeTime = 10;
|
2025-08-28 16:20:24 +08:00
|
|
|
|
2025-10-10 14:08:23 +08:00
|
|
|
private BulletDef bulletDef;
|
|
|
|
|
public Entity bulletSource { get; private set; }
|
|
|
|
|
|
|
|
|
|
private void OnTriggerEnter2D(Collider2D other)
|
|
|
|
|
{
|
|
|
|
|
var entity = other.GetComponent<EntityPrefab>()?.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;
|
|
|
|
|
}
|
2025-09-19 08:26:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public void SetBulletSource(Entity source)
|
|
|
|
|
{
|
|
|
|
|
bulletSource = source;
|
2025-10-03 00:31:34 +08:00
|
|
|
AttributesNow.attack = source.AttributesNow.attack;
|
2025-09-19 08:26:54 +08:00
|
|
|
var weapon = source.GetCurrentWeapon();
|
2025-10-10 14:08:23 +08:00
|
|
|
if (weapon != null) lifeTime = weapon.Attributes.attackRange / AttributesNow.moveSpeed;
|
2025-10-03 00:31:34 +08:00
|
|
|
|
|
|
|
|
affiliation = source.affiliation;
|
2025-09-19 08:26:54 +08:00
|
|
|
}
|
2025-10-10 14:08:23 +08:00
|
|
|
|
|
|
|
|
public override bool SetTarget(Vector3 pos)
|
2025-08-19 20:22:10 +08:00
|
|
|
{
|
|
|
|
|
base.SetTarget(pos);
|
2025-10-10 14:08:23 +08:00
|
|
|
RotateTool.RotateTransformToDirection(transform, Direction);
|
|
|
|
|
return true;
|
2025-08-19 20:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void AutoBehave()
|
|
|
|
|
{
|
2025-10-03 00:31:34 +08:00
|
|
|
Move();
|
2025-08-28 16:20:24 +08:00
|
|
|
lifeTime -= Time.deltaTime;
|
2025-10-10 14:08:23 +08:00
|
|
|
if (lifeTime <= 0) Kill();
|
2025-08-19 20:22:10 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|