//$ Copyright 2015-22, Code Respawn Technologies Pvt Ltd - All Rights Reserved $//
using System;
using System.Linq;
using UnityEngine;
using System.Collections.Generic;
namespace DungeonArchitect.Graphs
{
///
/// An ID provider for graph objects
///
[Serializable]
public class IndexCounter
{
[SerializeField]
int index = 0;
public int GetNext()
{
index++;
return index;
}
}
///
/// Theme Graph data structure holds all the theme nodes and their connections
///
[Serializable]
public class Graph : ScriptableObject
{
[SerializeField]
IndexCounter indexCounter;
public DungeonArchitect.Graphs.IndexCounter IndexCounter
{
get { return indexCounter; }
}
[SerializeField]
IndexCounter topZIndex;
[SerializeField]
List nodes;
///
/// List of graph nodes
///
public List Nodes
{
get
{
return nodes;
}
}
[SerializeField]
List links;
///
/// List of graph links connecting the nodes
///
public List Links
{
get
{
return links;
}
}
///
/// The z index of the top most node
///
public IndexCounter TopZIndex
{
get
{
return topZIndex;
}
}
public virtual void OnEnable()
{
//hideFlags = HideFlags.HideAndDontSave;
if (IndexCounter == null)
{
indexCounter = new IndexCounter();
}
if (topZIndex == null)
{
topZIndex = new IndexCounter();
}
if (nodes == null)
{
nodes = new List();
}
if (links == null)
{
links = new List();
}
// Remove any null nodes
for (int i = 0; i < nodes.Count; )
{
if (nodes[i] == null)
{
nodes.RemoveAt(i);
}
else
{
i++;
}
}
}
///
/// Gets the node by it's id
///
/// The ID of the node
/// The retrieved node. null if node with this id doesn't exist
public GraphNode GetNode(string id)
{
var result = from node in Nodes
where node.Id == id
select node;
return (result.Count() > 0) ? result.Single() : null;
}
///
/// Get all nodes of the specified type
///
/// The type of nodes to retrieve. Should be a subclass of GraphNode
/// List of all the nodes of the specified type
public T[] GetNodes() where T : GraphNode
{
var targetNodes = new List();
foreach (var node in nodes)
{
if (node is T)
{
targetNodes.Add(node as T);
}
}
return targetNodes.ToArray();
}
}
}