Example #1
0
    public void StartSingleplayerGame(int bots, bool pickupsToggled, bool vibrationToggled, int _roundLength)
    {
        //Health bar variables
        RectTransform healthBarRectTransform = healthBarPrefab.GetComponent <RectTransform>();
        RectTransform containerRectTransform = ui.GetComponent <RectTransform>();

        players = new GameObject[bots + 1];
        cam.GetComponent <PlayerCamera>().sq = new GameObject[bots + 1];
        roundLength = _roundLength * 10;

        playerStats.AddComponent <PlayerStatsTracker>();

        pickupSpawner.Setup(true);
        List <GameObject> spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint").ToList();

        for (int i = 0; i < bots + 1; i++)
        {
            GameObject point = spawnPoints[Random.Range(0, spawnPoints.Count)];
            spawnPoints.Remove(point);
            players[i] = (GameObject)Instantiate(player, point.transform.position, Quaternion.identity);

            foreach (SpriteRenderer part in players[i].GetComponentsInChildren <SpriteRenderer>())
            {
                part.sortingOrder += 40 - (i * 10);
            }

            cam.GetComponent <PlayerCamera>().sq[i] = players[i];

            //Tag for multiplayer:

            /*Text tag = ui.transform.FindChild("PlayerNum" + (i + 1)).GetComponent<Text>();
             * tag.GetComponent<PlayerNumTag>().SetupTag(i);*/

            Text tag = ui.transform.FindChild("PlayerNum" + (i + 1)).GetComponent <Text>();
            tag.GetComponent <PlayerNumTag>().playerTransform = players[i].transform;

            //Health Bar
            GameObject newBar = Instantiate(healthBarPrefab) as GameObject;
            newBar.name = "HealthBar " + (i + 1);
            newBar.transform.SetParent(ui.transform);

            if (i == 0 && Social.localUser.authenticated)
            {
                players[i].GetComponentInChildren <HealthBarController>().userImage = Social.localUser.image;
            }

            players[i].GetComponentInChildren <HealthBarController>().InitHealth(newBar, i);

            //position bar
            RectTransform newBarRectTransform = newBar.GetComponent <RectTransform>();
            newBarRectTransform.localScale = new Vector3(1, 1, 1);

            float barWidth  = healthBarRectTransform.rect.width;
            float barHeight = healthBarRectTransform.rect.height;
            float xPos;
            float yPos;

            xPos = healthBarRectTransform.rect.width * i + (healthBarRectTransform.rect.width * 0.5f);
            yPos = -healthBarRectTransform.rect.height - (healthBarRectTransform.rect.height * 0.5f);
            newBarRectTransform.offsetMin = new Vector2(xPos, yPos);

            xPos = newBarRectTransform.offsetMin.x + healthBarRectTransform.rect.width;
            yPos = newBarRectTransform.offsetMin.y + healthBarRectTransform.rect.height;
            newBarRectTransform.offsetMax = new Vector2(xPos, yPos);

            //RectTransform barTextRectTransform = newBar.transform.Find("Text").GetComponent<RectTransform>();

            if (i == 0)
            {
                tag.GetComponent <PlayerNumTag>().SetupTag(i);
                players[i].GetComponent <PlayerController>().isPrimaryPlayer = true;
                if (!vibrationToggled)
                {
                    players[i].GetComponent <PlayerController>().vibrationToggled = false;
                }
                else
                {
                    players[i].GetComponent <PlayerController>().vibrationToggled = true;
                }
                continue;
            }

            tag.GetComponent <PlayerNumTag>().SetupTag(-1);

            players[i].AddComponent <AISquirrel>();

            switch (Random.Range(0, bots + 1))
            {
            case 0: players[i].GetComponent <ReskinableAnimation>().ChangeBaseSprite("squirrelSkinRed");
                break;

            case 1: players[i].GetComponent <ReskinableAnimation>().ChangeBaseSprite("squirrelSkinWhite");
                break;

            case 2: players[i].GetComponent <ReskinableAnimation>().ChangeBaseSprite("squirrelSkinBrown");
                break;
            }
        }

        PlayerController playerControl = players[0].GetComponent <PlayerController>();

        ui.GetComponentInChildren <DPadController>().player    = playerControl;
        ui.GetComponentInChildren <AButtonController>().player = playerControl;
        ui.GetComponentInChildren <BButtonController>().player = playerControl;

        startCountdown = ui.transform.FindChild("StartCountdown").GetComponent <Text>();
        gameTimer      = ui.transform.FindChild("Timer").GetComponent <Text>();

        for (int i = 1; i < bots + 1; i++)
        {
            players[i].GetComponent <AISquirrel>().enemies       = new List <Transform>();
            players[i].GetComponent <AISquirrel>().nodeManager   = nodeManager;
            players[i].GetComponent <AISquirrel>().pickupSpawner = pickupSpawner;

            for (int j = 0; j < bots + 1; j++)
            {
                if (j != i)
                {
                    players[i].GetComponent <AISquirrel>().enemies.Add(players[j].GetComponent <Transform>());
                }
            }
        }

        if (!pickupsToggled)
        {
            Destroy(pickupSpawner);
            Destroy(GameObject.Find("PickupSpawner"));
        }

        progress = GameProgress.Countdown;
        timer    = 3;
    }