Exemple #1
0
    /// <summary>
    /// Update is called every frame, if the MonoBehaviour is enabled
    /// <see cref="Unity Documentation">
    /// </summary>
    protected virtual void Update()
    {
        // Cycle through each absorbable light source being touched by this GameObject
        for (int i = 0; i < absorbableLightDetector.NeighbourCount; i++)
        {
            GameObject absorbableLight = absorbableLightDetector.GetNeighbour(i);
            if (absorbableLight == null)
            {
                continue;
            }

            LightSource otherLightSource = absorbableLight.GetComponentInParent <LightSource>();
            if (otherLightSource == null)
            {
                continue;
            }

            // If this GameObject can absorb the touched light source,
            // Transfer light energy from the other light source to this one
            if (CanAbsorb(otherLightSource))
            {
                if (this is Player)
                {
                    PlayerSound playerSound = GetComponent <PlayerSound>();
                    playerSound.EatSound();
                }
                LightEnergy lightEnergyToAbsorb = otherLightSource.LightEnergy;

                // Calculate the amount of light to absorb from the other light source
                float lightToAbsorb = absorptionRate * Time.deltaTime;

                // If the player was hit
                if (otherLightSource is Player)
                {
                    if (this is AbstractFish)
                    {
                        // Absorb a certain amount of light from the player to the fish
                        AbstractFish fish = (AbstractFish)this;
                        lightToAbsorb = fish.damageInflicted;
                    }

                    // Debug.Log("Absorb " + lightToAbsorb + " from player");

                    // Knockback the player away from the enemy fish
                    otherLightSource.Knockback(this);
                }

                // Transfer light energy from the other light source to this one
                float lightAbsorbed = lightEnergyToAbsorb.Deplete(lightToAbsorb);
                lightEnergy.Add(lightAbsorbed);

                // Inform subscribers that this light source consumed another light source.
                ConsumedLightSource(otherLightSource);
            }
        }
    }