Beispiel #1
0
        public void Apply(GameTime gt, Player player, Level level)
        {
            float dt = gt.ElapsedGameTime.Milliseconds / 1000f;

             player.velocity.Y += Constants.gravity * dt;

             Box endBox = new Box(player.collision) ;

             float dx = player.velocity.X * dt;
             float dy = player.velocity.Y * dt;

             Rectangle checkRect ;

             IEnumerable<MapObject> collideObjects;

             if (dx != 0)
             {
            endBox.Left += dx;
            checkRect = endBox.rect;
            checkRect.Inflate(1, 1);
            collideObjects = level.map.GetObjectsInRegion(level.collisionLayer, checkRect);

            foreach (var obj in collideObjects)
            {
               Vector2 delta = adjustPos(ref endBox, obj, Axis.X);
               endBox.pos += delta;
            }
             }

             endBox.Top += dy;

             checkRect = endBox.rect;
             checkRect.Inflate(1, 1);
             collideObjects = level.map.GetObjectsInRegion(level.collisionLayer, checkRect);

             foreach (var obj in collideObjects)
             {
            Vector2 delta = adjustPos(ref endBox, obj, Axis.Y);
            endBox.pos -= delta;
            if (delta.Y != 0)
            {
               player.velocity.Y = 0;
               if (delta.Y > 0) player.grounded = true;
            }
             }

             player.pos = endBox.pos;
        }
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            physics = new Physics();
             player = new Player();

             currentKeyboardState = new KeyboardState();
             previousKeyboardState = new KeyboardState();

             camera = new Camera2D(player);
             camera.pos = new Vector2(500f, 200f);
             camera.zoom = 1f;
             camera.fixY(450f);

             background = new ParallaxBackground();
             currentLevel = new Level(this);

             base.Initialize();
        }