Example #1
0
        public void applyPhysics(Map map, Player player, Enemy[] enemies, GameTime gametime)
        {
            float x = 1.5f;
            player.Speed += new Vector2(0, 0.15f * x);

            mapCollisionPlayer(map, player);

            foreach (Enemy e in enemies)
            {
                e.Speed += new Vector2(0, 0.15f * x);
                if (e.IsAlive && e.IsOnScreen)
                {
                    mapCollisionEnemy(map, e);
                }
            }
        }
Example #2
0
 public void synchWithPlayer(Player player, GameTime gameTime, Rectangle clientBounds)
 {
     mapOffset.X = -player.PositionOffsetX;
 }
Example #3
0
        // ##################################################################################
        // Kram zum testen
        private void initializePlayer()
        {
            Texture2D playerTex;
            Vector2 playerPosition;
            Point playerFrameSize;
            Point playerCurrentFrame;
            Point playerSheetSize;
            Vector2 playerSpeed;

            playerTex = Content.Load<Texture2D>(@"Images\figur");
            playerPosition = new Vector2(100, 300);
            playerFrameSize = new Point(40, 80);
            playerCurrentFrame = new Point(0, 0);
            playerSheetSize = new Point(1, 1);
            playerSpeed = new Vector2(0, 0);
            player = new Player(playerTex, playerPosition, playerFrameSize, playerCurrentFrame, playerSheetSize, playerSpeed);
            player.MapWidth = map.WidthInPixel;
            player.ScreenWidth = screenSize.X * tileSize.X;
        }
Example #4
0
        private void mapCollisionPlayer(Map map, Player player)
        {
            if (player.Speed.Y != 0)
                player.IsOnGround = false;

            Vector2 moveAmount = player.Speed;

            //foreach (Tile tile in tileList)
            //{
            //    moveAmount = horizontalCollisionTest(moveAmount, player, tile);
            //    moveAmount = verticalCollisionTest(moveAmount, player, tile);
            //}

            moveAmount = horizontalCollisionTest(moveAmount, player, map);
            moveAmount = verticalCollisionTest(moveAmount, player, map);

            Vector2 newPosition = player.Position + moveAmount;

            newPosition = new Vector2(
                MathHelper.Clamp(newPosition.X, 0,
                  map.WidthInPixel - player.collisionRect.Width),
                MathHelper.Clamp(newPosition.Y, 2 * (map.TileHeight),
                  map.HeightInPixel - player.collisionRect.Height));
            player.Position = newPosition;
        }
Example #5
0
        private Vector2 verticalCollisionTest(Vector2 moveAmount, Player player, Map map)
        {
            if (moveAmount.Y == 0)
                return moveAmount;

            Rectangle afterMoveRect = player.collisionRect;
            afterMoveRect.Offset((int)moveAmount.X, (int)moveAmount.Y);
            Vector2 corner1, corner2;

            if (moveAmount.Y < 0)
            {
                corner1 = new Vector2(afterMoveRect.Left + 1,
                                      afterMoveRect.Top);
                corner2 = new Vector2(afterMoveRect.Right - 1,
                                      afterMoveRect.Top);
            }
            else
            {
                corner1 = new Vector2(afterMoveRect.Left + 1,
                                      afterMoveRect.Bottom);
                corner2 = new Vector2(afterMoveRect.Right - 1,
                                      afterMoveRect.Bottom);
            }

            Tile mapCell1 = map.getTile((int)(corner1.X / map.TileHeight), (int)(corner1.Y / map.TileWidth));
            Tile mapCell2 = map.getTile((int)(corner2.X / map.TileHeight), (int)(corner2.Y / map.TileWidth));

            if ((!mapCell1.IsBackground && !mapCell1.IsDestructible) ||
                (!mapCell2.IsBackground && !mapCell2.IsDestructible))
            {
                if (moveAmount.Y > 0)
                    player.IsOnGround = true;
                moveAmount.Y = 0;
                player.Speed = new Vector2(player.Speed.X, 0);
            }

            return moveAmount;
        }