/// <summary> /// A method that divides the node into four sub-nodes and moves the objects into the proper divisions /// </summary> public void Divide() { //Divides only if there are no divisions already if (divisions == null) { //Creates the new division nodes and sets the parent to this node divisions = new QuadTreeNode[4]; int x = rectangle.X; int y = rectangle.Y; int width = rectangle.Width / 2; int height = rectangle.Height / 2; divisions[0] = new QuadTreeNode(x, y, width, height); divisions[1] = new QuadTreeNode(x + width, y, width, height); divisions[2] = new QuadTreeNode(x, y + height, width, height); divisions[3] = new QuadTreeNode(x + width, y + height, width, height); divisions[0].Parent = this; divisions[1].Parent = this; divisions[2].Parent = this; divisions[3].Parent = this; //Checks which objects fit into the new divisions, and moves them into the new division object lists List <GameObject> objectsToRemove = new List <GameObject>(); for (int c = 0; c < objects.Count; c++) { for (int c2 = 0; c2 < divisions.Length; c2++) { if (divisions[c2].Rectangle.Contains((new Rectangle((int)objects[c].X, (int)objects[c].Y, (int)objects[c].Width, (int)objects[c].Height)))) { divisions[c2].Objects.Add(objects[c]); objectsToRemove.Add(objects[c]); } } } for (int c = 0; c < objectsToRemove.Count; c++) { objects.Remove(objectsToRemove[c]); } } }
//just use the gameObject parameterized constructor public PhysicsObject(int x, int y, int width, int height, QuadTreeNode node) : base(x, y, width, height, node) { }
//WIP #region Fields #endregion #region Properties #endregion #region Constructor /// <summary> /// Creates a generic NPC with X and Y values /// </summary> /// <param name="x">X location of the NPC</param> /// <param name="y">Y location of the NPC</param> /// <param name="node">Reference to the QuadTree</param> public NPC(int x, int y, QuadTreeNode node) // Defaults to a width of 64 and a height of 128 : base(x, y, 64, 128, node) { }
/// <summary> /// Creates a new Debug instance /// </summary> /// <param name="temp">???</param> public Debug(Dictionary <String, Texture2D> temp, GraphicsDevice device) { allTexts = temp; node = new QuadTreeNode(0, 0, device.Viewport.Width, device.Viewport.Height); }
//Imports a world saved in tiles with width and height all generated from a previously created binary file public void Import(Dictionary <string, Texture2D> allTextures, string filePath) { FileStream temp = new FileStream(filePath, FileMode.Open); BinaryReader worldReader = new BinaryReader(temp); width = worldReader.ReadInt32(); height = worldReader.ReadInt32(); QuadTree = new QuadTreeNode(0, 0, width * 64, height * 64); //load tiles tiles = new Tile[width, height]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { String source = worldReader.ReadString(); int index = worldReader.ReadInt32(); int depth = worldReader.ReadInt32(); // Set the texture based on the source Texture2D texture; if (source == "Default") { texture = null; } else { texture = allTextures[source + index]; } Tile t = new Tile(texture, allTextures["Background"], depth); t.Position = new Vector2(i * 64, j * 64); t.Size = new Vector2(64, 64); tiles[i, j] = t; QuadTree.AddObject(t); } } int events = worldReader.ReadInt32(); for (int i = 0; i < events; i++) { int type = worldReader.ReadInt32(); if (type == 0) { //Enemy int x = worldReader.ReadInt32(); int y = worldReader.ReadInt32(); Enemy e = new Enemy(x * 64, y * 64, QuadTree, PatrolType.Standing); e.Texture = allTextures["Enemy"]; AllObjects.Add(e); initialEnemies.Add(e.Clone(e.Texture, e.HP, QuadTree)); QuadTree.AddObject(e); } if (type == 1) { //Warp int x = worldReader.ReadInt32() * 64; int y = worldReader.ReadInt32() * 64; String destination = worldReader.ReadString(); int xOffset = worldReader.ReadInt32() * 64; int yOffset = worldReader.ReadInt32() * 64; Warp w = new Warp(x, y, destination, xOffset, yOffset, QuadTree); AllObjects.Add(w); warps.Add(w); } } //Finds Bounds of the world WorldMinX = float.MaxValue; WorldMinY = float.MaxValue; WorldMaxY = float.MinValue; WorldMaxX = float.MinValue; foreach (Tile t in tiles) { if (t.X < WorldMinX) { WorldMinX = t.X; } if (t.Y < WorldMinY) { WorldMinY = t.Y; } if (t.X + t.Size.X > WorldMaxX) { WorldMaxX = t.X + t.Size.X; } if (t.Y + t.Size.Y > WorldMaxY) { WorldMaxY = t.Y + t.Size.Y; } } }