dissertation / mainCode / UI / ResultsManager.cs
ResultsManager.cs
Raw
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ResultsManager : MonoBehaviour
{
    private GameResults results;

    private int BAD_VERTICAL_CROSSHAIR_PLACEMENT = 6;
    private int BAD_HORIZONTAL_CROSSHAIR_PLACEMENT = 13;
    private int BAD_MISSED_HORIZONTAL_SHOTS = 0;
    private int BAD_MISSED_VERTICAL_SHOTS = 0;
    private int BAD_DAMAGE_SOURCES = 1;
    private float BAD_ACCURACY = 1;
    private float BAD_HEADSHOT = 1;
    private float BAD_TIME_TO_KILL = 0.9f;
    private float BAD_TIME_TO_DAMAGE = 0.6f;

    public TextMeshProUGUI hsAverage,
        hsTarget,
        accAverage,
        accTarget,
        ttdAverage,
        ttdTarget,
        ttkAverage,
        ttkTarget,
        vcAverage,
        vcTarget,
        hcAverage,
        hcTarget,
        missAverage,
        missTarget,
        dsAverage,
        dsTarget,
        scoreText,
        levelText,
        levelShiftText,
        timeText,
        mainResults;
        
    
    
    private List<float>
        averageVc = new List<float>(),
        averageHc = new List<float>(),
        averageMisses = new List<float>();

    private List<int> averageDs = new List<int>();
    
    
    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
        results = JsonUtility.FromJson<GameResults>(PlayerPrefs.GetString("lastGame"));

        scoreText.text = "Score: " + (results.score != 0 ? ((int) results.score).ToString() : "play 5 times to calibrate");
        levelText.text = "Current Level: " + results.level;
        levelShiftText.text = "Level Shift: " + PlayerPrefs.GetInt("levelShift");
        timeText.text = "Time Taken: " + Math.Round(results.timeElapsed, 2) + " seconds";
        
        BAD_ACCURACY = PlayerPrefs.GetFloat("playerAccuracy");
        BAD_HEADSHOT = PlayerPrefs.GetFloat("playerHeadshot");
        BAD_TIME_TO_DAMAGE = PlayerPrefs.GetFloat("playerTtd");
        BAD_TIME_TO_KILL = PlayerPrefs.GetFloat("playerTtk");
        BAD_VERTICAL_CROSSHAIR_PLACEMENT = (int) PlayerPrefs.GetFloat("playerVerticalCrosshair");
        BAD_HORIZONTAL_CROSSHAIR_PLACEMENT = (int) PlayerPrefs.GetFloat("playerHorizontalCrosshair");
        BAD_MISSED_VERTICAL_SHOTS = (int) PlayerPrefs.GetFloat("playerMisses");
        BAD_MISSED_HORIZONTAL_SHOTS = (int) PlayerPrefs.GetFloat("playerMisses");
        BAD_DAMAGE_SOURCES = (int) PlayerPrefs.GetFloat("playerDamageSources");
        foreach (var engagement in results.engagements)
        {
            averageVc.Add(engagement.verticalCrosshairMovement);
            averageHc.Add(engagement.horizontalCrosshairMovement);
            averageDs.Add(engagement.noOfDamageSources.Count);
            averageMisses.Add(engagement.missedVerticalShots.Count + engagement.missedHorizontalShots.Count);
        }

        if (results.won)
        {
            mainResults.text = "You won with " + results.kills + " kills!";
            mainResults.color = Color.green;
        }
        else
        {
            mainResults.text = "You lost with " + results.kills + " kills!";
            mainResults.color = Color.red;
        }

        SetText(BAD_HEADSHOT, results.headshotPercentage, hsTarget, hsAverage, true);
        SetText(BAD_ACCURACY, results.accuracy, accTarget, accAverage, true);
        SetText(BAD_TIME_TO_DAMAGE, results.timeToDamage, ttdTarget, ttdAverage, false);
        SetText(BAD_TIME_TO_KILL, results.timeToKill, ttkTarget, ttkAverage, false);
        SetText(BAD_VERTICAL_CROSSHAIR_PLACEMENT, averageVc.Average(), vcTarget, vcAverage, false);
        SetText(BAD_HORIZONTAL_CROSSHAIR_PLACEMENT, averageHc.Average(), hcTarget, hcAverage, false);
        SetText(BAD_MISSED_VERTICAL_SHOTS + BAD_MISSED_HORIZONTAL_SHOTS, averageMisses.Average(), missTarget, missAverage, false);
        SetText(BAD_DAMAGE_SOURCES, (float) averageDs.Average(), dsTarget, dsAverage, false);
    }

    void SetText(float target, float average, TextMeshProUGUI targetText, TextMeshProUGUI averageText, bool lower)
    {
        targetText.text = target.ToString();
        averageText.text = average.ToString();
        if (average < target)
        {
            averageText.color = lower ? Color.red : Color.green;
        }
        else
        {
            averageText.color = lower ? Color.green : Color.red;
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void Retry()
    {
        SceneManager.LoadScene("Game", LoadSceneMode.Single);
    }

    public void MainMenu()
    {
        SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
    }
    
}