示例#1
0
        public Player(World world, float x, float y, Texture2D texture)
        {
            torso = BodyFactory.CreateRectangle(world, 60 * MainGame.PIXEL_TO_METER, 80 * MainGame.PIXEL_TO_METER, 1);
            torso.Position = new Vector2(x * MainGame.PIXEL_TO_METER, y * MainGame.PIXEL_TO_METER);
            torso.BodyType = BodyType.Dynamic;
            torso.UserData = this;
            legs = BodyFactory.CreateCircle(world, 31 * MainGame.PIXEL_TO_METER, 1);
            legs.Position = torso.Position + new Vector2(0, 40 * MainGame.PIXEL_TO_METER);
            legs.BodyType = BodyType.Dynamic;
            legs.Friction = 5.0f;
            legs.UserData = this;
            JointFactory.CreateFixedAngleJoint(world, torso);
            axis = JointFactory.CreateRevoluteJoint(world, torso, legs, Vector2.Zero);
            axis.CollideConnected = false;
            axis.MotorEnabled = true;
            axis.MotorSpeed = 0.0f;
            axis.MotorTorque = 3.0f;
            axis.MaxMotorTorque = 10.0f;
            onGround = false;
            facingLeft = false;
            jumpForce = new Vector2(0, -1f);
            rightAirForce = new Vector2(5f, 0);
            leftAirForce = -1 * rightAirForce;
            prevVelocity = Vector2.Zero;
            normal = Vector2.Zero;
            pressW = false;
            holdW = false;

            texIdle = new AnimatedTexture(texture, 24, 0, 0, 120, 140);
            texRun = new AnimatedTexture(texture, 19, 0, 140, 120, 140);
            texJump = new AnimatedTexture(texture, 9, 19 * 120, 140, 120, 140, 1, false, false);
            currentTexture = texIdle;
        }
示例#2
0
        public void Update(World w)
        {
            // Animate texture
            currentTexture.Update();
            if (!onGround)
            {
                if (currentTexture != texJump)
                    currentTexture = texJump;
                int frame = (int)(torso.LinearVelocity.Y * MainGame.METER_TO_PIXEL) / 16 + 3;
                if (frame < 0) frame = 0;
                else if (frame > 8) frame = 8;
                texJump.Frame = frame;
            }

            // Walking
            if (Keyboard.GetState().IsKeyDown(Keys.A))
            {
                facingLeft = true;
                axis.MotorSpeed = 2.5f * -MathHelper.TwoPi;
                if (onGround)
                {
                    currentTexture = texRun;
                }
                if (!onGround && torso.LinearVelocity.X > -2)
                    torso.ApplyForce(ref leftAirForce);
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.D))
            {
                facingLeft = false;
                axis.MotorSpeed = 2.5f * MathHelper.TwoPi;
                if (onGround)
                {
                    currentTexture = texRun;
                }
                if (!onGround && torso.LinearVelocity.X < 2)
                    torso.ApplyForce(ref rightAirForce);
            }
            else
            {
                if (onGround)
                    currentTexture = texIdle;
                axis.MotorSpeed = 0;
            }

            if (!onGround)
                axis.MotorEnabled = false;
            else
                axis.MotorEnabled = true;

            // Check if you're standing on something
            onGround = false;
            start = legs.Position + new Vector2(0, 24 * MainGame.PIXEL_TO_METER);
            end = start + new Vector2(0, 24 * MainGame.PIXEL_TO_METER);
            for (int i = -30; i <= 30; i += 15)
            {
                start.X = legs.Position.X + i * MainGame.PIXEL_TO_METER;
                end.X = legs.Position.X + i * MainGame.PIXEL_TO_METER;
                w.RayCast((f, p, n, fr) =>
                {
                    normal = n;
                    if (f != null)
                        onGround = true;
                    else
                        onGround = false;
                    return 0;
                }, start, end);

                if (onGround)
                    break;
            }

            // Jump
            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                if (!pressW)
                {
                    if (onGround)
                    {
                        if (Keyboard.GetState().IsKeyDown(Keys.A))
                            torso.LinearVelocity -= 0.7f * Vector2.UnitX;
                        if (Keyboard.GetState().IsKeyDown(Keys.D))
                            torso.LinearVelocity += 0.7f * Vector2.UnitX;
                        torso.LinearVelocity = new Vector2(torso.LinearVelocity.X, -8);
                        holdW = true;
                    }
                    pressW = true;
                }
            }
            else
            {
                pressW = false;
                holdW = false;
            }
            if (holdW && torso.LinearVelocity.Y < 0)
                torso.ApplyForce(jumpForce);

            prevVelocity = torso.LinearVelocity;
        }
示例#3
0
文件: Guy.cs 项目: TadCordle/MrGuyXNA
        private void UpdateMovement()
        {
            if (MovingLeft)
            {
                facingLeft = true;
                axis.MotorSpeed = 2.5f * -MathHelper.TwoPi;
                if (onGround)
                    if (Crouching)
                        currentTexture = texRoll;
                    else
                        currentTexture = texRun;
                if (!onGround && torso.LinearVelocity.X > -2)
                    torso.ApplyForce(ref left_airForce);
            }
            else if (MovingRight)
            {
                facingLeft = false;
                axis.MotorSpeed = 2.5f * MathHelper.TwoPi;
                if (onGround)
                    if (Crouching)
                        currentTexture = texRoll;
                    else
                        currentTexture = texRun;
                if (!onGround && torso.LinearVelocity.X < 2)
                    torso.ApplyForce(ref right_airForce);
            }
            else
            {
                if (onGround)
                    if (Crouching)
                        currentTexture = texCrouch;
                    else
                        currentTexture = texIdle;
                axis.MotorSpeed = 0;
            }

            if (!onGround)
                axis.MotorEnabled = false;
            else
                axis.MotorEnabled = true;
        }
示例#4
0
文件: Guy.cs 项目: TadCordle/MrGuyXNA
 private void UpdateTexture()
 {
     currentTexture.Update();
     if (!onGround)
     {
         if (currentTexture != texJump)
             currentTexture = texJump;
         int frame = (int)(torso.LinearVelocity.Y * MainGame.METER_TO_PIXEL) / 16 + 3;
         if (frame < 0) frame = 0;
         else if (frame > 8) frame = 8;
         texJump.Frame = frame;
     }
 }
示例#5
0
文件: Guy.cs 项目: TadCordle/MrGuyXNA
        private void SetUpTextures(Texture2D guyTexture)
        {
            texIdle = new AnimatedTexture(guyTexture, 24, 0, 0, 120, 140);
            texRun = new AnimatedTexture(guyTexture, 19, 0, 140, 120, 140);
            texJump = new AnimatedTexture(guyTexture, 9, 19 * 120, 140, 120, 140, 1, false, false);
            texCrouch = new AnimatedTexture(guyTexture, 1, 4 * 120, 280, 120, 140);
            texRoll = new AnimatedTexture(guyTexture, 1, 5 * 120, 280, 120, 140);

            currentTexture = texIdle;
        }