Ejemplo n.º 1
0
 private void HandleInput(IBlockLiner gamestate)
 {
     try
     {
         KeyboardState kbs = Keyboard.GetState();
         if (kbs.IsKeyDown(Keys.Left))
         {
             Move(Direction.Left, gamestate);
         }
         if (kbs.IsKeyDown(Keys.Right))
         {
             Move(Direction.Right, gamestate);
         }
         if (kbs.IsKeyDown(Keys.Down))
         {
             Move(Direction.Down, gamestate);
         }
         if (kbs.IsKeyDown(Keys.Up))
         {
             Rotate(gamestate);
         }
     }
     catch (UnplacableBlockException)
     {
     }
 }
Ejemplo n.º 2
0
 private BlockLinerState FallingProcess(IBlockLiner gamestate)
 {
     try
     {
         Move(Direction.Down, gamestate);
     }
     catch (UnplacableBlockException)
     {
         return(gamestate.GetStateInstance(Type.Checking));
     }
     return(this);
 }
Ejemplo n.º 3
0
        protected override void Initialize()
        {
            // gamelogic initialization
            _gamelogic = new BlockLinerLogic(BoardWidth, BoardHeight, this);
            //_gamelogic = new TestingBlockLiner((int)BoardWidth, (int)BoardHeight);

            // graphics initialization
            _renderer  = new MonoRenderer(GraphicsDevice, _graphics, BoardWidth, BoardHeight);
            _rendering = new RenderingSystem(_renderer, _gamelogic);

            base.Initialize();
        }
Ejemplo n.º 4
0
        public override BlockLinerState Update(IBlockLiner gamestate, GameTime delta)
        {
            KeyboardState keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.Enter))
            {
                System.Diagnostics.Debug.WriteLine("Enter key pressed : starting new game");
                return(gamestate.GetStateInstance(Type.NewBlock));
            }
            else
            {
                return(this);
            }
        }
Ejemplo n.º 5
0
        public override BlockLinerState Update(IBlockLiner gamestate, GameTime delta)
        {
            KeyboardState kb = Keyboard.GetState();

            if (kb.IsKeyDown(Keys.Y))
            {
                gamestate.Reset();
                return(gamestate.GetStateInstance(Type.Init));
            }
            if (kb.IsKeyDown(Keys.N))
            {
                gamestate.Exit();
            }
            return(this);
        }
Ejemplo n.º 6
0
        private static List <Block> GetFallingBlocks(IBlockLiner gamestate)
        {
            List <Block> fallingBlocks = new List <Block>();

            int xLength = gamestate.GameArea.GetLength(0);
            int yLength = gamestate.GameArea.GetLength(1);

            foreach (Block b in gamestate.GameArea)
            {
                if (b != null && b.Falling)
                {
                    fallingBlocks.Add(b);
                }
            }

            return(fallingBlocks);
        }
Ejemplo n.º 7
0
        public override BlockLinerState Update(IBlockLiner gamestate, GameTime delta)
        {
            // select tetrablock
            TetraBlock next = gamestate.PopNextTetraBlock();

            Block[,] gameArea = gamestate.GameArea;

            try
            {
                // add new tetra to gameArea
                AddTetra(next, gameArea);
            }
            catch (UnplacableBlockException)
            {
                return(gamestate.GetStateInstance(Type.GameOver));
            }
            return(gamestate.GetStateInstance(Type.Falling));
        }
Ejemplo n.º 8
0
        public override BlockLinerState Update(IBlockLiner gamestate, GameTime delta)
        {
            _fallingTime      += delta.ElapsedGameTime.TotalSeconds;
            _inputElapsedTime += delta.ElapsedGameTime.TotalSeconds;

            // Manage rotation and moving
            if (_inputElapsedTime > _KEYPOLLINGTIME)
            {
                _inputElapsedTime = 0;
                HandleInput(gamestate);
            }

            if (_fallingTime > _seconds)
            {
                _fallingTime = 0;
                return(FallingProcess(gamestate));
            }
            return(this);
        }
