Esempio n. 1
0
		protected override void Update(GameTime gameTime) {
			mOldMouseState = mNewMouseState;
			mOldKeyboardState = mNewKeyboardState;
			mNewMouseState = Mouse.GetState();
			mNewKeyboardState = Keyboard.GetState();

			elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

			mOldMousePos = mThisMousePos;

			mThisMousePos.X = mNewMouseState.X / mTileWidth;
			mThisMousePos.Y = mNewMouseState.Y / mTileWidth;

			int mouseDelta = (mOldMouseState.ScrollWheelValue - mNewMouseState.ScrollWheelValue) / 100;
			if (mouseDelta < 0 || (mouseDelta > 0 && mTileWidth > 1)) {
				mTileWidth = Math.Max(mTileWidth - mouseDelta, 1);
				MovableObject.Initalize(mGridWidth, mTileWidth);

				mGlobalScale = (float)mTileWidth / (float)mObjectTex.Width;
				mMovableObject = new MovableObject(new Vector2(16 * mGlobalScale, 16 * mGlobalScale));
			}

			if (WasPressed(Keys.Escape)) {
				this.Exit();
			}
			if (WasPressed(Keys.F1)) {
				graphics.ToggleFullScreen();
			}
			if (WasPressed(Keys.C)) {
				this.Initialize();
			}


			if (WasPressed(Keys.D)) {
				if (mDrawType != DrawType.DrawWay)
					mDrawType = DrawType.DrawWay;
				else
					mDrawType = DrawType.DrawWall;
			}

			if (WasPressed(EMouseButton.RightButton) == true) {
				Click = new Vector2(mNewMouseState.X, mNewMouseState.Y);

				mStartPoint = new Point((int)(mMovableObject.PositionCurrent.X / mTileWidth), (int)(mMovableObject.PositionCurrent.Y / mTileWidth));
				mEndPoint = new Point(mThisMousePos.X, mThisMousePos.Y);

				mWatch = Stopwatch.StartNew();
				if (mStartPoint == mEndPoint) {
					mMovableObject.LinearMove(mMovableObject.PositionCurrent, Click);
				} else {
					Stopwatch timeTest = Stopwatch.StartNew();
					foundPath = mPathFinder.FindPath(mStartPoint, mEndPoint);
					timeTest.Stop();
					Debug.Write("FindPath need " + timeTest.ElapsedMilliseconds + "ms");
					if (foundPath != null) {
						Debug.WriteLine(" [" + foundPath.Count + " steps]");
						mMovableObject.PathMove(ref foundPath, mMovableObject.PositionCurrent, Click);
					} else {
						Debug.WriteLine(" [FAILED TO FIND A WAY]");
					}
				}
			}

			//Wall drawing / removing
			if (mNewMouseState.LeftButton == ButtonState.Pressed) {
				mThisMousePos.X = mNewMouseState.X / mTileWidth;
				mThisMousePos.Y = mNewMouseState.Y / mTileWidth;

				if ((mThisMousePos.X < mGridWidth) && (mThisMousePos.Y < mGridHeight) && (mThisMousePos.X >= 0) && (mThisMousePos.Y >= 0)) {
					switch (mDrawType) {
						case DrawType.DrawWall:
							PathGrid[mThisMousePos.X, mThisMousePos.Y] = 0;
							DrawGrid[mThisMousePos.X, mThisMousePos.Y] = 0;
							break;
						case DrawType.DrawWay:
							PathGrid[mThisMousePos.X, mThisMousePos.Y] = 1;
							DrawGrid[mThisMousePos.X, mThisMousePos.Y] = 1;
							break;
					}
				}
			}

			mMovableObject.Update(elapsedTime);

			ushort GridX = (ushort)(mPathFinder.PathGrid.GetUpperBound(0) + 1);
			ushort GridY = (ushort)(mPathFinder.PathGrid.GetUpperBound(1) + 1);

			if (mMovableObject.PositionChanged == false) {
				if (mWatch.IsRunning == true)
					mWatch.Stop();

				mClearWalkPath += elapsedTime;
				if (mClearWalkPath >= 3f) {
					mClearWalkPath = 0f;
					mPathFinder.WalkedNodes = new byte[GridX, GridY];
				}
			} else {
				mClearWalkPath = 0f;
				if (((int)mMovableObject.PositionCurrent.X / mTileWidth) < GridX && ((int)mMovableObject.PositionCurrent.Y / mTileWidth) < GridY)
					mPathFinder.WalkedNodes[(int)mMovableObject.PositionCurrent.X / mTileWidth, (int)mMovableObject.PositionCurrent.Y / mTileWidth] = 1;
			}

			base.Update(gameTime);


		}
Esempio n. 2
0
		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();

		}
