Skip to content

Commit

Permalink
fix: endgame ui
Browse files Browse the repository at this point in the history
  • Loading branch information
pyoneerC committed Sep 26, 2024
1 parent 63a3e67 commit 46e814a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 48 deletions.
10 changes: 10 additions & 0 deletions Assets/Scenes/Reparation.unity
Original file line number Diff line number Diff line change
Expand Up @@ -24751,6 +24751,16 @@ MonoBehaviour:
reticle: {fileID: 1374808373}
restartButton: {fileID: 1480355432}
quitButton: {fileID: 931267296}
googlesSupportA: {fileID: 1589523900}
googlesSupportB: {fileID: 114400261}
googles: {fileID: 1979774045}
minimap: {fileID: 1521861653}
oxygenCount: {fileID: 353184058}
oxygenCountExplanation: {fileID: 483585573}
heightCount: {fileID: 598384038}
heightCountExplanation: {fileID: 1208940814}
healthCount: {fileID: 1578620231}
healthCountExplanation: {fileID: 1096392008}
--- !u!4 &1130256303 stripped
Transform:
m_CorrespondingSourceObject: {fileID: -5819118886272102141, guid: e0f672a015f8fee418afd70dfd718f0d, type: 3}
Expand Down
99 changes: 51 additions & 48 deletions Assets/Scripts/UI/Endgame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,49 @@

public class Endgame : MonoBehaviour
{
// Variables for UI elements
public TextMeshProUGUI scoreExplanationText;
public TextMeshProUGUI scoreExplanationTextOutOfTen;
public TextMeshProUGUI timePlayedTextExplanation;
public TextMeshProUGUI timePerPipeTextExplanation;
public TextMeshProUGUI oxygenWarningsAmountTextExplanation;
public TextMeshProUGUI distanceCoveredTextExplanation;

public TextMeshProUGUI endgameText; // Victory or Loss text
public TextMeshProUGUI scoreText; // 0 if player loses, random score 7 to 10 if wins
public TextMeshProUGUI timePlayedText; // Time played
public TextMeshProUGUI timePerPipeText; // Time played divided by 7
public TextMeshProUGUI oxygenWarningsAmountText; // Number of times an oxygen warning was triggered
public TextMeshProUGUI distanceCoveredText; // Distance covered as the sum of all 3 components (x, y, z)
public TextMeshProUGUI endgameText;
public TextMeshProUGUI scoreText;
public TextMeshProUGUI timePlayedText;
public TextMeshProUGUI timePerPipeText;
public TextMeshProUGUI oxygenWarningsAmountText;
public TextMeshProUGUI distanceCoveredText;
public Image reticle;
public Button restartButton;
public Button quitButton;

private Health _health; // Reference to player health
private FixLogic _fixLogic; // Reference to the fixing logic
private WaterLogic _waterLogic; // Reference to the water logic
private Transform _playerTransform; // Player's transform (captured automatically from prefab)
public Image googlesSupportA;
public Image googlesSupportB;
public Image googles;

private int _score; // Final score for winning
private float _timePlayed; // Total time played
private bool _gameEnded; // Check if the game has ended
private int _oxygenAlertsCount; // Oxygen alerts triggered
public Image minimap;
public TextMeshProUGUI oxygenCount;
public TextMeshProUGUI oxygenCountExplanation;
public TextMeshProUGUI heightCount;
public TextMeshProUGUI heightCountExplanation;
public TextMeshProUGUI healthCount;
public TextMeshProUGUI healthCountExplanation;

private Vector3 _startPosition; // Start position of the player
private Health _health;
private FixLogic _fixLogic;
private WaterLogic _waterLogic;
private Transform _playerTransform;

private int _score;
private float _timePlayed;
private bool _gameEnded;
private int _oxygenAlertsCount;

private Vector3 _startPosition;

private void Start()
{
// Get player transform from prefab
_playerTransform = GameObject.FindWithTag("Player").transform;

if (_playerTransform == null)
Expand All @@ -45,29 +55,22 @@ private void Start()
return;
}

// Initialize references
_health = FindObjectOfType<Health>();
_fixLogic = FindObjectOfType<FixLogic>();
_waterLogic = FindObjectOfType<WaterLogic>();

// Capture player's starting position
_startPosition = _playerTransform.position;

// Initialize the text elements to be transparent (not visible)
SetTextVisibility(false);

//hide buttons
restartButton.gameObject.SetActive(false);
quitButton.gameObject.SetActive(false);

//bind buttons

restartButton.onClick.AddListener(() => { UnityEngine.SceneManagement.SceneManager.LoadScene(0); });
quitButton.onClick.AddListener(Application.Quit);

// Start time tracking
_timePlayed = Time.time;

// Initialize explanation texts to be invisible
SetExplanationTextVisibility(0);
}

