refactor Player input/state handling to be less tangled
GitOrigin-RevId: 68bd79b722d7fcf2ef8abaf644e2b13544a08ae8
This commit is contained in:
parent
ea82c4ffd3
commit
459e43186a
110
Shared/Player.cs
110
Shared/Player.cs
@ -1,29 +1,27 @@
|
|||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace SemiColinGames {
|
namespace SemiColinGames {
|
||||||
class Player {
|
class Player {
|
||||||
|
|
||||||
enum Facing { Left, Right };
|
enum Facing { Left, Right };
|
||||||
enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
|
enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
|
||||||
enum AirState { Jumping, Ground, Falling };
|
|
||||||
|
private const int moveSpeed = 180;
|
||||||
|
private const int jumpSpeed = -600;
|
||||||
|
private const int gravity = 2400;
|
||||||
|
|
||||||
private Texture2D texture;
|
private Texture2D texture;
|
||||||
private const int spriteSize = 48;
|
private const int spriteSize = 48;
|
||||||
private const int spriteWidth = 7;
|
private const int spriteWidth = 7;
|
||||||
private const int moveSpeed = 180;
|
|
||||||
private const int jumpSpeed = 600;
|
|
||||||
private const int gravity = 2400;
|
|
||||||
|
|
||||||
private Point position = new Point(64, 16);
|
private Point position = new Point(64, 16);
|
||||||
|
private int jumps = 0;
|
||||||
private Facing facing = Facing.Right;
|
private Facing facing = Facing.Right;
|
||||||
private Pose pose = Pose.Standing;
|
private Pose pose = Pose.Jumping;
|
||||||
private AirState airState = AirState.Ground;
|
|
||||||
private double swordSwingTime = 0;
|
private double swordSwingTime = 0;
|
||||||
private double jumpTime = 0;
|
private double jumpTime = 0;
|
||||||
private double ySpeed = 0;
|
private float ySpeed = 0;
|
||||||
|
|
||||||
public Player(Texture2D texture) {
|
public Player(Texture2D texture) {
|
||||||
this.texture = texture;
|
this.texture = texture;
|
||||||
@ -37,8 +35,8 @@ namespace SemiColinGames {
|
|||||||
|
|
||||||
public void Update(GameTime time, History<Input> input, List<Rectangle> collisionTargets) {
|
public void Update(GameTime time, History<Input> input, List<Rectangle> collisionTargets) {
|
||||||
Point oldPosition = position;
|
Point oldPosition = position;
|
||||||
AirState oldAirState = airState;
|
Vector2 movement = HandleInput(time, input);
|
||||||
HandleInput(time, input);
|
position = new Point((int) (oldPosition.X + movement.X), (int) (oldPosition.Y + movement.Y));
|
||||||
|
|
||||||
Rectangle oldBbox = Bbox(oldPosition);
|
Rectangle oldBbox = Bbox(oldPosition);
|
||||||
Rectangle playerBbox = Bbox(position);
|
Rectangle playerBbox = Bbox(position);
|
||||||
@ -63,7 +61,7 @@ namespace SemiColinGames {
|
|||||||
int diff = playerBbox.Top - rect.Bottom;
|
int diff = playerBbox.Top - rect.Bottom;
|
||||||
position.Y -= diff;
|
position.Y -= diff;
|
||||||
} else {
|
} else {
|
||||||
airState = AirState.Ground;
|
standingOnGround = true;
|
||||||
int diff = playerBbox.Bottom - rect.Top;
|
int diff = playerBbox.Bottom - rect.Top;
|
||||||
position.Y -= diff;
|
position.Y -= diff;
|
||||||
}
|
}
|
||||||
@ -77,72 +75,55 @@ namespace SemiColinGames {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (oldAirState != AirState.Ground && standingOnGround) {
|
if (standingOnGround) {
|
||||||
airState = AirState.Ground;
|
jumps = 1;
|
||||||
ySpeed = 0.0;
|
ySpeed = 0;
|
||||||
}
|
|
||||||
if (airState == AirState.Ground && !standingOnGround) {
|
|
||||||
airState = AirState.Falling;
|
|
||||||
ySpeed = 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (airState == AirState.Ground) {
|
|
||||||
Debug.AddRect(playerBbox, Color.Red);
|
Debug.AddRect(playerBbox, Color.Red);
|
||||||
} else if (airState == AirState.Jumping) {
|
|
||||||
Debug.AddRect(playerBbox, Color.Orange);
|
|
||||||
} else {
|
} else {
|
||||||
Debug.AddRect(playerBbox, Color.Yellow);
|
jumps = 0;
|
||||||
}
|
Debug.AddRect(playerBbox, Color.Orange);
|
||||||
}
|
|
||||||
|
|
||||||
void HandleInput(GameTime time, History<Input> input) {
|
|
||||||
if (input[0].Jump && !input[1].Jump && airState == AirState.Ground) {
|
|
||||||
pose = Pose.Jumping;
|
|
||||||
airState = AirState.Jumping;
|
|
||||||
jumpTime = 0.5;
|
|
||||||
ySpeed = -jumpSpeed;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input[0].Attack && !input[1].Attack && swordSwingTime <= 0) {
|
if (movement.X > 0) {
|
||||||
pose = Pose.SwordSwing;
|
|
||||||
swordSwingTime = 0.3;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (input[0].Motion.X < 0) {
|
|
||||||
facing = Facing.Left;
|
|
||||||
pose = Pose.Walking;
|
|
||||||
position.X -= (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
|
|
||||||
} else if (input[0].Motion.X > 0) {
|
|
||||||
facing = Facing.Right;
|
facing = Facing.Right;
|
||||||
|
} else if (movement.X < 0) {
|
||||||
|
facing = Facing.Left;
|
||||||
|
}
|
||||||
|
if (swordSwingTime > 0) {
|
||||||
|
pose = Pose.SwordSwing;
|
||||||
|
} else if (jumps == 0) {
|
||||||
|
pose = Pose.Jumping;
|
||||||
|
} else if (movement.X != 0) {
|
||||||
pose = Pose.Walking;
|
pose = Pose.Walking;
|
||||||
position.X += (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
|
|
||||||
} else if (input[0].Motion.Y < 0) {
|
|
||||||
pose = Pose.Crouching;
|
|
||||||
} else if (input[0].Motion.Y > 0) {
|
} else if (input[0].Motion.Y > 0) {
|
||||||
pose = Pose.Stretching;
|
pose = Pose.Stretching;
|
||||||
|
} else if (input[0].Motion.Y < 0) {
|
||||||
|
pose = Pose.Crouching;
|
||||||
} else {
|
} else {
|
||||||
pose = Pose.Standing;
|
pose = Pose.Standing;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (jumpTime > 0) {
|
// Returns the desired (dx, dy) for the player to move this frame.
|
||||||
jumpTime -= time.ElapsedGameTime.TotalSeconds;
|
Vector2 HandleInput(GameTime time, History<Input> input) {
|
||||||
}
|
Vector2 result = new Vector2();
|
||||||
if (swordSwingTime > 0) {
|
result.X = (int) (input[0].Motion.X * moveSpeed * time.ElapsedGameTime.TotalSeconds);
|
||||||
swordSwingTime -= time.ElapsedGameTime.TotalSeconds;
|
|
||||||
pose = Pose.SwordSwing;
|
if (input[0].Jump && !input[1].Jump && jumps > 0) {
|
||||||
|
jumpTime = 0.3;
|
||||||
|
jumps--;
|
||||||
|
ySpeed = jumpSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (airState == AirState.Jumping || airState == AirState.Falling) {
|
if (input[0].Attack && !input[1].Attack && swordSwingTime <= 0) {
|
||||||
position.Y += (int) (ySpeed * time.ElapsedGameTime.TotalSeconds);
|
swordSwingTime = 0.3;
|
||||||
ySpeed += gravity * (float) time.ElapsedGameTime.TotalSeconds;
|
|
||||||
}
|
|
||||||
if (airState == AirState.Jumping && pose != Pose.SwordSwing) {
|
|
||||||
pose = Pose.Jumping;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
position.X = Math.Max(position.X, 0 + spriteWidth);
|
result.Y = ySpeed * (float) time.ElapsedGameTime.TotalSeconds;
|
||||||
|
ySpeed += gravity * (float) time.ElapsedGameTime.TotalSeconds;
|
||||||
|
jumpTime -= time.ElapsedGameTime.TotalSeconds;
|
||||||
|
swordSwingTime -= time.ElapsedGameTime.TotalSeconds;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int SpriteIndex(Pose pose, GameTime time) {
|
private int SpriteIndex(Pose pose, GameTime time) {
|
||||||
@ -156,9 +137,9 @@ namespace SemiColinGames {
|
|||||||
case Pose.Stretching:
|
case Pose.Stretching:
|
||||||
return 18 + frameNum;
|
return 18 + frameNum;
|
||||||
case Pose.Jumping:
|
case Pose.Jumping:
|
||||||
if (jumpTime > 0.25) {
|
if (jumpTime > 0.2) {
|
||||||
return 15;
|
return 15;
|
||||||
} else if (jumpTime > 0) {
|
} else if (jumpTime > 0.1) {
|
||||||
return 16;
|
return 16;
|
||||||
} else {
|
} else {
|
||||||
return 17;
|
return 17;
|
||||||
@ -181,6 +162,7 @@ namespace SemiColinGames {
|
|||||||
|
|
||||||
public void Draw(SpriteBatch spriteBatch, Camera camera, GameTime time) {
|
public void Draw(SpriteBatch spriteBatch, Camera camera, GameTime time) {
|
||||||
int index = SpriteIndex(pose, time);
|
int index = SpriteIndex(pose, time);
|
||||||
|
Debug.Toast("" + index);
|
||||||
Rectangle textureSource = new Rectangle(index * spriteSize, 0, spriteSize, spriteSize);
|
Rectangle textureSource = new Rectangle(index * spriteSize, 0, spriteSize, spriteSize);
|
||||||
Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
|
Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
|
||||||
SpriteEffects effect = facing == Facing.Right ?
|
SpriteEffects effect = facing == Facing.Right ?
|
||||||
|
Loading…
Reference in New Issue
Block a user