public Pathfinding() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; #if WINDOWS_PHONE // Frame rate is 30 fps by default for Windows Phone. TargetElapsedTime = TimeSpan.FromTicks(333333); graphics.IsFullScreen = true; #endif map = new Map(); tank = new Tank(); pathFinder = new PathFinder(); }
protected override void Initialize() { mGridWidth = 50; mGridHeight = 50; PathGrid = new byte[mGridWidth, mGridHeight]; DrawGrid = new byte[mGridWidth, mGridHeight]; for (int y = 0; y < mGridHeight; y += 1) { for (int x = 0; x < mGridWidth; x += 1) { PathGrid[x, y] = 1; DrawGrid[x, y] = 1; } } mPathFinder = new PathFinder(PathGrid); MovableObject.Initalize(mGridWidth, mTileWidth); mMovableObject = new MovableObject(new Vector2(16 * mGlobalScale, 16 * mGlobalScale)); base.Initialize(); }
protected override void Update(GameTime gameTime) { // Allows the game to exit if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); KeyboardState kState = Keyboard.GetState(); MouseState mState = Mouse.GetState(); Vector2 mPos = new Vector2(mState.X, mState.Y); if(CurrentState != SimState.STARTED) { if(GridDrawArea.Contains((int)mPos.X, (int)mPos.Y)) { if(mState.LeftButton == ButtonState.Pressed) { TileGrid.LeftClick(mPos, GridDrawArea); } else if(mState.RightButton == ButtonState.Pressed) { TileGrid.RightClick(mPos, GridDrawArea); } } } else { if(Keyboard.GetState().IsKeyDown(Keys.Enter)) { PathFinder.RunTillDone(); } PathFinder.DoStep(); } if(CurrentState == SimState.MENU_DEPTHFIRST_OPTIONS) { if(kState.IsKeyDown(Keys.F1) && LastKeyState.IsKeyUp(Keys.F1)) { ((DepthFirst)(PathFinder)).SetNeighbourOrder(NeighbourOrder.STANDARD); CurrentState = SimState.STARTED; } else if(kState.IsKeyDown(Keys.F2) && LastKeyState.IsKeyUp(Keys.F2)) { ((DepthFirst)(PathFinder)).SetNeighbourOrder(NeighbourOrder.RANDOM); CurrentState = SimState.STARTED; } else if(kState.IsKeyDown(Keys.F3) && LastKeyState.IsKeyUp(Keys.F3)) { ((DepthFirst)(PathFinder)).SetNeighbourOrder(NeighbourOrder.SMART); CurrentState = SimState.STARTED; } } if(CurrentState == SimState.MENU_ASTAR_OPTIONS) { if(kState.IsKeyDown(Keys.F1) && LastKeyState.IsKeyUp(Keys.F1)) { AStarTile.HScoreMultiplier = 1; CurrentState = SimState.STARTED; } else if(kState.IsKeyDown(Keys.F2) && LastKeyState.IsKeyUp(Keys.F2)) { AStarTile.HScoreMultiplier = 2; CurrentState = SimState.STARTED; } } if(CurrentState == SimState.MENU_ALGORITHM_SELECT) { if(kState.IsKeyDown(Keys.F1) && LastKeyState.IsKeyUp(Keys.F1)) { PathFinder = new DepthFirst(TileGrid); CurrentState = SimState.MENU_DEPTHFIRST_OPTIONS; } else if(kState.IsKeyDown(Keys.F2) && LastKeyState.IsKeyUp(Keys.F2)) { PathFinder = new AStar(TileGrid); CurrentState = SimState.MENU_ASTAR_OPTIONS; } else if(kState.IsKeyDown(Keys.F3) && LastKeyState.IsKeyUp(Keys.F3)) { TileGrid.GenRandomGrid(25); } } LastKeyState = kState; base.Update(gameTime); }
public int GetLengthToDestination(Point startNode, Point endNode, HackGameBoard board) { PathFinder pf = new PathFinder(); pf.Initialize(board); pf.Reset(startNode, endNode); bool success = pf.SearchPath(); if (success == true) { return pf.FinalPath().Count; } else { return -1; } }