I see… That actually means the code that I provided won’t truly tell you if you’ve cleared what you need for Scene2, because mine assumed you wanted to wipe Scene1 before you could get to Scene2, but you’re only after the barbarians…
This is probably ventured a bit away from IEnumerators…
Ok, the first part is actually easy… you can’t move to Scene 2 until you’ve cleared a certain type of enemy… You’ll need to expose the characterClass in BaseStats, say a method
public CharacterClass GetCharacterClass() {return characterClass;}
bool EnemiesAllDead(CharacterClass characterClass)
{
var enemies=FindObjectsOfType<AIController>();
foreach(var enemy in enemies)
{
if(enemy.GetComponent<BaseStats>().GetCharacterClass()!=characterClass) continue;
if(enemy.GetComponent<Health>().isAlive()) return false;
}
return true;
}
The second part is much trickier. I’d probably design a “ConditionManager” and a Condition scriptable object to describe the conditions…
[CreateAssetMenu(filename="New Condition", menuName = "Condition")]
public class Condition: ScriptableObject
{
///These Conditions need to be created in a folder named Resources,
/// and cannot under any circumstances have the same name as anything else
///in any other folder named Resources.
public int scene=1;
public CharacterClass characterClass;
}
///Put this the player character.
public class ConditionManager: Monobehavior, ISaveable
{
public Condition[] conditionsToManage;
Dictionary<Condition, bool> conditionList;
void Awake()
{
if(conditionList==null)
conditionList=new Dictionary<Condition, bool>();
public void TestConditions()
{
foreach(Condition condition in conditionsToManage())
{
if(condition.scene = SceneManager.GetActiveScene().buildIndex)
{
conditionList[condition] = EnemiesAllDead(condition.characterClass);
}
}
}
bool EnemiesAllDead(CharacterClass characterClass)
{
var enemies=FindObjectsOfType<AIController>();
foreach(var enemy in enemies)
{
if(enemy.GetComponent<BaseStats>().GetCharacterClass()!=characterClass) continue;
if(enemy.GetComponent<Health>().isAlive()) return false;
}
return true;
}
public bool ConditionSatisfied(Condition condition)
{
TestConditions();
if(conditionList.ContainsKey(condition) return conditionList[condition];
return false;
}
[System.Serializable]
public struct ConditionStruct
{
public string conditionName;
public bool fulfilled;
}
public object CaptureState()
{
List<ConditionStruct> conditions = new List<ConditionStruct>();
foreach(Condition condition in conditionsToManage)
{
If(conditionList.ContainsKey(condition)
{
ConditionStruct cs = new ConditionStruct();
cs.conditionName = condition.name;
cs.fullfilled=conditionList[condition];
conditions.add(cs);
}
}
return conditions;
}
public void RestoreState(object state)
{
List<ConditionStruct> conditions = (ConditionStruct)state;
conditionList = new Dictionary<Condition, bool>();
foreach(ConditionStruct condition in conditions)
{
Condition c = UnityEngine.Resources.Load<Condition>(condition.conditionName);
if(c!=null)
{
conditionList[c]=condition.fulfilled;
}
}
}
Now, Portal will have a Condition attached… (it’s a ScriptableObject).
Once you’ve gotten the player:
if(!player.GetComponent<ConditionManager>().ConditionSatisfied(condition) return;
As I said, it’s a bit more work, but it is doable.