added scoring system for the bow game
This commit is contained in:
@@ -5,6 +5,7 @@ using _PROJECT.Scripts.Bow;
|
||||
using FishNet.Object;
|
||||
using FishNet.Object.Synchronizing;
|
||||
using TMPro;
|
||||
using Unity.XR.CoreUtils;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
@@ -24,6 +25,8 @@ public class ArcheryRange : NetworkBehaviour
|
||||
public float roundLength = 60f;
|
||||
public float targetSpawnTime = 3f;
|
||||
|
||||
public KeyboardManager keyboardManager;
|
||||
|
||||
private List<ArcheryTarget> _targets;
|
||||
|
||||
[SyncVar]
|
||||
@@ -33,6 +36,10 @@ public class ArcheryRange : NetworkBehaviour
|
||||
private float _nextTargetTime;
|
||||
private bool _roundActive;
|
||||
|
||||
private readonly List<XROrigin> _presentPlayers = new();
|
||||
|
||||
private XROrigin _scoredPlayer;
|
||||
|
||||
public override void OnStartServer()
|
||||
{
|
||||
base.OnStartServer();
|
||||
@@ -41,7 +48,46 @@ public class ArcheryRange : NetworkBehaviour
|
||||
_score = 0;
|
||||
_maxScore = 0;
|
||||
_roundEndTime = 0f;
|
||||
SetHighScoreText("High Score: 0");
|
||||
_maxScore = LoadScoreFromDisk();
|
||||
//+Debug.Log($"highScore is {_maxScore}");
|
||||
|
||||
StartCoroutine(DelayedSetHighScoreText(10f)); // Cannot immediately assign these values because fishnet needs time.
|
||||
}
|
||||
|
||||
private System.Collections.IEnumerator DelayedSetHighScoreText(float delaySeconds)
|
||||
{
|
||||
yield return new WaitForSeconds(delaySeconds);
|
||||
SetHighScoreText($"High Score: {_maxScore}");
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
SetHighScoreText($"High Score: {_maxScore}"); // Refresh high score UI
|
||||
|
||||
XROrigin enteredPlayer = other.GetComponent<XROrigin>();
|
||||
if (enteredPlayer == null || _presentPlayers.Contains(enteredPlayer))
|
||||
return;
|
||||
|
||||
_presentPlayers.Add(enteredPlayer); // Adds to the end
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
XROrigin exitedPlayer = other.GetComponent<XROrigin>();
|
||||
if (exitedPlayer == null) return;
|
||||
|
||||
_presentPlayers.Remove(exitedPlayer); // Shifts others left automatically
|
||||
|
||||
if (exitedPlayer == _scoredPlayer) keyboardManager.DeActivate(); // If the player <20>refuses to enter their name.
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override void OnStopServer()
|
||||
{
|
||||
base.OnStopServer();
|
||||
SaveScoreToDisk(_maxScore);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
@@ -101,9 +147,30 @@ public class ArcheryRange : NetworkBehaviour
|
||||
|
||||
_targets = new List<ArcheryTarget>();
|
||||
if (_maxScore < _score) _maxScore = _score;
|
||||
|
||||
if(_presentPlayers.Count != 0) // If there are players in the area.
|
||||
{
|
||||
// Gives the score to the player longest-lasting in the area. It would be better to give it to the player that fired the starting arrow, but I'm not spending 10 hours on this.
|
||||
|
||||
XROrigin scoringPlayer = _presentPlayers.First();
|
||||
|
||||
Menu playermenu = scoringPlayer.GetComponentInChildren<Menu>();
|
||||
|
||||
_scoredPlayer = scoringPlayer;
|
||||
|
||||
playermenu.setCanvasVisibility(false); // Deactivates the menu to avoid overlap issues.
|
||||
|
||||
Transform keyboardPlacement = playermenu.updateMenuTransform();
|
||||
|
||||
keyboardManager.Activate(_score, keyboardPlacement);
|
||||
|
||||
SaveScoreToDisk(_maxScore);
|
||||
SetHighScoreText($"High Score: {_maxScore}");
|
||||
}
|
||||
_score = 0;
|
||||
SetHighScoreText($"High Score: {_maxScore}");
|
||||
|
||||
startTarget.ShowTarget();
|
||||
|
||||
_roundActive = false;
|
||||
SetTimeLeftText("");
|
||||
}
|
||||
@@ -139,5 +206,29 @@ public class ArcheryRange : NetworkBehaviour
|
||||
public void SetTimeLeftText(string text)
|
||||
{
|
||||
timeLeftText.text = text;
|
||||
}
|
||||
}
|
||||
}
|
||||
public class SaveData
|
||||
{
|
||||
public float HighScore;
|
||||
}
|
||||
|
||||
public void SaveScoreToDisk(float highScore)
|
||||
{
|
||||
SaveData data = new SaveData { HighScore = highScore };
|
||||
string json = JsonUtility.ToJson(data);
|
||||
System.IO.File.WriteAllText("highscore.json", json);
|
||||
}
|
||||
public float LoadScoreFromDisk()
|
||||
{
|
||||
if (System.IO.File.Exists("highscore.json"))
|
||||
{
|
||||
string json = System.IO.File.ReadAllText("highscore.json");
|
||||
SaveData data = JsonUtility.FromJson<SaveData>(json);
|
||||
return data.HighScore;
|
||||
}
|
||||
|
||||
return -1f; // default score
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user