Gameplay.TileManager - robblofield/TomeboundDocs GitHub Wiki

Gameplay.TileManager

Author: Shahrzad Beevis
Last Updated: 14/05/2024

Overview

A script controlling the mechanics within phase 2 of the first boss encounter.

Dependencies

N/A

Contents

  • Use case
  • Breakdown of code
    • Using Statements
    • Class Declaration
    • Variables
    • Method/StartAnimating()
    • Method/StopAnimating()
    • Method/SelectTiles()
    • Method/AnimateTile(int index)
    • Method/GetRandomTileIndex()
    • Method/FindSteamCollider(GameObject tile)
    • Method/FindSteamColumn(GameObject tile)
    • Method/FindDustParticles(GameObject tile)
  • Future Expansion
  • Full Code Reference

Use case

This script sequentially coordinates the animation of tiles, and their associated animations for phase 2 of the first boss encounter.

Breakdown of Code

Using Statements

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

Class Declaration

public class TileManager : MonoBehaviour

Variables

public GameObject[] tiles;
public float indicatorDuration = 2f;
public float activationDuration = 3f;
public int simultaneousTiles = 3;
public float delayBetweenSets = 4f;
public float dustWaitTime = 0.9f;

private List<int> animatedIndices = new List<int>();
private bool isAnimating = false;

public GameObject[] tiles: An array of GameObjects representing the tiles to be animated.

public float indicatorDuration = 2f: Duration of the indicator before tile activation.

public float activationDuration = 3f: Duration of the tile activation.

public int simultaneousTiles = 3: Number of tiles to animate simultaneously.

public float delayBetweenSets = 4f):

Delay between sets of simultaneous tile animations.

public float dustWaitTime = 0.9f:

Time to wait before activating dust particles after the tile animation.

private List animatedIndices = new List():

A list of integers representing the indices of tiles currently being animated.

private bool isAnimating = false:

A boolean flag indicating whether the tile animation process is ongoing.

StartAnimating()

public void StartAnimating()
{
    isAnimating = true;
    StartCoroutine(SelectTiles());
}

The StartAnimating() method initiates the tile animation process by setting the isAnimating flag to true and starting the SelectTiles() coroutine.

StopAnimating()

public void StopAnimating()
{
    isAnimating = false;
}

This method halts the animation by setting isAnimating to false.

SelectTiles()

IEnumerator SelectTiles()
{
    while (isAnimating)
    {
        for (int i = 0; i < simultaneousTiles; i++)
        {
            int index = GetRandomTileIndex();
            StartCoroutine(AnimateTile(index));
            yield return new WaitForSeconds(delayBetweenSets / simultaneousTiles);
        }
        yield return new WaitForSeconds(activationDuration + indicatorDuration);

        animatedIndices.Clear();

        yield return new WaitForSeconds(delayBetweenSets - simultaneousTiles * (activationDuration + indicatorDuration));
    }
}

The SelectTiles() coroutine orchestrates the animation of tiles, iterating while isAnimating is true. It selects tiles for animation based on the specified parameters, animates them in sets with delays between sets, and clears the list of animated indices after each set.

AnimateTile(int index)

IEnumerator AnimateTile(int index)
{
    animatedIndices.Add(index);

    GameObject steamColumn = FindSteamColumn(tiles[index]);
    GameObject dustParticles = FindDustParticles(tiles[index]);
    GlowController glowController = tiles[index].GetComponent<GlowController>(); 

    if (steamColumn != null)
    {
        steamColumn.SetActive(true);
    }
    else
    {
        Debug.LogWarning("SteamColumn GameObject not found as a sibling.");
    }

    glowController.TurnEmissionOn(); 
    yield return new WaitForSeconds(indicatorDuration);

    tiles[index].GetComponent<Animator>().SetBool("isFloating", true);

    GameObject steamCollider = FindSteamCollider(tiles[index]);

    if (steamCollider != null)
    {
        steamCollider.SetActive(true);
    }

    yield return new WaitForSeconds(activationDuration);

    tiles[index].GetComponent<Animator>().SetBool("isFloating", false);

    if (steamCollider != null)
    {
        steamCollider.SetActive(false);
    }

    if (dustParticles != null)
    {
        yield return new WaitForSeconds(dustWaitTime);
        dustParticles.SetActive(true);
        glowController.TurnEmissionOff(); 
        yield return new WaitForSeconds(4f);
        dustParticles.SetActive(false);
        
        
    }
    else
    {
        Debug.LogWarning("DustParticles GameObject not found as a sibling.");
    }
    
    yield return new WaitForSeconds(2f);
    
    
    if (steamColumn != null)
    {
        steamColumn.SetActive(false);
    }

    animatedIndices.Remove(index);
}

The AnimateTile(int index) coroutine animates an individual tile. It activates the associated steam column and glow effect, initiates the floating animation, activates the steam collider, activates dust particles, deactivates the glow effect, and then deactivates the steam column.

GetRandomTileIndex()

int GetRandomTileIndex()
{
    List<int> availableTiles = new List<int>();
    for (int i = 0; i < tiles.Length; i++)
    {
        if (!animatedIndices.Contains(i))
        {
            availableTiles.Add(i);
        }
    }

    return availableTiles[Random.Range(0, availableTiles.Count)];
}

The GetRandomTileIndex() method retrieves a random index from available tiles by constructing a list of available tiles excluding those already animated and randomly selecting an index from the available tile list.

FindSteamCollider(GameObject tile)

GameObject FindSteamCollider(GameObject tile)
{
    Transform parentTransform = tile.transform.parent;
    if (parentTransform != null)
    {
        foreach (Transform sibling in parentTransform)
        {
            if (sibling != tile.transform && sibling.name == "SteamCollider")
            {
                return sibling.gameObject;
            }
        }
    }
    return null;
}

