From f5ca85264f3e060e57a9a2cc0e628b25c8a2b951 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Fri, 24 Jan 2020 20:42:27 -0500 Subject: [PATCH] Add Clock class & use it from Player and SneakGame GitOrigin-RevId: fe20c836ca693fdabad4ce72bfdf8952b59b735b --- Shared/Clock.cs | 15 +++++++++++++++ Shared/Player.cs | 4 +--- Shared/Shared.projitems | 1 + Shared/SneakGame.cs | 1 + 4 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 Shared/Clock.cs diff --git a/Shared/Clock.cs b/Shared/Clock.cs new file mode 100644 index 0000000..7a35db8 --- /dev/null +++ b/Shared/Clock.cs @@ -0,0 +1,15 @@ +using System; + +namespace SemiColinGames { + class Clock { + private static TimeSpan modelTime = new TimeSpan(); + + public static void AddModelTime(double seconds) { + modelTime += TimeSpan.FromSeconds(seconds); + } + + public static TimeSpan ModelTime { + get { return modelTime; } + } + } +} diff --git a/Shared/Player.cs b/Shared/Player.cs index b76e813..4635f7a 100644 --- a/Shared/Player.cs +++ b/Shared/Player.cs @@ -22,7 +22,6 @@ 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; @@ -35,7 +34,6 @@ namespace SemiColinGames { } public void Update(float modelTime, History input, List collisionTargets) { - totalModelTime += modelTime; Point oldPosition = position; Vector2 movement = HandleInput(modelTime, input); position = new Point((int) (oldPosition.X + movement.X), (int) (oldPosition.Y + movement.Y)); @@ -130,7 +128,7 @@ namespace SemiColinGames { } private int SpriteIndex(Pose pose) { - int frameNum = ((int) (totalModelTime * 1000) / 125) % 4; + int frameNum = (int) Clock.ModelTime.TotalMilliseconds / 125 % 4; if (frameNum == 3) { frameNum = 1; } diff --git a/Shared/Shared.projitems b/Shared/Shared.projitems index 009b974..1a9b199 100644 --- a/Shared/Shared.projitems +++ b/Shared/Shared.projitems @@ -10,6 +10,7 @@ + diff --git a/Shared/SneakGame.cs b/Shared/SneakGame.cs index 94d2001..86fb604 100644 --- a/Shared/SneakGame.cs +++ b/Shared/SneakGame.cs @@ -90,6 +90,7 @@ namespace SemiColinGames { if (!paused) { float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds; + Clock.AddModelTime(modelTime); List collisionTargets = world.CollisionTargets(); player.Update(modelTime, input, collisionTargets); camera.Update(player.Position);