Add score

This commit is contained in:
James Hodgson
2022-04-13 23:21:15 +01:00
parent 2efbbfa61a
commit a8b5c87285
10 changed files with 2738 additions and 8 deletions

View File

@@ -2,13 +2,16 @@ using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FishCollector : MonoBehaviour
{
public GameObject collectiblesContainer;
public Text scoreText;
// Total score of all collectibles
public int totalScore = 0;
public int totalScore = 0;
private int maxScore = 0;
Collectible[] collectibles;
@@ -23,11 +26,16 @@ public class FishCollector : MonoBehaviour
cc = GetComponent<CharacterController>();
collectibles = collectiblesContainer.GetComponentsInChildren<Collectible>();
foreach (Collectible collectible in collectibles)
{
maxScore += collectible.score;
}
UpdateUI();
}
// FixedUpdate is called consistently at 50 fps (by default)
// Figured we can do collection checks slightly less frequently :)
void FixedUpdate()
void Update()
{
Vector3 fishPosition = r.bounds.center;
@@ -47,8 +55,6 @@ public class FishCollector : MonoBehaviour
bool y = false;
bool z = false;
// Debug.Log(String.Format("FishPosition: {0}, CollectiblePosition: {1}, CollectibleRadius: {2}", fishPosition, collectiblePosition, collectibleRadius));
// Check if we are colliding with the collectible
if (fishPosition.x >= collectiblePosition.x - collectibleRadius &&
fishPosition.x <= collectiblePosition.x + collectibleRadius)
@@ -72,11 +78,18 @@ public class FishCollector : MonoBehaviour
{
totalScore += collectible.score;
collectible.Collect();
// Update UI
UpdateUI();
// Assume we aren't colliding with 2 at the same time!
break;
}
}
}
void UpdateUI()
{
scoreText.text = String.Format("Score: {0}/{1}", totalScore, maxScore);
}
}