Add Clock class & use it from Player and SneakGame

GitOrigin-RevId: fe20c836ca693fdabad4ce72bfdf8952b59b735b
This commit is contained in:
Colin McMillen 2020-01-24 20:42:27 -05:00
parent c7ec9e3ad7
commit f5ca85264f
4 changed files with 18 additions and 3 deletions

15
Shared/Clock.cs Normal file
View File

@ -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; }
}
}
}

View File

@ -22,7 +22,6 @@ namespace SemiColinGames {
private double swordSwingTime = 0; private double swordSwingTime = 0;
private double jumpTime = 0; private double jumpTime = 0;
private float ySpeed = 0; private float ySpeed = 0;
private float totalModelTime = 0;
public Player(Texture2D texture) { public Player(Texture2D texture) {
this.texture = texture; this.texture = texture;
@ -35,7 +34,6 @@ namespace SemiColinGames {
} }
public void Update(float modelTime, History<Input> input, List<Rectangle> collisionTargets) { public void Update(float modelTime, History<Input> input, List<Rectangle> collisionTargets) {
totalModelTime += modelTime;
Point oldPosition = position; Point oldPosition = position;
Vector2 movement = HandleInput(modelTime, input); Vector2 movement = HandleInput(modelTime, input);
position = new Point((int) (oldPosition.X + movement.X), (int) (oldPosition.Y + movement.Y)); position = new Point((int) (oldPosition.X + movement.X), (int) (oldPosition.Y + movement.Y));
@ -130,7 +128,7 @@ namespace SemiColinGames {
} }
private int SpriteIndex(Pose pose) { private int SpriteIndex(Pose pose) {
int frameNum = ((int) (totalModelTime * 1000) / 125) % 4; int frameNum = (int) Clock.ModelTime.TotalMilliseconds / 125 % 4;
if (frameNum == 3) { if (frameNum == 3) {
frameNum = 1; frameNum = 1;
} }

View File

@ -10,6 +10,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Camera.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Camera.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Clock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Input.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Input.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Debug.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Debug.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FpsCounter.cs" /> <Compile Include="$(MSBuildThisFileDirectory)FpsCounter.cs" />

View File

@ -90,6 +90,7 @@ namespace SemiColinGames {
if (!paused) { if (!paused) {
float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds; float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
Clock.AddModelTime(modelTime);
List<Rectangle> collisionTargets = world.CollisionTargets(); List<Rectangle> collisionTargets = world.CollisionTargets();
player.Update(modelTime, input, collisionTargets); player.Update(modelTime, input, collisionTargets);
camera.Update(player.Position); camera.Update(player.Position);