How to do it...

We will need to create two different classes: one for handling the nodes, and one for handling the whole tree and high-level operations.

Let's start with the node file:

  1. Create a new class named DungeonNode2D:
using UnityEngine;using System.Collections.Generic;[System.Serializable]public class DungeonNode2D{  // next steps }
  1. Define its member variables:
public Rect area;public Rect block;public Dungeon2D root;public DungeonNode2D left;public DungeonNode2D right;protected int depth;
  1. Implement its constructor for initialization:
public DungeonNode2D (Rect area, Dungeon2D root, int depth = 0){this.area = area;this.root = root;this.depth = depth;this.root.leaves.Add(this);if (!this.root.tree.ContainsKey(depth)) this.root.tree.Add(depth, ...

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.