static private List <PathNode> findAdjacentNodes( PathNode currentNode, PathNode endNode) { List <PathNode> adjacentNodes = new List <PathNode>(); int X = currentNode.GridX; int Y = currentNode.GridY; bool upLeft = true; bool upRight = true; bool downLeft = true; bool downRight = true; if ((X > 0) && (!TileMap.IsWallTile(X - 1, Y))) { adjacentNodes.Add(new PathNode( currentNode, endNode, new Vector2(X - 1, Y), CostStraight + currentNode.DirectCost)); } else { upLeft = false; downLeft = false; } if ((X < 49) && (!TileMap.IsWallTile(X + 1, Y))) { adjacentNodes.Add(new PathNode( currentNode, endNode, new Vector2(X + 1, Y), CostStraight + currentNode.DirectCost)); } else { upRight = false; downRight = false; } if ((Y > 0) && (!TileMap.IsWallTile(X, Y - 1))) { adjacentNodes.Add(new PathNode( currentNode, endNode, new Vector2(X, Y - 1), CostStraight + currentNode.DirectCost)); } else { upLeft = false; upRight = false; } if ((Y < 49) && (!TileMap.IsWallTile(X, Y + 1))) { adjacentNodes.Add(new PathNode( currentNode, endNode, new Vector2(X, Y + 1), CostStraight + currentNode.DirectCost)); } else { downLeft = false; downRight = false; } if ((upLeft) && (!TileMap.IsWallTile(X - 1, Y - 1))) { adjacentNodes.Add(new PathNode( currentNode, endNode, new Vector2(X - 1, Y - 1), CostDiagonal + currentNode.DirectCost)); } if ((upRight) && (!TileMap.IsWallTile(X + 1, Y - 1))) { adjacentNodes.Add(new PathNode( currentNode, endNode, new Vector2(X + 1, Y - 1), CostDiagonal + currentNode.DirectCost)); } if ((downLeft) && (!TileMap.IsWallTile(X - 1, Y + 1))) { adjacentNodes.Add(new PathNode( currentNode, endNode, new Vector2(X - 1, Y + 1), CostDiagonal + currentNode.DirectCost)); } if ((downRight) && (!TileMap.IsWallTile(X + 1, Y + 1))) { adjacentNodes.Add(new PathNode( currentNode, endNode, new Vector2(X + 1, Y + 1), CostDiagonal + currentNode.DirectCost)); } return(adjacentNodes); }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); if (gameState == GameStates.TitleScreen) { spriteBatch.Draw( titleScreen, new Rectangle(0, 0, 800, 600), Color.White); } if ((gameState == GameStates.Playing) || (gameState == GameStates.WaveComplete) || (gameState == GameStates.GameOver)) { TileMap.Draw(spriteBatch); WeaponManager.Draw(spriteBatch); Player.Draw(spriteBatch); EnemyManager.Draw(spriteBatch); EffectsManager.Draw(spriteBatch); GoalManager.Draw(spriteBatch); checkPlayerDeath(); spriteBatch.DrawString( pericles14, "Score: " + GameManager.Score.ToString(), new Vector2(30, 5), Color.White); spriteBatch.DrawString( pericles14, "Terminals Remaining: " + GoalManager.ActiveTerminals, new Vector2(520, 5), Color.White); } if (gameState == GameStates.WaveComplete) { spriteBatch.DrawString( pericles14, "Beginning Wave " + (GameManager.CurrentWave + 1).ToString(), new Vector2(300, 300), Color.White); } if (gameState == GameStates.GameOver) { spriteBatch.DrawString( pericles14, "G A M E O V E R!", new Vector2(300, 300), Color.White); } spriteBatch.End(); base.Draw(gameTime); }
static public List <Vector2> FindPath( Vector2 startTile, Vector2 endTile) { if (TileMap.IsWallTile(endTile) || TileMap.IsWallTile(startTile)) { return(null); } openList.Clear(); nodeCosts.Clear(); nodeStatus.Clear(); PathNode startNode; PathNode endNode; endNode = new PathNode(null, null, endTile, 0); startNode = new PathNode(null, endNode, startTile, 0); addNodeToOpenList(startNode); while (openList.Count > 0) { PathNode currentNode = openList[openList.Count - 1]; if (currentNode.IsEqualToNode(endNode)) { List <Vector2> bestPath = new List <Vector2>(); while (currentNode != null) { bestPath.Insert(0, currentNode.GridLocation); currentNode = currentNode.ParentNode; } return(bestPath); } openList.Remove(currentNode); nodeCosts.Remove(currentNode.GridLocation); foreach ( PathNode possibleNode in findAdjacentNodes(currentNode, endNode)) { if (nodeStatus.ContainsKey(possibleNode.GridLocation)) { if (nodeStatus[possibleNode.GridLocation] == NodeStatus.Closed) { continue; } if ( nodeStatus[possibleNode.GridLocation] == NodeStatus.Open) { if (possibleNode.TotalCost >= nodeCosts[possibleNode.GridLocation]) { continue; } } } addNodeToOpenList(possibleNode); } nodeStatus[currentNode.GridLocation] = NodeStatus.Closed; } return(null); }
static private List <PathNode> FindAdjacentNodes(PathNode currentNode, PathNode endNode) { List <PathNode> adjacentNodes = new List <PathNode>(); int x = currentNode.GridX; int y = currentNode.GridY; bool upLeft = true; bool upRight = true; bool downLeft = true; bool downRight = true; if ((x > 0) && (!TileMap.IsWallTile(x - 1, y))) { adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(x - 1, y), CostStraight + currentNode.DirectCost)); } else { upLeft = false; downLeft = false; } if ((x < 49) && (!TileMap.IsWallTile(x + 1, y))) { adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(x + 1, y), CostStraight + currentNode.DirectCost)); } else { upRight = false; downRight = false; } if ((y > 0) && (!TileMap.IsWallTile(x, y - 1))) { adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(x, y - 1), CostStraight + currentNode.DirectCost)); } else { upLeft = false; upRight = false; } if ((y < 49) && (!TileMap.IsWallTile(x, y + 1))) { adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(x, y + 1), CostStraight + currentNode.DirectCost)); } else { downLeft = false; downRight = false; } if ((upLeft) && (!TileMap.IsWallTile(x - 1, y - 1))) { adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(x - 1, y - 1), CostDiagonal + currentNode.DirectCost)); } if ((upRight) && (!TileMap.IsWallTile(x + 1, y - 1))) { adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(x + 1, y - 1), CostDiagonal + currentNode.DirectCost)); } if ((downLeft) && (!TileMap.IsWallTile(x - 1, y + 1))) { adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(x - 1, y + 1), CostDiagonal + currentNode.DirectCost)); } if ((downLeft) && (!TileMap.IsWallTile(x + 1, y + 1))) { adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(x + 1, y + 1), CostDiagonal + currentNode.TotalCost)); } return(adjacentNodes); }
private static Vector2 checkTileObstacles( float elapsedTime, Vector2 moveAngle) { Vector2 newHorizontalLocation = BaseSprite.WorldLocation + (new Vector2(moveAngle.X, 0) * (playerSpeed * elapsedTime)); Vector2 newVerticalLocation = BaseSprite.WorldLocation + (new Vector2(0, moveAngle.Y) * (playerSpeed * elapsedTime)); Rectangle newHorizontalRect = new Rectangle( (int)newHorizontalLocation.X, (int)BaseSprite.WorldLocation.Y, BaseSprite.FrameWidth, BaseSprite.FrameHeight); Rectangle newVerticalRect = new Rectangle( (int)BaseSprite.WorldLocation.X, (int)newVerticalLocation.Y, BaseSprite.FrameWidth, BaseSprite.FrameHeight); int horizLeftPixel = 0; int horizRightPixel = 0; int vertTopPixel = 0; int vertBottomPixel = 0; if (moveAngle.X < 0) { horizLeftPixel = (int)newHorizontalRect.Left; horizRightPixel = (int)BaseSprite.WorldRectangle.Left; } if (moveAngle.X > 0) { horizLeftPixel = (int)BaseSprite.WorldRectangle.Right; horizRightPixel = (int)newHorizontalRect.Right; } if (moveAngle.Y < 0) { vertTopPixel = (int)newVerticalRect.Top; vertBottomPixel = (int)BaseSprite.WorldRectangle.Top; } if (moveAngle.Y > 0) { vertTopPixel = (int)BaseSprite.WorldRectangle.Bottom; vertBottomPixel = (int)newVerticalRect.Bottom; } if (moveAngle.X != 0) { for (int x = horizLeftPixel; x < horizRightPixel; x++) { for (int y = 0; y < BaseSprite.FrameHeight; y++) { if (TileMap.IsWallTileByPixel( new Vector2(x, newHorizontalLocation.Y + y))) { moveAngle.X = 0; break; } } if (moveAngle.X == 0) { break; } } } if (moveAngle.Y != 0) { for (int y = vertTopPixel; y < vertBottomPixel; y++) { for (int x = 0; x < BaseSprite.FrameWidth; x++) { if (TileMap.IsWallTileByPixel( new Vector2(newVerticalLocation.X + x, y))) { moveAngle.Y = 0; break; } } if (moveAngle.Y == 0) { break; } } } return(moveAngle); }
public static void AddComputerTerminal() { int startX = rand.Next(2, TileMap.MapWidth - 2); int startY = rand.Next(0, TileMap.MapHeight - 2); Vector2 tileLocation = new Vector2(startX, startY); if ((TerminalInSquare(tileLocation) != null) || (TileMap.IsWallTile(tileLocation))) { return; } if (Vector2.Distance( TileMap.GetSquareCenter(startX, startY), Player.BaseSprite.WorldCenter) < minDistanceFromPlayer) { return; } List <Vector2> path = PathFinder.FindPath( new Vector2(startX, startY), TileMap.GetSquareAtPixel( Player.BaseSprite.WorldCenter)); if (path != null) { Rectangle squareRect = TileMap.SquareWorldRectangle(startX, startY); Sprite activeSprite = new Sprite( new Vector2(squareRect.X, squareRect.Y), texture, initialActiveFrame, Vector2.Zero); for (int x = 1; x < 3; x++) { activeSprite.AddFrame( new Rectangle( initialActiveFrame.X + (x * initialActiveFrame.Width), initialActiveFrame.Y, initialActiveFrame.Width, initialActiveFrame.Height)); } activeSprite.CollisionRadius = 15; Sprite disabledSprite = new Sprite( new Vector2(squareRect.X, squareRect.Y), texture, initialDisabledFrame, Vector2.Zero); ComputerTerminal terminal = new ComputerTerminal( activeSprite, disabledSprite, new Vector2(startX, startY)); float timerOffset = (float)rand.Next(1, 100); terminal.LastSpawnCounter = timerOffset / 100f; computerTerminals.Add(terminal); activeCount++; } }
private Vector2 getNewTargetSquare() { List <Vector2> path = PathFinder.FindPath(TileMap.GetSquareAtPixel(EnemyBase.WorldCenter), TileMap.GetSquareAtPixel(Player.BaseSprite.WorldCenter)); if (path.Count > 1) { return(new Vector2(path[1].X, path[1].Y)); } else { return(TileMap.GetSquareAtPixel(Player.BaseSprite.WorldCenter)); } }
private bool reachedTargetSquare() { return(Vector2.Distance(EnemyBase.WorldCenter, TileMap.GetSquareCenter((int)currentTargetSquare.X, (int)currentTargetSquare.Y)) <= 2); }