-
Notifications
You must be signed in to change notification settings - Fork 4
/
factory_lua.go
85 lines (62 loc) · 1.62 KB
/
factory_lua.go
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
package size
import (
"github.com/gravestench/akara"
lua "github.com/yuin/gopher-lua"
)
const (
componentName = "size"
)
func (concrete *ComponentFactory) ExportToLua(state *lua.LState, table *lua.LTable) *lua.LTable {
factoryTable := state.NewTypeMetatable(componentName)
state.SetField(factoryTable, "add", concrete.luaAdd(state))
state.SetField(factoryTable, "get", concrete.luaGet(state))
state.SetField(factoryTable, "remove", concrete.luaRemove(state))
state.SetField(table, componentName, factoryTable)
return table
}
func (concrete *ComponentFactory) luaAdd(state *lua.LState) *lua.LFunction {
fn := func(L *lua.LState) int {
if L.GetTop() != 1 {
return 0
}
e := akara.EID(state.CheckNumber(1))
size := concrete.Add(e)
L.Push(size.ExportToLua(state, state.NewTable()))
return 1
}
return state.NewFunction(fn)
}
func (concrete *ComponentFactory) luaGet(state *lua.LState) *lua.LFunction {
fn := func(L *lua.LState) int {
if L.GetTop() != 1 {
return 0
}
id := L.CheckNumber(1)
size, found := concrete.Get(akara.EID(id))
truthy := lua.LFalse
if !found {
L.Push(lua.LNil)
L.Push(truthy)
return 2
} else {
truthy = lua.LTrue
}
table := size.ExportToLua(state, state.NewTable())
L.SetMetatable(table, L.GetTypeMetatable(componentName))
L.Push(table)
L.Push(truthy)
return 2
}
return state.NewFunction(fn)
}
func (concrete *ComponentFactory) luaRemove(state *lua.LState) *lua.LFunction {
fn := func(L *lua.LState) int {
if L.GetTop() != 1 {
return 0
}
e := akara.EID(state.CheckNumber(1))
concrete.Remove(e)
return 0
}
return state.NewFunction(fn)
}