-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua.go
146 lines (120 loc) · 4.05 KB
/
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
lua "github.com/yuin/gopher-lua"
)
// LuaFunction is a struct that stores basic info about a function in a Lua file
type LuaFunction struct {
name string // Basically, the filename
numReturns int // Number of things the function returns
isFinished bool // Is the function done running?
}
func createLuaFunction(name string, numReturns int) LuaFunction {
isFinished := false
return LuaFunction{name, numReturns, isFinished}
}
// THE ITEM API:
// ItemAPI is the API for Lua-written items in the game
type ItemAPI struct {
setPlayerHealth lua.LGFunction // Player health
playerHealth lua.LGFunction
setPlayerEnergy lua.LGFunction // Player energy
playerEnergy lua.LGFunction
setPlayerWalkSpeed lua.LGFunction // Player walkspeed
playerWalkSpeed lua.LGFunction
setPlayerRunSpeed lua.LGFunction // Player runspeed
playerRunSpeed lua.LGFunction
setGunFireSpeed lua.LGFunction // Player gun firespeed
gunFireSpeed lua.LGFunction
setAccuracy lua.LGFunction // Player accuracy
accuracy lua.LGFunction
setGunRange lua.LGFunction // Player gun fire range
gunRange lua.LGFunction
}
var itemAPI ItemAPI = ItemAPI{
setPlayerHealth: func(L *lua.LState) int {
lv := L.ToInt(1) // Get argument
gameReference.player.health = lv // Set it to player's health
return 0 // # of returns/results
},
playerHealth: func(L *lua.LState) int {
lv := lua.LNumber(gameReference.player.health) // Get health
L.Push(lv) // Push result
return 1 // # of returns/results
},
setPlayerEnergy: func(L *lua.LState) int {
lv := L.ToInt(1)
gameReference.player.energy = lv
return 0
},
playerEnergy: func(L *lua.LState) int {
lv := lua.LNumber(gameReference.player.energy)
L.Push(lv)
return 1
},
setPlayerWalkSpeed: func(L *lua.LState) int {
lv := L.ToInt(1)
gameReference.player.walkSpeed = float64(lv)
return 0
},
playerWalkSpeed: func(L *lua.LState) int {
lv := lua.LNumber(gameReference.player.walkSpeed)
L.Push(lv)
return 1
},
setPlayerRunSpeed: func(L *lua.LState) int {
lv := L.ToInt(1)
gameReference.player.runSpeed = float64(lv)
return 0
},
playerRunSpeed: func(L *lua.LState) int {
lv := lua.LNumber(gameReference.player.runSpeed)
L.Push(lv)
return 1
},
setGunFireSpeed: func(L *lua.LState) int {
lv := L.ToInt(1)
gameReference.player.gun.fireSpeed = lv
return 0
},
gunFireSpeed: func(L *lua.LState) int {
lv := lua.LNumber(gameReference.player.gun.fireSpeed)
L.Push(lv)
return 1
},
setAccuracy: func(L *lua.LState) int {
lv := L.ToInt(1)
gameReference.player.accuracy = lv
return 0
},
accuracy: func(L *lua.LState) int {
lv := lua.LNumber(gameReference.player.accuracy)
L.Push(lv)
return 1
},
setGunRange: func(L *lua.LState) int {
lv := L.ToInt(1)
gameReference.player.gunRange = float64(lv)
return 0
},
gunRange: func(L *lua.LState) int {
lv := lua.LNumber(gameReference.player.gunRange)
L.Push(lv)
return 1
},
}
func initLuaFunctions(L *lua.LState) {
L.SetGlobal("SetPlayerHealth", L.NewFunction(itemAPI.setPlayerHealth))
L.SetGlobal("PlayerHealth", L.NewFunction(itemAPI.playerHealth))
L.SetGlobal("SetPlayerEnergy", L.NewFunction(itemAPI.setPlayerEnergy))
L.SetGlobal("PlayerEnergy", L.NewFunction(itemAPI.playerEnergy))
L.SetGlobal("SetPlayerWalkSpeed", L.NewFunction(itemAPI.setPlayerWalkSpeed))
L.SetGlobal("PlayerWalkSpeed", L.NewFunction(itemAPI.playerWalkSpeed))
L.SetGlobal("SetPlayerRunSpeed", L.NewFunction(itemAPI.setPlayerRunSpeed))
L.SetGlobal("PlayerRunSpeed", L.NewFunction(itemAPI.playerRunSpeed))
L.SetGlobal("SetGunFireSpeed", L.NewFunction(itemAPI.setGunFireSpeed))
L.SetGlobal("GunFireSpeed", L.NewFunction(itemAPI.gunFireSpeed))
L.SetGlobal("SetAccuracy", L.NewFunction(itemAPI.setAccuracy))
L.SetGlobal("Accuracy", L.NewFunction(itemAPI.accuracy))
L.SetGlobal("SetGunRange", L.NewFunction(itemAPI.setGunRange))
L.SetGlobal("GunRange", L.NewFunction(itemAPI.gunRange))
}