Skip to content

Commit

Permalink
- Pokemon agnostic capturing
Browse files Browse the repository at this point in the history
- Pokeball rotation with Main Camera
  • Loading branch information
nekroadmin committed Aug 21, 2016
1 parent 7321c6c commit c2d8119
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 39 deletions.
Binary file modified Assets/Scenes/Game.unity
Binary file not shown.
45 changes: 12 additions & 33 deletions Assets/Scripts/PlayerInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<Rigidbody>().isKinematic = false;
pokeball.GetComponent<Rigidbody>().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<SphereCollider>().enabled = false;

yield return StartCoroutine(pokemon.Collect(pokeball));

yield return new WaitForSeconds(1);

pokeball.GetComponent<Rigidbody>().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<SphereCollider>().enabled = true;

shooting = false;
collidedWithPokemon = false;
}
yield return new WaitUntil(() =>
{
return pokeball.ready;
});

public void OnPokemonCollided()
{
collidedWithPokemon = true;
canThrow = true;
}
}
116 changes: 114 additions & 2 deletions Assets/Scripts/Pokeball.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Rigidbody>();
}
}

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<Pokemon>();

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;
}
}
43 changes: 39 additions & 4 deletions Assets/Scripts/Pokemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SphereCollider>().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<SphereCollider>().enabled = true;
}
}
Binary file modified ProjectSettings/TagManager.asset
Binary file not shown.

0 comments on commit c2d8119

Please sign in to comment.