-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathentity.lua
342 lines (280 loc) · 10.6 KB
/
entity.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
--the entity library
--hold the library
entity = {}
--store all the entities
entity_table = {}
entity_count = 0
--create entities
function entity.create_entity(type,sizex,sizey,texture,chunkx,chunky,posx,posy,inertiax,inertiay,item,timer)
entity_count = entity_count + 1
entity_table[entity_count] = {}
entity_table[entity_count]["sizex"] = sizex
entity_table[entity_count]["sizey"] = sizey
entity_table[entity_count]["chunkx"] = chunkx
entity_table[entity_count]["chunky"] = chunky
entity_table[entity_count]["posx"] = posx
entity_table[entity_count]["posy"] = posy
entity_table[entity_count]["inertiax"] = inertiax
entity_table[entity_count]["inertiay"] = inertiay
entity_table[entity_count]["on_block"] = false
entity_table[entity_count]["entity_in_unloaded_chunk"] = false
entity_table[entity_count]["item"] = item
--allow for dropping items
if timer then
entity_table[entity_count]["timer"] = timer
else
entity_table[entity_count]["timer"] = 0
end
if item then
entity_table[entity_count]["texture"] = texture_table[item]
entity_table[entity_count]["magnetized"] = false
else
entity_table[entity_count]["texture"] = texture
end
--print(entity_count)
end
--draw entities
function entity.render_entity()
if table.getn(entity_table) > 0 then
--print(table.getn(entity_table))
for i = 1,table.getn(entity_table) do
--print(i,entity_table[i])
if entity_table[i] then
local drawx = (player_drawnx+(entity_table[i]["posx"]*scale)+(map_max*scale*(entity_table[i]["chunkx"]-chunkx))-(player.playerx*scale))-((entity_table[i]["sizex"]/2)*scale)
local drawy = (player_drawny+(entity_table[i]["posy"]*scale)+(map_max*scale*(chunky-entity_table[i]["chunky"]))-(player.playery*scale))-((entity_table[i]["sizey"]/2)*scale)
love.graphics.draw(entity_table[i]["texture"], drawx,drawy,0, scale*entity_table[i]["sizex"]/21, scale*entity_table[i]["sizey"]/21)--,scale*(entity_table[i]["sizex"]),scale*(entity_table[i]["sizey"]))
--love.graphics.circle( "fill", drawx+((entity_table[i]["sizex"]/2)*scale), drawy+((entity_table[i]["sizey"]/2)*scale), 3 )
end
end
end
end
--entity gravity
function entity.gravity()
if table.getn(entity_table) > 0 then
for i = 1,table.getn(entity_table) do
if entity_table[i] then
if entity_table[i]["magnetized"] == false then
if entity_table[i]["entity_in_unloaded_chunk"] == false then
--print(entity_table[i]["on_block"])
if entity_table[i]["on_block"] == true then
entity_table[i]["inertiay"] = 0
--lastheight = math.floor(((map_max-player.playery)+(chunky*map_max)))
--print(lastheight)
--player.on_block = false
else
if entity_table[i]["inertiay"] < 0.3 then
entity_table[i]["inertiay"] = entity_table[i]["inertiay"] + 0.01
--print(entity_table[i]["inertiay"])
end
end
else
--print("entity "..i.." in unloaded chunk")
entity_table[i]["inertiax"] = 0
entity_table[i]["inertiay"] = 0
end
end
end
end
end
end
--applying the entity physics
function entity.physics_apply(dt)
--local oldposx,oldposy = player.playerx,player.playery
if table.getn(entity_table) > 0 then
for i = 1,table.getn(entity_table) do
if entity_table[i] then
entity.collision(i,entity_table[i].posx,entity_table[i].posy)
entity.new_chunk(i,entity_table[i].posx,entity_table[i].posy)
entity_table[i]["timer"] = entity_table[i]["timer"] + dt
--collisionx(oldposx)
--print(entity_table[i].inertiax)
if math.abs(entity_table[i].inertiax) <= 0.005 then
entity_table[i].inertiax = 0
elseif entity_table[i].inertiax < 0 then
entity_table[i].inertiax = entity_table[i].inertiax + 0.005
elseif entity_table[i].inertiax > 0 then
entity_table[i].inertiax = entity_table[i].inertiax - 0.005
else
entity_table[i].inertiax = entity_table[i].inertiax
end
entity.item_magnet(i)
end
end
end
end
--
function entity.collision(i,oldposx,oldposy)
entity_table[i]["entity_in_unloaded_chunk"] = false
local xer = {-entity_table[i]["sizex"]/4,entity_table[i]["sizex"]/4}
local yer = {-entity_table[i]["sizey"]/4,entity_table[i]["sizey"]/4}
local fall = true
--check the corners (y)
entity_table[i]["posy"] = entity_table[i]["posy"] + entity_table[i]["inertiay"]
for q = 1,2 do
for r = 1,2 do
local squarex = math.floor(entity_table[i]["posx"]+xer[q])
local squarey = math.floor(entity_table[i]["posy"]+yer[r])
--use this to detect outside chunk 00
local chunkerx = 0
local chunkery = 0
if squarex < 1 then
chunkerx = -1
squarex = map_max
--print("adjusting x -1")
elseif squarex > map_max then
chunkerx = 1
squarex = 1
--print("adjusting x +1")
end
if squarey < 1 then
chunkery = 1
squarey = map_max
--print("adjusting y +1")
elseif squarey > map_max then
chunkery = -1
squarey = 1
--print("adjusting y -1")
end
--print(chunkerx, chunkery, "|", squarex,squarey)
--print( loaded_chunks[chunkerx][chunkery][squarex][squarey]["block"])
--if (squarex1 > map_max or squarex1 <= 0) or (squarey1 > map_max or squarey1 <= 0) or blocks[loaded_chunks[0][0][squarex1][squarey1]["block"]]["collide"] ~= false then
local entity_chunkx = entity_table[i]["chunkx"]
local entity_chunky = entity_table[i]["chunky"]
if loaded_chunks[entity_chunkx+chunkerx] and loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky] and loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky][squarex] and loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky][squarex][squarey] then
if blocks[loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky][squarex][squarey]["block"]]["collide"] ~= false then
---print("PUTTING BACK TO OLD")
entity_table[i]["posy"] = oldposy
if r == 2 then
entity_table[i]["on_block"] = true
--print("IT'S ON THE BLOCK")
fall = false
end
if r == 1 then
entity_table[i]["on_block"] = false
end
end
else
entity_table[i]["entity_in_unloaded_chunk"] = true
end
end
end
if fall == true then
entity_table[i]["on_block"] = false
end
--check the corners(x)
entity_table[i]["posx"] = entity_table[i]["posx"] + entity_table[i]["inertiax"]
for q = 1,2 do
for r = 1,3 do
local squarex = math.floor(entity_table[i]["posx"]+xer[q])
local squarey
if r < 3 then
squarey = math.floor(entity_table[i]["posy"]+yer[r])
else
squarey = math.floor(entity_table[i]["posy"])
end
--print(squarex)
--use this to detect outside chunk 00
local chunkerx = 0
local chunkery = 0
if squarex < 1 then
chunkerx = -1
squarex = map_max
--print("adjusting x -1")
elseif squarex > map_max then
chunkerx = 1
squarex = 1
--print("adjusting x +1")
end
if squarey < 1 then
chunkery = 1
squarey = map_max
--print("adjusting y +1")
elseif squarey > map_max then
chunkery = -1
squarey = 1
--print("adjusting y -1")
end
local entity_chunkx = entity_table[i]["chunkx"]
local entity_chunky = entity_table[i]["chunky"]
--if (squarex1 > map_max or squarex1 <= 0) or (squarey1 > map_max or squarey1 <= 0) or blocks[loaded_chunks[0][0][squarex1][squarey1]["block"]]["collide"] ~= false then
if loaded_chunks[entity_chunkx+chunkerx] and loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky] and loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky][squarex] and loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky][squarex][squarey] then
if blocks[loaded_chunks[entity_chunkx+chunkerx][chunkery+entity_chunky][squarex][squarey]["block"]]["collide"] ~= false then
entity_table[i]["inertiax"] = 0
entity_table[i]["posx"] = oldposx
--print("stopping x inertia and pos")
end
else
entity_table[i]["entity_in_unloaded_chunk"] = true
end
end
end
end
--move entity into new chunk
function entity.new_chunk(i,posx,posy)
--print(math.floor(player.playerx))
if math.floor(posx) < 1 then
entity_table[i]["chunkx"] = entity_table[i]["chunkx"] - 1
entity_table[i]["posx"] = entity_table[i]["posx"] + map_max -- put entity on other side of screen
--print(" block x -1")
return false
elseif math.floor(posx) > map_max then
entity_table[i]["chunkx"] = entity_table[i]["chunkx"] + 1
entity_table[i]["posx"] = entity_table[i]["posx"] - map_max -- put entity on other side of screen
--print("block x +1")
return false
elseif math.floor(posy) < 1 then
entity_table[i]["chunky"] = entity_table[i]["chunky"] + 1
entity_table[i]["posy"] = entity_table[i]["posy"] + map_max -- put entity on other side of screen
--print(" block y -1")
return false
elseif math.floor(posy) > map_max then
entity_table[i]["chunky"] = entity_table[i]["chunky"] - 1
entity_table[i]["posy"] = entity_table[i]["posy"] - map_max -- put entity on other side of screen
--print("block y +1")
return false
end
return true
end
--magnetize items towards player
--taken from sfan5's nuke mod in Minetest - https://forum.minetest.net/viewtopic.php?id=638
function entity.item_magnet(i)
local x = player.playerx - entity_table[i]["posx"]
local y = player.playery - entity_table[i]["posy"]
--adjust for chunk border
if chunkx ~= entity_table[i]["chunkx"] then
x = ((entity_table[i]["chunkx"]-chunkx)*map_max-x)*-1
end
if chunky ~= entity_table[i]["chunky"] then
y = ((chunky-entity_table[i]["chunky"])*map_max-y)*-1
end
local calc1 = x*x+y*y --optimize
entity_table[i]["magnetized"] = false
--item magnet
if entity_table[i]["timer"] > time_before_add and calc1 <= magnet_radius * magnet_radius + magnet_radius then
local normalx,normaly,length = math.normalize(x,y)
normalx,normaly = normalx*0.05,normaly*0.05
entity_table[i]["inertiax"] = entity_table[i]["inertiax"] + normalx
entity_table[i]["inertiay"] = entity_table[i]["inertiay"] + normaly
entity_table[i]["magnetized"] = true
--limit the inertia
if entity_table[i]["inertiax"] > 0.5 then
entity_table[i]["inertiax"] = 0.5
elseif entity_table[i]["inertiax"] < -0.5 then
entity_table[i]["inertiax"] = -0.5
end
if entity_table[i]["inertiay"] > 0.5 then
entity_table[i]["inertiay"] = 0.5
elseif entity_table[i]["inertiay"] < -0.5 then
entity_table[i]["inertiay"] = -0.5
end
end
--inventory magnet add
if entity_table[i]["timer"] > time_before_add and calc1 <= add_inventory_radius * add_inventory_radius + add_inventory_radius then
inventory_add(entity_table[i]["item"])
--entity_table[i] = nil
table.remove(entity_table,i)
entity_count = entity_count - 1
--print("add to inventory")
sound_play(item_magnet_pickup,80,120)
end
end