示例#1
0
        protected override void Initialize()
        {
            // store the scale transformation that will be used for all drawing
            Vector3 scaleVector3 = new Vector3(scaleFactor, scaleFactor, 1);

            scaleTransormation = Matrix.CreateScale(scaleVector3);

            // TODO: Add your initialization logic here

            //graphics init
            graphics.PreferredBackBufferWidth  = (int)(gameScreenSize.X * scaleFactor);
            graphics.PreferredBackBufferHeight = (int)(gameScreenSize.Y * scaleFactor);
            graphics.ApplyChanges();

            //tiles init (set size, then set tiles
            var width  = (int)(gameScreenSize.X / tileSize.X);
            var height = (int)(gameScreenSize.Y / tileSize.Y);

            tileSet = new TileSet(tileSize, width, height);

            player = new Player(
                position: new Vector2(
                    gameScreenSize.X / 2,
                    gameScreenSize.Y / 2),
                size: tileSize);

            dog = new Dog(
                position: new Vector2(
                    gameScreenSize.X / 2,
                    gameScreenSize.Y - 5),
                size: tileSize);

            //need a cleaner way to add behaviors
            //also, should be able to add a behavior without needing to specify owner
            //owner should always be the class that the behaviors are being added to
            //maybe behaviors cant be edited directly
            //and put an add behavior function in the entity class?

            //player behaviors
            player.AddBehavior(
                new Gravity(
                    acceleration: new Vector2(0, 10)));

            player.AddBehavior(
                new Friction());

            player.AddBehavior(
                new Move(
                    lateralAcceleration: 16f));

            player.AddBehavior(
                new Jump(
                    playerControlled: true,
                    velocity: new Vector2(0, -150)));

            //dog behaviors
            dog.AddBehavior(
                new Gravity(
                    acceleration: new Vector2(0, 10)));

            dog.AddBehavior(
                new Friction());

            dog.AddBehavior(
                new Follow(
                    target: player,
                    lateralAcceleration: 5f));

            dog.AddBehavior(
                new Jump(
                    playerControlled: false,
                    velocity: new Vector2(0, -150)));

            base.Initialize();
        }
示例#2
0
        void MoveAndCollide(TileSet tileSet)
        {
            //right now this does not handle all level boundaries

            //set that we are not on the ground initially
            //only say we are on the ground if we collide from the bottom
            tileStandingOn = null;

            //do X movement + collision
            if (velocity.X != 0)
            {
                int move = MathUtil.Round(velocity.X);

                if (move != 0)
                {
                    int sign = Math.Sign(move);

                    while (move != 0)
                    {
                        //check our location, moving one pixel at a time
                        //check the collision one pixel in the x direction
                        var collidedTile = collidesWithTile(tileSet, position + new Vector2(sign, 0));
                        if (collidedTile != null)
                        {
                            //collision occurred
                            //do not move
                            //break out of this loop
                            velocity.X = 0;
                            break;
                        }
                        else
                        {
                            //did not collide
                            //do one pixel of the move
                            position.X += sign;
                            move       -= sign;
                        }
                    }
                }
            }

            //then do Y
            if (velocity.Y != 0)
            {
                int move = MathUtil.Round(velocity.Y);

                if (move != 0)
                {
                    int sign = Math.Sign(move);

                    while (move != 0)
                    {
                        var collidedTile = collidesWithTile(tileSet, position + new Vector2(0, sign));

                        if (collidedTile != null)
                        {
                            //collision occurred

                            //we know we are on the ground if the sign of the move is 1 (moving down, collided)
                            //is that true?
                            if (sign == 1)
                            {
                                tileStandingOn = collidedTile;
                            }

                            //do not move
                            //break out of this loop
                            velocity.Y = 0;
                            break;
                        }
                        else
                        {
                            //did not collide
                            //do one pixel of the move
                            position.Y += sign;
                            move       -= sign;
                        }
                    }
                }
            }
        }
示例#3
0
        public override void Update(GameTime gameTime, Vector2 gameScreenSize, KeyboardState keyboardState, TileSet tileSet)
        {
            //hmm, nothing special about dog either?
            //is this the right approach?
            //where should behaviors be initiated?

            base.Update(gameTime, gameScreenSize, keyboardState, tileSet);
        }
示例#4
0
        public override void Update(GameTime gameTime, Vector2 gameScreenSize, KeyboardState keyboardState, TileSet tileSet)
        {
            //nothing special about the player for now!

            base.Update(gameTime, gameScreenSize, keyboardState, tileSet);
        }