Commit 97452fe6 authored by Jonas's avatar Jonas

Score Calculation

parent 17f8d808
...@@ -14,12 +14,19 @@ namespace _Game.Scripts.GameManager { ...@@ -14,12 +14,19 @@ namespace _Game.Scripts.GameManager {
public Color color; public Color color;
public bool isWinner; public bool isWinner;
public void Reset(string name, Color color) { public void Reset(string name, Color color, Role initRole) {
this.name = name; this.name = name;
score = 0f; score = 0f;
scoreLastRound = 0f; scoreLastRound = 0f;
this.color = color; this.color = color;
isWinner = false; isWinner = false;
role = initRole;
Debug.Log("Player Reset aufgerufen");
}
public void UpdateScore(float scoreNew)
{
this.score += scoreNew;
} }
} }
......
...@@ -5,29 +5,35 @@ using UnityEngine; ...@@ -5,29 +5,35 @@ using UnityEngine;
using UnityEngine.InputSystem.Interactions; using UnityEngine.InputSystem.Interactions;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
namespace _Game.Scripts.GameManager { namespace _Game.Scripts.GameManager
public class GameHandler : SingletonMonobehaviour<GameHandler> { {
public class GameHandler : SingletonMonobehaviour<GameHandler>
{
[SerializeField] private GameData data; [SerializeField] private GameData data;
[SerializeField] private Countdown startDelay; [SerializeField] private Countdown startDelay;
[SerializeField] private Countdown roundTime; [SerializeField] private Countdown roundTime;
[SerializeField] private Transform _handler1, _handler2; [SerializeField] private Transform _handler1, _handler2;
private Transform _head, _body; private Transform _head, _body;
private void OnEnable() { private void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded; SceneManager.sceneLoaded += OnSceneLoaded;
} }
private void OnDisable() { private void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded; SceneManager.sceneLoaded -= OnSceneLoaded;
} }
private void OnSceneLoaded(Scene scene, LoadSceneMode mode) { private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
print("On Scene Loaded " + scene.ToString() + " Mode : " + mode.ToString()); print("On Scene Loaded " + scene.ToString() + " Mode : " + mode.ToString());
SetReferences(); SetReferences();
StartLogic(); StartLogic();
} }
private void SetReferences() { private void SetReferences()
{
_head = GameObject.FindWithTag("HeadPlayer").transform; _head = GameObject.FindWithTag("HeadPlayer").transform;
_body = GameObject.FindWithTag("BodyPlayer").transform; _body = GameObject.FindWithTag("BodyPlayer").transform;
...@@ -38,11 +44,13 @@ namespace _Game.Scripts.GameManager { ...@@ -38,11 +44,13 @@ namespace _Game.Scripts.GameManager {
startDelay.Register(OnStartRound); startDelay.Register(OnStartRound);
} }
private void StartLogic() { private void StartLogic()
{
StartCoroutine(startDelay.StartCountdown()); StartCoroutine(startDelay.StartCountdown());
} }
private void OnStartRound() { private void OnStartRound()
{
print("Round Start"); print("Round Start");
roundTime.Clear(); roundTime.Clear();
roundTime.Duration = data.roundTime; roundTime.Duration = data.roundTime;
...@@ -51,26 +59,106 @@ namespace _Game.Scripts.GameManager { ...@@ -51,26 +59,106 @@ namespace _Game.Scripts.GameManager {
StartCoroutine(roundTime.StartCountdown()); StartCoroutine(roundTime.StartCountdown());
} }
private void OnRoundEnd() { private void OnRoundEnd()
{
print("Round End"); print("Round End");
roundTime.StopCountdown = true; roundTime.StopCountdown = true;
UpdateScorePoints();
SwitchRolesOfPlayers(); // changes playerX.role in GameData
data.RoundsPlayed++; data.RoundsPlayed++;
print("Rounds Played = " + data.RoundsPlayed); print("Rounds Played = " + data.RoundsPlayed);
if (data.RoundsPlayed >= data.roundsToPlay) { if (data.RoundsPlayed >= data.roundsToPlay)
{
print("Game Ends"); print("Game Ends");
SceneManager.LoadScene("StartMenu"); SceneManager.LoadScene("StartMenu");
return; return;
} }
if (data.IsSwitched) { if (data.IsSwitched)
{
data.IsSwitched = false; data.IsSwitched = false;
SceneManager.LoadScene("MainLevel"); SceneManager.LoadScene("MainLevel");
} }
else { else
{
data.IsSwitched = true; data.IsSwitched = true;
SceneManager.LoadScene("MainLevelSwitched"); SceneManager.LoadScene("MainLevelSwitched");
}
}
private void UpdateScorePoints()
{
Debug.Log("Updated Scores");
if (data.player1.role == GameData.Role.Body)
{
// Punkte = [Punkte von letzter Runde] + ( [MaxRundenzeit] - [Noch Übrige Zeit] );
data.player1.UpdateScore(data.player1.scoreLastRound + data.roundTime - roundTime.CurrentTime);
}
else if (data.player1.role == GameData.Role.Head)
{
// Punkte = [Punkte von letzter Runde] + ( Rundenzeit ) - ( [ Rundenzeit ] - [ noch übrige Zeit ] ) )
data.player1.UpdateScore(data.player1.scoreLastRound +
((data.roundTime) - (data.roundTime - roundTime.CurrentTime)));
}
else
{
throw new UnityException(
"Can't update score of player 1 because he didn't does not have a role (Head or Body)");
}
if (data.player2.role == GameData.Role.Body)
{
// Punkte = [Punkte von letzter Runde] + ( [MaxRundenzeit] - [Noch Übrige Zeit] );
data.player2.UpdateScore(data.player2.scoreLastRound + data.roundTime - roundTime.CurrentTime);
}
else if (data.player2.role == GameData.Role.Head)
{
// Punkte = [Punkte von letzter Runde] + ( Rundenzeit ) - ( [ Rundenzeit ] - [ noch übrige Zeit ] ) )
data.player2.UpdateScore(data.player2.scoreLastRound +
((data.roundTime) - (data.roundTime - roundTime.CurrentTime)));
}
else
{
throw new UnityException(
"Can't update score of player 2 because he didn't does not have a role (Head or Body)");
}
}
void SwitchRolesOfPlayers()
{
Debug.Log("Switching Roles of Players");
switch (data.player1.role)
{
case GameData.Role.Body:
data.player1.role = GameData.Role.Head;
break;
case GameData.Role.Head:
data.player1.role = GameData.Role.Body;
break;
default:
throw new UnityException(
"Can't update roles of player 1 because he didn't does not have a role (Head or Body)");
break;
}
switch (data.player2.role)
{
case GameData.Role.Body:
data.player2.role = GameData.Role.Head;
break;
case GameData.Role.Head:
data.player2.role = GameData.Role.Body;
break;
default:
throw new UnityException(
"Can't update roles of player 2 because he didn't does not have a role (Head or Body)");
break;
} }
} }
} }
......
...@@ -31,8 +31,8 @@ public class CharacterCustomization : MonoBehaviour { ...@@ -31,8 +31,8 @@ public class CharacterCustomization : MonoBehaviour {
public void GoToGameScene() { public void GoToGameScene() {
data.Reset(); data.Reset();
data.player1.Reset(player1InputField.text, Color.green); data.player1.Reset(player1InputField.text, Color.green, GameData.Role.Head);
data.player2.Reset(player2InputField.text, Color.red); data.player2.Reset(player2InputField.text, Color.red, GameData.Role.Body);
SceneManager.LoadScene("MainLevel"); SceneManager.LoadScene("MainLevel");
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment