Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Player_Control : MonoBehaviour
- {
- public SoundManager sound;
- // Tapping
- float startTime;
- bool isJump;
- // Debug Control
- public bool endless;
- int mistakeCount;
- int lastMistakeCount;
- // Control Variables
- [HideInInspector] public bool gameOver;
- [HideInInspector] public bool victory;
- [HideInInspector] public bool dead;
- [HideInInspector] public bool diedInLiquid;
- [HideInInspector] public bool fullStop;
- // The RidigBody 2D
- Rigidbody2D Rb2D;
- // The Movement Class
- Movement MV;
- // Initialization
- void Start ()
- {
- MV = GetComponent<Movement> ();
- Rb2D = GetComponent<Rigidbody2D> ();
- mistakeCount = 0;
- lastMistakeCount = 0;
- gameOver = false;
- dead = false;
- diedInLiquid = false;
- fullStop = false;
- victory = false;
- }
- // Fixed Update of the Player
- void FixedUpdate ()
- {
- if (fullStop)
- {
- Rb2D.velocity = new Vector2 (0, 0);
- return;
- }
- // Mistake Mechanics
- processMistakes();
- if (dead) {
- if(diedInLiquid)
- Rb2D.velocity = new Vector2 (0, 0);
- else
- Rb2D.velocity = new Vector2 (0, Rb2D.velocity.y);
- } else {
- // Horizontal Update
- Tap ();
- if (endless)
- Rb2D.velocity = new Vector2 (5, Rb2D.velocity.y);
- else
- MV.Horizontal_Velocity_Update (Rb2D);
- // Check for player controlled jumps
- MV.Jump (Rb2D);
- // Tells the movement script to update his axis
- MV.UpdateAxis ();
- }
- // Animate
- MV.Animate(Rb2D, dead);
- }
- public void Jump()
- {
- if(MV.slide == 1)
- MV.slide = 0;
- MV.jump = 1;
- }
- public void Slide()
- {
- sound.play = Sound.SLIDE;
- MV.slide = 1;
- }
- public void processMistakes()
- {
- if (!endless || dead)
- return;
- // Mistake Mechanics
- if (Rb2D.velocity.x < 1)
- mistakeCount++;
- if (mistakeCount == lastMistakeCount)
- mistakeCount = 0;
- lastMistakeCount = mistakeCount;
- if (mistakeCount > 1) {
- Debug.Log ("sound");
- sound.play = Sound.DEAD;
- dead = true;
- }
- }
- public void setGameOver()
- {
- gameOver = true;
- }
- public void setVictory()
- {
- victory = true;
- }
- void OnTriggerEnter2D(Collider2D other)
- {
- if (dead)
- return;
- GameObject go = other.gameObject;
- if (go.tag == "Exit") {
- UnityAds.ShowAd ();
- sound.play = Sound.VICTORY;
- victory = true;
- } else if ((go.tag == "Enemy") || (go.tag == "Water")) {
- sound.play = Sound.DEAD;
- dead = true;
- }
- if (go.tag == "Water")
- diedInLiquid = true;
- }
- public void Reset()
- {
- gameOver = false;
- dead = false;
- diedInLiquid = false;
- fullStop = false;
- victory = false;
- mistakeCount = 0;
- lastMistakeCount = 0;
- MV.Animate (Rb2D, dead);
- MV.ResetAxis ();
- }
- public void Tap()
- {
- if (Input.touchCount > 0)
- {
- Touch touch = Input.touches[0];
- switch (touch.phase)
- {
- case TouchPhase.Began:
- isJump = true;
- startTime = Time.time;
- break;
- case TouchPhase.Moved:
- isJump = false;
- break;
- case TouchPhase.Stationary:
- Slide ();
- isJump = false;
- break;
- case TouchPhase.Ended:
- if (isJump)
- Jump;
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement