-
Notifications
You must be signed in to change notification settings - Fork 17
/
portalprojectile.lua
168 lines (126 loc) · 4.7 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
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
portalprojectile = class("portalprojectile")
function portalprojectile:init(x, y, tx, ty, color, hit, payload, mirror, mirrored, bounces)
self.x = x
self.y = y
self.startx = x
self.starty = y
self.endx = tx
self.endy = ty
self.bounces = bounces or portalbouncethreshold
self.color = color
self.hit = hit
self.payload = payload
self.payloaddelivered = false
self.mirror = mirror
self.sinestart = math.random(math.pi*10)/10
if mirrored then
self.timer = 0
else
self.timer = 0.005
end
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
self:createportal()
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-yscroll-0.5)*16*scale), 0, scale, scale, 6, 6)
end
end
portalprojectileparticle = class("portalprojectileparticle")
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*1.2
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-yscroll-.5)*16*scale), 0, scale, scale, 2, 2)
end
function portalprojectile:createportal()
if self.mirror then
local portal, i, cox, coy, side, tendency, x, y = unpack(self.payload)
portaldelay[portal.number] = 0
local angle
if side == "up" or side == "down" then
angle = -math.atan2(self.endx-self.startx, self.endy-self.starty)
else
angle = math.atan2(self.endx-self.startx, self.starty-self.endy)
end
if self.bounces > 0 then
shootportal(portal.number, i, self.endx, self.endy, angle, true, self.bounces-1)
end
else
portal.createportal(unpack(self.payload))
end
end