Added Scoring
added ability to get rid of treats and increment a score
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class BasketController : MonoBehaviour
|
||||
{
|
||||
public ScoreManager scoreManager;
|
||||
|
||||
private void OnCollisionEnter2D(Collision2D collision)
|
||||
{
|
||||
Debug.Log("2D Solid hit with: " + collision.gameObject.name);
|
||||
|
||||
if (collision.gameObject.CompareTag("Interactable"))
|
||||
{
|
||||
scoreManager.AddPoints(1);
|
||||
|
||||
GameObject.Destroy(collision.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bba2c7dd7c77e0a4ab1157bab0c5bd0f
|
||||
@@ -0,0 +1,31 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
public class ScoreManager : MonoBehaviour
|
||||
{
|
||||
public int Score = 0;
|
||||
private Label _scoreLabel;
|
||||
|
||||
public static System.Action<int> OnScoreChanged;
|
||||
|
||||
|
||||
public void AddPoints(int points)
|
||||
{
|
||||
Score += points;
|
||||
UpdateScoreDisplay();
|
||||
Debug.Log("Score updated: " + Score);
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
var root = GetComponent<UIDocument>().rootVisualElement;
|
||||
// Find your Label by name in the UXML
|
||||
_scoreLabel = root.Q<Label>("Score");
|
||||
}
|
||||
|
||||
private void UpdateScoreDisplay()
|
||||
{
|
||||
OnScoreChanged?.Invoke(Score);
|
||||
_scoreLabel.text = $"Score: {Score}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d6e51e2135855944dbc7ba0763ce1cba
|
||||
Reference in New Issue
Block a user