/// <summary> /// Creates a generic GameObject with the provided Rectangle dimensions /// </summary> /// <param name="x">X dimension of the draw box</param> /// <param name="y">Y dimension of the draw box</param> /// <param name="width">Width of the draw box</param> /// <param name="height">Height of the draw box</param> public GameObject(int x, int y, int width, int height, QuadTreeNode node) { Position = new Vector2(x, y); Size = new Vector2(width, height); this.node = node; node.AddObject(this); }
//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; } } }