-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcamerashake.lua
107 lines (93 loc) · 3.14 KB
/
camerashake.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
local easing = require("easing")
--
-- Adapted from
-- Shank2 / Ninja CameraShake.cpp
--
local ShakeDeltas =
{
[CAMERASHAKE.FULL] =
{
--------------------------------------------------------------------------
-- 4
-- 7 1
-- 2 S 6
-- 5 3
-- 0
--------------------------------------------------------------------------
Vector3( 0,-1):GetNormalized(),
Vector3( 1, 1):GetNormalized(),
Vector3(-1, 0):GetNormalized(),
Vector3( 1,-1):GetNormalized(),
Vector3( 0, 1):GetNormalized(),
Vector3(-1,-1):GetNormalized(),
Vector3( 1, 0):GetNormalized(),
Vector3(-1, 1):GetNormalized(),
},
[CAMERASHAKE.SIDE] =
{
--------------------------------------------------------------------------
-- 0 S 1
--------------------------------------------------------------------------
Vector3(-1, 0):GetNormalized(),
Vector3( 1, 0):GetNormalized(),
},
[CAMERASHAKE.VERTICAL] =
{
--------------------------------------------------------------------------
-- 0
-- S
-- 1
--------------------------------------------------------------------------
Vector3( 0, 1):GetNormalized(),
Vector3( 0,-1):GetNormalized(),
},
}
CameraShake = Class(function(self, mode, duration, speed, scale)
self:StopShaking()
--TheInputProxy:RemoveVibration(VIBRATION_CAMERA_SHAKE)
self.mode = mode or CAMERASHAKE.FULL
self.duration = duration or 1
self.speed = speed or 0.05
self.scale = scale or 1
end)
function CameraShake:StopShaking()
--TheInputProxy:RemoveVibration(VIBRATION_CAMERA_SHAKE)
self.currentTime = 0
self.duration = 0
self.speed = 0
self.scale = 1
self.mode = nil
end
function CameraShake:Update(dt)
if self.mode == nil or self.speed == 0 or self.duration == 0 then
self:StopShaking()
return
end
self.currentTime = self.currentTime + dt
if self.currentTime > self.duration + 2*self.speed then
self:StopShaking()
return
end
local shakeDeltas = ShakeDeltas[self.shakeType] or ShakeDeltas[CAMERASHAKE.FULL]
local fromPos = nil
local toPos = nil
if self.currentTime < self.speed then
fromPos = Vector3()
toPos = shakeDeltas[1]
elseif self.currentTime >= self.duration + self.speed then
local last = math.floor(self.duration/self.speed) % #shakeDeltas
fromPos = shakeDeltas[last+1]
toPos = Vector3()
else
local fromIndex = math.floor( (self.currentTime - self.speed)/self.speed) % #shakeDeltas
local toIndex = (fromIndex+1) % #shakeDeltas
fromIndex = fromIndex + 1
toIndex = toIndex + 1
fromPos = shakeDeltas[fromIndex]
toPos = shakeDeltas[toIndex]
end
local t = self.currentTime / self.speed - math.floor(self.currentTime/self.speed)
local scale = easing.linear(self.currentTime, self.scale, -self.scale, self.duration)
local shakeDelta = (fromPos*(1-t) + toPos*t) * scale
return shakeDelta
end