Source

Twisted Potions Double Trouble / Assets / scripts / InputController.cs

using UnityEngine;
using System.Collections;
using System;

public class InputController : MonoBehaviour {	
	private const string PlayerGoName = "Player";
	private const string LeftButtonGoName = "LeftButton";
	private const string RightButtonGoName = "RightButton";	
	
	public enum InputMode {Moving=0};
	public InputMode InputModeState;

	public enum Key {LeftButton=0, RightButton, AButton, BButton};
	private int buttonStatus = 0;
	private int touchStatus = 0;

	private PlayerChar player;
	private VirtualPad leftButton;
	private VirtualPad rightButton;
	private RaycastHit hit;
	private Ray ray;
	
	void Awake() {
		Debug.Log("input controller");
		switch (this.InputModeState) {
			case InputMode.Moving:
				awakeMove();
				break;
		}
	}
	
	void Update() {
		switch (this.InputModeState) {
			case InputMode.Moving:
				manageMove();
				break;
		}
	}
	
	void awakeMove() {
		GameObject playerGo = GameObject.Find(PlayerGoName);
		if (!playerGo)
			throw new Exception("Cannot find a gameobject named " + PlayerGoName);
		this.player = (PlayerChar) playerGo.GetComponent(typeof(PlayerChar));

		#if UNITY_IPHONE || UNITY_ANDROID
			GameObject leftButtonGo = GameObject.Find(LeftButtonGoName);
			GameObject rightButtonGo = GameObject.Find(RightButtonGoName);		
			if (!leftButtonGo)
				throw new Exception("Cannot find a gameobject named " + LeftButtonGoName );
			if (!rightButtonGo)
				throw new Exception("Cannot find a gameobject named " + RightButtonGoName);
			this.leftButton = (VirtualPad) leftButtonGo.GetComponent(typeof(VirtualPad));
			this.rightButton = (VirtualPad) rightButtonGo.GetComponent(typeof(VirtualPad));		
		#endif
	}
	
	void manageMove() {
		// First we'll analyse the Movement axis, we'll get, -1 for left and 1 for right.
		buttonStatus = (int)Input.GetAxis("Horizontal");
		foreach (Touch touch in Input.touches) {
	        if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) {
	            ray = Camera.main.ScreenPointToRay(touch.position);
	            if (Physics.Raycast (ray, out hit)) {
					if (hit.transform.gameObject.name == LeftButtonGoName) 
						touchStatus = -1;
					else if (hit.transform.gameObject.name == RightButtonGoName)
						touchStatus = 1;
				} 
	        } else if (touch.phase == TouchPhase.Ended) {
				touchStatus = 0;					
			}
	    }
		if (buttonStatus == 1 || touchStatus == 1) {
			player.move(PlayerChar.WalkingState.WalkRight);
			#if UNITY_IPHONE || UNITY_ANDROID
				leftButton.press(VirtualPad.ButtonStatus.Normal);
				rightButton.press(VirtualPad.ButtonStatus.Pressed);		
			#endif
		} else if (buttonStatus == -1 || touchStatus == -1)  {
			player.move(PlayerChar.WalkingState.WalkLeft);
			#if UNITY_IPHONE || UNITY_ANDROID
				leftButton.press(VirtualPad.ButtonStatus.Pressed);
				rightButton.press(VirtualPad.ButtonStatus.Normal);
			#endif
		} else if (buttonStatus == 0 && touchStatus == 0) {
			player.move(PlayerChar.WalkingState.Standing);	
			#if UNITY_IPHONE || UNITY_ANDROID
				leftButton.press(VirtualPad.ButtonStatus.Normal);
				rightButton.press(VirtualPad.ButtonStatus.Normal);
			#endif

		}		
	}
}