Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (cooledDown)
        {
            //If the gun has been fired
            if (fireNow)
            {
                Transform            targetLocation;
                ObjectPoolingBaby    babiesScript  = SpawnManager.GetComponent <ObjectPoolingBaby>();
                ObjectPoolingMama    mamaScript    = SpawnManager.GetComponent <ObjectPoolingMama>();
                ObjectPoolingShooter shooterScript = SpawnManager.GetComponent <ObjectPoolingShooter>();
                ObjectPoolingTank    tankScript    = SpawnManager.GetComponent <ObjectPoolingTank>();
                babies   = babiesScript.pooledObjectsBaby;
                mamas    = mamaScript.pooledObjectsMama;
                shooters = shooterScript.pooledObjectsShooter;
                tanks    = tankScript.pooledObjectsTank;

                //Get the closest enemy to the turret
                targetLocation = GetClosestEnemy(babies, mamas, shooters, tanks);

                //If the target is valid and the bullet isn't already fired
                if (targetLocation && !bullet.activeInHierarchy)
                {
                    //Shoot the enemy
                    Vector3 difference = targetLocation.position - transform.position;
                    float   rotationZ  = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
                    turretHead.transform.rotation = Quaternion.Euler(0.0f, 180.0f, (rotationZ + 3.0f));
                    ShootEnemy(targetLocation);
                    cooledDown = false;
                    fireButton.interactable = false;
                    cooldownTimer           = 5.0f;
                }
                //Reset the turret to not fire again
                fireNow = false;
            }
        }
        //If the bullet is fired
        if (bullet.activeInHierarchy)
        {
            //Move it towards its target
            bullet.transform.Translate(this.transform.forward * bulletSpeed * Time.deltaTime, Space.Self);
            //Tick down its timer
            bulletLife -= Time.deltaTime;
            //If it's less than bulletLife then assume the bullet has missed and reset it
            if (bulletLife < 0)
            {
                bullet.SetActive(false);
                bulletLife = 5.0f;
            }
        }
        cooldownTimer -= Time.deltaTime;
        if (cooldownTimer < 0)
        {
            cooledDown              = true;
            cooldownTimer           = 5.0f;
            fireButton.interactable = true;
        }
    }
    // Use this for initialization
    void Awake()
    {
        current           = this;
        int[,] tempSpawns = this.gameObject.GetComponent <EnemySpawning> ().spawnLimit;

        for (int i = 0; i < (tempSpawns.GetLength(0)); i++)
        {
            pooledAmount[i] = tempSpawns[i, 1];
        }
    }