From 87100eaa6736d52e5a255fea622e078102c02292 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Fri, 24 Jan 2020 18:36:55 -0500 Subject: [PATCH] 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 --- Shared/Player.cs | 26 ++++++++++++++------------ Shared/SneakGame.cs | 5 +++-- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Shared/Player.cs b/Shared/Player.cs index 76826d1..840e8ef 100644 --- a/Shared/Player.cs +++ b/Shared/Player.cs @@ -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, List collisionTargets) { + public void Update(float modelTime, History input, List 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) { + Vector2 HandleInput(float modelTime, History 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 ? diff --git a/Shared/SneakGame.cs b/Shared/SneakGame.cs index 7608b00..25dba68 100644 --- a/Shared/SneakGame.cs +++ b/Shared/SneakGame.cs @@ -86,8 +86,9 @@ namespace SemiColinGames { } if (!paused) { + float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds; List 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);