forked from Stabyourself/mari0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portalprojectile.lua
142 lines (103 loc) · 3.78 KB
/
portalprojectile.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
portalprojectile = class:new()
function portalprojectile:init(x, y, tx, ty, color, hit, payload)
self.x = x
self.y = y
self.startx = x
self.starty = y
self.endx = tx
self.endy = ty
self.color = color
self.hit = hit
self.payload = payload
self.payloaddelivered = false
self.sinestart = math.random(math.pi*10)/10
self.timer = 0.005
self.length = math.sqrt((tx-x)^2 + (ty-y)^2)
self.time = self.length/portalprojectilespeed
self.angle = math.atan2(tx-x, ty-y)
self.particles = {}
self.lastparticle = math.floor(self.timer/portalprojectileparticledelay)
end
function portalprojectile:update(dt)
self.timer = self.timer + dt
if self.timer < self.time then
self.x = self.startx + (self.endx-self.startx)*(self.timer/self.time)
self.y = self.starty + (self.endy-self.starty)*(self.timer/self.time)
local currentparticle = math.floor(self.timer/portalprojectileparticledelay)
for i = self.lastparticle+1, currentparticle do
local t = i*portalprojectileparticledelay
local tx = self.startx + (self.endx-self.startx)*(t/self.time)
local ty = self.starty + (self.endy-self.starty)*(t/self.time)
--INNER LINE
local r, g, b = unpack(self.color)
table.insert(self.particles, portalprojectileparticle:new(tx, ty, {r, g, b}))
--OUTER LINES
local r, g, b = unpack(self.color)
r, g, b = r/2, g/2, b/2
--Sine
local x = tx + math.sin(self.angle+math.pi/2)*(math.sin(t*portalprojectilesinemul+self.sinestart))*portalprojectilesinesize
local y = ty + math.cos(self.angle+math.pi/2)*(math.sin(t*portalprojectilesinemul+self.sinestart))*portalprojectilesinesize
table.insert(self.particles, portalprojectileparticle:new(x, y, {r, g, b}))
--Sine
local x = tx + math.sin(self.angle-math.pi/2)*(math.sin(t*portalprojectilesinemul+self.sinestart))*portalprojectilesinesize
local y = ty + math.cos(self.angle-math.pi/2)*(math.sin(t*portalprojectilesinemul+self.sinestart))*portalprojectilesinesize
table.insert(self.particles, portalprojectileparticle:new(x, y, {r, g, b}))
end
self.lastparticle = currentparticle
end
--Update particles
local delete = {}
for i, v in pairs(self.particles) do
if v:update(dt) == true then
table.insert(delete, i)
end
end
table.sort(delete, function(a,b) return a>b end)
for i, v in pairs(delete) do
table.remove(self.particles, v)
end
if (self.timer >= self.time and self.timer-dt < self.time) or (self.time <= 0.005 and self.payloaddelivered == false) then
createportal(unpack(self.payload))
self.payloaddelivered = true
end
if self.timer >= self.time + portalprojectiledelay then
return true
end
return false
end
function portalprojectile:draw()
for i, v in pairs(self.particles) do
v:draw()
end
if self.timer < self.time then
love.graphics.setColor(unpack(self.color))
love.graphics.draw(portalprojectileimg, math.floor((self.x-xscroll)*16*scale), math.floor((self.y-0.5)*16*scale), 0, scale, scale, 3, 3)
end
end
portalprojectileparticle = class:new()
function portalprojectileparticle:init(x, y, color, r, g, b)
self.x = x
self.y = y
self.color = color
self.speedx = math.random(-10, 10)/70
self.speedy = math.random(-10, 10)/70
self.alpha = 0.6
self.timer = 0
end
function portalprojectileparticle:update(dt)
self.timer = self.timer + dt
self.speedx = self.speedx + math.random(-10, 10)/70
self.speedy = self.speedy + math.random(-10, 10)/70
self.x = self.x + self.speedx*dt
self.y = self.y + self.speedy*dt
self.alpha = self.alpha - dt*(300/255)
if self.alpha < 0 then
self.alpha = 0
return true
end
end
function portalprojectileparticle:draw()
local r, g, b = unpack(self.color)
love.graphics.setColor(r, g, b, self.alpha)
love.graphics.draw(portalprojectileparticleimg, math.floor((self.x-xscroll)*16*scale), math.floor((self.y-.5)*16*scale), 0, scale, scale, 0.5, 0.5)
end