The FindSteamCollider(GameObject tile) method locates the SteamCollider GameObject associated with a tile. It searches among the siblings of the tile's parent transform and returns the respective game object if found.

FindSteamColumn(GameObject tile)

GameObject FindSteamColumn(GameObject tile)
{
    Transform parentTransform = tile.transform.parent;
    if (parentTransform != null)
    {
        foreach (Transform sibling in parentTransform)
        {
            if (sibling != tile.transform && sibling.name == "SteamColumn")
            {
                return sibling.gameObject;
            }
        }
    }
    return null;
}

The FindSteamColumn(GameObject tile) method locates the SteamColumn GameObject associated with a tile. It searches among the siblings of the tile's parent transform and returns the respective game object if found.

FindDustParticles(GameObject tile)

GameObject FindDustParticles(GameObject tile)
{
    Transform parentTransform = tile.transform.parent;
    if (parentTransform != null)
    {
        foreach (Transform sibling in parentTransform)
        {
            if (sibling != tile.transform && sibling.name == "DustParticles")
            {
                return sibling.gameObject;
            }
        }
    }
    return null;
}

The FindDustParticles(GameObject tile) method locates the DustParticles GameObject associated with a tile. It searches among the siblings of the tile's parent transform and returns the respective game object if found.

Future Expansion

N/A

Full Code Reference

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TileManager : MonoBehaviour
{
    public GameObject[] tiles;
    public float indicatorDuration = 2f;
    public float activationDuration = 3f;
    public int simultaneousTiles = 3;
    public float delayBetweenSets = 4f;
    public float dustWaitTime = 0.9f;

    private List<int> animatedIndices = new List<int>();
    private bool isAnimating = false;

    public void StartAnimating()
    {
        isAnimating = true;
        StartCoroutine(SelectTiles());
    }

    public void StopAnimating()
    {
        isAnimating = false;
    }

    IEnumerator SelectTiles()
    {
        while (isAnimating)
        {
            for (int i = 0; i < simultaneousTiles; i++)
            {
                int index = GetRandomTileIndex();
                StartCoroutine(AnimateTile(index));
                yield return new WaitForSeconds(delayBetweenSets / simultaneousTiles);
            }
            yield return new WaitForSeconds(activationDuration + indicatorDuration);

            animatedIndices.Clear();

            yield return new WaitForSeconds(delayBetweenSets - simultaneousTiles * (activationDuration + indicatorDuration));
        }
    }

    IEnumerator AnimateTile(int index)
    {
        animatedIndices.Add(index);

        GameObject steamColumn = FindSteamColumn(tiles[index]);
        GameObject dustParticles = FindDustParticles(tiles[index]);
        GlowController glowController = tiles[index].GetComponent<GlowController>(); 

        if (steamColumn != null)
        {
            steamColumn.SetActive(true);
        }
        else
        {
            Debug.LogWarning("SteamColumn GameObject not found as a sibling.");
        }

        glowController.TurnEmissionOn(); 
        yield return new WaitForSeconds(indicatorDuration);

        tiles[index].GetComponent<Animator>().SetBool("isFloating", true);

        GameObject steamCollider = FindSteamCollider(tiles[index]);

        if (steamCollider != null)
        {
            steamCollider.SetActive(true);
        }

        yield return new WaitForSeconds(activationDuration);

        tiles[index].GetComponent<Animator>().SetBool("isFloating", false);

        if (steamCollider != null)
        {
            steamCollider.SetActive(false);
        }

        

        if (dustParticles != null)
        {
            yield return new WaitForSeconds(dustWaitTime);
            dustParticles.SetActive(true);
            glowController.TurnEmissionOff(); 
            yield return new WaitForSeconds(4f);
            dustParticles.SetActive(false);
            
            
        }
        else
        {
            Debug.LogWarning("DustParticles GameObject not found as a sibling.");
        }
        
        yield return new WaitForSeconds(2f);
        
        
        if (steamColumn != null)
        {
            steamColumn.SetActive(false);
        }

        animatedIndices.Remove(index);
    }

    int GetRandomTileIndex()
    {
        List<int> availableTiles = new List<int>();
        for (int i = 0; i < tiles.Length; i++)
        {
            if (!animatedIndices.Contains(i))
            {
                availableTiles.Add(i);
            }
        }

        return availableTiles[Random.Range(0, availableTiles.Count)];
    }

    GameObject FindSteamCollider(GameObject tile)
    {
        Transform parentTransform = tile.transform.parent;
        if (parentTransform != null)
        {
            foreach (Transform sibling in parentTransform)
            {
                if (sibling != tile.transform && sibling.name == "SteamCollider")
                {
                    return sibling.gameObject;
                }
            }
        }
        return null;
    }

    GameObject FindSteamColumn(GameObject tile)
    {
        Transform parentTransform = tile.transform.parent;
        if (parentTransform != null)
        {
            foreach (Transform sibling in parentTransform)
            {
                if (sibling != tile.transform && sibling.name == "SteamColumn")
                {
                    return sibling.gameObject;
                }
            }
        }
        return null;
    }

    GameObject FindDustParticles(GameObject tile)
    {
        Transform parentTransform = tile.transform.parent;
        if (parentTransform != null)
        {
            foreach (Transform sibling in parentTransform)
            {
                if (sibling != tile.transform && sibling.name == "DustParticles")
                {
                    return sibling.gameObject;
                }
            }
        }
        return null;
    }
}
⚠️ **GitHub.com Fallback** ⚠️