Browse Source

Add: TimeLibrary

AnnulusGames 11 months ago
parent
commit
f049b7515b

+ 6 - 0
src/Lua.Unity/Assets/Lua.Unity/Runtime/LuaStateExtensions.cs

@@ -27,6 +27,12 @@ namespace Lua.Unity
             }
             }
             color.Metatable = ColorLibrary.Instance.Metatable;
             color.Metatable = ColorLibrary.Instance.Metatable;
             state.Environment["color"] = color;
             state.Environment["color"] = color;
+
+            var time = new LuaTable
+            {
+                Metatable = TimeLibrary.Instance.Metatable
+            };
+            state.Environment["time"] = time;
         }
         }
     }
     }
 }
 }

+ 35 - 17
src/Lua.Unity/Assets/Lua.Unity/Runtime/TimeLibrary.cs

@@ -1,6 +1,4 @@
-using System;
-using System.Threading;
-using System.Threading.Tasks;
+using Lua.Runtime;
 using UnityEngine;
 using UnityEngine;
 
 
 namespace Lua.Unity
 namespace Lua.Unity
@@ -9,23 +7,43 @@ namespace Lua.Unity
     {
     {
         public static readonly TimeLibrary Instance = new();
         public static readonly TimeLibrary Instance = new();
 
 
-        public readonly LuaFunction[] functions = new LuaFunction[]
-        {
-            new("time", GetTime),
-            new("delta_time", GetDeltaTime),
-        };
+        public readonly LuaTable Metatable = new();
 
 
-        public static ValueTask<int> GetTime(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
+        public TimeLibrary()
         {
         {
-            buffer.Span[0] = Time.timeAsDouble;
-            return new(1);
-        }
+            Metatable[Metamethods.Index] = new LuaFunction((context, buffer, ct) =>
+            {
+                var name = context.GetArgument<string>(1);
+                buffer.Span[0] = name switch
+                {
+                    "time" => Time.timeAsDouble,
+                    "unscaled_time" => Time.unscaledTimeAsDouble,
+                    "delta_time" => Time.deltaTime,
+                    "unscaled_delta_time" => Time.unscaledDeltaTime,
+                    "fixed_time" => Time.fixedTimeAsDouble,
+                    "fixed_unscaled_time" => Time.fixedUnscaledTimeAsDouble,
+                    "fixed_delta_time" => Time.fixedDeltaTime,
+                    "fixed_unscaled_delta_time" => Time.fixedUnscaledDeltaTime,
+                    "time_since_level_load" => Time.timeSinceLevelLoadAsDouble,
+                    "in_fixed_time_step" => Time.inFixedTimeStep,
+                    "frame_count" => Time.frameCount,
+                    "time_scale" => Time.timeScale,
+                    _ => LuaValue.Nil,
+                };
+                return new(1);
+            });
 
 
-        public static ValueTask<int> GetDeltaTime(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
-        {
-            buffer.Span[0] = Time.deltaTime;
-            return new(1);
+            Metatable[Metamethods.NewIndex] = new LuaFunction((context, buffer, ct) =>
+            {
+                var name = context.GetArgument<string>(1);
+                switch (name)
+                {
+                    case "time_scale":
+                        Time.timeScale = context.GetArgument<float>(2);
+                        break;
+                }
+                return new(0);
+            });
         }
         }
-
     }
     }
 }
 }