Ejemplo n.º 1
0
    void Update()
    {
        Enemy closestEnemy = GetClosestEnemy();

        if (closestEnemy != null && barrel != null)
        {
            barrel.AimAt(closestEnemy.transform.position);
        }

        if (shootCooldownCounter > 0)
        {
            shootCooldownCounter -= Time.deltaTime;
            if (shootCooldownCounter < 0)
            {
                shootCooldownCounter = 0;
            }
        }
        else
        {
            if (autoAim)
            {
                if (closestEnemy != null && Vector2.Distance(transform.position, closestEnemy.transform.position) < 18)
                {
                    RequestAutoFire(closestEnemy.transform.position);
                }
            }
        }

        if (shotQueued && shootCooldownCounter <= 0)
        {
            shotQueued           = false;
            shootCooldownCounter = fireRate[upgradeTier];



            if (!Physics2D.Raycast(projectileSpawn.position, queuedShotCrosshair - new Vector2(projectileSpawn.position.x, projectileSpawn.position.y), 40, friendlyLayers))
            {
                switch (projectileType)
                {
                case ProjectileType.BULLET:
                    Bullet bullet = Instantiate(bulletPrefab, projectileSpawn.position, Quaternion.identity);
                    bullet.Initiate(queuedShotCrosshair - new Vector2(projectileSpawn.position.x, projectileSpawn.position.y), damage[upgradeTier], bulletSpeed[upgradeTier]);
                    SoundController.PlayShoot();
                    break;

                case ProjectileType.LASER:
                    RaycastHit2D hit = Physics2D.Raycast(projectileSpawn.position, queuedShotCrosshair - new Vector2(projectileSpawn.position.x, projectileSpawn.position.y), 40, enemyLayers);
                    Vector2      end = Vector2.zero;
                    SoundController.PlayLaser();
                    if (hit)
                    {
                        Enemy enemy = hit.collider.gameObject.GetComponent <Enemy>();
                        if (enemy != null)
                        {
                            enemy.Damage(damage[upgradeTier]);
                        }
                        end = hit.point;
                        Laser laser = Instantiate(laserPrefab, projectileSpawn.position, Quaternion.identity);
                        laser.Initiate(new Vector2(projectileSpawn.position.x, projectileSpawn.position.y), end);
                    }
                    else
                    {
                        end = queuedShotCrosshair - new Vector2(projectileSpawn.position.x, projectileSpawn.position.y);
                        end.Normalize();

                        //max laser distance
                        end *= 40;
                        Laser laser = Instantiate(laserPrefab, projectileSpawn.position, Quaternion.identity);
                        laser.Initiate(new Vector2(projectileSpawn.position.x, projectileSpawn.position.y), new Vector2(projectileSpawn.position.x, projectileSpawn.position.y) + end);
                    }
                    break;

                case ProjectileType.MISSLE:
                    Missle missle = Instantiate(misslePrefab, projectileSpawn.position, Quaternion.identity);
                    missle.Initiate(queuedShotCrosshair - new Vector2(projectileSpawn.position.x, projectileSpawn.position.y), new Vector2(projectileSpawn.position.x, projectileSpawn.position.y), queuedShotCrosshair, damage[upgradeTier], bulletSpeed[upgradeTier], explosionRadius[upgradeTier]);
                    SoundController.PlayShoot();
                    break;
                }
            }
        }
    }