diff --git a/Jumpy.Shared/Player.cs b/Jumpy.Shared/Player.cs index 383e605..d5b09c0 100644 --- a/Jumpy.Shared/Player.cs +++ b/Jumpy.Shared/Player.cs @@ -8,7 +8,7 @@ namespace Jumpy { class Player { enum Facing { Left, Right }; enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping }; - enum AirState { Jumping, Ground }; + enum AirState { Jumping, Ground, Falling }; private Texture2D texture; private const int spriteSize = 48; @@ -17,9 +17,9 @@ namespace Jumpy { private const int moveSpeed = 200; private const int jumpSpeed = 600; private const int gravity = 2000; - private const int groundLevel = Camera.Height - spriteSize / 2 + bottomPadding - 32; + private const int groundLevel = 10000; - private Point position = new Point(Camera.Width / 2, groundLevel); + private Point position = new Point(Camera.Width / 2, 10); private Facing facing = Facing.Right; private Pose pose = Pose.Standing; private AirState airState = AirState.Ground; @@ -35,18 +35,55 @@ namespace Jumpy { // instead of complicated if-statements in this function. public void Update( GameTime time, History gamePad, List collisionTargets) { + Point oldPosition = position; + AirState oldAirState = airState; UpdateFromGamePad(time, gamePad); - Rectangle playerBbox = - new Rectangle(position.X - spriteWidth, position.Y - 9, spriteWidth * 2, 28); - Debug.AddRect(playerBbox, Color.Red); + bool someIntersection = false; + + Rectangle playerBbox = new Rectangle(position.X - spriteWidth, position.Y - 8, spriteWidth * 2, 27); + bool standingOnGround = false; foreach (var rect in collisionTargets) { + playerBbox = new Rectangle(position.X - spriteWidth, position.Y - 8, spriteWidth * 2, 27); + if (playerBbox.Intersects(rect)) { + someIntersection = true; + if (oldPosition.Y > position.Y) { + int diff = playerBbox.Top - rect.Bottom; + position.Y -= diff; + ySpeed *= 0.9; + } else { + airState = AirState.Ground; + int diff = playerBbox.Bottom - rect.Top; + position.Y -= diff; + } Debug.AddRect(rect, Color.Yellow); } else { - Debug.AddRect(rect, Color.Green); + playerBbox.Height += 1; + if (playerBbox.Intersects(rect)) { + standingOnGround = true; + Debug.AddRect(rect, Color.Cyan); + } else { + Debug.AddRect(rect, Color.Green); + } } } + if (oldAirState != AirState.Ground && standingOnGround) { + airState = AirState.Ground; + ySpeed = 0.0; + } + if (airState == AirState.Ground && !standingOnGround) { + airState = AirState.Falling; + ySpeed = 0.0; + } + + if (airState == AirState.Ground) { + Debug.AddRect(playerBbox, Color.Red); + } else if (airState == AirState.Jumping) { + Debug.AddRect(playerBbox, Color.Orange); + } else { + Debug.AddRect(playerBbox, Color.Yellow); + } } void UpdateFromGamePad(GameTime time, History gamePad) { @@ -90,7 +127,7 @@ namespace Jumpy { pose = Pose.SwordSwing; } - if (airState == AirState.Jumping) { + if (airState == AirState.Jumping || airState == AirState.Falling) { position.Y += (int) (ySpeed * time.ElapsedGameTime.TotalSeconds); ySpeed += gravity * (float) time.ElapsedGameTime.TotalSeconds; if (position.Y > groundLevel) {