vendredi 11 novembre 2016

Infinite color generation with certain conditions in Unity

I have some infinite ball color game where when you pick up the ball with the same color as yours you get the next random color and then you need to pick up that color and that goes on... I am using object pooler and the spawn point of the balls I need to pick up is above the camera. The biggest problem I'm encountering is that when I change color, the same color as mine doesn't fall from above, and that leads to my death.

I've thought of solution that in my LevelManager (which is singleton) has a queue of spriterenderers, and each time a ball gets active (From object pool), it gets into the queue,and after it becomes visible, it gets dequeued. Also, when the color gets changed, I peek in queue and I change the color of the first ball to fall into the camera, but for some reason that doesn't work.

Can someone please guide me to some tutorials/courses in which something similar is done? Thanks.

Here is my Ball script

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour
{
public Color CurrentColor;

private SpriteRenderer _spriteRenderer;
private bool _firstTime = true;
public static int i = 0;

void OnBecameVisible()
{

    LevelManager.Instance.BallsOutsideCamera.Dequeue();
}


void Awake()
{
    _spriteRenderer = GetComponent<SpriteRenderer>();
}

void OnEnable()
{
    //needed to do this because when the object is added to the pool it gets enabled and I don't want
    //the logic below to get done when the object is created, i only want it after it gets out from the pool
    if (!_firstTime)
    {

        LevelManager.Instance.BallsOutsideCamera.Enqueue(_spriteRenderer);
        if (LevelManager.Instance.CurrentColorExistsOutside())
            CurrentColor = WaveGenerator.Instance.Colors[WaveGenerator.Instance.ChooseElement()].Color;
        else
        {
            CurrentColor = LevelManager.Instance.CurrentColor;

        }
        _spriteRenderer.color = CurrentColor;
        print(LevelManager.Instance.BallsOutsideCamera.Count);
    }
    else
        _firstTime = false;



}

void OnCollisionEnter2D(Collision2D other)
{
        gameObject.SetActive(false);   
}

}

And this is the method I use to change the color on the player

IEnumerator SameColor()
{
    var index = 0;
    var currentIndex = WaveGenerator.Instance.Colors.FindIndex(a => LevelManager.CompareColors(a.Color, _playerSpriteRenderer.color));

    while (true)
    {
        index = Random.Range(0, WaveGenerator.Instance.Colors.Count);

        if (!LevelManager.CompareColors(WaveGenerator.Instance.Colors[index].Color, _playerSpriteRenderer.color))
            break;
        yield return null;
    }


    //Zamena procenta verovatnoce padanja korisnikove boje sa bojom koja nije korisnikova i dodeljivanje trenutne boje Instanci levelManagera i bojenje player-a.
    LevelManager.Instance.CurrentColor = _playerSpriteRenderer.color =  WaveGenerator.Instance.Colors[index].Color;

    //This one doesn't work! 
    LevelManager.Instance.BallsOutsideCamera.Peek().color = _playerSpriteRenderer.color;

    yield return null;
}




Aucun commentaire:

Enregistrer un commentaire