From b7d3bcce2750be7fac34f7da15ce7108817d8f51 Mon Sep 17 00:00:00 2001 From: Martijn de Boer Date: Sat, 15 Oct 2022 23:11:21 +0200 Subject: [PATCH] bullets and sounds --- assets/weapons/bullet.png | Bin 0 -> 174 bytes bullet/bullet.lua | 73 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 assets/weapons/bullet.png create mode 100644 bullet/bullet.lua diff --git a/assets/weapons/bullet.png b/assets/weapons/bullet.png new file mode 100644 index 0000000000000000000000000000000000000000..af2fd7e6f767c5cc1cf1a1e3ab9e6b23a8042aab GIT binary patch literal 174 zcmeAS@N?(olHy`uVBq!ia0vp^tU%1k!3HFMt$j8R#3}HIEM{Qf76xHPhFNnYfPx90 zE{-7_Geb`u$PAy_;pXzy(RffU*E2HK@ UW4_MsK)V<`UHx3vIVCg!0K5n}Jpcdz literal 0 HcmV?d00001 diff --git a/bullet/bullet.lua b/bullet/bullet.lua new file mode 100644 index 0000000..c12416d --- /dev/null +++ b/bullet/bullet.lua @@ -0,0 +1,73 @@ +Bullet = Object.extend(Object) + +function Bullet:new( player ) + self.player = player + self.ex = 0 + self.ey = 0 + self.ox = 0 + self.oy = 0 + self.angle = 0 + self.emitted = 0 + self.lifetime = 1 + self.finished = false + self.speed = 8 +end + +function Bullet:update(dt) + self.emitted = self.emitted + dt + + if self.emitted > self.lifetime then + self.finished = true + else + self.ex = self.ex + math.lengthdir_x( self.speed, self.angle ) + self.ox + self.ey = self.ey + math.lengthdir_y( self.speed, self.angle ) + self.oy + + self.source:setPosition(self.ex, self.ey,0) + end +end + +function Bullet:draw() + if self.finished == false then + local bx = self.ex + local by = self.ey + local a = math.clamp(0, 1-(-0.3+(self.emitted/self.lifetime)), 1) + + love.graphics.setColor(1,1,1,a) + love.graphics.draw( bulletShot, bx, by, self.angle*(math.pi/180) ) + end +end + +function Bullet:emit( ex, ey, ox, oy, angle, lifetime ) + self.ex = ex + self.ey = ey + self.ox = ox + self.oy = oy + self.angle = angle + self.lifetime = lifetime + + self:generateSound(ex,ey) +end + +function Bullet:generateSound(px,py) + local sound = sfxr.newSound() + sound:resetParameters() + sound.waveform = 0 + sound.frequency.start = math.variance(0.50948863032722, 50) + sound.frequency.min = math.variance(0.23191561855507, 50) + sound.frequency.slide = -(math.variance(0.30302405172478, 50)) + sound.duty.ratio = math.variance(0.11477388876231, 50) + sound.duty.sweep = math.variance(0.10811502385025, 50) + sound.envelope.attack = math.random(0.0,0.1) + sound.envelope.sustain = 0.14060104631004 * (math.random(1,100)/100) + sound.envelope.decay = math.variance(0.22882606362934, 50) + sound.envelope.punch = math.variance(0.29031108049385,50) + sound.phaser.offset = 0 + sound.phaser.sweep = 0 + sound.highpass.cutoff = math.variance(0.25467943576175, 50) + + local soundData = sound:generateSoundData() + self.source = love.audio.newSource(soundData) + self.source:setVolume(1) + self.source:setPosition(px,py) + self.source:play() +end