Player.Update now takes a float which is the model time elapsed.

Player.Draw doesn't depend on time elapsed any more.

GitOrigin-RevId: 3396f85f61c8ee430fda8986770663f561538294
This commit is contained in:
Colin McMillen 2020-01-24 18:36:55 -05:00
parent 83839c92ac
commit 87100eaa67
2 changed files with 17 additions and 14 deletions

View File

@ -22,6 +22,7 @@ namespace SemiColinGames {
private double swordSwingTime = 0;
private double jumpTime = 0;
private float ySpeed = 0;
private float totalModelTime = 0;
public Player(Texture2D texture) {
this.texture = texture;
@ -33,9 +34,10 @@ namespace SemiColinGames {
return new Rectangle(position.X - spriteWidth, position.Y - 7, spriteWidth * 2, 26);
}
public void Update(GameTime time, History<Input> input, List<Rectangle> collisionTargets) {
public void Update(float modelTime, History<Input> input, List<Rectangle> collisionTargets) {
totalModelTime += modelTime;
Point oldPosition = position;
Vector2 movement = HandleInput(time, input);
Vector2 movement = HandleInput(modelTime, input);
position = new Point((int) (oldPosition.X + movement.X), (int) (oldPosition.Y + movement.Y));
Rectangle oldBbox = Bbox(oldPosition);
@ -106,9 +108,9 @@ namespace SemiColinGames {
}
// Returns the desired (dx, dy) for the player to move this frame.
Vector2 HandleInput(GameTime time, History<Input> input) {
Vector2 HandleInput(float modelTime, History<Input> input) {
Vector2 result = new Vector2();
result.X = (int) (input[0].Motion.X * moveSpeed * time.ElapsedGameTime.TotalSeconds);
result.X = (int) (input[0].Motion.X * moveSpeed * modelTime);
if (input[0].Jump && !input[1].Jump && jumps > 0) {
jumpTime = 0.3;
@ -120,15 +122,15 @@ namespace SemiColinGames {
swordSwingTime = 0.3;
}
result.Y = ySpeed * (float) time.ElapsedGameTime.TotalSeconds;
ySpeed += gravity * (float) time.ElapsedGameTime.TotalSeconds;
jumpTime -= time.ElapsedGameTime.TotalSeconds;
swordSwingTime -= time.ElapsedGameTime.TotalSeconds;
result.Y = ySpeed * modelTime;
ySpeed += gravity * modelTime;
jumpTime -= modelTime;
swordSwingTime -= modelTime;
return result;
}
private int SpriteIndex(Pose pose, GameTime time) {
int frameNum = (time.TotalGameTime.Milliseconds / 125) % 4;
private int SpriteIndex(Pose pose) {
int frameNum = ((int) (totalModelTime * 1000) / 125) % 4;
if (frameNum == 3) {
frameNum = 1;
}
@ -161,8 +163,8 @@ namespace SemiColinGames {
}
}
public void Draw(SpriteBatch spriteBatch, Camera camera, GameTime time) {
int index = SpriteIndex(pose, time);
public void Draw(SpriteBatch spriteBatch, Camera camera) {
int index = SpriteIndex(pose);
Rectangle textureSource = new Rectangle(index * spriteSize, 0, spriteSize, spriteSize);
Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
SpriteEffects effect = facing == Facing.Right ?

View File

@ -86,8 +86,9 @@ namespace SemiColinGames {
}
if (!paused) {
float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
List<Rectangle> collisionTargets = world.CollisionTargets();
player.Update(gameTime, input, collisionTargets);
player.Update(modelTime, input, collisionTargets);
camera.Update(player.Position);
}
@ -122,7 +123,7 @@ namespace SemiColinGames {
spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
// Draw player.
player.Draw(spriteBatch, camera, gameTime);
player.Draw(spriteBatch, camera);
// Draw foreground tiles.
world.Draw(spriteBatch, camera);