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

public class PieceSpawner : MonoBehaviour
{
    public GameState gameState;
    public GameObject plane;

    public GameObject pieceDefault;
    public GameObject pieceFaster;
    public GameObject pieceSwitch;
    public JointController jointController;

    public float spawnInterval = 1.0f;
    public int maxPieces = 100;
    public int currentPieces;

    // Use this for initialization
    void Start()
    {
        currentPieces = 0;
    }

    float spawnTimer;

    // Update is called once per frame
    void Update()
    {
        if (gameState.state == GameState.GameStates.Play)
        {
            spawnTimer += Time.deltaTime;
        }
        if (spawnTimer >= spawnInterval)
        {
            SpawnPieces(1);
            spawnTimer = 0;
        }
    }
    

    GameObject _pieceToSpawn;


    int weightDefault = 10;
    int weightFaster = 5;
    int weightSwitch = 5;

    public void SpawnPieces(int amount)
    {
        int weightTotal = weightDefault + weightFaster + weightSwitch;
        for (int i = 0; i < amount; i++)
        {
            currentPieces++;
            int roll = Random.Range(0, weightTotal);
            if (roll <= weightDefault)
            {
                _pieceToSpawn = pieceDefault;
            }
            else if(roll > weightDefault && roll <= (weightDefault + weightFaster))
            {
                _pieceToSpawn = pieceFaster;
            }
            else if (roll > (weightDefault + weightFaster))
            {
                _pieceToSpawn = pieceSwitch;
            }

            GameObject piece = Instantiate(_pieceToSpawn);
            piece.GetComponent<SnakePiece>().jointController = jointController;
            piece.transform.localPosition = new Vector3(Random.Range(-50, 50), 5, Random.Range(-50, 50));
        }
    }
}
