示例#1
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        GameObject collider = collision.gameObject;
        Projectile proj     = collider.GetComponent <Projectile>();

        /* Don't collide with source */
        if (collider == source)
        {
            return;
        }

        /* Don't collide with the floor */
        if (collider.layer == LayerMask.NameToLayer("Ground") || collider.layer == LayerMask.NameToLayer("Object"))
        {
            return;
        }

        /* Do / Don't collide with other projectiles */
        if (!collideWithProjectile && (proj))
        {
            return;
        }

        /* Do / Don't collide with walls */
        if (!collideWithWall && collider.tag == "Wall")
        {
            return;
        }

        /* Do / Don't collide with things w/ the same tag as source */
        if (collider.tag == sourceTag && !friendlyFire)
        {
            return;
        }

        /* Don't collide with other projectiles shot by the same source */
        if (proj && (proj.source == source))
        {
            return;
        }

        /* Deflect with weapons if applicable, which will abort the rest of the collision */
        if (proj && proj.tag != sourceTag)
        {
            if ((proj.ProjectileType() == 1 && reflectMelee) || (proj.ProjectileType() != 1 && reflectRanged))
            {
                Reflect(proj);
                return;
            }
        }

        EntityStats ent = collider.GetComponent <EntityStats>();

        if (ent)
        {
            // If the collision is with something that uniquely reacts to projectiles
            EnemyCombat ec = collider.GetComponent <EnemyCombat>();
            if (ec)
            {
                if (ec.OnProjectileHit(this))
                {
                    return;
                }
            }
            pierceCount++;
            if (weaponSystem != null)
            {
                weaponSystem.OnHit(this, ent);
            }

            // Deal damage
            if (weaponSystem != null)
            {
                ent.setLastHit(ammoRefill);
            }
            EntityStats attacker = (source) ? source.GetComponent <EntityStats>() : null;
            ent.TakeDamage(damage, attacker, false, sourceName);


            // Inflict attribute if relevant
            if ((attrHit != null) && (Random.value <= attrChance))
            {
                ent.AddAttribute(attrHit, attacker);
            }
        }

        /* Range proj. always destroy on non-entities */
        //var weaponIsMelee = tables.tagWeapon.get(weaponClass) == WeaponFactory.TAG.MELEE;
        if ((destroyOnNonEntity && !ent) || destroyOnCollide)
        {
            Destroy();
        }
    }