AstronautController.cs

    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class AstronautController : MonoBehaviour
{
	//Public variables
    public float MaxJumpSpeed = 100f;
    public float InitialOxygen = 100f;
    public float OxygenUse = 10f;
    public float OxygenLow = 20f;

    public float MaxJumpCharge = 100f;
    public float JumpChargeIncrease = 50f;

    //Private variables
    private Vector2 _Acceleration;
    private Rigidbody2D _Rigidbody2D;

    private bool _CanJump;
    private bool _OnGround;

    private Vector2 _JumpDirection;
    private float _JumpSpeed;

    private GameObject[] _Asteroids;
    private Asteroid _CurrentAsteroid;

    private AimAssist _AimAssist;

    private bool _HasJetpack;

    private bool HasJetpack
    {
        get { return _HasJetpack; }
        set
        {
            _HasJetpack = value;
            _AimAssist.Visible = _HasJetpack;
            GameObject.Find("JetpackText").GetComponent().enabled = _HasJetpack;
        }
    }

    private float _CurrentOxygen;
    private float Oxygen
    {
        get { return _CurrentOxygen; }
        set
        {
            _CurrentOxygen = value;
            GameObject.Find("OxygenText").GetComponent().text = "Oxygen: " + _CurrentOxygen + "%";

            if (_CurrentOxygen == OxygenLow)
            {
                GameObject.Find("LowAirText").GetComponent().StartFade();
                GameObject.Find("OxygenText").GetComponent().color = Color.red;
            }
            else if(_CurrentOxygen > OxygenLow)
                GameObject.Find("OxygenText").GetComponent().color = Color.white;

            if (_CurrentOxygen < 0f)
               Application.LoadLevel(Application.loadedLevelName);
        }
    }

    private float _CurrentJumpCharge;
    private bool _PoweringUp;
    private float JumpCharge
    {
        get { return _CurrentJumpCharge; }
        set
        {
            _CurrentJumpCharge = value;

            if (_CurrentJumpCharge > MaxJumpCharge && JumpChargeIncrease > 0)
                JumpChargeIncrease *= -1;
            else if (_CurrentJumpCharge < 0 && JumpChargeIncrease < 0)
                JumpChargeIncrease *= -1;
            
            GameObject.Find("JumpChargeText").GetComponent().text = "Power: " + Mathf.Round(_CurrentJumpCharge) + "%";
        }
    }

    private int _JumpCounter;
    public int JumpCounter
    {
        get { return _JumpCounter; }
        set
        {
            _JumpCounter = value;
            GameObject.Find("JumpsTakenText").GetComponent().text = "Jumps taken: " + _JumpCounter;
        }
    }

    /// 
    /// Initialize/constructor
    /// 
    void Start()
    {
        _Rigidbody2D = GetComponent();

        _CanJump = false;
        _OnGround = false;

        _Asteroids = GameObject.FindGameObjectsWithTag("Asteroid");
        _AimAssist = GetComponent();

        _PoweringUp = false;
        HasJetpack = false;

        Oxygen = InitialOxygen;
    }

    /// 
    /// Update every frame
    /// 
    void Update()
    {
        _Acceleration = Vector2.zero;

        if (Input.GetMouseButtonDown(0) && ((_CanJump && !_PoweringUp) || _HasJetpack))
        {
            if (GameObject.Find("HelpText") != null)
                GameObject.Find("HelpText").SetActive(false);

            GameObject.Find("JumpChargeText").GetComponent().enabled = true;
            GameObject.Find("JumpChargeText").transform.position = transform.position;

            _PoweringUp = true;
            
        }

        if (_PoweringUp)
            JumpCharge += Time.deltaTime * JumpChargeIncrease;

        if (Input.GetMouseButtonUp(0) && ((_CanJump && _PoweringUp) || _HasJetpack))
        {
            StartJump();
        }
    }

    void FixedUpdate()
    {
        CheckAsteroidGravity();
    }


    /// 
    /// Check if an asteroid has gravity on them
    /// 
    void CheckAsteroidGravity()
    {
        foreach (var ast in _Asteroids)
        {
            Asteroid asteroid = ast.GetComponent();
            float dist = Vector2.Distance(asteroid.transform.position, transform.position);

            //Make sure that the asteroid has no gravity when jumping (Yes I know that's not realistic)
            //Enable it when out of the gravityzone.
            if (_CurrentAsteroid != null && _CurrentAsteroid == asteroid && _OnGround)
            {
                if (dist >= _CurrentAsteroid.MaxGravDist)
                {
                    asteroid.HasGravity = true;
                    _OnGround = false;
                    _CurrentAsteroid = null;

                    if (HasJetpack)
                        _AimAssist.Visible = true;
                }
            }

            if (!asteroid.HasGravity)
                continue;

            //Check if you are in the gravityzone
            if (dist <= asteroid.MaxGravDist)
            {
                Vector3 v = asteroid.transform.position - transform.position;
                //Calculate the gravity force applied to the astronaut
                _Rigidbody2D.AddForce(v.normalized * (1.0f - dist / asteroid.MaxGravDist) * asteroid.MaxGravity);
                
            }
        }
    }

    /// 
    /// Start the jump mechanic
    /// 
    void StartJump()
    {
        JumpCounter++;

        _PoweringUp = false;
        _JumpSpeed = MaxJumpSpeed * (JumpCharge / 100f);
        _Rigidbody2D.isKinematic = false;

        //Jump towards the released mouse position
        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        _JumpDirection = mousePos - transform.position;
        _JumpDirection.Normalize();

        JumpCharge = 0f;
        GameObject.Find("JumpChargeText").GetComponent().enabled = false;
        _AimAssist.Visible = false;

        if (HasJetpack && !_OnGround)
            HasJetpack = false;

        Oxygen -= OxygenUse;
        Text oxygenUseText = GameObject.Find("OxygenUseText").GetComponent();
        oxygenUseText.text = "Used " + OxygenUse + "% Oxygen";
        oxygenUseText.GetComponent().StartFade();

        _Acceleration = _JumpDirection * _JumpSpeed * Time.deltaTime;
        _Rigidbody2D.velocity = _Acceleration;
        _JumpSpeed = 0f;
        _CanJump = false;
    }

    /// 
    /// Default unity collision check
    /// 
    /// 
    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.tag == "Asteroid" && !_OnGround)
        {
            _CanJump = true;

            _CurrentAsteroid = other.gameObject.GetComponent();
            _CurrentAsteroid.HasGravity = false;

            _Rigidbody2D.velocity = Vector3.zero;
            StandOnAsteroid(other.gameObject);
            
            _AimAssist.Visible = true;
            _OnGround = true;

            //Check if the current asteroid has any special effects
            if (_CurrentAsteroid.HasOxygen)
            {
                Oxygen += _CurrentAsteroid.OxygenAdd;
                Text oxygenUseText = GameObject.Find("OxygenUseText").GetComponent();
                oxygenUseText.text = "Added " + _CurrentAsteroid.OxygenAdd + "% Oxygen";
                oxygenUseText.GetComponent().StartFade();

                _CurrentAsteroid.EnableOxygen(false);
            }
            if (_CurrentAsteroid.HasJetpack && !HasJetpack)
            {
                HasJetpack = true;
                _CurrentAsteroid.EnableJetpack(false);
            }
        }
        if (other.gameObject.tag == "EndGoal")
        {
            PlayerPrefs.SetInt("JumpsTaken", JumpCounter);
            Application.LoadLevel("GameOverMenu");
        }
    }

    /// 
    /// Make sure the austronaut stands on the asteroid
    /// 
    /// The asteroid wich the astronaut collided with
    void StandOnAsteroid(GameObject asteroid)
    {
        _Rigidbody2D.isKinematic = true;

        //Calculate the rotation the astronaut the needs to face
        Vector3 asteroidPos = asteroid.transform.position;
        Vector3 astronautPos = transform.position;

        asteroidPos.x -= astronautPos.x;
        asteroidPos.y -= astronautPos.y;

        //With his feet on the asteroid
        float angle = Mathf.Atan2(asteroidPos.y, asteroidPos.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle + 90f));
    }
}