Ejemplo n.º 9
0
        private static void Move(Direction direction, IBlockLiner gamestate)
        {
            List <Block> movableBlock = new List <Block>();

            // get gameArea dimension
            int xLength = gamestate.GameArea.GetLength(0);
            int yLength = gamestate.GameArea.GetLength(1);

            // iterate through gameArea for falling blocks that going
            // to be affected by movement
            for (int y = 0; y < yLength; y++)
            {
                for (int x = 0; x < xLength; x++)
                {
                    Block b = gamestate.GameArea[x, y];
                    if (b != null && b.Falling)
                    {
                        gamestate.GameArea[x, y] = null;
                        MoveBlock(b, direction);
                        movableBlock.Add(b);
                    }
                }
            }

            if (movableBlock.Count == 0)
            {
                throw new UnplacableBlockException();
            }

            // if any error during iteration, revert block position
            if (AreAbleToMove(movableBlock, direction, gamestate.GameArea))
            {
                foreach (Block b in movableBlock)
                {
                    gamestate.GameArea[(int)b.X, (int)b.Y] = b;
                }
            }
            else
            {
                RevertMove(movableBlock, direction, gamestate.GameArea);
                throw new UnplacableBlockException();
            }
        }
Ejemplo n.º 10
0
        private static void Rotate(IBlockLiner gamestate)
        {
            // retrieve fallingBlocks
            List <Block> fallingBlocks = GetFallingBlocks(gamestate);


            // extract (x.y) min and max position
            int minX = int.MaxValue;
            int minY = int.MaxValue;
            int maxX = int.MinValue;
            int maxY = int.MinValue;

            foreach (Block b in fallingBlocks)
            {
                minX = (b.X < minX) ? (int)b.X : minX;
                minY = (b.Y < minY) ? (int)b.Y : minY;
                maxX = (b.X > maxX) ? (int)b.X : maxX;
                maxY = (b.Y > maxY) ? (int)b.Y : maxY;
            }

            // create rotation pattern

            // should be refactored !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            Block[,] rotationMatrix = new Block[4, 4];

            // declaring (x,y) mapping function
            Func <int, int> XMapping = (int realx) => { return(realx - minX); };
            Func <int, int> YMapping = (int realy) => { return(realy - minY); };

            // foreach falling blocks put block inside the pattern
            // according to mapping functions
            foreach (Block b in fallingBlocks)
            {
                int xPatternPos = XMapping((int)b.X);
                int yPatternPos = YMapping((int)b.Y);

                rotationMatrix[xPatternPos, yPatternPos] = b;
            }
        }
Ejemplo n.º 11
0
        private BlockLinerState ValidateLines(IBlockLiner gamestate)
        {
            List <int> toRemove = new List <int>();

            Block[,] matrix = gamestate.GameArea;
            int width  = matrix.GetLength(0);
            int height = matrix.GetLength(1);


            for (int y = 0; y < height; y++)
            {
                if (IsFullLine(matrix, y))
                {
                    toRemove.Add(y);
                }
            }

            if (toRemove.Count != 0)
            {
                RemoveLines(matrix, toRemove);
            }

            return(gamestate.GetStateInstance(Type.NewBlock));
        }
Ejemplo n.º 12
0
 public override BlockLinerState Update(IBlockLiner gamestate, GameTime delta)
 {
     return(ValidateLines(gamestate));
 }
Ejemplo n.º 13
0
 public RenderingSystem(IBlockLinerRenderer renderer, IBlockLiner gamestate)
 {
     _renderer  = renderer;
     _gamestate = gamestate;
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Update gamelogic for the given delta time
 /// </summary>
 /// <param name="delta">Current delta time</param>
 /// <returns>New state of the current gamelogic</returns>
 public abstract BlockLinerState Update(IBlockLiner gamestate, GameTime delta);