diff --git a/Assets/Scenes/Game.unity b/Assets/Scenes/Game.unity index 5caf311..bf103b0 100644 Binary files a/Assets/Scenes/Game.unity and b/Assets/Scenes/Game.unity differ 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 42e4a2d..a515f23 100644 Binary files a/ProjectSettings/TagManager.asset and b/ProjectSettings/TagManager.asset differ