Created
June 4, 2014 13:31
-
-
Save jonasdeherdt/717df576700e1dce7745 to your computer and use it in GitHub Desktop.
Movement manager
This file contains 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
#pragma strict | |
private var position : Vector2; | |
private var startPosition : Vector2; | |
private var guiButtonPressed : boolean = false; | |
private var relativeRotation : Vector2; | |
private var relativeMovement : Vector2; | |
private var textureSize : int = 75; | |
private var texturePosition : int = 10; | |
private var screenWidth : int; | |
private var screenHeight : int; | |
private var mTouchStartPos : Vector2; | |
private var mTouchMoved : boolean = false; | |
private var mTimeElapsed : float = 0.0f; | |
private var mTapped : boolean = false; | |
private var mTimeElapsedSinceTap: float = 0.0f; | |
private var damping : int = 6.0f; | |
private var menuIsShowing : boolean = true; | |
private var menuIsAnimating : boolean = false; | |
private var MainContainer : GameObject; | |
private var XContainer : GameObject; | |
private var YContainer : GameObject; | |
private var iContainer : GameObject; | |
private var xpositionOne : Vector2; | |
private var xpositionTwo : Vector2; | |
var optionalTextures : Texture[]; | |
var textureTargets : GameObject[]; | |
var button : GameObject; | |
private var font : Font; | |
private var textSize : int = 20; | |
var buttonStyle : GUIStyle; | |
var resetRotationTexture : Texture; | |
var ScreenShotButton : GameObject; | |
var targetMaterial : Material; | |
var screenshotMaterial : Material; | |
var printableMaterial : Material; | |
var screenshotFound : boolean = false; | |
var video : GameObject; | |
var videoContainer : GameObject; | |
private var screenshotTexture : Texture2D; | |
private var absoluteRotation : boolean = true; | |
private static var rotateDrag : float = 0.02; | |
private var isRotating : boolean = false; | |
private var Xvelocity : float; | |
private var Yvelocity : float; | |
var menuposIphone : Vector3 = new Vector3(0,0.375,0); | |
private var backgrounddetectionlayer = 1 << 11; | |
private var interfacdetectionlayer = 1 << 12; | |
private var videodetectionlayer = 1 << 13; | |
function Start () { | |
} | |
function Update() | |
{ | |
MainContainer = gameObject; | |
XContainer = MainContainer.Find("Xrotator"); | |
YContainer = MainContainer.Find("Yrotator"); | |
iContainer = GameObject.Find("Interface Container"); | |
ScreenShotButton = iContainer.Find("ScreenShotButton"); | |
screenWidth = Screen.currentResolution.width; | |
screenHeight = Screen.currentResolution.height; | |
//Turned off | |
if(isRotating){ | |
if(Yvelocity < -0.5 ) Yvelocity += 0.05; | |
else if( Yvelocity > 0.5 ) Yvelocity -= 0.05; | |
else Yvelocity = 0; | |
if( Yvelocity == 0 ) isRotating = false; | |
YContainer.transform.Rotate( 0 , Yvelocity * (-1), 0); | |
} | |
if( GUIUtility.hotControl == 0 ) | |
{ | |
var count = Input.touchCount; | |
if ( count == 1 ) | |
{ | |
var touch : Touch = Input.GetTouch(0); | |
var ray = Camera.main.ScreenPointToRay (touch.position); | |
var hit : RaycastHit; | |
if (Physics.Raycast (ray, hit, Mathf.Infinity, interfacdetectionlayer)) | |
{ | |
var targetName = hit.collider.gameObject.name; | |
targetMaterial = hit.collider.gameObject.renderer.material; | |
switch (targetName) | |
{ | |
case "ToggleButton" : ToggleButtonClick(); | |
break; | |
case "FacebookButton" : //FacebookButtonClick(); | |
break; | |
case "ResetButton" : resetRotation(); | |
break; | |
case "CameraButton" : //CameraButtonClick(); | |
break; | |
case "Texturebutton" : TexturebuttonClick(targetMaterial); | |
break; | |
/*case default: | |
Debug.Log ("no interface definition found for : " + targetName); | |
break;*/ | |
} | |
} | |
if (touch.phase == TouchPhase.Began) | |
{ | |
mTouchStartPos = touch.position; | |
mTouchMoved = false; | |
mTimeElapsed = 0.0f; | |
} | |
else | |
{ | |
mTimeElapsed += Time.deltaTime; | |
} | |
if (touch.phase == TouchPhase.Moved) | |
{ | |
if (Vector2.Distance(mTouchStartPos, touch.position) > 40) | |
{ | |
mTouchMoved = true; | |
handleMove(ray, hit); | |
} | |
} | |
else if (touch.phase == TouchPhase.Ended) | |
{ | |
if (!mTouchMoved && mTimeElapsed < 1.0) | |
{ | |
if (mTapped) | |
{ | |
//handleDoubleTap(ray, hit); | |
mTapped = false; | |
} | |
else | |
{ | |
mTapped = true; | |
mTimeElapsedSinceTap = 0.0f; | |
} | |
} | |
} | |
} | |
else if ( count == 2 ) | |
{ | |
xpositionOne = Input.touches[0].deltaPosition.normalized * (-1); // inverse x position to unify movement | |
xpositionTwo = Input.touches[1].deltaPosition.normalized; | |
var scale : float = (xpositionOne.x + xpositionTwo.x) / 10; // if this is 1 moving left, -1 moving right | |
scaleObject(scale); | |
} | |
} | |
if (mTapped) | |
{ | |
if (mTimeElapsedSinceTap >= 0.5f) | |
{ | |
mTapped = false; | |
handleTap(ray, hit); | |
} | |
else | |
{ | |
mTimeElapsedSinceTap += Time.deltaTime; | |
} | |
} | |
} | |
/*function OnGUI () { | |
GUI.skin.label.font = GUI.skin.button.font = GUI.skin.box.font = font; | |
GUI.skin.label.fontSize = GUI.skin.box.fontSize = GUI.skin.button.fontSize = textSize; | |
if (GUI.Button (Rect ( 15 , Screen.height - ( textureSize + 15 ) ,textureSize ,textureSize), resetRotationTexture, buttonStyle)) { | |
resetRotation(); | |
} | |
} */ | |
function handleMove(ray : Ray, hit : RaycastHit) { | |
if (Physics.Raycast (ray, hit, Mathf.Infinity, backgrounddetectionlayer)) { | |
var targetName = hit.collider.gameObject.name; | |
xpositionOne = Input.touches[0].deltaPosition.normalized * 3; | |
Xvelocity = xpositionOne.y; | |
Yvelocity = xpositionOne.x; | |
XContainer.transform.Rotate( xpositionOne.y, 0, 0); | |
YContainer.transform.Rotate( 0, xpositionOne.x * (-1), 0); | |
if(XContainer.transform.rotation.x > 0) XContainer.transform.rotation.x = 0; | |
if(XContainer.transform.rotation.x < -0.70) XContainer.transform.rotation.x = -0.70; | |
isRotating = true; | |
} | |
} | |
function handleTap(ray : Ray, hit : RaycastHit) { | |
isRotating = false; | |
//Debug.Log("handleSlowTap"); // delay causes assync reference to ray and hittest object | |
} | |
function handleDoubleTap(ray : Ray, hit : RaycastHit) { | |
Debug.Log ("handleDoubleTap for : " + hit.collider.gameObject.name); | |
} | |
function setVideo(target : String) { | |
for (var child : Transform in videoContainer.transform) { | |
child.localPosition = Vector3(0, 5, 0); | |
} | |
video = gameObject.Find(target); | |
video.transform.localPosition = new Vector3(0, 0, 0); | |
} | |
function setRotation(target : GameObject) { | |
resetScale(); | |
resetRotation(); | |
var currentRotation = YContainer.transform.rotation.eulerAngles; | |
var rotateTarget = target.transform.rotation.eulerAngles; | |
//var newRotation = Vector3(currentRotation.x, currentRotation.y - rotateTarget.y, currentRotation.z); | |
//YContainer.transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(newRotation), Time.deltaTime * damping); | |
isRotating = false; | |
} | |
function resetRotation() { | |
var rotation = Quaternion.identity; | |
var target = transform.rotation; | |
XContainer.transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping); | |
YContainer.transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping); | |
isRotating = false; | |
} | |
function scaleObject(refScale : float) { | |
for (var child : Transform in YContainer.transform) { | |
var scale = child.localScale.x + refScale; | |
if(scale < 0.2) scale = 0.2; | |
if(scale > 3) scale = 3; | |
child.localScale = Vector3(scale, scale, scale); | |
} | |
} | |
function resetScale() { | |
for (var child : Transform in YContainer.transform) { | |
child.localScale = Vector3(1, 1, 1); | |
} | |
} | |
function TexturebuttonClick(targetMaterial : Material) { | |
for each(var target : GameObject in textureTargets) target.renderer.material = targetMaterial; | |
} | |
function CameraButtonClick() { | |
/*yield new WaitForEndOfFrame(); | |
screenshotTexture = new Texture2D(Screen.width, Screen.height); | |
screenshotTexture.ReadPixels(new Rect(0,0,Screen.width,Screen.height), 0, 0); | |
screenshotTexture.Apply(); | |
*/ | |
//0.03 | |
var tex : Texture = screenshotMaterial.GetTexture ("_MainTex"); | |
ScreenShotButton.renderer.material = printableMaterial; | |
ScreenShotButton.renderer.material.mainTexture = tex; | |
/*ScreenShotButton.renderer.material.mainTexture = screenshotTexture;*/ | |
} | |
function FacebookButtonClick() { | |
for each(var target : GameObject in textureTargets) | |
{ | |
target.renderer.material = targetMaterial; | |
} | |
} | |
function ToggleButtonClick() { | |
if( ! menuIsAnimating) | |
{ | |
var menupos : Vector3 = new Vector3(0,0,0); | |
if(menuIsShowing) menupos = menuposIphone; | |
HOTween.To(iContainer.transform, .6, new TweenParms().Prop("position", menupos).OnComplete(onToggleComplete)); | |
menuIsAnimating = true; | |
//interfaceContainer.transform.position = Vector3.Lerp(interfaceContainer.transform.position, menupos, Time.deltaTime * damping); | |
} | |
} | |
function onToggleComplete() { | |
menuIsAnimating = false; | |
menuIsShowing = !menuIsShowing; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment