Skip to content

Commit

Permalink
add timescale feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzyhau committed Jan 13, 2023
1 parent b545e9b commit b3e7bba
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions FEZ.FEZUG.mm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<Compile Include="Features\Allow3DRotation.cs" />
<Compile Include="Features\GetTrileInfo.cs" />
<Compile Include="Features\InvisibleTrilesDraw.cs" />
<Compile Include="Features\Timescaler.cs" />
<Compile Include="Helpers\DrawingTools.cs" />
<Compile Include="Features\IFezugFeature.cs" />
<Compile Include="Features\ItemCountSet.cs" />
Expand Down
56 changes: 56 additions & 0 deletions Features/Timescaler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using FezEngine.Services;
using FezEngine.Tools;
using FEZUG.Features.Console;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FEZUG.Features
{
internal class Timescaler : IFezugCommand
{
public static float Timescale = 1.0f;

public string Name => "timescale";

public string HelpText => "timescale <value> - changes game's simulation scale";

[ServiceDependency]
public ISoundManager SoundManager { private get; set; }

public List<string> Autocomplete(string[] args)
{
string timescaleStr = Timescale.ToString("0.000", CultureInfo.InvariantCulture);
if (timescaleStr.StartsWith(args[0])) return new List<string> { timescaleStr };
return null;
}

public bool Execute(string[] args)
{
if (args.Length != 1)
{
FezugConsole.Print($"Incorrect number of parameters: '{args.Length}'", FezugConsole.OutputType.Warning);
return false;
}

if (!float.TryParse(args[0], NumberStyles.Number, CultureInfo.InvariantCulture, out float timescale))
{
FezugConsole.Print($"Incorrect timescale: '{args[0]}'", FezugConsole.OutputType.Warning);
return false;
}

Timescale = timescale;

FezugConsole.Print($"Timescale has been set to {Timescale.ToString("0.000", CultureInfo.InvariantCulture)}");

return true;
}
}
}
32 changes: 32 additions & 0 deletions Patches/Fez.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
using FEZUG;
using FEZUG.Features;
using Microsoft.Xna.Framework;
using System;
using System.Diagnostics;

namespace FezGame
{
public class patch_Fez : Fez
{
private double timescaledGameTime;
private double timescaledElapsedTime;

public Fezug Fezug;

protected extern void orig_Initialize();
Expand All @@ -21,6 +26,33 @@ protected override void Initialize()
ServiceHelper.AddComponent(Fezug = new Fezug(this));
ServiceHelper.AddComponent(Fezug.Rendering);
Logger.Log("FEZUG", "FEZUG initialized!");

timescaledGameTime = 0.0f;
}

protected extern void orig_Update(GameTime gameTime);
protected override void Update(GameTime gameTime)
{
if(gameTime.TotalGameTime.Ticks == 0)
{
timescaledGameTime = 0.0f;
}
timescaledElapsedTime = gameTime.ElapsedGameTime.TotalSeconds * Timescaler.Timescale;
timescaledGameTime += timescaledElapsedTime;

orig_Update(new GameTime(
TimeSpan.FromSeconds(timescaledGameTime),
TimeSpan.FromSeconds(timescaledElapsedTime)
));
}

protected extern void orig_Draw(GameTime gameTime);
protected override void Draw(GameTime gameTime)
{
orig_Draw(new GameTime(
TimeSpan.FromSeconds(timescaledGameTime),
TimeSpan.FromSeconds(timescaledElapsedTime)
));
}
}
}

0 comments on commit b3e7bba

Please sign in to comment.