Skip to content

Commit ced23bc

Browse files
committed
LuaDLight
1 parent cc87d59 commit ced23bc

2 files changed

Lines changed: 129 additions & 1 deletion

File tree

‎Game.Client/GarrysMod/LuaDLight.cs‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using Source;
2+
using Source.Common;
3+
using Source.Common.GarrysMod.Lua;
4+
5+
namespace Game.Client.GarrysMod;
6+
7+
public static partial class LuaDLight
8+
{
9+
[LuaClass(typeof(DLight), NullError = "Tried to use a NULL dlight_t!")]
10+
public static readonly LuaClass LC_dlight_t = new("dlight_t", LuaType.DLight, null, null);
11+
12+
[LuaGlobal]
13+
static int DynamicLight(ILuaInterface lua) {
14+
int key = (int)lua.CheckNumber(1);
15+
DLight? light;
16+
if (lua.GetType(2) != LuaType.Nil && lua.GetBool(2))
17+
light = effects.AllocElight(key);
18+
else
19+
light = effects.AllocDlight(key);
20+
if (light == null)
21+
return 0;
22+
LC_dlight_t.Push(light);
23+
return 1;
24+
}
25+
26+
[LuaMethod]
27+
static int dlight_t____tostring(ILuaInterface lua) {
28+
DLight? light = (DLight?)LC_dlight_t.Get(1);
29+
if (light == null) {
30+
lua.Error("Tried to use a NULL dlight_t!");
31+
return 0;
32+
}
33+
lua.PushString($"dlight_t [{light.Key}]");
34+
return 1;
35+
}
36+
37+
[LuaMethod]
38+
static int dlight_t____newindex(ILuaInterface lua) {
39+
DLight? light = (DLight?)LC_dlight_t.Get(1);
40+
if (light == null) {
41+
lua.Error("Tried to use a NULL dlight_t!");
42+
return 0;
43+
}
44+
45+
string key = lua.CheckString(2);
46+
if (key.Length == 0)
47+
return 0;
48+
49+
if (stricmp(key, "pos") == 0 || stricmp(key, "position") == 0) {
50+
light.Origin = LuaVector.Get_Vector(3);
51+
return 0;
52+
}
53+
if (stricmp(key, "noworld") == 0) {
54+
if (lua.GetBool(3))
55+
light.Flags |= DLightFlags.NoWorldIllumination;
56+
else
57+
light.Flags &= ~DLightFlags.NoWorldIllumination;
58+
return 0;
59+
}
60+
if (stricmp(key, "nomodel") == 0) {
61+
if (lua.GetBool(3))
62+
light.Flags |= DLightFlags.NoModelIllumination;
63+
else
64+
light.Flags &= ~DLightFlags.NoModelIllumination;
65+
return 0;
66+
}
67+
if (stricmp(key, "dir") == 0 || stricmp(key, "direction") == 0) {
68+
light.Direction = LuaVector.Get_Vector(3);
69+
return 0;
70+
}
71+
if (stricmp(key, "radius") == 0 || stricmp(key, "size") == 0) {
72+
float radius = (float)lua.GetNumber(3);
73+
if (radius <= 0.0f)
74+
radius = 0.0f;
75+
if (2048.0f <= radius)
76+
radius = 2048.0f;
77+
light.Radius = radius;
78+
return 0;
79+
}
80+
if (stricmp(key, "die") == 0 || stricmp(key, "dietime") == 0) {
81+
light.Die = lua.GetNumber(3);
82+
return 0;
83+
}
84+
if (stricmp(key, "decay") == 0 || stricmp(key, "fade") == 0) {
85+
light.Decay = (float)lua.GetNumber(3);
86+
return 0;
87+
}
88+
if (stricmp(key, "minlight") == 0) {
89+
light.MinLight = (float)lua.GetNumber(3);
90+
return 0;
91+
}
92+
if (stricmp(key, "style") == 0) {
93+
light.Style = (int)lua.GetNumber(3);
94+
return 0;
95+
}
96+
if (stricmp(key, "key") == 0) {
97+
light.Key = (int)lua.GetNumber(3);
98+
return 0;
99+
}
100+
if (stricmp(key, "InnerAngle") == 0) {
101+
light.InnerAngle = (float)lua.GetNumber(3);
102+
return 0;
103+
}
104+
if (stricmp(key, "OuterAngle") == 0) {
105+
light.OuterAngle = (float)lua.GetNumber(3);
106+
return 0;
107+
}
108+
if (stricmp(key, "r") == 0) {
109+
light.Color.R = (byte)(int)lua.GetNumber(3);
110+
return 0;
111+
}
112+
if (stricmp(key, "g") == 0) {
113+
light.Color.G = (byte)(int)lua.GetNumber(3);
114+
return 0;
115+
}
116+
if (stricmp(key, "b") == 0) {
117+
light.Color.B = (byte)(int)lua.GetNumber(3);
118+
return 0;
119+
}
120+
if (stricmp(key, "Brightness") == 0) {
121+
light.Color.Exponent = (sbyte)(int)lua.GetNumber(3);
122+
return 0;
123+
}
124+
125+
lua.ErrorNoHalt($"Tried to set wrong key '{key}' on Dynamic Light object.\n Valid keys: Pos, Dir, Size, DieTime, Brightness, Decay, MinLight, Style, Key, InnerAngle, OuterAngle, r, g, b, nomodel, noworld.\n");
126+
return 0;
127+
}
128+
}

‎Source.Common/DLight.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class DLight
1818
public Vector3 Origin;
1919
public float Radius;
2020
public ColorRGBExp32 Color;
21-
public float Die;
21+
public TimeUnit_t Die;
2222
public float Decay;
2323
public float MinLight;
2424
public int Key;

0 commit comments

Comments
 (0)