Example #1
0
    void Update()
    {
        playerHealth.DeathCheck();    // first things first =)

        // game timer
        if (currentLives >= 0 && player != null)
        {
            time += Time.deltaTime;
            ui.setTime((int)time);
        }

        if (player == null)    // player has died
        {
            if (currentLives > 0)
            {
                ui.ShowRespawnScreen();

                if (Input.GetButtonDown("Restart"))    // button space
                {
                    // player respawn --> set defaults
                    ui.HideRespawnScreen();
                    player         = spawner.SpawnPlayer();
                    playerHealth   = player.GetComponent <Health>();
                    playerControls = player.GetComponent <PlayerControls>();
                    currentLives--;
                    forceField = GameObject.FindWithTag("Force_Field");
                    forceField.SetActive(false);
                    ui.toggleAutoCannon();
                    ui.setMechs(currentLives, lives);
                    soundManager.PlaySound("WeaponChange");
                }
            }
            else
            {
                // game over
                ui.ShowEndScreen(score, time);
            }
        }

        // player controls
        if (Input.GetButtonDown("Autocannon"))    // button 1
        {
            playerControls.weapon      = PlayerControls.Weapon.Autocannon;
            playerControls.weaponIndex = 0;
            ui.toggleAutoCannon();
            soundManager.PlaySound("WeaponChange");
        }
        else if (Input.GetButtonDown("Missiles"))    // button 2
        {
            playerControls.weapon      = PlayerControls.Weapon.Missiles;
            playerControls.weaponIndex = 1;
            ui.toggleMissiles();
            soundManager.PlaySound("WeaponChange");
        }
        else if (Input.GetButtonDown("Energy beam"))    // button 3
        {
            playerControls.weapon      = PlayerControls.Weapon.Beam;
            playerControls.weaponIndex = 2;
            ui.toggleBeam();
            soundManager.PlaySound("WeaponChange");
        }
        else if (Input.GetButtonDown("Shields"))    // button F
        {
            if (!playerHealth.shieldsOn && playerHealth.GetCurrentPower() > 0)
            {
                playerHealth.shieldsOn = true;
                ui.shieldsOn();
                forceField.SetActive(true);
                soundManager.PlaySound("ShieldsOn");
            }
            else
            {
                playerHealth.shieldsOn = false;
                ui.shieldsOff();
                forceField.SetActive(false);
                soundManager.PlaySound("ShieldsOff");
            }
        }

        // heat damage is reduced over time
        if (playerHealth.GetCurrentHeat() > 0)
        {
            playerHealth.AddHeat(-playerHealth.heatCoolingRate * Time.deltaTime);
            SetHeat(playerHealth.GetCurrentHeat(), playerHealth.maxHeat);
        }

        // shields being on drains power
        if (playerHealth.GetCurrentPower() > 0 && playerHealth.shieldsOn)
        {
            playerHealth.ReducePower(playerHealth.heatCoolingRate * Time.deltaTime);
            SetPower(playerHealth.GetCurrentPower(), playerHealth.maxPower);
        }
    }
Example #2
0
    void FixedUpdate()
    {
        // ************
        // MOVEMENT AND ROTATION

        Vector3 currentRotation = rb.transform.eulerAngles;

        rb.rotation = Quaternion.Euler(0f, currentRotation.y, 0f);    // tank is allowed to rotate only around y-axis  --> prevents from rolling over

        float inputHorizontal = Input.GetAxis("Horizontal");
        float inputVertical   = Input.GetAxis("Vertical");

        // turning lower torso left & right
        if (inputHorizontal != 0 && inputVertical != 0)
        {
            Vector3 turning = Vector3.up * inputHorizontal * turningSpeed;
            rb.angularVelocity = turning;
            animator.SetBool("Walk", true);
            animator.SetBool("Run", false);
        }

        // moving forward: walk & run
        if (inputVertical > 0)
        {
            // walk
            Vector3 movement = transform.forward * inputVertical * movementSpeed;
            rb.velocity = movement;
            animator.SetBool("Walk", true);
            animator.SetBool("Run", false);

            // run
            if (Input.GetKey(KeyCode.LeftShift))
            {
                Vector3 movement2 = transform.forward * inputVertical * movementSpeed * runningCoefficient;
                rb.velocity = movement2;
                animator.SetBool("Run", true);
                animator.SetBool("Walk", false);

                // running generates heat
                playerHealth.AddHeat(1 * Time.deltaTime);
                gameController.SetHeat(playerHealth.GetCurrentHeat(), playerHealth.maxHeat);
            }
        }

        // moving backwards is 30% slower
        if (inputVertical < 0)
        {
            Vector3 movement = transform.forward * inputVertical * (movementSpeed * reverseCoefficient);
            rb.velocity = movement;
            animator.SetBool("Walk", true);
            animator.SetBool("Run", false);
        }

        // not moving
        if (inputHorizontal == 0 && inputVertical == 0)
        {
            animator.SetBool("Walk", false);
            animator.SetBool("Run", false);
        }

        // turret rotation and aiming
        Ray        ray = mainCamera.ScreenPointToRay(Input.mousePosition); // ray pointing towards mouse cursor
        RaycastHit hit;                                                    // point where ray hits

        if (Physics.Raycast(ray, out hit, maxRayDistance, floorMask))
        {
            Vector3 targetDirection = hit.point - turret.position;
            targetDirection.y = 0f;

            Vector3 turningDirection = Vector3.RotateTowards(turret.forward, targetDirection, turretTurningSpeed * Time.deltaTime, 0f);
            turret.rotation = Quaternion.LookRotation(turningDirection);
        }

        // ****************
        // FIRING

        // set current weapon
        shootingCooldown = projectiles[weaponIndex].GetComponent <Projectile>().shootingCooldown;
        SetWeapon();

        if (t <= 0)    // cooling time ok --> proceed to fire
        {
            if (Input.GetButton("Fire1"))
            {
                if (gameController.CheckOkToShoot(weapon))    // enough ammo/power to fire
                {
                    GameObject proj1 = Instantiate(projectiles[weaponIndex], muzzle.position, muzzle.rotation);
                    proj1.GetComponent <Projectile>().shooterTag = tag;
                    t = shootingCooldown;

                    // different muzzle flashes for AC and beam
                    if (weapon == Weapon.Autocannon)
                    {
                        bulletFlash.SetActive(true);
                    }
                    else if (weapon == Weapon.Beam)
                    {
                        beamFlash.SetActive(true);
                    }
                }
            }
            else
            {
                bulletFlash.SetActive(false);
                beamFlash.SetActive(false);
            }
        }
        else
        {
            t -= Time.deltaTime;
        }
    }