Skip to content

Commit

Permalink
- Improved for the demo
Browse files Browse the repository at this point in the history
  • Loading branch information
nekroadmin committed Oct 27, 2016
1 parent 3276747 commit d12eb7f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 88 deletions.
Binary file modified Assets/Scenes/Game.unity
Binary file not shown.
48 changes: 24 additions & 24 deletions Assets/Scripts/PlayerInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,24 +66,24 @@ 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());
}
}

IEnumerator Coroutine_ReturnPokeball()
{
yield return new WaitUntil(() =>
{
return pokeball.ready;
});
yield return new WaitUntil(() => { return pokeball.readyToThrow; });

canThrow = true;
canThrowPokeball = true;
}
}
77 changes: 34 additions & 43 deletions Assets/Scripts/Pokeball.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@

public class Pokeball : MonoBehaviour
{
public Transform enterPoint;
/// <summary>
/// We don't have a real pokeball so it can't open for the pokemon to go in
/// </summary>
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<Rigidbody>();
}
}

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)
Expand All @@ -38,19 +35,19 @@ void OnCollisionEnter(Collision col)
{
hitPokemon = col.transform.GetComponent<Pokemon>();

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());
}
Expand All @@ -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);

Expand All @@ -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)
Expand All @@ -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;
}
}
43 changes: 22 additions & 21 deletions Assets/Scripts/Pokemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SphereCollider>().enabled = false;
GetComponent<Collider>().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<SphereCollider>().enabled = true;
GetComponent<Collider>().enabled = true;
}
}

0 comments on commit d12eb7f

Please sign in to comment.