-
Notifications
You must be signed in to change notification settings - Fork 4
/
particles.lua
336 lines (273 loc) · 10.9 KB
/
particles.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
--the particles library
--hold the library
particle = {}
--store all the entities
particle_table = {}
particle_count = 0
--create entities
function particle.create_particle(type,amount,sizex,sizey,texture,chunkx,chunky,posx,posy,min_velx,max_velx,min_vely,max_vely,item,max_life)
for i = 1,amount do
particle_count = particle_count + 1
particle_table[particle_count] = {}
particle_table[particle_count]["sizex"] = sizex
particle_table[particle_count]["sizey"] = sizey
particle_table[particle_count]["chunkx"] = chunkx
particle_table[particle_count]["chunky"] = chunky
particle_table[particle_count]["posx"] = posx
particle_table[particle_count]["posy"] = posy
particle_table[particle_count]["inertiax"] = math.random(min_velx,max_velx)/1000
particle_table[particle_count]["inertiay"] = math.random(min_vely,max_vely)/1000
particle_table[particle_count]["on_block"] = false
particle_table[particle_count]["particle_in_unloaded_chunk"] = false
particle_table[particle_count]["item"] = item
particle_table[particle_count]["timer"] = 0
particle_table[particle_count]["max_life"] = max_life
if item then
particle_table[particle_count]["texture"] = texture_table[item]
else
particle_table[particle_count]["texture"] = texture
end
end
end
--draw entities
function particle.render_particle()
if table.getn(particle_table) > 0 then
--print(table.getn(particle_table))
for i = 1,table.getn(particle_table) do
--print(i,particle_table[i])
if particle_table[i] then
local drawx = (player_drawnx+(particle_table[i]["posx"]*scale)+(map_max*scale*(particle_table[i]["chunkx"]-chunkx))-(player.playerx*scale))-((particle_table[i]["sizex"]/2)*scale)
local drawy = (player_drawny+(particle_table[i]["posy"]*scale)+(map_max*scale*(chunky-particle_table[i]["chunky"]))-(player.playery*scale))-((particle_table[i]["sizey"]/2)*scale)
love.graphics.draw(particle_table[i]["texture"], drawx,drawy,0, scale*particle_table[i]["sizex"]/21, scale*particle_table[i]["sizey"]/21)--,scale*(particle_table[i]["sizex"]),scale*(particle_table[i]["sizey"]))
--love.graphics.circle( "fill", drawx+((particle_table[i]["sizex"]/2)*scale), drawy+((particle_table[i]["sizey"]/2)*scale), 3 )
end
end
end
end
--particle gravity
function particle.gravity()
if table.getn(particle_table) > 0 then
for i = 1,table.getn(particle_table) do
if particle_table[i] then
--if particle_table[i]["magnetized"] == false then
if particle_table[i]["particle_in_unloaded_chunk"] == false then
--print(particle_table[i]["on_block"])
if particle_table[i]["on_block"] == true then
particle_table[i]["inertiay"] = 0
--lastheight = math.floor(((map_max-player.playery)+(chunky*map_max)))
--print(lastheight)
--player.on_block = false
else
if particle_table[i]["inertiay"] < 0.3 then
particle_table[i]["inertiay"] = particle_table[i]["inertiay"] + 0.01
--print(particle_table[i]["inertiay"])
end
end
else
--print("particle "..i.." in unloaded chunk")
particle_table[i]["inertiax"] = 0
particle_table[i]["inertiay"] = 0
end
--end
end
end
end
end
--applying the particle physics
function particle.physics_apply(dt)
--local oldposx,oldposy = player.playerx,player.playery
if table.getn(particle_table) > 0 then
for i = 1,table.getn(particle_table) do
if particle_table[i] then
particle.collision(i,particle_table[i].posx,particle_table[i].posy)
particle.new_chunk(i,particle_table[i].posx,particle_table[i].posy)
--collisionx(oldposx)
--print(particle_table[i].inertiax)
if math.abs(particle_table[i].inertiax) <= 0.005 then
particle_table[i].inertiax = 0
elseif particle_table[i].inertiax < 0 then
particle_table[i].inertiax = particle_table[i].inertiax + 0.005
elseif particle_table[i].inertiax > 0 then
particle_table[i].inertiax = particle_table[i].inertiax - 0.005
else
particle_table[i].inertiax = particle_table[i].inertiax
end
particle_table[i]["timer"] = particle_table[i]["timer"] + dt
if particle_table[i]["timer"] > particle_table[i]["max_life"] then
table.remove(particle_table,i)
particle_count = particle_count - 1
end
end
end
end
end
--
function particle.collision(i,oldposx,oldposy)
particle_table[i]["particle_in_unloaded_chunk"] = false
local xer = {-particle_table[i]["sizex"]/4,particle_table[i]["sizex"]/4}
local yer = {-particle_table[i]["sizey"]/4,particle_table[i]["sizey"]/4}
local fall = true
--check the corners (y)
particle_table[i]["posy"] = particle_table[i]["posy"] + particle_table[i]["inertiay"]
for q = 1,2 do
for r = 1,2 do
local squarex = math.floor(particle_table[i]["posx"]+xer[q])
local squarey = math.floor(particle_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 particle_chunkx = particle_table[i]["chunkx"]
local particle_chunky = particle_table[i]["chunky"]
if loaded_chunks[particle_chunkx+chunkerx] and loaded_chunks[particle_chunkx+chunkerx][chunkery+particle_chunky] and loaded_chunks[particle_chunkx+chunkerx][chunkery+particle_chunky][squarex] and loaded_chunks[particle_chunkx+chunkerx][chunkery+particle_chunky][squarex][squarey] then
if blocks[loaded_chunks[particle_chunkx+chunkerx][chunkery+particle_chunky][squarex][squarey]["block"]]["collide"] ~= false then
---print("PUTTING BACK TO OLD")
particle_table[i]["posy"] = oldposy
if r == 2 then
particle_table[i]["on_block"] = true
--print("IT'S ON THE BLOCK")
fall = false
end
if r == 1 then
particle_table[i]["on_block"] = false
end
end
else
particle_table[i]["particle_in_unloaded_chunk"] = true
end
end
end
if fall == true then
particle_table[i]["on_block"] = false
end
--check the corners(x)
particle_table[i]["posx"] = particle_table[i]["posx"] + particle_table[i]["inertiax"]
for q = 1,2 do
for r = 1,3 do
local squarex = math.floor(particle_table[i]["posx"]+xer[q])
local squarey
if r < 3 then
squarey = math.floor(particle_table[i]["posy"]+yer[r])
else
squarey = math.floor(particle_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 particle_chunkx = particle_table[i]["chunkx"]
local particle_chunky = particle_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[particle_chunkx+chunkerx] and loaded_chunks[particle_chunkx+chunkerx][chunkery+particle_chunky] and loaded_chunks[particle_chunkx+chunkerx][chunkery+particle_chunky][squarex] and loaded_chunks[particle_chunkx+chunkerx][chunkery+particle_chunky][squarex][squarey] then
if blocks[loaded_chunks[particle_chunkx+chunkerx][chunkery+particle_chunky][squarex][squarey]["block"]]["collide"] ~= false then
particle_table[i]["inertiax"] = 0
particle_table[i]["posx"] = oldposx
--print("stopping x inertia and pos")
end
else
particle_table[i]["particle_in_unloaded_chunk"] = true
end
end
end
end
--move particle into new chunk
function particle.new_chunk(i,posx,posy)
--print(math.floor(player.playerx))
if math.floor(posx) < 1 then
particle_table[i]["chunkx"] = particle_table[i]["chunkx"] - 1
particle_table[i]["posx"] = particle_table[i]["posx"] + map_max -- put particle on other side of screen
--print(" block x -1")
return false
elseif math.floor(posx) > map_max then
particle_table[i]["chunkx"] = particle_table[i]["chunkx"] + 1
particle_table[i]["posx"] = particle_table[i]["posx"] - map_max -- put particle on other side of screen
--print("block x +1")
return false
elseif math.floor(posy) < 1 then
particle_table[i]["chunky"] = particle_table[i]["chunky"] + 1
particle_table[i]["posy"] = particle_table[i]["posy"] + map_max -- put particle on other side of screen
--print(" block y -1")
return false
elseif math.floor(posy) > map_max then
particle_table[i]["chunky"] = particle_table[i]["chunky"] - 1
particle_table[i]["posy"] = particle_table[i]["posy"] - map_max -- put particle 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
--use this for magic
--[[
function particle.particle_magnet(i)
local x = player.playerx - particle_table[i]["posx"]
local y = player.playery - particle_table[i]["posy"]
--adjust for chunk border
if chunkx ~= particle_table[i]["chunkx"] then
x = ((particle_table[i]["chunkx"]-chunkx)*map_max-x)*-1
end
if chunky ~= particle_table[i]["chunky"] then
y = ((chunky-particle_table[i]["chunky"])*map_max-y)*-1
end
local calc1 = x*x+y*y --optimize
particle_table[i]["magnetized"] = false
--item magnet
if particle_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
particle_table[i]["inertiax"] = particle_table[i]["inertiax"] + normalx
particle_table[i]["inertiay"] = particle_table[i]["inertiay"] + normaly
particle_table[i]["magnetized"] = true
end
--inventory magnet add
if particle_table[i]["timer"] > time_before_add and calc1 <= add_inventory_radius * add_inventory_radius + add_inventory_radius then
inventory_add(particle_table[i]["item"])
--particle_table[i] = nil
table.remove(particle_table,i)
particle_count = particle_count - 1
--print("add to inventory")
item_magnet_pickup:setPitch(love.math.random(80,120)/100)
item_magnet_pickup:stop()
item_magnet_pickup:play()
end
end
]]--