Creating a new Win Condition
To create a new win condition, create a new class and derive it from the WinCondition.
There is an abstract method bool CheckCondition(out int winnerTeam), you need to override it with your own. It should return true if one of the teams wins. Also should fill the winnerTeam variable, so the asset will know which team wins.
Don’t forget to add CreateAssetMenu since this is a Scriptable Object and you will need a way to create asset of your WinCondition type.
Final code can look like this:
using UnityEngine;
namespace InsaneSystems.RTSStarterKit
{
// code of the built-in condition for example
[CreateAssetMenu(fileName = "CollectResourcesAmount", menuName = Storage.AssetName + "/Win Conditions/Collect Resources Amount")]
public sealed class CollectResourcesAmount : WinCondition
{
[SerializeField] int neededAmount = 15000;
[Tooltip("If AI player should not win if he collect such money amount, check this true.")]
[SerializeField] bool onlyForNonAIPlayers = true;
public override bool CheckCondition(out int winnerTeam)
{
var playersIngame = GameController.Instance.PlayersController.PlayersIngame;
winnerTeam = -1;
for (int i = 0; i < playersIngame.Count; i++)
{
if (onlyForNonAIPlayers && playersIngame[i].IsAIPlayer)
continue;
if (playersIngame[i].Money >= neededAmount)
{
winnerTeam = playersIngame[i].TeamIndex;
return true;
}
}
return false;
}
}
}
You can add any parameters and it will be shown in the asset of your win condition.