Example #1
0
        private void OnCollisionEnter(Collision collision)
        {
            var body_hit   = collision.collider.GetComponentInParent <ColoredBody>();
            var bullet_hit = collision.collider.GetComponentInParent <Bullet>();

            if (body_hit != null &&                                        // hit a colored body...
                (bullet_hit == null || allow_collision_with_other_bullets) // ... which is not another bullet, or is allowed to be hit...
                )
            {
                // Debug.Log("OnCollisionEnter" + name + " and " + collision.gameObject.name);

                bool colors_matches    = ColorSystem.colors_matches(body_hit.color_family, my_body.color_family);
                bool hitting_the_enemy = clan_who_owns != body_hit.clan; // ... we are either enemy bullet hitting player or the reverse...
                bool on_hit_invoked    = false;
                if (colors_matches)
                {
                    if (hitting_the_enemy)
                    {
                        // ... We hit an enemy matching the right color!
                        BulletEvents.InvokeOnHit(this, body_hit);
                        on_hit_invoked = true;
                        body_hit.on_hit();
                    }
                    if (body_hit.surface_effect == ColoredBody.SurfaceEffect.reflective)
                    {
                        body_hit.play_reflected_collision_sound();
                        my_body.play_reflected_collision_sound();
                        end_with_reflection(collision, body_hit);
                    }
                }

                // We hit something solid, so the bullet will end anyway.
                if (body_hit.surface_effect == ColoredBody.SurfaceEffect.solid)
                {
                    end_with_impact(collision);
                }

                if (!on_hit_invoked)
                {
                    BulletEvents.InvokeOnAbsorved(this, body_hit);
                    if (clan_who_owns == Clan.player && hitting_the_enemy)
                    {
                        MusicEventManager.Instance.play_bullet_ineffective_sound();
                    }
                }
                has_hit_body = true;
            }
        }