From c2d811968101b8e9a290ad3e010e338a4c0bc188 Mon Sep 17 00:00:00 2001 From: nekroadmin Date: Sun, 21 Aug 2016 04:42:29 +0200 Subject: [PATCH] - Pokemon agnostic capturing - Pokeball rotation with Main Camera --- Assets/Scenes/Game.unity | Bin 23760 -> 23592 bytes Assets/Scripts/PlayerInput.cs | 45 ++++-------- Assets/Scripts/Pokeball.cs | 116 ++++++++++++++++++++++++++++++- Assets/Scripts/Pokemon.cs | 43 ++++++++++-- ProjectSettings/TagManager.asset | Bin 4308 -> 4320 bytes 5 files changed, 165 insertions(+), 39 deletions(-) diff --git a/Assets/Scenes/Game.unity b/Assets/Scenes/Game.unity index 5caf311e6f61ea20fb64cdd0d3cf4a71fd161dd0..bf103b02c65365252a8d87554031bc297df50655 100644 GIT binary patch delta 1092 zcmZ8gT}YEr7=F)w^jlgjWNFUX`~wRurJ@aWGcw#}W7CbfzzW5MncHkiW%e_psBXe~ zi6Fd~U5J>0x{LBcTM^}j!ssFl6oiOE3W|i1wDq3-n0Da(&Nc`+Z+{oDDu> za)6qf01brzu$9(d?EZO9q6=TUt_%t6B)u5yQukrYgth=!YE-gzG#izoUjnuoGgSc@zU5l@207t_x2*+7kV(Y*ff!*OnexOU?BJC?-T z&}VrQty6DClPPmsJAJ$vM6ZxEPhr6)rhXQzJ31!DKAV8o9fc@2%dyuYL5;OCg}0Em z-R<=tGb>PCUx*=VKp+@qrl%(eR1+XzZ*8`yvXZu0I9)r;-s9c6Y7ASdNJ)xPvoct2 zOBKtPBnO`JQa)&{N2ep3?uQo~v(BM{uCnf_D|*dJX@$-_r^2H^4_F>3b6wE*NO7s_6rw7{r delta 1295 zcmb7Ddq`7Z6hGhHHKzzJ%&exlN-IHXW|5-#C}^8X-l?HIX_difZfh-cy&htCx-0qtq*e7%V zHM0QL1_2*%>Lyd z0Vj?pm@^KKCzu<UT6wrJzML29tyblS`8~xg3t>OP2s=&stPRwf-Q>d^k|7e|ni`+JO5{6)PfU6-7vIy@jY(#G)<^=qi!{6t z9%=xe{!eeBt#xCiSuaAkj=mevN7h=Jshcdu#=dc(wTQjiu*a;IAPFCvr-^P1lX)oV zZ}Y)N@2c2b!5J2P!rNpj7Ar0F?YvmuqeV!phLg4Tw((s-6H+t);V8wPb4t}6q2FN& z*^H7OKb6b>_VgH!3A#zg0T>f%8%9-~K{u{04FuAvP78F`$bsJKUJdT4okKzrj;xT; z*r3I0wMFP|$nU71!Zv7By0nFbfwH`{hi{{;#z26!!#Z7p3X(Fh)OvYpliG5wC7zf0 z=NPt{jEhGLZZE0tU2-wkSF-c|Uj9em(Zh%R$2&WT_m82jy8wnt3Oh+8&^Mccr=07m z)9~WLCwWmh6DiX0&dOA2;W7-a9J2mzc#ajDJwTs2PsZX$M$z~Jo2)4~(Bw#9OCx?< zlZLugnSsr#1|)2zS08DR^La7&jD2xB$(Go%8`N#5+wR0*ote9t6Cc^jU_pP>n>w&A Qb9b~U#1iJOz?!=Jp8#q})Bpeg diff --git a/Assets/Scripts/PlayerInput.cs b/Assets/Scripts/PlayerInput.cs index 0aa5902..7e5569e 100644 --- a/Assets/Scripts/PlayerInput.cs +++ b/Assets/Scripts/PlayerInput.cs @@ -4,15 +4,12 @@ public class PlayerInput : MonoBehaviour { - public Pokemon pokemon; - public Transform initialPokemonPoint; + public Pokeball pokeball; - public Transform pokeball; public Vector3 forceLeft; public Vector3 forceRight; - bool shooting = false; - bool collidedWithPokemon = false; + bool canThrow = true; #if !UNITY_EDITOR @@ -69,42 +66,24 @@ private void Gr_HoldStartedEvent(InteractionSourceKind source, Ray headRay) private void Gr_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay) { - if (!shooting) + if (canThrow) { - pokeball.GetComponent().isKinematic = false; - pokeball.GetComponent().velocity = Vector3.Lerp(forceLeft, forceRight, Random.Range(0, 100) / 100f); + canThrow = false; - StartCoroutine(Coroutine_ReturnPokeball()); + 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))); - shooting = true; + StartCoroutine(Coroutine_ReturnPokeball()); } } IEnumerator Coroutine_ReturnPokeball() { - yield return new WaitUntil(() => { return collidedWithPokemon; }); - - pokemon.GetComponent().enabled = false; - - yield return StartCoroutine(pokemon.Collect(pokeball)); - - yield return new WaitForSeconds(1); - - pokeball.GetComponent().isKinematic = true; - pokeball.position = transform.position; - pokeball.rotation = transform.rotation; - - pokemon.transform.position = initialPokemonPoint.position; - pokemon.transform.rotation = initialPokemonPoint.rotation; - pokemon.transform.localScale = initialPokemonPoint.localScale; - pokemon.GetComponent().enabled = true; - - shooting = false; - collidedWithPokemon = false; - } + yield return new WaitUntil(() => + { + return pokeball.ready; + }); - public void OnPokemonCollided() - { - collidedWithPokemon = true; + canThrow = true; } } diff --git a/Assets/Scripts/Pokeball.cs b/Assets/Scripts/Pokeball.cs index 8720a34..e3c4539 100644 --- a/Assets/Scripts/Pokeball.cs +++ b/Assets/Scripts/Pokeball.cs @@ -3,10 +3,122 @@ public class Pokeball : MonoBehaviour { - public PlayerInput playerInput; + public Transform enterPoint; + + Pokemon hitPokemon = null; + + bool isWaitingBeforeCapture = false; + public bool ready = true; + + Rigidbody Rigidbody + { + get + { + return GetComponent(); + } + } + + Vector3 startPosition; + Quaternion startRotation; + + void Awake() + { + startPosition = transform.localPosition; + startRotation = transform.localRotation; + } void OnCollisionEnter(Collision col) { - playerInput.OnPokemonCollided(); + if (hitPokemon != null) + { + return; + } + + if (col.transform.tag == "Pokemon") + { + hitPokemon = col.transform.GetComponent(); + + isWaitingBeforeCapture = true; + StartCoroutine(Coroutine_WaitAndCapture()); + } + } + + public void Throw(Vector3 velocity, Vector3 angularVelocity) + { + ready = false; + + Rigidbody.isKinematic = false; + Rigidbody.velocity = velocity; + Rigidbody.angularVelocity = angularVelocity; + + StartCoroutine(Coroutine_WaitForPokemonReset()); + } + + IEnumerator Coroutine_WaitForPokemonReset() + { + yield return new WaitForSeconds(2); + + if (hitPokemon != null) + { + yield return new WaitUntil(() => + { + return !isWaitingBeforeCapture; + }); + + yield return new WaitForSeconds(1); + + hitPokemon.ResetPokemon(); + hitPokemon = null; + } + + ResetPokeball(); + } + + IEnumerator Coroutine_WaitAndCapture() + { + Vector3 lastVelocity = Rigidbody.velocity; + if (lastVelocity.z < 0) + { + lastVelocity.z = -lastVelocity.z; + } + + Rigidbody.velocity = lastVelocity; + lastVelocity *= 0.5f; + Vector3 angularVelocity = Rigidbody.angularVelocity; + + yield return new WaitForSeconds(0.25f); + + Rigidbody.isKinematic = true; + + Vector3 eulerAngles = Quaternion.LookRotation(hitPokemon.transform.position - transform.position, Vector3.up).eulerAngles; + if (eulerAngles.x > 180) + { + eulerAngles.x -= 360; + } + transform.rotation = Quaternion.Euler(Mathf.Lerp(eulerAngles.x, 0, 0.75f), Mathf.Lerp(eulerAngles.y, 180, 0.5f), 0); + + hitPokemon.Capture(enterPoint); + + yield return new WaitUntil(() => + { + return !hitPokemon.IsBeingCaptured; + }); + + yield return new WaitForSeconds(0.5f); + + Rigidbody.isKinematic = false; + Rigidbody.velocity = lastVelocity; + Rigidbody.angularVelocity = angularVelocity; + + isWaitingBeforeCapture = false; + } + + public void ResetPokeball() + { + Rigidbody.isKinematic = true; + transform.localPosition = startPosition; + transform.localRotation = startRotation; + + ready = true; } } \ No newline at end of file diff --git a/Assets/Scripts/Pokemon.cs b/Assets/Scripts/Pokemon.cs index 48cfa6f..93a6f65 100644 --- a/Assets/Scripts/Pokemon.cs +++ b/Assets/Scripts/Pokemon.cs @@ -3,21 +3,56 @@ public class Pokemon : MonoBehaviour { - public float collectionSpeed = 5; + public float moveToPokeballSpeed = 2; - public IEnumerator Collect(Transform pokeball) + public bool IsBeingCaptured + { + get; + private set; + } + + Vector3 startPosition; + Quaternion startRotation; + + void Awake() + { + startPosition = transform.localPosition; + startRotation = transform.localRotation; + } + + public void Capture(Transform target) + { + IsBeingCaptured = true; + + GetComponent().enabled = false; + + StartCoroutine(Coroutine_Capture(target)); + } + + IEnumerator Coroutine_Capture(Transform target) { float delta = 0; while (delta < 1f) { - delta = Mathf.Min(1f, delta + Time.deltaTime * collectionSpeed); + delta = Mathf.Min(1f, delta + Time.deltaTime * moveToPokeballSpeed); transform.localScale = new Vector3(1f - delta, 1f - delta, 1f - delta); - transform.position = Vector3.Lerp(transform.position, pokeball.position, delta); + transform.position = Vector3.Lerp(startPosition, target.position, delta); yield return null; } + + IsBeingCaptured = false; + } + + public void ResetPokemon() + { + transform.localPosition = startPosition; + transform.localRotation = startRotation; + transform.localScale = Vector3.one; + + GetComponent().enabled = true; } } diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index 42e4a2d040cbc8f999f1e2188c6fba10ba0c1435..a515f235402b85ec8d15ce28b92051b3bdad2f66 100644 GIT binary patch delta 72 zcmcbj_&`ybfr05C1B1W=1_lOxAPuA#7)=4K_+&Vd7;33qgo0oBuK~^G;S^ KW|=G?Z~*{^i40Ky