You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

48 lines
1.3 KiB

using System;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public struct Condition<T>
{
[Serializable]
struct ConditionElement
{
public T condition;
public bool mustMatch;
}
[SerializeField] ConditionElement[] conditions;
[SerializeField] int shouldMatchCount;
public bool CheckCondition(IReadOnlyList<T> currentState)
{
if(shouldMatchCount == 0)
shouldMatchCount = conditions.Length;
var currentRemainConditions = new List<ConditionElement>();
int mustMatchCount = 0;
for(int i = 0; i < conditions.Length; i++)
{
currentRemainConditions.Add(conditions[i]);
if (conditions[i].mustMatch)
mustMatchCount++;
}
int currentMatchCount = 0;
for (int i = 0; i < currentState.Count; i++)
{
int targetIdx = currentRemainConditions.FindIndex(x => x.condition.Equals(currentState[i]));
if (targetIdx == -1) continue;
if(currentRemainConditions[targetIdx].mustMatch)
currentMatchCount--;
currentMatchCount++;
currentRemainConditions.RemoveAt(targetIdx);
}
return mustMatchCount <= 0 && currentMatchCount >= shouldMatchCount;
}
}