Untitled
unknown
plain_text
a year ago
4.0 kB
11
Indexable
using UnityEngine;
using System.Collections;
public class PointsCounter : MonoBehaviour
{
public int Points;
public int Streak; // Tracks the current scoring streak
public int BestStreak; // Tracks the highest streak
public bool isActive = true;
public GameObject Area;
public float PointCooldown = 2f;
public bool didScore = false; // Tracks if a score has occurred
[Header("Streak Feature")]
private const string PointsKey = "SavedPoints";
private const string StreakKey = "SavedStreak"; // Key for saving streak
private const string BestStreakKey = "SavedBestStreak";
public AudioSource pointGet;
[Header("Dependencies")]
public FallCheck fallCheck; // Reference to FallCheck script
public EnterCheck enterCheck; // Reference to EnterCheck script
[Header("Reset Settings")]
public float scoreResetDelay = 2f; // Time in seconds before resetting didScore
public void SetActive(bool state)
{
isActive = state;
}
private void Start()
{
Points = PlayerPrefs.GetInt(PointsKey, 0);
BestStreak = PlayerPrefs.GetInt(BestStreakKey, 0);
Streak = PlayerPrefs.GetInt(StreakKey, 0); // Load the saved streak
}
private void OnTriggerEnter(Collider other)
{
if (!isActive) return;
if (other.CompareTag("Basketball"))
{
Debug.Log("Scored Point!");
didScore = true; // Set didScore to true when scoring
pointGet.Play();
Points++;
Streak++;
// Update the best streak if the current streak is higher
if (Streak > BestStreak)
{
BestStreak = Streak;
PlayerPrefs.SetInt(BestStreakKey, BestStreak);
}
PlayerPrefs.SetInt(PointsKey, Points);
PlayerPrefs.SetInt(StreakKey, Streak); // Save the current streak
PlayerPrefs.Save();
if (Area != null)
{
BoxCollider areaCollider = Area.GetComponent<BoxCollider>();
if (areaCollider != null)
{
areaCollider.enabled = false;
StartCoroutine(ReenableColliderAfterCooldown(areaCollider));
}
}
}
}
// Call this method when the player fails to score (implement failure detection in your game logic)
public void ResetStreak()
{
Streak = 0;
PlayerPrefs.SetInt(StreakKey, Streak); // Save the reset streak
PlayerPrefs.Save();
didScore = false; // Reset didScore on failure
}
private IEnumerator ReenableColliderAfterCooldown(BoxCollider collider)
{
yield return new WaitForSeconds(PointCooldown);
collider.enabled = true;
}
private void Update()
{
// Check if FallCheck hasCollidedWithGround is enabled
if (fallCheck != null && fallCheck.hasCollidedWithGround)
{
if (enterCheck != null && enterCheck.didEnter)
{
if (didScore)
{
// Maintain the streak
}
else
{
// Reset streak if both didEnter and hasCollidedWithGround are active but didScore is not
ResetStreak();
}
}
}
// Reset didScore after a delay if required
if (fallCheck != null && fallCheck.hasCollidedWithGround && didScore)
{
StartCoroutine(ResetDidScoreAfterDelay());
}
}
private IEnumerator ResetDidScoreAfterDelay()
{
yield return new WaitForSeconds(scoreResetDelay);
didScore = false;
}
public void ResetPoints()
{
Points = 0;
PlayerPrefs.SetInt(PointsKey, Points);
PlayerPrefs.Save();
}
}
Editor is loading...
Leave a Comment