using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
#region VARIABLES
#region PUBLIC HIDDEN VARIABLES
[HideInInspector]
public bool isFacingRight = true; // True if the GameObject is facing to the right
[HideInInspector]
public bool isGrounded = false; // True if the GameObject is contacting the ground
[HideInInspector]
public bool isDoubleJumping = false; // True if the GameObject has executed a double jump
#endregion
#region PUBLIC VARIABLES
public float maxSpeed = Constants.playerMaxSpeed; // The maximum speed of the player
public float jumpForce = Constants.playerJumpForce; // The force applied when a player jumps
public PhysicsMaterial2D jumpMaterial; // The material used when the player jumps
public Transform groundCheck; // The Transform used to test whether or not the player is grounded
public LayerMask groundLayers; // The layers that count as "ground" when checked
public AudioClip[] footstepSounds; // Array of footstep sound effects
public AudioClip jumpSound; // Sound used when the player jumps
public AudioClip damageSound; // Sound used when the player takes damage
#endregion
#region PRIVATE VARIABLES
private float groundCheckRadius = Constants.playerGroundCheckRadius; // The radius used when testing whether the player is grounded or not
private AudioSource audioSource; // The Audio Source used to play sound effects
private Animator anim; // The Animator that controls player animations
private float deathTimeElapsed; // Amount of time elapsed after player death
#endregion
#endregion
#region INHERENT METHODS [Awake, Update, FixedUpdate]
///
/// Gets the Animator and Audio Source that are on the Player GameObject
///
void Awake()
{
anim = this.GetComponent();
audioSource = this.GetComponent();
}
///
/// Manages player input in the form of jump and double jump actions
///
void Update ()
{
if(Input.GetButtonDown(Constants.inputJump))
{
if(isGrounded == true)
{
this.rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x,0);
this.rigidbody2D.AddForce(new Vector2(0, jumpForce));
this.anim.SetTrigger(Constants.animJump);
PlayJumpAudio();
}
else if(isDoubleJumping == false)
{
isDoubleJumping = true;
this.rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x,0);
this.rigidbody2D.AddForce(new Vector2(0, jumpForce));
PlayJumpAudio();
}
}
}
///
/// Manages the ground check and input of the player in regards to movement
///
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayers);
PhysicsMaterial2D material = this.gameObject.GetComponent().sharedMaterial;
if(isGrounded == true)
{
this.isDoubleJumping = false;
}
if(isGrounded == true && material == this.jumpMaterial)
{
CircleCollider2D collision = this.gameObject.GetComponent();
collision.sharedMaterial = null;
collision.enabled = false;
collision.enabled = true;
}
else if(isGrounded == false && this.gameObject.GetComponent().sharedMaterial == null)
{
CircleCollider2D collision = this.gameObject.GetComponent();
collision.sharedMaterial = this.jumpMaterial;
collision.enabled = false;
collision.enabled = true;
}
try
{
float move = Input.GetAxis(Constants.inputMove);
this.rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
this.anim.SetFloat(Constants.animSpeed, Mathf.Abs(move));
if((move > 0.0f && isFacingRight == false) || (move < 0.0f && isFacingRight == true))
{
Flip ();
}
}
catch(UnityException error)
{
Debug.LogError(error.ToString());
}
}
#endregion
#region UTILITY METHODS [Flip]
///
/// Inverts the facing of the Player GameObject
///
void Flip()
{
isFacingRight = !isFacingRight;
Vector3 playerScale = transform.localScale;
playerScale.x = playerScale.x * -1;
transform.localScale = playerScale;
}
#endregion
#region AUDIO METHODS [PlayFootstepAudio, PlayJumpAudio, PlayDamageAudio]
///
/// Plays a randomly selected foot step sound effect when called
///
void PlayFootstepAudio()
{
this.audioSource.clip = footstepSounds[(Random.Range(0, footstepSounds.Length))];
this.audioSource.Play();
}
///
/// Plays the jump sound effect when called
///
void PlayJumpAudio()
{
AudioSource.PlayClipAtPoint(this.jumpSound, this.transform.position);
}
///
/// Plays the damage sound effect when called
///
public void PlayDamageAudio()
{
this.audioSource.clip = damageSound;
this.audioSource.Play();
}
#endregion
}