Пример #1
0
        public CollisionController(GameManager gameManager)
        {
            GameManager      = gameManager;
            MoveVector       = Vector2.Zero;
            SurroundingTiles = new Tuple <Vector2, int, Vector2> [8];

            CurrentTile = new CollisionTile(SqrtSize);
            CurrentTile.CalculateTilePos(gameManager.player.Position, 0);
            TileArea = CalculateArea(CurrentTile.A,
                                     CurrentTile.B,
                                     CurrentTile.D,
                                     CurrentTile.C);

            GenerateGrid(gameManager.GameWorld.WorldSize * 4,
                         gameManager.GameWorld.WorldSize * 4);

            // Placing collision tiles in these places below:
            // (feel free to test or remove this)
            Grid[0][0] = new Tuple <Vector2, int>(Grid[0][0].Item1, 1);
            Grid[0][2] = new Tuple <Vector2, int>(Grid[0][2].Item1, 1);
            Grid[0][4] = new Tuple <Vector2, int>(Grid[0][4].Item1, 1);
            Grid[0][6] = new Tuple <Vector2, int>(Grid[0][6].Item1, 1);
            Grid[0][8] = new Tuple <Vector2, int>(Grid[0][8].Item1, 1);

            CalcSurrTiles();
        }
Пример #2
0
        public void Update(GameTime gameTime)
        {
            Player player       = GameManager.player;
            float  deltaTime    = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            float  moveDistance = player.MoveSpeed * deltaTime;

            if (player.MovingUp)
            {
                player.NextPos = new Vector2(player.Position.X,
                                             player.Position.Y - moveDistance);

                for (int i = 0; i < SurroundingTiles.Length; i++)
                {
                    if (SurroundingTiles[i] == null)
                    {
                        continue;
                    }

                    CurrentTile.CalculateTilePos(SurroundingTiles[i].Item1,
                                                 SurroundingTiles[i].Item2);

                    if (IsColliding(player, CurrentTile))
                    {
                        if (SurroundingTiles[i].Item2 == 0)
                        {
                            CenterTileIndex = SurroundingTiles[i].Item3;
                        }
                        else if (SurroundingTiles[i].Item2 == 1)
                        {
                            Collided = true;
                        }
                    }
                }
                if (!Collided)
                {
                    MoveVector = new Vector2(MoveVector.X, MoveVector.Y - moveDistance);
                }
                player.PlayerDirection = Direction.Up;
                player.MovingUp        = false;
                Collided = false;
            }

            if (player.MovingLeft)
            {
                player.NextPos = new Vector2(player.Position.X - moveDistance,
                                             player.Position.Y);

                for (int i = 0; i < SurroundingTiles.Length; i++)
                {
                    if (SurroundingTiles[i] == null)
                    {
                        continue;
                    }
                    CurrentTile.CalculateTilePos(SurroundingTiles[i].Item1,
                                                 SurroundingTiles[i].Item2);

                    if (IsColliding(player, CurrentTile))
                    {
                        if (SurroundingTiles[i].Item2 == 0)
                        {
                            CenterTileIndex = SurroundingTiles[i].Item3;
                        }
                        else if (SurroundingTiles[i].Item2 == 1)
                        {
                            Collided = true;
                        }
                    }
                }
                if (!Collided)
                {
                    MoveVector = new Vector2(MoveVector.X - moveDistance, MoveVector.Y);
                }
                player.PlayerDirection = Direction.Left;
                player.MovingLeft      = false;
                Collided = false;
            }

            if (player.MovingDown)
            {
                player.NextPos = new Vector2(player.Position.X,
                                             player.Position.Y + moveDistance);

                for (int i = 0; i < SurroundingTiles.Length; i++)
                {
                    if (SurroundingTiles[i] == null)
                    {
                        continue;
                    }
                    CurrentTile.CalculateTilePos(SurroundingTiles[i].Item1,
                                                 SurroundingTiles[i].Item2);

                    if (IsColliding(player, CurrentTile))
                    {
                        if (SurroundingTiles[i].Item2 == 0)
                        {
                            CenterTileIndex = SurroundingTiles[i].Item3;
                        }
                        else if (SurroundingTiles[i].Item2 == 1)
                        {
                            Collided = true;
                        }
                    }
                }
                if (!Collided)
                {
                    MoveVector = new Vector2(MoveVector.X, MoveVector.Y + moveDistance);
                }

                player.PlayerDirection = Direction.Down;
                player.MovingDown      = false;
                Collided = false;
            }

            if (player.MovingRight)
            {
                player.NextPos = new Vector2(player.Position.X + moveDistance,
                                             player.Position.Y);
                for (int i = 0; i < SurroundingTiles.Length; i++)
                {
                    if (SurroundingTiles[i] == null)
                    {
                        continue;
                    }
                    CurrentTile.CalculateTilePos(SurroundingTiles[i].Item1,
                                                 SurroundingTiles[i].Item2);

                    if (IsColliding(player, CurrentTile))
                    {
                        if (SurroundingTiles[i].Item2 == 0)
                        {
                            CenterTileIndex = SurroundingTiles[i].Item3;
                        }
                        else if (SurroundingTiles[i].Item2 == 1)
                        {
                            Collided = true;
                        }
                    }
                }
                if (!Collided)
                {
                    MoveVector = new Vector2(MoveVector.X + moveDistance, MoveVector.Y);
                }

                player.PlayerDirection = Direction.Right;
                player.MovingRight     = false;
                Collided = false;
            }

            CalcSurrTiles();
            player.Position = new Vector2(player.Position.X + MoveVector.X,
                                          player.Position.Y + MoveVector.Y);
            MoveVector = Vector2.Zero;
        }