-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuaObject.cs
88 lines (70 loc) · 2.27 KB
/
LuaObject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Loaders;
using System.Collections.Generic;
using System.Numerics;
using BBQLib;
using System;
namespace Snowball
{
public class LuaObject
{
public static List<Table> objects = new List<Table>();
public static Script script = new Script();
public static Table GetObject(int id)
{
foreach(var value in objects)
{
int _id = (int)value["id"];
if(id == _id)
{
Console.WriteLine("cum");
return value;
}
}
return null;
}
public static Table AddObject(Table table)
{
table["id"] = objects.Count;
objects.Add(table);
return table;
}
public static void SetProperty(string name, object value, Table dyn)
{
dyn[name] = value;
}
public static T GetProperty<T>(string name, Table dyn)
{
return (T)dyn[name];
}
public static void Update()
{
script.Globals["dt"] = BBQLib.BBQLib.DeltaTime;
((Closure)script.Globals["update"]).Call();
foreach(var obj in objects)
{
((Closure)obj["update"]).Call(obj);
var sprite = obj["sprite"] as Sprite;
if(sprite != null)
{
sprite.position = GetProperty<Vector2>("position", obj);
sprite.rotation = (float)GetProperty<double>("rotation", obj);
BBQLib.BBQLib.Draw(sprite as Sprite);
}
}
}
public static void LoadLua()
{
script.Options.ScriptLoader = new FileSystemScriptLoader()
{
ModulePaths = new string[]
{
DirectoryConsts.scriptDirectory + "?.lua"
}
};
LuaUtils.LoadGlobalFunctions(script, new LuaObject());
script.DoFile(DirectoryConsts.scriptDirectory + "load.lua");
((Closure)script.Globals["start"]).Call();
}
}
}