예제 #1
0
        /// <summary>
        /// Move current floating block to left, right or down.
        /// If anything is blocking, moving is not possible and
        /// nothing gets changed!
        /// </summary>
        /// <returns>Returns true if moving was successful, otherwise false</returns>
        public bool MoveBlock(EMoveTypes moveType)
        {
            // Clear old pos
            for (int x = 0; x < GridWidth; x++)
            {
                for (int y = 0; y < GridHeight; y++)
                {
                    if (mFloatingGrid[x, y])
                    {
                        mGrid[x, y] = EBlockTypes.Empty;
                    }
                }
            }

            // Move stuff to new position
            bool anythingBlocking = false;

            Point[] newPos    = new Point[4];
            int     newPosNum = 0;

            if (moveType == EMoveTypes.Left)
            {
                for (int x = 0; x < GridWidth; x++)
                {
                    for (int y = 0; y < GridHeight; y++)
                    {
                        if (mFloatingGrid[x, y])
                        {
                            if (x - 1 < 0 || mGrid[x - 1, y] != EBlockTypes.Empty)
                            {
                                anythingBlocking = true;
                            }
                            else if (newPosNum < 4)
                            {
                                newPos[newPosNum] = new Point(x - 1, y);
                                newPosNum++;
                            }
                        }
                    }
                }
            }
            else if (moveType == EMoveTypes.Right)
            {
                for (int x = 0; x < GridWidth; x++)
                {
                    for (int y = 0; y < GridHeight; y++)
                    {
                        if (mFloatingGrid[x, y])
                        {
                            if (x + 1 >= GridWidth || mGrid[x + 1, y] != EBlockTypes.Empty)
                            {
                                anythingBlocking = true;
                            }
                            else if (newPosNum < 4)
                            {
                                newPos[newPosNum] = new Point(x + 1, y);
                                newPosNum++;
                            }
                        }
                    }
                }
            }
            else if (moveType == EMoveTypes.Down)
            {
                for (int x = 0; x < GridWidth; x++)
                {
                    for (int y = 0; y < GridHeight; y++)
                    {
                        if (mFloatingGrid[x, y])
                        {
                            if (y + 1 >= GridHeight || mGrid[x, y + 1] != EBlockTypes.Empty)
                            {
                                anythingBlocking = true;
                            }
                            else if (newPosNum < 4)
                            {
                                newPos[newPosNum] = new Point(x, y + 1);
                                newPosNum++;
                            }
                        }
                    }
                }
                if (anythingBlocking == true)
                {
                    MovingDownWasBlocked = true;
                }
            }

            // If anything is blocking restore old state
            // Or we didn't get all 4 new positions?
            if (anythingBlocking || newPosNum != 4)
            {
                for (int x = 0; x < GridWidth; x++)
                {
                    for (int y = 0; y < GridHeight; y++)
                    {
                        if (mFloatingGrid[x, y])
                        {
                            mGrid[x, y] = (EBlockTypes)mCurrentBlockType;
                        }
                    }
                }
                return(false);
            }
            else
            {
                if (moveType == EMoveTypes.Left)
                {
                    mCurrentBlockPos = new Point(mCurrentBlockPos.X - 1, mCurrentBlockPos.Y);
                }
                else if (moveType == EMoveTypes.Right)
                {
                    mCurrentBlockPos = new Point(mCurrentBlockPos.X + 1, mCurrentBlockPos.Y);
                }
                else if (moveType == EMoveTypes.Down)
                {
                    mCurrentBlockPos = new Point(mCurrentBlockPos.X, mCurrentBlockPos.Y + 1);
                }

                // Else we can move to the new position, lets do it!
                for (int x = 0; x < GridWidth; x++)
                {
                    for (int y = 0; y < GridHeight; y++)
                    {
                        mFloatingGrid[x, y] = false;
                    }
                }
                for (int i = 0; i < 4; i++)
                {
                    mGrid[newPos[i].X, newPos[i].Y]         = (EBlockTypes)mCurrentBlockType;
                    mFloatingGrid[newPos[i].X, newPos[i].Y] = true;
                }

                Sound.Play(ESoundTypes.BlockMove);

                return(true);
            }
        }
예제 #2
0
		/// <summary>
		/// Move current floating block to left, right or down.
		/// If anything is blocking, moving is not possible and
		/// nothing gets changed!
		/// </summary>
		/// <returns>Returns true if moving was successful, otherwise false</returns>
		public bool MoveBlock(EMoveTypes moveType) {
			// Clear old pos
			for (int x = 0; x < GridWidth; x++) {
				for (int y = 0; y < GridHeight; y++) {
					if (mFloatingGrid[x, y]) {
						mGrid[x, y] = EBlockTypes.Empty;
					}
				}
			}

			// Move stuff to new position
			bool anythingBlocking = false;
			Point[] newPos = new Point[4];
			int newPosNum = 0;
			if (moveType == EMoveTypes.Left) {
				for (int x = 0; x < GridWidth; x++) {
					for (int y = 0; y < GridHeight; y++) {
						if (mFloatingGrid[x, y]) {
							if (x - 1 < 0 || mGrid[x - 1, y] != EBlockTypes.Empty) {
								anythingBlocking = true;
							} else if (newPosNum < 4) {
								newPos[newPosNum] = new Point(x - 1, y);
								newPosNum++;
							}
						}
					}
				}
			} else if (moveType == EMoveTypes.Right) {
				for (int x = 0; x < GridWidth; x++) {
					for (int y = 0; y < GridHeight; y++) {
						if (mFloatingGrid[x, y]) {
							if (x + 1 >= GridWidth || mGrid[x + 1, y] != EBlockTypes.Empty) {
								anythingBlocking = true;
							} else if (newPosNum < 4) {
								newPos[newPosNum] = new Point(x + 1, y);
								newPosNum++;
							}
						}
					}
				}
			} else if (moveType == EMoveTypes.Down) {
				for (int x = 0; x < GridWidth; x++) {
					for (int y = 0; y < GridHeight; y++) {
						if (mFloatingGrid[x, y]) {
							if (y + 1 >= GridHeight || mGrid[x, y + 1] != EBlockTypes.Empty) {
								anythingBlocking = true;
							} else if (newPosNum < 4) {
								newPos[newPosNum] = new Point(x, y + 1);
								newPosNum++;
							}
						}
					}
				}
				if (anythingBlocking == true) {
					MovingDownWasBlocked = true;
				}
			}

			// If anything is blocking restore old state
			// Or we didn't get all 4 new positions?
			if (anythingBlocking || newPosNum != 4) {
				for (int x = 0; x < GridWidth; x++) {
					for (int y = 0; y < GridHeight; y++) {
						if (mFloatingGrid[x, y]) {
							mGrid[x, y] = (EBlockTypes)mCurrentBlockType;
						}
					}
				}
				return false;
			} else {
				if (moveType == EMoveTypes.Left) {
					mCurrentBlockPos = new Point(mCurrentBlockPos.X - 1, mCurrentBlockPos.Y);
				} else if (moveType == EMoveTypes.Right) {
					mCurrentBlockPos = new Point(mCurrentBlockPos.X + 1, mCurrentBlockPos.Y);
				} else if (moveType == EMoveTypes.Down) {
					mCurrentBlockPos = new Point(mCurrentBlockPos.X, mCurrentBlockPos.Y + 1);
				}

				// Else we can move to the new position, lets do it!
				for (int x = 0; x < GridWidth; x++) {
					for (int y = 0; y < GridHeight; y++) {
						mFloatingGrid[x, y] = false;
					}
				}
				for (int i = 0; i < 4; i++) {
					mGrid[newPos[i].X, newPos[i].Y] = (EBlockTypes)mCurrentBlockType;
					mFloatingGrid[newPos[i].X, newPos[i].Y] = true;
				}

				Sound.Play(ESoundTypes.BlockMove);

				return true;
			}
		}