Created
April 2, 2016 01:30
-
-
Save pablomayobre/a058bf4c01304153e35cf2fe0cb3a360 to your computer and use it in GitHub Desktop.
Screen shake for Lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Based on S0lll0s code https://love2d.org/forums/viewtopic.php?f=4&t=79207#p176746 | |
local shake = {} | |
shake.__index = shake | |
local new = function (angle, intensity, duration, speed) | |
self = { | |
angle = angle, | |
intensity = intensity, | |
duration = duration, | |
speed = speed, | |
time = duration, | |
finished = false, | |
x = math.cos(angle), | |
y = math.sin(angle) | |
} | |
return setmetatable(self, shake) | |
end | |
shake.update = function (self, dt) | |
if self.time > 0 then | |
self.time = self.time - dt | |
else | |
self.finished = true | |
end | |
end | |
shake.restart = function (self) | |
self.time = 0 | |
end | |
shake.get = function (self) | |
if not self.finished then | |
local perc = math.sqrt(self.time/self.duration) | |
local r = perc * math.sin(self.time * self.speed * perc) * self.intensity | |
local dx = self.x * r | |
local dy = self.y * r | |
return dx, dy, self.angle, r | |
end | |
return 0, 0, self.angle, 0 | |
end | |
return new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment