Ejemplo n.º 1
0
        public static void DestroyOnExplosionAndRevealPowerUp(EntityManager em, int entityLiveId, uint message, ref MessageData data)
        {
            // TODO: There is a bug where two power ups can appear in the same spot if that block is hit by
            // two explosions in the same update cycle. We need to check if the block is on the delete list (since
            // we delay deletion). An alternative is that we could just remove the handler?
            Debug.Assert(message == Messages.HitByInitialExplosion);
            em.DelayFreeByLiveId(entityLiveId, EntityFreeOptions.Deep);

            if (random.NextDouble() < GameConstants.PowerUpChance)
            {
                // Choose based on weights
                float totalWeight = 0f;
                foreach (PowerUpProbabilities pup in powerUpTemplates)
                {
                    totalWeight += pup.Probability;
                }
                float  choice    = (float)random.NextDouble();
                string template  = null;
                float  accWeight = 0f;
                foreach (PowerUpProbabilities pup in powerUpTemplates)
                {
                    accWeight += pup.Probability;
                    if (choice <= (accWeight / totalWeight))
                    {
                        template = pup.TemplateName;
                        break;
                    }
                }

                Entity entityPowerUp = em.AllocateForGeneratedContent(template, Universe.TopLevelGroupUniqueIdBase);
                Helpers.TransferPlacement(em, entityLiveId, entityPowerUp.LiveId);
                // Depending on our game settings, power ups may or may not be affected by explosions
                if (true) // For now we'll always assume they are
                {
                    bool           allocatedNew;
                    MessageHandler messageHandler = (MessageHandler)em.AddComponentToEntity(entityPowerUp, ComponentTypeIds.MessageHandler, out allocatedNew);
                    messageHandler.Add(new MessageAndHandler(Messages.HitByInitialExplosion, "SimplyDestroy".CRC32Hash()));
                }
            }
            data.Handled = true;
        }