Esempio n. 1
0
        public void DoYCollision(Character character)
        {
            //Check surrounding cells.
            int col = (int)Math.Floor(character.position.X / (CELL_SIZE * 2f));
            int row = (int)Math.Floor(character.position.Y / (CELL_SIZE * 2f));

            for (int i = col - 2; i < col + 3; i++)
            {
                for (int j = row - 2; j < row + 3; j++)
                {
                    if (i > -1 && i < grid.GetLength(0) &&
                        j > -1 && j < grid.GetLength(1))
                    {
                        Cell cell = (Cell)grid[i, j];
                        tempRect.X = i * (CELL_SIZE * 2);
                        tempRect.Y = j * (CELL_SIZE * 2);
                        tempRect.Width = CELL_SIZE * 2;
                        tempRect.Height = CELL_SIZE * 2;
                        switch (cell)
                        {
                            case Cell.None:
                                //No collision
                                break;
                            case Cell.Start: //Door
                                break;
                            case Cell.End: //Door
                                break;
                            case Cell.Spikes://Spikes
                                if (character.CollRect.Intersects(tempRect))
                                {
                                    character.alive = false;
                                }
                                break;

                            default: //Collidable cell found
                                if (character.CollRect.Intersects(tempRect))
                                {
                                    if (character.inAir)
                                    {
                                        //Collision found
                                        if (character.velocity.Y > 0f)
                                        {
                                            //Move to top side
                                            character.position.Y = tempRect.Top - character.CollRect.Height;
                                            character.Land();
                                            //collRects.Add(tempRect);
                                        }
                                        else if (character.velocity.Y < 0f)
                                        {
                                            //Move to bottom side, bump head into top.
                                            character.velocity.Y = 0;
                                            character.position.Y = tempRect.Bottom;
                                            character.SetAnim("jump");
                                            character.inAir = true;
                                            //collRects.Add(tempRect);
                                        }
                                    }
                                }
                                break;
                        }
                    }
                }
            }
        }