From d12eb7ffd05c4f4fcd69866fda54a24178840485 Mon Sep 17 00:00:00 2001 From: nekroadmin Date: Thu, 27 Oct 2016 20:27:11 +0200 Subject: [PATCH] - Improved for the demo --- Assets/Scenes/Game.unity | Bin 39536 -> 39624 bytes Assets/Scripts/PlayerInput.cs | 48 ++++++++++----------- Assets/Scripts/Pokeball.cs | 77 +++++++++++++++------------------- Assets/Scripts/Pokemon.cs | 43 +++++++++---------- 4 files changed, 80 insertions(+), 88 deletions(-) diff --git a/Assets/Scenes/Game.unity b/Assets/Scenes/Game.unity index 873477620aeb67a33361a1af6f6891f20c59fbcb..5bdea174f981b4fcfed5df5fc4dd95b948f45c04 100644 GIT binary patch delta 1447 zcmZWpZA@Eb6h3cz*)E7%(P)=l?p7u$Mli+w0W-0D2Hj-)NJ>e{NkHU&1N;o2-A`A6M8H(f@&RWG<0P{(Hh-|KkJH%R_^oGn`WLL*$0DWQRLT!#VPPnNARI z`<;0|A2;V`aK#8(7hU<1oRjtVwfdF~{(Sv=pkhP7iHS{0YnZQjAfOJo54t^JI1Ukb z32r-Sb8i?{Y1KWr7=Q5M<=A|A-|us6%kd&d%eecU6RU^2mUcgPt@6`tRKAK<-D|*= zvMT*zaji|yG%qst3NE&Euzgr@FhDxWwayNmW^@^A?O-GLQ>$4{Z3pmD1x~6;-0;u* z%H>`Bti!T4iM@|Cq^;Q8Cdt!8qKhJ)tYk8Dz}`!ZoNWMnnQ#+0Mfz8~K( z_L0^w(k{s+YPu1f+}8aRM{9V#T`Cf50rufiyP4WtrHr@fK1$K`^}k*G^BJCD!3u}O zKEWqR+pxzWRcO130E(Q>N^Y7T^5`VRT>4uMGk>QKJNVPd&M(<8UUHg=-G7{t3=Mbk zI@R`dh&NNPwo_tWJk%)_r5pK>!<}Zi?&&)l{vD7_F{}@>tI%y3XJ=Z#5nt zs>j;69c^AQ<}E~R-WgVcH+pySPP)k#*^IuZ9aF)F*78$95iOwvCSn%sJynY7Q2EyE zBro(w+-kEwI1SIzXRnc=FT7qXV9%%>O%sJo(*$ijk-*8Z z0uHUwdi-YGjGcmj%q;rgNVcRtn7B8I5`hF$!lD>i>iJsQ7j|5GMiG04C>5XY>!%O-#Q;O6U{@58%y_Rap@yKQR+IXp!Jc?}Qn=1wa4 zQ0+Biz~a_z_p5IyVO3V&P!7hD$xupB4(Vd+V{Tm@SY&Jr#laBsVR}%de3@rGu=fO` zV)^+H%V4+PF8;g;AVH3+)oN9%tq$PZm0vgWCT@)R1vZPvDfi*HUl7mJMxunoYf-Jz z19n~Z@YR6@e%yg~UYu9XE5900Nr;94!}>_3i9baS?h9$8=sJV5y(W{6#W1mHL! z{P!`avrm2ZbWffA8S!r8QcZJ>*cV;(P0-j?6S2AKt8;JnadsIU0f7x+e?Tz6c6=$Y zN$lGW!0!=XAg6PhvTyE5{ydLGqC4>dq}BPfOPaxWb9)pupzw1Ij-9I4C%BkCU2^6Sb&MvwW9bCM>L9A!j15 zhWM{L68@ek*c-9ac)KHlc)S1Jy-xanNb_bs9E%7nijxsx%~cPt=OT8o=b3vO-crS* zTyadl8L75nIWA%LO)E+Gxfrl^}HR1eW560qNEF>%_Cz_-}Vg}C~cIk=Xv>DIlyeOp~ZWu^u z3#QW3SlDmEU-F&!gy!66<#Qxs8AaQrj*Mz*GO1`dHSEJ#t;f2wxHt!pb<>S>)45-- zXs&fwiQcC>$1-N@D0s0aZ^25g*}f+~gW;m2-^u^R@MOHRHmVf2=+={KP54dGi_t=J zo@jx-uP}`(B^PFkJ8(JWEtMK*sYKsZn#PTq)HIQBg_+uDVqN-Z*n}+M#qxMVKt~Ho K6Vs+vBK!*iRCRFx diff --git a/Assets/Scripts/PlayerInput.cs b/Assets/Scripts/PlayerInput.cs index 7e5569e..c29b1eb 100644 --- a/Assets/Scripts/PlayerInput.cs +++ b/Assets/Scripts/PlayerInput.cs @@ -6,35 +6,35 @@ public class PlayerInput : MonoBehaviour { public Pokeball pokeball; - public Vector3 forceLeft; - public Vector3 forceRight; + public Vector3 throwForceLeft = new Vector3(-1, 4, 3.5f); + public Vector3 throwForceRight = new Vector3(1, 4, 3.5f); - bool canThrow = true; + bool canThrowPokeball = true; #if !UNITY_EDITOR - GestureRecognizer gr; + GestureRecognizer gestureRecognizer; void Awake() { - gr = new GestureRecognizer(); + gestureRecognizer = new GestureRecognizer(); - gr.SetRecognizableGestures(GestureSettings.Tap | GestureSettings.Hold | GestureSettings.ManipulationTranslate); + gestureRecognizer.SetRecognizableGestures(GestureSettings.Tap | GestureSettings.Hold | GestureSettings.ManipulationTranslate); - gr.TappedEvent += Gr_TappedEvent; - gr.HoldStartedEvent += Gr_HoldStartedEvent; - gr.HoldCanceledEvent += Gr_HoldCanceledEvent; - gr.HoldCompletedEvent += Gr_HoldCompletedEvent; + gestureRecognizer.TappedEvent += Gr_TappedEvent; + gestureRecognizer.HoldStartedEvent += Gr_HoldStartedEvent; + gestureRecognizer.HoldCanceledEvent += Gr_HoldCanceledEvent; + gestureRecognizer.HoldCompletedEvent += Gr_HoldCompletedEvent; - gr.StartCapturingGestures(); + gestureRecognizer.StartCapturingGestures(); } void OnDestroy() { - gr.TappedEvent -= Gr_TappedEvent; - gr.HoldStartedEvent -= Gr_HoldStartedEvent; - gr.HoldCanceledEvent -= Gr_HoldCanceledEvent; - gr.HoldCompletedEvent -= Gr_HoldCompletedEvent; + gestureRecognizer.TappedEvent -= Gr_TappedEvent; + gestureRecognizer.HoldStartedEvent -= Gr_HoldStartedEvent; + gestureRecognizer.HoldCanceledEvent -= Gr_HoldCanceledEvent; + gestureRecognizer.HoldCompletedEvent -= Gr_HoldCompletedEvent; } #else @@ -66,12 +66,15 @@ private void Gr_HoldStartedEvent(InteractionSourceKind source, Ray headRay) private void Gr_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay) { - if (canThrow) + if (canThrowPokeball) { - canThrow = false; + canThrowPokeball = false; - pokeball.Throw(Camera.main.transform.rotation * Vector3.Lerp(forceLeft, forceRight, Random.Range(0f, 1f)), - new Vector3(Random.Range(0f, 50f), Random.Range(-2f, 2f), Random.Range(-1f, 1f))); + Quaternion cameraRotation = Camera.main.transform.rotation; + Vector3 randomThrowForce = Vector3.Lerp(throwForceLeft, throwForceRight, Random.Range(0f, 1f)); + Vector3 randomThrowAngularForce = new Vector3(Random.Range(0f, 50f), Random.Range(-2f, 2f), Random.Range(-1f, 1f)); + + pokeball.Throw(cameraRotation * randomThrowForce, randomThrowAngularForce); StartCoroutine(Coroutine_ReturnPokeball()); } @@ -79,11 +82,8 @@ private void Gr_TappedEvent(InteractionSourceKind source, int tapCount, Ray head IEnumerator Coroutine_ReturnPokeball() { - yield return new WaitUntil(() => - { - return pokeball.ready; - }); + yield return new WaitUntil(() => { return pokeball.readyToThrow; }); - canThrow = true; + canThrowPokeball = true; } } diff --git a/Assets/Scripts/Pokeball.cs b/Assets/Scripts/Pokeball.cs index 2fab7cb..b63e141 100644 --- a/Assets/Scripts/Pokeball.cs +++ b/Assets/Scripts/Pokeball.cs @@ -3,28 +3,25 @@ public class Pokeball : MonoBehaviour { - public Transform enterPoint; + /// + /// We don't have a real pokeball so it can't open for the pokemon to go in + /// + public Transform pokemonEnterPoint; Pokemon hitPokemon = null; - bool isWaitingBeforeCapture = false; - public bool ready = true; + [HideInInspector] + public bool readyToThrow = true; + bool isWaitingBeforePokemonCapture = false; - Rigidbody Rigidbody - { - get - { - return GetComponent(); - } - } - - Vector3 startPosition; - Quaternion startRotation; + new public Rigidbody rigidbody; + Vector3 initialPosition; + Quaternion initialRotation; void Awake() { - startPosition = transform.localPosition; - startRotation = transform.localRotation; + initialPosition = transform.localPosition; + initialRotation = transform.localRotation; } void OnCollisionEnter(Collision col) @@ -38,19 +35,19 @@ void OnCollisionEnter(Collision col) { hitPokemon = col.transform.GetComponent(); - isWaitingBeforeCapture = true; - StartCoroutine(Coroutine_WaitAndCapture()); + isWaitingBeforePokemonCapture = true; + StartCoroutine(Coroutine_CaptureSequence()); } } public void Throw(Vector3 velocity, Vector3 angularVelocity) { - ready = false; + readyToThrow = false; transform.parent = null; - Rigidbody.isKinematic = false; - Rigidbody.velocity = velocity; - Rigidbody.angularVelocity = angularVelocity; + rigidbody.isKinematic = false; + rigidbody.velocity = velocity; + rigidbody.angularVelocity = angularVelocity; StartCoroutine(Coroutine_WaitForPokemonReset()); } @@ -61,10 +58,7 @@ IEnumerator Coroutine_WaitForPokemonReset() if (hitPokemon != null) { - yield return new WaitUntil(() => - { - return !isWaitingBeforeCapture; - }); + yield return new WaitUntil(() => { return !isWaitingBeforePokemonCapture; }); yield return new WaitForSeconds(1); @@ -75,21 +69,21 @@ IEnumerator Coroutine_WaitForPokemonReset() ResetPokeball(); } - IEnumerator Coroutine_WaitAndCapture() + IEnumerator Coroutine_CaptureSequence() { - Vector3 lastVelocity = Rigidbody.velocity; + Vector3 lastVelocity = rigidbody.velocity; if (lastVelocity.z < 0) { lastVelocity.z = -lastVelocity.z; } - Rigidbody.velocity = lastVelocity; + rigidbody.velocity = lastVelocity; lastVelocity *= 0.5f; - Vector3 angularVelocity = Rigidbody.angularVelocity; + Vector3 angularVelocity = rigidbody.angularVelocity; yield return new WaitForSeconds(0.25f); - Rigidbody.isKinematic = true; + rigidbody.isKinematic = true; Vector3 eulerAngles = Quaternion.LookRotation(hitPokemon.transform.position - transform.position, Vector3.up).eulerAngles; if (eulerAngles.x > 180) @@ -100,29 +94,26 @@ IEnumerator Coroutine_WaitAndCapture() yield return new WaitForSeconds(0.15f); - hitPokemon.Capture(enterPoint); + hitPokemon.Capture(pokemonEnterPoint); - yield return new WaitUntil(() => - { - return !hitPokemon.IsBeingCaptured; - }); + yield return new WaitUntil(() => { return !hitPokemon.CanBeCaptured; }); yield return new WaitForSeconds(0.5f); - Rigidbody.isKinematic = false; - Rigidbody.velocity = lastVelocity; - Rigidbody.angularVelocity = angularVelocity; + rigidbody.isKinematic = false; + rigidbody.velocity = lastVelocity; + rigidbody.angularVelocity = angularVelocity; - isWaitingBeforeCapture = false; + isWaitingBeforePokemonCapture = false; } public void ResetPokeball() { transform.parent = Camera.main.transform; - Rigidbody.isKinematic = true; - transform.localPosition = startPosition; - transform.localRotation = startRotation; + rigidbody.isKinematic = true; + transform.localPosition = initialPosition; + transform.localRotation = initialRotation; - ready = true; + readyToThrow = true; } } \ No newline at end of file diff --git a/Assets/Scripts/Pokemon.cs b/Assets/Scripts/Pokemon.cs index 93a6f65..249b6e4 100644 --- a/Assets/Scripts/Pokemon.cs +++ b/Assets/Scripts/Pokemon.cs @@ -3,56 +3,57 @@ public class Pokemon : MonoBehaviour { - public float moveToPokeballSpeed = 2; + const float MOVE_TO_POKEBALL_SPEED = 3; - public bool IsBeingCaptured + Vector3 initialPosition; + Quaternion initialRotation; + Vector3 initialScale; + + public bool CanBeCaptured { get; private set; } - Vector3 startPosition; - Quaternion startRotation; - void Awake() { - startPosition = transform.localPosition; - startRotation = transform.localRotation; + initialPosition = transform.localPosition; + initialRotation = transform.localRotation; + initialScale = transform.localScale; } - public void Capture(Transform target) + public void Capture(Transform pokeball) { - IsBeingCaptured = true; - - GetComponent().enabled = false; + GetComponent().enabled = false; - StartCoroutine(Coroutine_Capture(target)); + StartCoroutine(Coroutine_Capture(pokeball)); } - IEnumerator Coroutine_Capture(Transform target) + IEnumerator Coroutine_Capture(Transform pokeball) { + CanBeCaptured = false; + float delta = 0; while (delta < 1f) { - delta = Mathf.Min(1f, delta + Time.deltaTime * moveToPokeballSpeed); + delta = Mathf.Min(1f, delta + Time.deltaTime * MOVE_TO_POKEBALL_SPEED); + transform.position = Vector3.Lerp(initialPosition, pokeball.position, delta); transform.localScale = new Vector3(1f - delta, 1f - delta, 1f - delta); - transform.position = Vector3.Lerp(startPosition, target.position, delta); - yield return null; } - IsBeingCaptured = false; + CanBeCaptured = true; } public void ResetPokemon() { - transform.localPosition = startPosition; - transform.localRotation = startRotation; - transform.localScale = Vector3.one; + transform.localPosition = initialPosition; + transform.localRotation = initialRotation; + transform.localScale = initialScale; - GetComponent().enabled = true; + GetComponent().enabled = true; } }