Esempio n. 3
0
        protected override void Update(GameTime gameTime)
        {
            mOldMouseState    = mNewMouseState;
            mOldKeyboardState = mNewKeyboardState;
            mNewMouseState    = Mouse.GetState();
            mNewKeyboardState = Keyboard.GetState();

            elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            mOldMousePos = mThisMousePos;

            mThisMousePos.X = mNewMouseState.X / mTileWidth;
            mThisMousePos.Y = mNewMouseState.Y / mTileWidth;

            int mouseDelta = (mOldMouseState.ScrollWheelValue - mNewMouseState.ScrollWheelValue) / 100;

            if (mouseDelta < 0 || (mouseDelta > 0 && mTileWidth > 1))
            {
                mTileWidth = Math.Max(mTileWidth - mouseDelta, 1);
                MovableObject.Initalize(mGridWidth, mTileWidth);

                mGlobalScale   = (float)mTileWidth / (float)mObjectTex.Width;
                mMovableObject = new MovableObject(new Vector2(16 * mGlobalScale, 16 * mGlobalScale));
            }

            if (WasPressed(Keys.Escape))
            {
                this.Exit();
            }
            if (WasPressed(Keys.F1))
            {
                graphics.ToggleFullScreen();
            }
            if (WasPressed(Keys.C))
            {
                this.Initialize();
            }


            if (WasPressed(Keys.D))
            {
                if (mDrawType != DrawType.DrawWay)
                {
                    mDrawType = DrawType.DrawWay;
                }
                else
                {
                    mDrawType = DrawType.DrawWall;
                }
            }

            if (WasPressed(EMouseButton.RightButton) == true)
            {
                Click = new Vector2(mNewMouseState.X, mNewMouseState.Y);

                mStartPoint = new Point((int)(mMovableObject.PositionCurrent.X / mTileWidth), (int)(mMovableObject.PositionCurrent.Y / mTileWidth));
                mEndPoint   = new Point(mThisMousePos.X, mThisMousePos.Y);

                mWatch = Stopwatch.StartNew();
                if (mStartPoint == mEndPoint)
                {
                    mMovableObject.LinearMove(mMovableObject.PositionCurrent, Click);
                }
                else
                {
                    Stopwatch timeTest = Stopwatch.StartNew();
                    foundPath = mPathFinder.FindPath(mStartPoint, mEndPoint);
                    timeTest.Stop();
                    Debug.Write("FindPath need " + timeTest.ElapsedMilliseconds + "ms");
                    if (foundPath != null)
                    {
                        Debug.WriteLine(" [" + foundPath.Count + " steps]");
                        mMovableObject.PathMove(ref foundPath, mMovableObject.PositionCurrent, Click);
                    }
                    else
                    {
                        Debug.WriteLine(" [FAILED TO FIND A WAY]");
                    }
                }
            }

            //Wall drawing / removing
            if (mNewMouseState.LeftButton == ButtonState.Pressed)
            {
                mThisMousePos.X = mNewMouseState.X / mTileWidth;
                mThisMousePos.Y = mNewMouseState.Y / mTileWidth;

                if ((mThisMousePos.X < mGridWidth) && (mThisMousePos.Y < mGridHeight) && (mThisMousePos.X >= 0) && (mThisMousePos.Y >= 0))
                {
                    switch (mDrawType)
                    {
                    case DrawType.DrawWall:
                        PathGrid[mThisMousePos.X, mThisMousePos.Y] = 0;
                        DrawGrid[mThisMousePos.X, mThisMousePos.Y] = 0;
                        break;

                    case DrawType.DrawWay:
                        PathGrid[mThisMousePos.X, mThisMousePos.Y] = 1;
                        DrawGrid[mThisMousePos.X, mThisMousePos.Y] = 1;
                        break;
                    }
                }
            }

            mMovableObject.Update(elapsedTime);

            ushort GridX = (ushort)(mPathFinder.PathGrid.GetUpperBound(0) + 1);
            ushort GridY = (ushort)(mPathFinder.PathGrid.GetUpperBound(1) + 1);

            if (mMovableObject.PositionChanged == false)
            {
                if (mWatch.IsRunning == true)
                {
                    mWatch.Stop();
                }

                mClearWalkPath += elapsedTime;
                if (mClearWalkPath >= 3f)
                {
                    mClearWalkPath          = 0f;
                    mPathFinder.WalkedNodes = new byte[GridX, GridY];
                }
            }
            else
            {
                mClearWalkPath = 0f;
                if (((int)mMovableObject.PositionCurrent.X / mTileWidth) < GridX && ((int)mMovableObject.PositionCurrent.Y / mTileWidth) < GridY)
                {
                    mPathFinder.WalkedNodes[(int)mMovableObject.PositionCurrent.X / mTileWidth, (int)mMovableObject.PositionCurrent.Y / mTileWidth] = 1;
                }
            }

            base.Update(gameTime);
        }