private IEnumerator SetRandomColors()
    {
        float duration = Random.Range(bb.bodyColorsCarrouselMinTime, bb.bodyColorsCarrouselMaxTime);

        float elapsedTime = 0;

        while (elapsedTime < duration)
        {
            if (invulnerable)
            {
                SetMaterial(rsc.coloredObjectsMng.GetWormBodyDimMaterial(ChromaColorInfo.Random));
            }
            else
            {
                SetMaterial(rsc.coloredObjectsMng.GetWormBodyMaterial(ChromaColorInfo.Random));
            }

            yield return(new WaitForSeconds(bb.bodyColorsCarrouselChangeInterval));

            elapsedTime += bb.bodyColorsCarrouselChangeInterval;
        }

        if (invulnerable)
        {
            SetMaterial(rsc.coloredObjectsMng.GetWormBodyDimMaterial(color));
        }
        else
        {
            SetMaterial(rsc.coloredObjectsMng.GetWormBodyMaterial(color));
        }

        bodyState = BodySubState.NORMAL;
    }
    void Awake()
    {
        blinkController = GetComponent <BlinkController>();
        rend            = GetComponentInChildren <Renderer>();
        col             = GetComponent <BoxCollider>();
        voxelization    = GetComponentInChildren <VoxelizationClient>();
        bodyState       = BodySubState.NORMAL;

        bodyDeactivate = new ParticleSystem[bodyDeactivatePrefabs.Length];

        Transform fx = transform.FindDeepChild("FX");

        GameObject temp;

        for (int i = 0; i < bodyDeactivatePrefabs.Length; ++i)
        {
            temp = Instantiate(bodyDeactivatePrefabs[i]);
            temp.transform.SetParent(fx);
            temp.transform.localPosition = Vector3.zero;
            bodyDeactivate[i]            = temp.GetComponent <ParticleSystem>();
        }

        temp = Instantiate(bodyDestructionPrefab);
        temp.transform.SetParent(fx);
        temp.transform.localPosition = Vector3.zero;
        bodyDestruction = temp.GetComponent <ParticleSystem>();

        overground   = false;
        invulnerable = false;
    }
    public void Consolidate(ChromaColor color)
    {
        if (bodyState == BodySubState.DESTROYED)
        {
            return;
        }

        if (bodyState == BodySubState.DEACTIVATED)
        {
            SetMaterial(rsc.coloredObjectsMng.GetWormBodyWireframeMaterial());
            bodyDestruction.Play();
            bodyState = BodySubState.DESTROYED;
            //col.enabled = false;
            col.isTrigger = true;
        }
        else
        {
            ChromaColor oldColor = this.color;
            this.color              = color;
            currentDamage           = 0;
            currentDamageWrongColor = 0;

            ColorEventInfo.eventInfo.oldColor = oldColor;
            ColorEventInfo.eventInfo.newColor = color;
            rsc.eventMng.TriggerEvent(EventManager.EventType.WORM_SECTION_COLOR_CHANGED, ColorEventInfo.eventInfo);

            bodyState = BodySubState.SETTING;
            StartCoroutine(SetRandomColors());
        }
    }
    public void ResetColor(ChromaColor color)
    {
        if (bodyState == BodySubState.DESTROYED)
        {
            return;
        }

        ChromaColor oldColor = this.color;

        if (bodyState == BodySubState.DEACTIVATED)
        {
            ColorEventInfo.eventInfo.newColor = color;
            rsc.eventMng.TriggerEvent(EventManager.EventType.WORM_SECTION_ACTIVATED, ColorEventInfo.eventInfo);
        }
        else
        {
            ColorEventInfo.eventInfo.oldColor = oldColor;
            ColorEventInfo.eventInfo.newColor = color;
            rsc.eventMng.TriggerEvent(EventManager.EventType.WORM_SECTION_COLOR_CHANGED, ColorEventInfo.eventInfo);
        }

        this.color              = color;
        currentDamage           = 0;
        currentDamageWrongColor = 0;
        bodyState = BodySubState.SETTING;

        StartCoroutine(SetRandomColors());
    }
    public void ImpactedByShot(ChromaColor shotColor, float damage, PlayerController player)
    {
        if (bodyState != BodySubState.NORMAL || invulnerable)
        {
            return;
        }

        blinkController.BlinkWhiteOnce();

        if (shotColor != color && !rsc.debugMng.alwaysKillOk)
        {
            currentDamageWrongColor += damage * bb.HealthSettingsPhase.bodyWrongColorDamageModifier;

            if (currentDamageWrongColor >= bb.HealthSettingsPhase.bodyMaxHealth)
            {
                bodyState = BodySubState.DEACTIVATED; //not really deactivated but flagged to allow notify properly when colors reset

                explosionWrongSoundFx.Play();
                rsc.rumbleMng.Rumble(player.Id, 0.25f, 0f, 0.5f);
                rsc.camerasMng.PlayEffect(0, 0.25f, 0.2f);

                EnemyDiedEventInfo.eventInfo.color           = color;
                EnemyDiedEventInfo.eventInfo.infectionValue  = 0;
                EnemyDiedEventInfo.eventInfo.killerPlayer    = player;
                EnemyDiedEventInfo.eventInfo.killedSameColor = (color == shotColor);
                EnemyDiedEventInfo.eventInfo.specialKill     = false;
                rsc.eventMng.TriggerEvent(EventManager.EventType.WORM_SECTION_DESTROYED, EnemyDiedEventInfo.eventInfo);

                player.ColorMismatch();

                head.DischargeHead();
            }
        }
        else
        {
            currentDamage += damage;

            if (currentDamage >= bb.HealthSettingsPhase.bodyMaxHealth)
            {
                //Set material grey
                SetMaterial(rsc.coloredObjectsMng.GetWormBodyGreyMaterial());
                bodyDeactivate[(int)color].Play();
                bodyState = BodySubState.DEACTIVATED;

                explosionOkSoundFx.Play();
                rsc.rumbleMng.Rumble(player.Id, 0.25f, 0f, 0.5f);
                rsc.camerasMng.PlayEffect(0, 0.25f, 0.2f);

                EnemyDiedEventInfo.eventInfo.color           = color;
                EnemyDiedEventInfo.eventInfo.infectionValue  = 0;
                EnemyDiedEventInfo.eventInfo.killerPlayer    = player;
                EnemyDiedEventInfo.eventInfo.killedSameColor = (color == shotColor);
                EnemyDiedEventInfo.eventInfo.specialKill     = false;
                rsc.eventMng.TriggerEvent(EventManager.EventType.WORM_SECTION_DESTROYED, EnemyDiedEventInfo.eventInfo);

                head.ChargeHead();
            }
        }
    }
    public void Disable()
    {
        if (bodyState != BodySubState.NORMAL)
        {
            return;
        }

        SetMaterial(rsc.coloredObjectsMng.GetWormBodyDimMaterial(color));
        bodyState = BodySubState.NORMAL_DISABLED;
    }
    public void SetInitialState(ChromaColor c)
    {
        color                   = c;
        currentDamage           = 0;
        currentDamageWrongColor = 0;
        SetMaterial(rsc.coloredObjectsMng.GetWormBodyMaterial(color));

        ColorEventInfo.eventInfo.newColor = color;
        rsc.eventMng.TriggerEvent(EventManager.EventType.WORM_SECTION_ACTIVATED, ColorEventInfo.eventInfo);

        bodyState = BodySubState.NORMAL;
    }