Exemplo n.º 1
0
        void HandleCollision()
        {
            #region Pickups
            foreach (PlayerShip pShip in PlayerShips)
            {
                for (int i = 0; i < Pickups.Count; i++)
                {
                    if (pShip.AABB.Intersects(new Rectangle(Pickups[i].Location.Xi(), Pickups[i].Location.Yi(), 32, 32)))
                    {
                        switch (Pickups[i].PickupType)
                        {
                        case ePickupType.HP:
                            pShip.Heal(65);
                            break;

                        case ePickupType.Boom1:
                            PickupWeapon(pShip, eEnemyGunType.Boom1Enemy);
                            break;

                        case ePickupType.Aim:
                            PickupWeapon(pShip, eEnemyGunType.AutoAim);
                            break;

                        case ePickupType.Missile:
                            PickupWeapon(pShip, eEnemyGunType.Missile);
                            break;

                        case ePickupType.Speed:
                            if (!pShip.UpgradeSpeed())
                            {
                                pShip.Heal(DOUBLE_PICKUP_HEAL_AMOUNT);
                            }
                            break;

                        case ePickupType.Shield:
                            if (!pShip.UpgradeShield())
                            {
                                pShip.Heal(DOUBLE_PICKUP_HEAL_AMOUNT);
                            }
                            pShip.Shield.CurrentHP += 7;
                            break;

                        case ePickupType.DualMissile:
                            PickupWeapon(pShip, eEnemyGunType.DualMissile45);
                            break;

                        case ePickupType.ShieldRegen:
                            if (!pShip.UpgradeShieldRegen())
                            {
                                pShip.Heal(DOUBLE_PICKUP_HEAL_AMOUNT);
                            }
                            break;

                        default:
                            throw new NotImplementedException();
                        }

                        Pickups[i].IsDisposed = true;
                        pShip.Owner.Score    += (int)((5 + WaveNr) * Level.Instance.ScoreModifier);
                    }
                }
            }
            #endregion

            #region Scrapstation
            if (ActiveScrapStation != null && !ActiveScrapStation.IsGoingHome)
            {
                if (PlayerShips[0].AABB.Intersects(ActiveScrapStation.AABB))
                {
                    foreach (Rectangle2 rect in PlayerShips[0].CollisionRects)
                    {
                        foreach (Rectangle2 rect2 in ActiveScrapStation.CollisionRects)
                        {
                            if (rect.Absolute.Intersects(rect2.Absolute))
                            {
                                ActiveScrapStation.Visit(PlayerShips[0]);
                                return;
                            }
                        }
                    }
                }
            }
            #endregion

            #region Other Collisions
            // A note about the 'is' operator performance: http://stackoverflow.com/questions/686412/c-is-operator-performance
            for (int y = 0; y < BroadPhase.GRID_CNT; y++)
            {
                for (int x = 0; x < BroadPhase.GRID_CNT; x++)
                {
                    if (BroadPhase.Instance.Blocks[x, y].Entities.Count > 0)
                    {
                        for (int i = 0; i < BroadPhase.Instance.Blocks[x, y].Entities.Count; i++)
                        {
                            PlayerShip pShip = BroadPhase.Instance.Blocks[x, y].Entities[i] as PlayerShip;
                            if (pShip != null)
                            {
                                for (int j = 0; j < BroadPhase.Instance.Blocks[x, y].Entities.Count; j++)
                                {
                                    // Player <--> enemy projectile
                                    BaseProjectile p = BroadPhase.Instance.Blocks[x, y].Entities[j] as BaseProjectile;
                                    if (p != null)
                                    {
                                        if (p.Owner == null && !p.IsDisposed && pShip.AABB.Intersects(BroadPhase.Instance.Blocks[x, y].Entities[j].AABB))
                                        {
                                            foreach (Rectangle2 rect in pShip.CollisionRects)
                                            {
                                                if (p.AABB.Intersects(rect.Absolute))
                                                {
                                                    pShip.TakeDamage(p.Damage);
                                                    p.IsDisposed = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    // Player <--> enemy collision
                                    else
                                    {
                                        BaseEnemy e = BroadPhase.Instance.Blocks[x, y].Entities[j] as BaseEnemy;
                                        if (e != null)
                                        {
                                            if (!e.IsDisposed && pShip.AABB.Intersects(e.AABB))
                                            {
                                                foreach (Rectangle2 rect in pShip.CollisionRects)
                                                {
                                                    if (e.AABB.Intersects(rect.Absolute))
                                                    {
                                                        pShip.TakeDamage(e.CollisionDamage);
                                                        pShip.Owner.Kills++;
                                                        e.Die();
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            // enemy <--> player projectile
                            else if (BroadPhase.Instance.Blocks[x, y].Entities[i] is BaseEnemy)
                            {
                                BaseEnemy e = (BaseEnemy)BroadPhase.Instance.Blocks[x, y].Entities[i];

                                for (int j = 0; j < BroadPhase.Instance.Blocks[x, y].Entities.Count; j++)
                                {
                                    BaseProjectile p = BroadPhase.Instance.Blocks[x, y].Entities[j] as BaseProjectile;
                                    if (p != null)
                                    {
                                        if (p.Owner != null && e.AABB.Intersects(p.AABB) && !p.IsDisposed)
                                        {
                                            e.TakeDamage(p.Damage, p.Owner);
                                            p.IsDisposed = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion
        }