dissertation / mainCode / Game / ScoreManager.cs
ScoreManager.cs
Raw
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;


public class ScoreManager : MonoBehaviour
{
        private float startTime;
        private GameManager gameManager;
        private double killsPerSecond = 0f; // higher better
        private double accuracy = 0f; // higher better
        private double headshotPercentage = 0f; // higher better
        private double averageHealthLossPerEnemy = 0f; // lower better
        private double timeToDamage = 0f; // lower better
        private double timeToKill = 0f; // lower better

        public int headshots = 0;
        public int hitshots = 0;
        public int firedshots = 0;
        public List<double> timesToDamage = new List<double>();
        public List<double> timesToKill = new List<double>();
        public List<double> healthLossPerEnemy = new List<double>();
        private double score;
        
        private void Start()
        {
                startTime = Time.time;
                gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
        }
        
        private void Update()
        {
                if (timesToDamage.Any())
                {
                        timeToDamage = timesToDamage.Average();
                }

                if (timesToKill.Any())
                {
                        timeToKill = timesToKill.Average();
                }

                if (healthLossPerEnemy.Any())
                {
                        averageHealthLossPerEnemy = healthLossPerEnemy.Average();
                }

                if (hitshots > 0)
                {
                        headshotPercentage = headshots / (double) hitshots;
                }

                if (firedshots > 0)
                {
                        accuracy = hitshots / (double) firedshots;
                }
                
                killsPerSecond = gameManager.GetNoKills() / (Time.time - startTime);
                
                score = killsPerSecond + accuracy + headshotPercentage + timeToDamage +
                        averageHealthLossPerEnemy + timeToKill;
        }

        public float GetScore()
        {
                return (float) score;
        }

        public GameResults SetResults(GameResults results)
        {
                results.accuracy = (float) accuracy;
                results.killsPerSecond = (float) killsPerSecond;
                results.headshotPercentage = (float) headshotPercentage;
                results.timeToDamage = (float) timeToDamage;
                results.averageHealthLoss = (float) averageHealthLossPerEnemy;
                results.timeToKill = (float) timeToKill;
                return results;
        }
        
}