Expand All @@ -87,41 +90,42 @@ private void Update()

private void EndGame(bool isVictory)
{
_gameEnded = true; // Stop further updates
reticle.enabled = false; // Hide the reticle
_gameEnded = true;
reticle.enabled = false;

// Calculate total time played
_timePlayed = Time.time - _timePlayed; // Time since the game started
_oxygenAlertsCount = _waterLogic.oxygenAlertsCount; // Adjusted to use the correct instance
_timePlayed = Time.time - _timePlayed;
_oxygenAlertsCount = _waterLogic.oxygenAlertsCount;

// Calculate distance covered using player's current position
Vector3 endPosition = _playerTransform.position;
float distanceCovered = Vector3.Distance(_startPosition, endPosition);

// Calculate the score if the player wins
if (isVictory)
{
_score = Random.Range(7, 11); // Random score between 7 and 10
endgameText.text = "Felicidades!"; // Victory message
_score = Random.Range(7, 11);
endgameText.text = "Felicidades!";
}
else
{
_score = 0; // No score for losing
endgameText.text = "Perdiste!"; // Loss message
_score = 0;
endgameText.text = "Perdiste!";
}

// Update the UI elements
UpdateUI();

// Make the text visible
SetTextVisibility(true);
SetExplanationTextVisibility(1f);

endgameText.color = isVictory ? Color.green : // Change text color to green
Color.red; // Change text color to red
endgameText.color = isVictory ? Color.green : Color.red;
scoreText.color = isVictory ? Color.green : Color.red;

googlesSupportA.gameObject.SetActive(false);
googlesSupportB.gameObject.SetActive(false);
googles.gameObject.SetActive(false);

scoreText.color = isVictory ? Color.green : // Change text color to green
Color.red; // Change text color to red
minimap.gameObject.SetActive(false);
oxygenCount.gameObject.SetActive(false);
oxygenCountExplanation.gameObject.SetActive(false);
heightCount.gameObject.SetActive(false);
heightCountExplanation.gameObject.SetActive(false);
healthCount.gameObject.SetActive(false);
healthCountExplanation.gameObject.SetActive(false);
}

private void UpdateUI()
Expand All @@ -132,14 +136,13 @@ private void UpdateUI()
oxygenWarningsAmountText.text = $"{_oxygenAlertsCount}";
distanceCoveredText.text = $"{Vector3.Distance(_startPosition, _playerTransform.position):F2} m";

// Show buttons
restartButton.gameObject.SetActive(true);
quitButton.gameObject.SetActive(true);
}

private void SetTextVisibility(bool visible)
{
Color color = visible ? Color.white : new Color(1, 1, 1, 0); // White or Transparent
Color color = visible ? Color.white : new Color(1, 1, 1, 0);
endgameText.color = color;
scoreText.color = color;
timePlayedText.color = color;
Expand All @@ -150,7 +153,7 @@ private void SetTextVisibility(bool visible)

private void SetExplanationTextVisibility(float alpha)
{
Color color = new Color(1, 1, 1, alpha); // Set alpha for explanation texts
Color color = new Color(1, 1, 1, alpha);
scoreExplanationText.color = color;
scoreExplanationTextOutOfTen.color = color;
timePlayedTextExplanation.color = color;
Expand Down

0 comments on commit 46e814a

Please sign in to comment.