/// <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); } } }
/// <summary> /// Propulse in the given direction, pushing the gameObject in the given direction /// <param name="strength">Value between 0 and 1. Determines the speed of the propulsion.</param> /// </summary> public void Propulse(Vector2 direction, float strength) { // Compute how much the gameObject has to turn opposite to its velocity vector float angleChange = Vector2.Angle((Vector2)rigidbody.velocity, direction); // Augment the thrusting power depending on how much the player has to turn float thrustBoost = 1 + (angleChange / 180) * changeDirectionBoost; // Calculate the final propulsion force Vector3 thrustVector = thrustForce * direction * thrustBoost * strength; // Push the character in the given direction rigidbody.AddForce(thrustVector, ForceMode.Force); // Deplete energy from the player for each ejection lightEnergy.Deplete(thrustEnergyCost * strength); }