Ejemplo n.º 1
0
 public void ClearDrawing()
 {
     lines.Clear();
     lineDrawer.ClearAll();
 }
Ejemplo n.º 2
0
    private void LoadLevel()
    {
        if (gameManager == null)
        {
            return;
        }
        gameType = gameManager.GetGameType();

        var fields = dataManager.GetFields();

        Utils.Shuffle <DataManager.Field>(fields);

        lineDrawer.ClearAll();
        int circlePosGranularity = 4;

        List <List <Vector2> > allFieldPositions = new List <List <Vector2> >();

        float curPosX = -1.0f;
        float curPosY = -1.0f;

        // to ensure we dont spawn circles on top of each other, we create
        // a list of all possible circle locations and shuffle them.
        foreach (var field in fields)
        {
            float xIncrement = field.width / (float)circlePosGranularity;
            float yIncrement = field.height / (float)circlePosGranularity;

            List <Vector2> currentFieldPositions = new List <Vector2>();
            for (int i = 0; i < circlePosGranularity; i++)
            {
                // we add a a slight offset to x and y to avoid border decision
                // cases between intersecting fields.
                float offset = (yIncrement / 8);
                curPosY = yIncrement * i + field.yMin + offset;

                for (int j = 0; j < circlePosGranularity; j++)
                {
                    offset  = (xIncrement / 4);
                    curPosX = xIncrement * j + field.xMin + offset;

                    // we need either the incrementer or the index to be above 0 for x and y.
                    // this enables us to skip positions which are located on screen edges.
                    if ((j > 0 || field.xIndex > 0) && (i > 0 || field.yIndex > 0))
                    {
                        // We offset some circles a bit to make the level feel less like a grid
                        // at the cost of some inconsistency in terms how many samples we
                        // have per field (matters mostly in low difficulty).
                        if (field.yIndex % 2 == 0)
                        {
                            curPosX += (xIncrement / 4);
                        }
                        if (field.xIndex % 2 == 0)
                        {
                            curPosY -= (yIncrement / 8);
                        }

                        Vector2 pos = new Vector2(curPosX, curPosY);
                        currentFieldPositions.Add(pos);
                    }
                }
            }

            Utils.Shuffle(currentFieldPositions);
            Debug.Log("field [" + field.xIndex + "," + field.yIndex + "]: (" + Utils.StringFromVector2List(currentFieldPositions) + ")");
            allFieldPositions.Add(currentFieldPositions);
        }

        int            targetAmount    = gameManager.GetAmountOfCircles();
        List <Vector2> targetPositions = new List <Vector2>();

        // Now we select 1 position from each field list.
        // the order of lists has been shuffled, as has the locations each list stores.
        // if we still have targets left after going over all lists, we select again.
        // To avoid duplicate locations, we remove the positions we select.
        while (targetAmount > 0)
        {
            Utils.Shuffle(allFieldPositions);
            foreach (var fieldposList in allFieldPositions)
            {
                if (targetAmount > 0)
                {
                    Vector2 pos = fieldposList[0];
                    targetPositions.Add(pos);
                    fieldposList.RemoveAt(0);
                }
                targetAmount -= 1;
            }
        }

        Debug.Log(targetPositions.Count + " targetPositions: [" + Utils.StringFromVector2List(targetPositions) + "]");

        int[] circleIDs = Enumerable.Range(0, targetPositions.Count).ToArray();
        Debug.Log("Generating " + circleIDs.Length + " CircleIDs: [" + Utils.StringFromIntArray(circleIDs) + "]");

        // Destroy any previous spawned targets before instantiation of new targets.
        if (targetObjects != null && targetObjects.Count > 0)
        {
            Debug.Log(targetObjects.Count + " target Objects exists, destroying level..");
            DestroyLevel();
        }

        targetObjects = new List <GameObject>();

        targets = new List <Target>();
        int index = 0;

        foreach (Vector2 pos in targetPositions)
        {
            Vector3    worldPos = mainCam.ViewportToWorldPoint(new Vector3(pos.x, pos.y, 0.0f));
            GameObject obj      = Instantiate(targetPrefab, new Vector3(worldPos.x, worldPos.y, 0.0f), Quaternion.identity);
            targetObjects.Add(obj);
            Target target = obj.GetComponent <Target>();

            target.SetID(circleIDs[index]);
            target.SetObstacle(false);

            if (gameType == "gameA")
            {
                target.SetLabel(labelsA[index]);
            }
            else
            {
                target.SetLabel(labelsB[index]);
            }

            obj.SetActive(false);
            targets.Add(target);
            Debug.Log(index + ": [" + pos.x + "," + pos.y + "] given ID " + circleIDs[index] + "and name {" + labelsA[index] + "}");
            index += 1;
        }
        currentTarget   = 0;
        lastValid       = currentTarget;
        spawnSound.clip = spawnTargetsSounds[UnityEngine.Random.Range(0, spawnTargetsSounds.Length - 1)];
        StartCoroutine(SpawnTargets(targetObjects.ToArray()));
    }