public PathFinder(Map map) { levelWidth = map.width; levelHeight = map.height; InitializeSearchNodes(map); }
private void InitializeSearchNodes(Map map) { searchNodes = new SearchNode[levelWidth, levelHeight]; for (int x = 0; x < levelWidth; ++x) { for (int y = 0; y < levelHeight; ++y) { SearchNode node = new SearchNode(); node.Position = new Point(x, y); node.Walkable = map.map[x, y].walkable; if (node.Walkable) { node.Neighbors = new SearchNode[8]; searchNodes[x, y] = node; } } } for (int x = 0; x < levelWidth; x++) { for (int y = 0; y < levelHeight; y++) { SearchNode node = searchNodes[x, y]; if (node == null || node.Walkable == false) { continue; } Point[] neighbors = new Point[] { new Point (x, y - 1), new Point (x, y + 1), new Point (x - 1, y), new Point (x + 1, y) }; for (int i = 0; i < neighbors.Length; i++) { Point position = neighbors[i]; if (position.X < 0 || position.X > levelWidth - 1 || position.Y < 0 || position.Y > levelHeight - 1) continue; SearchNode neighbor = searchNodes[position.X, position.Y]; if (neighbor == null || neighbor.Walkable == false) continue; node.Neighbors[i] = neighbor; } } } }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); sharedContent = Content; grid = new Grid(GraphicsDevice, Color.Black); greenSquare = Content.Load<Texture2D>(@"GreenSquare"); // TODO: use this.Content to load your game content here enemy = new Enemy(@"whiteSquare"); enemy.Position = new Vector2(48 * 3.0f, 48 * 3.0f); enemy.Speed = 550f; map = new Map(26, 14); map.squareTexture = Content.Load<Texture2D>(@"GrassTile1"); PathFinder.map = map; PathFinder.RebuildMap(); enemyManager = new EnemyManager(); tower = new Tower("Turret", Vector2.Zero); }