Created
May 10, 2013 08:51
-
-
Save N-Carter/5553276 to your computer and use it in GitHub Desktop.
Player script from Pacing, demonstrating multitouch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Player : MonoBehaviour | |
{ | |
[SerializeField] protected Hint m_HintPrefab; | |
[SerializeField] protected Footfall m_FootfallPrefab; | |
[SerializeField] protected Material m_FootfallMaterial; | |
[SerializeField] protected EndlessTiles m_EndlessTiles; | |
[SerializeField] protected GUIText m_LivesText; | |
[SerializeField] protected GUIText m_ScoreText; | |
[SerializeField] protected GUIText m_MultiplierText; | |
[SerializeField] protected GUIText m_GameOverText; | |
[SerializeField] protected AudioClip[] m_Sounds; | |
bool m_MouseTouchEmulation; | |
bool m_MouseIsTouching; | |
Vector3 m_MouseDragPosition; | |
Vector3 m_Velocity; | |
int m_Lives = 3; | |
static int s_Score; | |
int m_Multiplier; | |
float m_EndOfGameTime; | |
static Player s_Player; | |
protected class Drag | |
{ | |
public bool isTouching; | |
public Vector3 position; | |
public Vector3 velocity; | |
} | |
protected const int kNumDrags = 5; | |
protected Drag[] m_Drags = new Drag[kNumDrags]; | |
protected void Awake() | |
{ | |
for(int i = 0; i < m_Drags.Length; ++i) | |
m_Drags[i] = new Drag(); | |
s_Player = this; | |
} | |
protected void Start() | |
{ | |
Application.targetFrameRate = 60; | |
m_FootfallMaterial.renderQueue = 2050; | |
s_Score = 0; | |
SetUpLivesText(); | |
} | |
protected void Update() | |
{ | |
if(Input.GetKeyDown(KeyCode.M)) | |
m_MouseTouchEmulation = !m_MouseTouchEmulation; | |
if(m_Lives > 0) | |
HandleDrag(); | |
else if(Time.time - m_EndOfGameTime > 1.0f && Input.GetMouseButtonDown(0)) | |
Application.LoadLevel("Menu"); | |
// m_Velocity += new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical")) * 0.5f; | |
m_EndlessTiles.Move(m_Velocity * Time.deltaTime); | |
// Scoring: | |
if(m_Lives > 0) | |
{ | |
m_Multiplier = (int)m_Velocity.magnitude; | |
m_MultiplierText.text = m_Multiplier.ToString(" 0x"); | |
} | |
m_Velocity -= m_Velocity * 0.5f * Time.deltaTime; | |
} | |
public void HandleDrag() | |
{ | |
if(m_MouseTouchEmulation) | |
{ | |
if(!m_MouseIsTouching) | |
BeginMouseDrag(); | |
else | |
ContinueOrEndMouseDrag(); | |
} | |
else | |
{ | |
int numTouches = Input.touchCount; | |
for(int i = 0; i < numTouches; ++i) | |
{ | |
var touch = Input.GetTouch(i); | |
if(!m_Drags[touch.fingerId].isTouching) | |
BeginTouch(touch); | |
else | |
ContinueOrEndTouch(touch); | |
} | |
// Average the drag vectors: | |
Vector3 total = Vector3.zero; | |
int count = 0; | |
for(int i = 0; i < m_Drags.Length; ++i) | |
{ | |
if(m_Drags[i].isTouching) | |
{ | |
total += m_Drags[i].velocity; | |
++count; | |
} | |
} | |
if(count > 0) | |
total /= count; | |
m_Velocity = Vector3.ClampMagnitude(m_Velocity + total, 10.0f); | |
m_Velocity.y = 0.0f; | |
} | |
} | |
protected void BeginMouseDrag() | |
{ | |
if(Input.GetMouseButtonDown(0)) | |
{ | |
m_MouseDragPosition = m_EndlessTiles.GetPositionOnPlane(Input.mousePosition); | |
var tile = m_EndlessTiles.GetTileAtPosition(m_MouseDragPosition); | |
if(tile != null) | |
tile.tileState.Touch(); | |
var footfall = (Footfall)Instantiate(m_FootfallPrefab, m_MouseDragPosition, Quaternion.identity); | |
footfall.endlessTiles = m_EndlessTiles; | |
m_MouseIsTouching = true; | |
} | |
} | |
protected void ContinueOrEndMouseDrag() | |
{ | |
Vector3 position = m_EndlessTiles.GetPositionOnPlane(Input.mousePosition); | |
Vector3 direction = position - m_MouseDragPosition; | |
// Vector3 cameraPosition = m_MainCamera.transform.position; | |
// cameraPosition.y = m_CameraHeight; | |
// m_MainCamera.transform.position = cameraPosition - direction; | |
// m_LevelRoot.Translate(direction); | |
m_Velocity = Vector3.ClampMagnitude(m_Velocity + direction, 10.0f); | |
m_Velocity.y = 0.0f; | |
if(Input.GetMouseButtonUp(0)) | |
m_MouseIsTouching = false; | |
m_MouseDragPosition = position; | |
} | |
protected void BeginTouch(Touch touch) | |
{ | |
if(touch.phase == TouchPhase.Began) | |
{ | |
var drag = m_Drags[touch.fingerId]; | |
drag.position = m_EndlessTiles.GetPositionOnPlane(touch.position); | |
var tile = m_EndlessTiles.GetTileAtPosition(drag.position); | |
if(tile != null) | |
tile.tileState.Touch(); | |
var footfall = (Footfall)Instantiate(m_FootfallPrefab, drag.position, Quaternion.identity); | |
footfall.endlessTiles = m_EndlessTiles; | |
drag.isTouching = true; | |
} | |
} | |
protected void ContinueOrEndTouch(Touch touch) | |
{ | |
var drag = m_Drags[touch.fingerId]; | |
Vector3 position = m_EndlessTiles.GetPositionOnPlane(touch.position); | |
drag.velocity = position - drag.position; | |
drag.velocity.y = 0.0f; | |
if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) | |
drag.isTouching = false; | |
drag.position = position; | |
} | |
#region Static API | |
public static void AddScore(int points) | |
{ | |
s_Score = Mathf.Max(0, s_Score + CalculateScore(points)); | |
s_Player.m_ScoreText.text = s_Score.ToString("00000"); | |
} | |
public static void AddScore(int points, Vector3 position) | |
{ | |
points = CalculateScore(points); | |
var hint = (Hint)Instantiate(s_Player.m_HintPrefab, position, s_Player.m_HintPrefab.transform.rotation); | |
hint.endlessTiles = s_Player.m_EndlessTiles; | |
hint.points = points; | |
s_Score = Mathf.Max(0, s_Score + points); | |
s_Player.m_ScoreText.text = s_Score.ToString("00000"); | |
} | |
public static int Score {get {return s_Score;}} | |
public static void SpeedChange(float factor) | |
{ | |
s_Player.m_Velocity = Vector3.ClampMagnitude(s_Player.m_Velocity * factor, 10.0f); | |
} | |
public static void LoseLife() | |
{ | |
PlaySound(2); | |
--s_Player.m_Lives; | |
s_Player.SetUpLivesText(); | |
if(s_Player.m_Lives <= 0) | |
{ | |
s_Player.m_Lives = 0; | |
EndGame(); | |
} | |
} | |
public static void EndGame() | |
{ | |
if(s_Player.m_EndOfGameTime == 0.0f) | |
{ | |
s_Player.m_EndOfGameTime = Time.time; | |
s_Player.m_GameOverText.gameObject.SetActive(true); | |
} | |
} | |
public static void PlaySound(int soundIndex) | |
{ | |
s_Player.audio.PlayOneShot(s_Player.m_Sounds[soundIndex]); | |
} | |
#endregion | |
protected static int CalculateScore(int points) | |
{ | |
return points * s_Player.m_Multiplier; | |
} | |
protected void SetUpLivesText() | |
{ | |
string livesString = ""; | |
for(int i = 0; i < m_Lives; ++i) | |
livesString += "!"; | |
m_LivesText.text = livesString; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment