TO SQUASH: Yummy text on get.
This commit is contained in:
@@ -8,6 +8,8 @@ public class CakeSpawner : Spawner
|
||||
|
||||
public WorldFiller worldFiller;
|
||||
|
||||
public Canvas canvas;
|
||||
|
||||
private Bounds cakeBounds;
|
||||
|
||||
private Bounds gameBoardBounds;
|
||||
@@ -48,6 +50,7 @@ public class CakeSpawner : Spawner
|
||||
if (Random.value < spawnProbability)
|
||||
{
|
||||
GameObject cake = Instantiate(cakePrefab, spawnPosition, Quaternion.identity);
|
||||
cake.GetComponent<YummyCake>().canvas = canvas;
|
||||
}
|
||||
|
||||
spawned = true;
|
||||
|
||||
@@ -9,6 +9,12 @@ public abstract class YummyBase : MonoBehaviour
|
||||
|
||||
public AudioClip missedSound;
|
||||
|
||||
public Canvas canvas;
|
||||
|
||||
public GameObject notificationPrefab;
|
||||
|
||||
protected GameObject notificationInstance;
|
||||
|
||||
private AudioSource audioSource;
|
||||
|
||||
private Animator animator;
|
||||
@@ -113,4 +119,53 @@ public abstract class YummyBase : MonoBehaviour
|
||||
}
|
||||
|
||||
protected abstract void ObtainedCallback(Player player);
|
||||
|
||||
protected void ShowNotification(string text)
|
||||
{
|
||||
if (notificationPrefab == null || canvas == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
notificationInstance = Instantiate(notificationPrefab);
|
||||
notificationInstance.transform.SetParent(canvas.transform, false);
|
||||
notificationInstance.transform.localScale = Vector3.one;
|
||||
|
||||
var notificationRect = notificationInstance.GetComponent<RectTransform>();
|
||||
var canvasRect = canvas.GetComponent<RectTransform>();
|
||||
var label = notificationInstance.GetComponent<UnityEngine.UI.Text>();
|
||||
|
||||
if (label != null)
|
||||
{
|
||||
label.text = text;
|
||||
}
|
||||
|
||||
if (notificationRect != null && canvasRect != null)
|
||||
{
|
||||
Camera worldCamera = canvas.worldCamera != null ? canvas.worldCamera : Camera.main;
|
||||
Camera uiCamera = canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : worldCamera;
|
||||
|
||||
if (worldCamera != null)
|
||||
{
|
||||
Vector2 screenPoint = worldCamera.WorldToScreenPoint(transform.position);
|
||||
|
||||
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPoint, uiCamera, out Vector2 localPoint))
|
||||
{
|
||||
notificationRect.anchoredPosition = localPoint;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StartCoroutine(HideNotification());
|
||||
}
|
||||
|
||||
protected IEnumerator HideNotification()
|
||||
{
|
||||
yield return new WaitForSeconds(1.5f);
|
||||
|
||||
if (notificationInstance != null)
|
||||
{
|
||||
Destroy(notificationInstance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,13 +18,15 @@ public class YummyCake : MonoBehaviour
|
||||
|
||||
public GameObject keyPrefab;
|
||||
|
||||
public Canvas canvas;
|
||||
|
||||
private AudioSource audioSource;
|
||||
|
||||
private Animator animator;
|
||||
|
||||
private WorldFiller worldFiller;
|
||||
|
||||
private static float laserProbability = 0.4f;
|
||||
private static float laserProbability = 0.25f;
|
||||
|
||||
private static float magnetProbability = 0.2f;
|
||||
|
||||
@@ -70,21 +72,31 @@ public class YummyCake : MonoBehaviour
|
||||
|
||||
private void SpawnItem(float randomValue)
|
||||
{
|
||||
if (randomValue < laserProbability)
|
||||
float laserThreshold = laserProbability;
|
||||
float magnetThreshold = laserThreshold + magnetProbability;
|
||||
float keyThreshold = magnetThreshold + keyProbability;
|
||||
GameObject item = null;
|
||||
|
||||
if (randomValue < laserThreshold)
|
||||
{
|
||||
Instantiate(laserPrefab, transform.position, Quaternion.identity);
|
||||
item = Instantiate(laserPrefab, transform.position, Quaternion.identity);
|
||||
}
|
||||
else if (randomValue < magnetProbability)
|
||||
else if (randomValue < magnetThreshold)
|
||||
{
|
||||
Instantiate(magnetPrefab, transform.position, Quaternion.identity);
|
||||
item = Instantiate(magnetPrefab, transform.position, Quaternion.identity);
|
||||
}
|
||||
else if (randomValue < keyProbability)
|
||||
else if (randomValue < keyThreshold)
|
||||
{
|
||||
Instantiate(keyPrefab, transform.position, Quaternion.identity);
|
||||
item = Instantiate(keyPrefab, transform.position, Quaternion.identity);
|
||||
}
|
||||
else
|
||||
{
|
||||
Instantiate(lightningPrefab, transform.position, Quaternion.identity);
|
||||
item = Instantiate(lightningPrefab, transform.position, Quaternion.identity);
|
||||
}
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
item.GetComponent<YummyBase>().canvas = canvas;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,14 +118,11 @@ public class YummyCake : MonoBehaviour
|
||||
else
|
||||
{
|
||||
// The first yummy is guaranteed.
|
||||
float randomValue = 0.0f;
|
||||
SpawnItem(Random.value);
|
||||
|
||||
while (randomValue < manyProbability)
|
||||
while (Random.value < manyProbability)
|
||||
{
|
||||
SpawnItem(randomValue);
|
||||
|
||||
// Update randomValue to determine if we should spawn another item.
|
||||
randomValue = Random.value;
|
||||
SpawnItem(Random.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@ public class YummyKey : YummyBase
|
||||
{
|
||||
protected override void ObtainedCallback(Player player)
|
||||
{
|
||||
player.lives += 1;
|
||||
player.lives += Random.Range(1, 5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@ public class YummyLaser : YummyBase
|
||||
{
|
||||
protected override void ObtainedCallback(Player player)
|
||||
{
|
||||
player.laserCount += 1;
|
||||
int awarded = Random.Range(1, 15);
|
||||
player.laserCount += awarded;
|
||||
|
||||
ShowNotification($"{awarded}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@ public class YummyMagnet : YummyBase
|
||||
{
|
||||
protected override void ObtainedCallback(Player player)
|
||||
{
|
||||
player.magnetCount += 1;
|
||||
int awarded = Random.Range(1, 15);
|
||||
player.magnetCount += awarded;
|
||||
|
||||
ShowNotification($"{awarded}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user