Re: Auto generate quests - monsters not spawned
Posted: Tue Jul 14, 2020 10:06 am
Here's a method to determine if an EntityType is a monster:
Here's a more complete example with proper error checking for nulls and looping (e.g., monster is parent of wolf, but wolf is parent of monster). If you add the script to a Quest Generator Entity and assign your Monster type, when it generates a world model for quests it will report which entities are monsters.
Code: Select all
public EntityType monsterType; // Assign in inspector.
private bool IsMonster(EntityType entityType)
{
if (entityType == monsterType) return true;
foreach (var parent in entityType.parents)
{
if (IsMonster(parent)) return true;
}
return false;
}
Here's a more complete example with proper error checking for nulls and looping (e.g., monster is parent of wolf, but wolf is parent of monster). If you add the script to a Quest Generator Entity and assign your Monster type, when it generates a world model for quests it will report which entities are monsters.
Code: Select all
using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.QuestMachine;
public class TestGenerator : MonoBehaviour
{
public EntityType monsterType;
private void Start()
{
GetComponent<QuestGeneratorEntity>().updateWorldModel += OnUpdateWorldModel;
}
private void OnUpdateWorldModel(WorldModel worldModel)
{
foreach (var fact in worldModel.facts)
{
if (IsMonster(fact.entityType))
{
Debug.Log("MONSTER: " + fact.count + " " + fact.entityType.name + " in " + fact.domainType.name);
}
}
}
private bool IsMonster(EntityType entityType)
{
var checkedEntities = new List<EntityType>();
return IsMonsterRecursive(entityType, checkedEntities);
}
private bool IsMonsterRecursive(EntityType entityType, List<EntityType> checkedEntities)
{
if (entityType == null || checkedEntities.Contains(entityType)) return false;
checkedEntities.Add(entityType);
if (entityType == monsterType) return true;
foreach (var parent in entityType.parents)
{
if (IsMonsterRecursive(parent, checkedEntities)) return true;
}
return false;
}
}