1portalprojectile = class:new() 2 3function portalprojectile:init(x, y, tx, ty, color, hit, payload) 4 self.x = x 5 self.y = y 6 7 self.startx = x 8 self.starty = y 9 self.endx = tx 10 self.endy = ty 11 12 self.color = color 13 self.hit = hit 14 15 self.payload = payload 16 self.payloaddelivered = false 17 18 self.sinestart = math.random(math.pi*10)/10 19 self.timer = 0.005 20 self.length = math.sqrt((tx-x)^2 + (ty-y)^2) 21 self.time = self.length/portalprojectilespeed 22 self.angle = math.atan2(tx-x, ty-y) 23 24 self.particles = {} 25 self.lastparticle = math.floor(self.timer/portalprojectileparticledelay) 26end 27 28function portalprojectile:update(dt) 29 self.timer = self.timer + dt 30 31 if self.timer < self.time then 32 self.x = self.startx + (self.endx-self.startx)*(self.timer/self.time) 33 self.y = self.starty + (self.endy-self.starty)*(self.timer/self.time) 34 35 local currentparticle = math.floor(self.timer/portalprojectileparticledelay) 36 37 for i = self.lastparticle+1, currentparticle do 38 local t = i*portalprojectileparticledelay 39 local tx = self.startx + (self.endx-self.startx)*(t/self.time) 40 local ty = self.starty + (self.endy-self.starty)*(t/self.time) 41 42 --INNER LINE 43 local r, g, b = unpack(self.color) 44 table.insert(self.particles, portalprojectileparticle:new(tx, ty, {r, g, b})) 45 46 --OUTER LINES 47 local r, g, b = unpack(self.color) 48 r, g, b = r/2, g/2, b/2 49 50 --Sine 51 local x = tx + math.sin(self.angle+math.pi/2)*(math.sin(t*portalprojectilesinemul+self.sinestart))*portalprojectilesinesize 52 local y = ty + math.cos(self.angle+math.pi/2)*(math.sin(t*portalprojectilesinemul+self.sinestart))*portalprojectilesinesize 53 54 table.insert(self.particles, portalprojectileparticle:new(x, y, {r, g, b})) 55 56 --Sine 57 local x = tx + math.sin(self.angle-math.pi/2)*(math.sin(t*portalprojectilesinemul+self.sinestart))*portalprojectilesinesize 58 local y = ty + math.cos(self.angle-math.pi/2)*(math.sin(t*portalprojectilesinemul+self.sinestart))*portalprojectilesinesize 59 60 table.insert(self.particles, portalprojectileparticle:new(x, y, {r, g, b})) 61 end 62 63 self.lastparticle = currentparticle 64 end 65 66 --Update particles 67 local delete = {} 68 69 for i, v in pairs(self.particles) do 70 if v:update(dt) == true then 71 table.insert(delete, i) 72 end 73 end 74 75 table.sort(delete, function(a,b) return a>b end) 76 77 for i, v in pairs(delete) do 78 table.remove(self.particles, v) 79 end 80 81 if (self.timer >= self.time and self.timer-dt < self.time) or (self.time <= 0.005 and self.payloaddelivered == false) then 82 createportal(unpack(self.payload)) 83 self.payloaddelivered = true 84 end 85 86 if self.timer >= self.time + portalprojectiledelay then 87 return true 88 end 89 90 return false 91end 92 93function portalprojectile:draw() 94 for i, v in pairs(self.particles) do 95 v:draw() 96 end 97 98 if self.timer < self.time then 99 love.graphics.setColor(unpack(self.color)) 100 101 love.graphics.draw(portalprojectileimg, math.floor((self.x-xscroll)*16*scale), math.floor((self.y-0.5)*16*scale), 0, scale, scale, 3, 3) 102 end 103end 104 105portalprojectileparticle = class:new() 106 107function portalprojectileparticle:init(x, y, color, r, g, b) 108 self.x = x 109 self.y = y 110 self.color = color 111 112 113 self.speedx = math.random(-10, 10)/70 114 self.speedy = math.random(-10, 10)/70 115 116 self.alpha = 150 117 118 self.timer = 0 119end 120 121function portalprojectileparticle:update(dt) 122 self.timer = self.timer + dt 123 124 self.speedx = self.speedx + math.random(-10, 10)/70 125 self.speedy = self.speedy + math.random(-10, 10)/70 126 127 self.x = self.x + self.speedx*dt 128 self.y = self.y + self.speedy*dt 129 130 self.alpha = self.alpha - dt*300 131 if self.alpha < 0 then 132 self.alpha = 0 133 return true 134 end 135end 136 137function portalprojectileparticle:draw() 138 local r, g, b = unpack(self.color) 139 love.graphics.setColor(r, g, b, self.alpha) 140 141 love.graphics.draw(portalprojectileparticleimg, math.floor((self.x-xscroll)*16*scale), math.floor((self.y-.5)*16*scale), 0, scale, scale, 2, 2) 142end