-
Notifications
You must be signed in to change notification settings - Fork 1
/
Light_source.lua
228 lines (151 loc) · 4.75 KB
/
Light_source.lua
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
---- settings
--cofigure os IDS na sua escolha/configure the IDS of your choice
Block_id = 2001 --bloco de luz/light block
Item_id = 11055 -- id do item / item id
Battery_level = 60 -- 1 minuto -tempo em segundos/1 minute -time in seconds
Uses_battery = true -- usa bateria ou não /use battery or not true/false
--- Code by PlayCraft uid 869061
-- report [email protected]
-- version 0.1
Players_in_game = {}
-----------Light_source
Light_source = {
position_x = 0,
position_y = 0,
position_z = 0,
block_id = 0,
item_id = 0,
battery_level = 0,
uses_battery = false,
without_battery = false,
active = false
}
function Light_source:new(table)
table = table or {}
setmetatable(table, self)
self.__index = self
return table
end
function Light_source:remove_block()
local result, id = Block:getBlockID(self.position_x, self.position_y, self.position_z)
if id == self.block_id then
Block:destroyBlock(self.position_x, self.position_y, self.position_z,false)
end
end
function Light_source:palce_block(x, y, z)
local air_block_id = Block:isSolidBlock(x,y,z)
if self.active and air_block_id == 1001 and not self.without_battery then
self.position_x = x
self.position_y = y
self.position_z = z
Block:placeBlock(self.block_id, x, y, z, 0)
end
end
function Light_source:update(x,y,z)
self:remove_block()
self:palce_block(x ,y ,z)
end
function Light_source:discharge_battery()
if self.uses_battery and self.active and not self.without_battery then
self.battery_level = self.battery_level - 1
if self.battery_level <= 0 then
self.without_battery = true
self:remove_block()
end
end
end
function Light_source:charge_battery(battery)
if self.uses_battery then
self.battery_level = battery
end
end
function Light_source:set_state(state)
self.active = state
end
-------------------Player
Player = {
uid = 0,
Ligth = Light_source:new({
position_x = 0,
position_y = 0,
position_z = 0,
block_id = 0,
item_id = 0,
battery_level = 0,
uses_battery = false,
without_battery = false,
active = false
})
}
function Player:new(table)
table = table or {}
setmetatable(table, self)
self.__index = self
return table
end
---------- Game Events
function login_player(e)
local player_uid = e.eventobjid
local result,id=Player:getCurToolID(player_uid)
Players_in_game[#Players_in_game + 1] = Player:new({
uid = player_uid,
Ligth = Light_source:new({
position_x = 0,
position_y = 0,
position_z = 0,
block_id = Block_id,
item_id = Item_id,
battery_level = Battery_level,
uses_battery = Uses_battery,
without_battery = false,
active = false
})
})
if id == Item_id then
local result,x ,y ,z = Actor:getPosition(player_uid)
Players_in_game[#Players_in_game].Ligth:set_state(true)
Players_in_game[#Players_in_game].Ligth:update(x,y,z)
end
end
ScriptSupportEvent:registerEvent([=[Game.AnyPlayer.EnterGame]=],login_player)
function get_player(uid)
for index = 1 ,#Players_in_game do
if Players_in_game[index].uid == uid then
return Players_in_game[index]
end
end
return nil
end
function item_selectShortcut(e)
local player_uid = e.eventobjid
local item = e.itemid
local result,x ,y ,z = Actor:getPosition(player_uid)
local player = get_player(player_uid)
player.Ligth:set_state(player.Ligth.item_id == item)
player.Ligth:update(x,y,z)
end
ScriptSupportEvent:registerEvent([=[Player.SelectShortcut]=],item_selectShortcut)
function player_move(e)
local player_uid = e.eventobjid
local player = get_player(player_uid)
local result,x,y,z=Actor:getPosition(player_uid)
player.Ligth:update(x,y,z)
end
ScriptSupportEvent:registerEvent([=[Player.MoveOneBlockSize]=],player_move)
function player_drop_item(e)
local player_uid = e.eventobjid
local player = get_player(player_uid)
if e.itemid == player.Ligth.item_id and player.Ligth.active then
player.Ligth:remove_block()
player.Ligth.active = false
end
end
ScriptSupportEvent:registerEvent([=[Player.DiscardItem]=],player_drop_item)
function update_battery(e)
if type(e.second) == "number" then
for index = 1 ,#Players_in_game do
Players_in_game[index].Ligth:discharge_battery()
end
end
end
ScriptSupportEvent:registerEvent([=[Game.RunTime]=],update_battery)