炸弹箭 (U7)

炸弹箭 (U7)

跟随箭矢狂潮,现在推出爆炸箭!因为我们永远不嫌强力武器多。查看视频吧,真的很难拍到好照片。

武器

这是代码(旧版,来自U6)

public class ItemBombArrow : MonoBehaviour
{
    protected Item item;
    private ParticleSystem explosionVFX;
    private Transform mesh;
    private AudioSource explosionSFX;

    private bool hasExploded;
    private bool isBombActive;
    private float distNPC = 0f;
    private float expReductor = 0f;
    private Vector3 expForceDirection;
    public float explosionRadius = 5f;
    public float expForceMultiplier = 50f;

    protected void Awake()
    {
        item = this.GetComponent<Item>();
        mesh = item.transform.Find("Mesh");
        item.OnCollisionEvent += OnBombCollisionEvent;
        item.OnGrabEvent += OnBombGrabEvent;
        item.OnTeleGrabEvent += OnBombTeleGrab;
        item.OnUngrabEvent += OnBombUngrabEvent;
        item.OnSpawnEvent += OnBombSpawn;
    }

    protected void Start()
    {
        hasExploded = false;
        isBombActive = false;
        mesh.gameObject.SetActive(true);
        item.gameObject.SetActive(true);
        explosionVFX = item.gameObject.GetComponentInChildren<ParticleSystem>();
        explosionSFX = item.transform.Find("ExplosionSFX").GetComponent<AudioSource>();
    }

    void OnBombSpawn()
    {
        hasExploded = false;
        isBombActive = false;
        mesh.gameObject.SetActive(true);
        item.gameObject.SetActive(true);
    }

    void OnBombCollisionEvent(ref CollisionInstance collisionInstance)
    {
        if (isBombActive && !hasExploded)
        {
            hasExploded = true;
            mesh.gameObject.SetActive(false);
            Explode(explosionRadius);
        }
    }

    void OnBombTeleGrab(Handle handle, TeleGrabber teleGrabber)
    {
        DeactivateBomb();
    }

    void OnBombGrabEvent(Handle handle, Interactor interactor)
    {
        DeactivateBomb();
    }

    void OnBombUngrabEvent(Handle handle, Interactor interactor, bool throwing)
    {
    }

    void FixedUpdate()
    {
        if (item.isFlying)
        {
            isBombActive = true;
        }
        else
        {
            isBombActive = false;
        }

        if (hasExploded && !explosionSFX.isPlaying && !explosionVFX.isPlaying)
        {
            item.gameObject.SetActive(false);
        }
    }

    void DeactivateBomb()
    {
        isBombActive = false;
    }

    void Explode(float radius)
    {
        PlayExplosionEffects();
        foreach (Creature npc in CreatureList.Instance.creatures)
        {
            if (npc != PlayerCharacter.LocalPlayer)
            {
                distNPC = Vector3.Distance(npc.gameObject.transform.position, item.transform.position);
                if (distNPC < radius)
                {
                    expReductor = (radius - distNPC) / radius;
                    expForceDirection = npc.gameObject.transform.position - item.transform.position;
                    expForceDirection.Normalize();
                    expForceDirection += Vector3.up;
                    expForceDirection.Normalize();
                    if (npc.state != Creature.State.Dead)
                    {
                        npc.ragdoll.SetState(BS.Ragdoll.State.Fallen);
                    }
                    foreach (RagdollPart ragdollPart in npc.ragdoll.parts)
                    {
                        ragdollPart.rb.AddForce(expForceDirection * expForceMultiplier * expReductor, ForceMode.Impulse);
                    }
                    npc.health.currentHealth -= 2000f * expReductor;
                }
            }
        }
    }

    void PlayExplosionEffects()
    {
        explosionVFX.Play();
        explosionSFX.Play();
    }
}