Space-Smash-Out/Assets/Scripts/MatchLogic.cs
Jakob Feldmann 6d89d9d48e fix: introduced assetbundles, instead of reading from folder
Reading Assets from folders was only possible in Editor over  LoadAssetFromPath. Now I'm using asset bundles for ScriptedObjects and Audio Prefabs, when the project is built.
When in Editor the folders are still used for convenience (can make changes to assets without rebuilding asset bundles)
2024-04-18 20:23:17 +02:00

104 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace GameLogic
{
/// <summary>
/// Updates and checks a matches rules according to the state of the match.
/// </summary>
public static class MatchLogic
{
public static MatchRule currentRule;
/// <summary>
/// Update match conditions and check for match conclusion.
/// </summary>
/// <param name="p">Affected player</param>
/// <param name="args">Update to the matches conditions</param>
/// <param name="mps">Dictionary of the players in the match and their current stats</param>
/// <returns></returns>
public static void UpdateMatchResult(Player p, MatchConditionUpdate args,
Dictionary<Player, MatchPlayerStatistic> mps, GameResult result)
{
switch (args.Condition)
{
case WinCondition.Lives:
UpdateLives(mps[p], args.Count);
break;
case WinCondition.Score:
UpdateScore(mps[p], args.Count);
break;
case WinCondition.Time:
UpdateTime(mps[p], args.Count);
break;
}
result.UpdateGameResult(mps, currentRule);
}
public static void UpdateLives(MatchPlayerStatistic mps, int count)
{
mps.Lives += count;
if (mps.Lives <= 0)
{
mps.IsOut = true;
}
}
public static void UpdateScore(MatchPlayerStatistic mps, int count)
{
throw new NotImplementedException();
}
public static void UpdateTime(MatchPlayerStatistic mps, int count)
{
throw new NotImplementedException();
}
}
/// <summary>
/// Data class which records the results of a Match
/// </summary>
public class GameResult
{
/// <summary>
/// Indicates whether a round or the whole match was won
/// </summary>
public bool IsMatchWon { get; private set; }
public int RoundsPlayed { get; private set; }
public Player Winner { get; private set; }
public List<Player> Opponents { get; private set; }
/// <summary>
/// Checks whether a round is won and if that round decided the match.
/// Sets the class properties accordingly.
/// </summary>
/// <param name="mps">Dictionary of players with their statistics for this match</param>
/// <param name="rules">The rules for this match</param>
public void UpdateGameResult(Dictionary<Player, MatchPlayerStatistic> mps, MatchRule rules)
{
int outPlayers = mps.Count(p => p.Value.IsOut);
// TODO: blatant error here right now
if (outPlayers == mps.Count - 1)
{
Winner = mps.First(p => p.Value.IsOut != true).Key;
Opponents = mps.Keys.Where(player => player != Winner).ToList();
mps[Winner].RoundsWon += 1;
RoundsPlayed += 1;
}
else
{
Winner = null;
}
// TODO: this is wrong winning 2 rounds can decide the match
if (RoundsPlayed == rules.rounds)
{
IsMatchWon = true;
Winner = mps.Aggregate((p1, p2) =>
p1.Value.RoundsWon > p2.Value.RoundsWon ? p1 : p2).Key;
Opponents = mps.Keys.Where(player => player != Winner).ToList();
}
}
}
}