-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdv_CCam.lua
More file actions
249 lines (203 loc) · 9.08 KB
/
Copy pathdv_CCam.lua
File metadata and controls
249 lines (203 loc) · 9.08 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
behaviour("dvCCam")
function dvCCam:Start()
self.attractMode = false
self.cameraStyle = 1 -- 1: Ravenfield Default, 2: Fly By, 3: Frontal View, 4: Frontal Fixed View
self.attractModeTimer = 0
self.attractModetimerDuration = 5
self.attractModeOffset = Vector3.zero
self.attractModePosition = Vector3.zero
self.attractModeRotation = Quaternion.identity
self.targetAttractPosition = Vector3.zero
self.targetAttractRotation = Quaternion.identity
self.attractModeTarget = nil
self.flyByTimer = 0
self.flyByPosition = Vector3.zero
self.frontalOffset = Vector3(0, 2, 10)
self.fovMultiplier = 1
self.baseFov = 60
end
function dvCCam:Update()
if Input.GetKey(KeyCode.LeftShift) then
if Input.GetKeyDown(KeyCode.C) then
self:StartAttractMode()
elseif Input.GetKeyDown(KeyCode.X) then
self:EndAttractMode()
end
end
if Input.GetKeyDown(KeyCode.O) then
self:SwitchCameraStyle(-1) -- Previous style
elseif Input.GetKeyDown(KeyCode.P) then
self:SwitchCameraStyle(1) -- Next style
end
if self.cameraStyle == 3 or self.cameraStyle == 4 then
self:AdjustFrontalView()
end
if Input.GetKey(KeyCode.LeftControl) then
self:AdjustFovMultiplier()
end
end
function dvCCam:LateUpdate()
if self.attractMode then
if Input.GetKeyDown(KeyCode.L) then
self:NewAttractModeShot()
end
self:UpdateAttractMode()
self:ApplyCameraStyle()
end
if self.attractModeTarget ~= nil then
local tpCamera = PlayerCamera.tpCamera
self.distanceToTarget = Vector3.Distance(tpCamera.transform.position, self.attractModeTarget.position)
end
end
function dvCCam:AdjustFovMultiplier()
local scroll = Input.GetAxis("Mouse ScrollWheel")
self.fovMultiplier = self.fovMultiplier + scroll * 0.1
if self.fovMultiplier < 0.1 then
self.fovMultiplier = 0.1
end
local tpCamera = PlayerCamera.tpCamera
tpCamera.fieldOfView = self.baseFov * self.fovMultiplier
end
function dvCCam:CalculateDynamicFov()
local distance = self.distanceToTarget
local baseDistance = 10
local baseFov = self.baseFov * self.fovMultiplier
if distance <= 0 then
return baseFov -- Prevent division by zero or negative distances
end
return baseFov * (baseDistance / distance)
end
function dvCCam:StartAttractMode()
self.attractMode = true
PlayerCamera.ThirdPersonCamera()
PlayerHud.hudPlayerEnabled = false
PlayerHud.hudGameModeEnabled = false
self:NewAttractModeShot()
end
function dvCCam:EndAttractMode()
self.attractMode = false
PlayerCamera.FirstPersonCamera()
PlayerHud.hudPlayerEnabled = true
PlayerHud.hudGameModeEnabled = true
end
function dvCCam:UpdateAttractMode()
self.attractModeTimer = self.attractModeTimer + Time.deltaTime
if self.attractModeTimer > self.attractModetimerDuration then
self.attractModeTimer = 0
end
if self.attractModeTarget ~= nil and self.attractModeTarget.isDead then
self.attractModeTarget = nil
self.attractModetimerDuration = Random.Range(7, 15)
end
if self.attractModeTarget ~= nil then
-- Update targetDirection to follow the body movement
if self.attractModeTarget.velocity.magnitude > 0 then
self.targetDirection = self.attractModeTarget.transform.forward
end
self.targetAttractRotation = Quaternion.LookRotation(self.attractModeTarget.position - self.attractModePosition + Vector3(0, 3, 0))
self.targetAttractPosition = self.attractModeTarget.position + self.attractModeOffset
end
self.attractModePosition = Vector3.Lerp(self.attractModePosition, self.targetAttractPosition, Time.deltaTime * 2)
self.attractModeRotation = Quaternion.Slerp(self.attractModeRotation, self.targetAttractRotation, Time.deltaTime * 2)
end
function dvCCam:ApplyCameraStyle()
local tpCamera = PlayerCamera.tpCamera
if self.cameraStyle == 1 then -- Ravenfield Default
tpCamera.transform.position = self.attractModePosition
tpCamera.transform.rotation = self.attractModeRotation
elseif self.cameraStyle == 2 then -- Fly By
self.flyByTimer = self.flyByTimer + Time.deltaTime
if self.flyByTimer > self.attractModetimerDuration or self.flyByPosition == Vector3.zero then
self.flyByTimer = 0
self:NewFlyByPosition()
end
tpCamera.transform.position = self.flyByPosition
tpCamera.transform.LookAt(self.attractModeTarget.position)
elseif self.cameraStyle == 3 then -- Frontal View
if self.attractModeTarget ~= nil then
local movementDirection = self.attractModeTarget.velocity.magnitude > 0 and self.attractModeTarget.velocity.normalized or self.targetDirection
local right = Vector3.Cross(Vector3.up, movementDirection).normalized
local up = Vector3.Cross(movementDirection, right).normalized
local cameraOffset = right * self.frontalOffset.x + up * self.frontalOffset.y + movementDirection * self.frontalOffset.z
local cameraPosition = self.attractModeTarget.position + cameraOffset
tpCamera.transform.position = cameraPosition
tpCamera.transform.LookAt(self.attractModeTarget.position)
end
elseif self.cameraStyle == 4 then -- Frontal Fixed View
if self.attractModeTarget ~= nil then
local facingDirection = self.targetDirection -- Use stored direction when stationary
local right = Vector3.Cross(Vector3.up, facingDirection).normalized
local up = Vector3.Cross(facingDirection, right).normalized
local cameraOffset = right * self.frontalOffset.x + up * self.frontalOffset.y + facingDirection * self.frontalOffset.z
local cameraPosition = self.attractModeTarget.position + cameraOffset
tpCamera.transform.position = cameraPosition
tpCamera.transform.LookAt(self.attractModeTarget.position)
end
end
end
function dvCCam:AdjustFrontalView()
if Input.GetKey(KeyCode.UpArrow) then
self.frontalOffset.y = self.frontalOffset.y + 0.1
elseif Input.GetKey(KeyCode.DownArrow) then
self.frontalOffset.y = self.frontalOffset.y - 0.1
end
if Input.GetKey(KeyCode.LeftArrow) then
self.frontalOffset.x = self.frontalOffset.x + 0.1
elseif Input.GetKey(KeyCode.RightArrow) then
self.frontalOffset.x = self.frontalOffset.x - 0.1
end
local scroll = Input.GetAxis("Mouse ScrollWheel")
self.frontalOffset.z = self.frontalOffset.z + scroll * 1
end
function dvCCam:NewFlyByPosition()
if self.attractModeTarget ~= nil then
local movementDirection = self.attractModeTarget.velocity.normalized
local randomOffset = Vector3(Random.Range(-15, 15), Random.Range(-15, 15), Random.Range(-15, 15))
-- Bias the position towards the movement direction
local biasedPosition = self.attractModeTarget.position + movementDirection * 200 + randomOffset
self.flyByPosition = biasedPosition
self.attractModetimerDuration = Random.Range(5, 8)
end
end
function dvCCam:NewAttractModeShot()
local actors = ActorManager.GetAliveActorsOnTeam(self:GetRandomTeam())
if #actors > 0 then
local newTarget = nil
while newTarget == nil do
local num = math.floor(Random.Range(1, #actors))
for i = #actors, 1, -1 do
local index = (num + i - 1) % #actors + 1
if actors[index].isBot and actors[index] ~= self.attractModeTarget then
newTarget = actors[index]
break
end
end
end
self.attractModeTarget = newTarget
self.attractModeOffset = Vector3(Random.Range(-2, 2), 5, -8)
self.attractModetimerDuration = Random.Range(7, 15)
self.attractModeRotation = Quaternion.LookRotation(self.attractModeTarget.facingDirection)
self.attractModePosition = Matrix4x4.TRS(self.attractModeTarget.centerPosition, self.attractModeRotation, Vector3.one).MultiplyPoint(self.attractModeOffset)
print("New target acquired: " .. self.attractModeTarget.name)
end
end
function dvCCam:SwitchCameraStyle(direction)
self.cameraStyle = self.cameraStyle + direction
if self.cameraStyle > 4 then
self.cameraStyle = 1
elseif self.cameraStyle < 1 then
self.cameraStyle = 4
end
local styleNames = { "Ravenfield Default", "Fly By", "Frontal View", "Frontal Fixed View" }
print("Camera style: " .. styleNames[self.cameraStyle])
if self.cameraStyle == 2 then
self:NewFlyByPosition() -- Generate a new position when switching to Fly By mode
end
end
function dvCCam:GetRandomTeam()
local num = math.random()
if num > 0.5 then
return Team.Blue
end
return Team.Red
end