-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathItemBinoculars.cs
161 lines (141 loc) · 7.38 KB
/
ItemBinoculars.cs
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
using ThunderRoad;
using UnityEngine;
namespace TOR {
public class ItemBinoculars : ThunderBehaviour {
protected Item item;
protected ItemModuleBinoculars module;
protected Rigidbody body;
protected Handle leftGrip;
protected Handle rightGrip;
protected AudioSource[] zoomSounds;
Renderer scopeL;
Camera scopeCameraL;
RenderTexture renderScopeTextureL;
Renderer scopeR;
Camera scopeCameraR;
RenderTexture renderScopeTextureR;
int currentScopeZoom;
MaterialInstance _scopeMaterialInstanceL;
public MaterialInstance scopeMaterialInstanceL {
get {
if (_scopeMaterialInstanceL == null) {
scopeL.gameObject.TryGetOrAddComponent(out MaterialInstance mi);
_scopeMaterialInstanceL = mi;
}
return _scopeMaterialInstanceL;
}
}
MaterialInstance _scopeMaterialInstanceR;
public MaterialInstance scopeMaterialInstanceR {
get {
if (_scopeMaterialInstanceR == null) {
scopeR.gameObject.TryGetOrAddComponent(out MaterialInstance mi);
_scopeMaterialInstanceR = mi;
}
return _scopeMaterialInstanceR;
}
}
protected void Awake() {
item = this.GetComponent<Item>();
module = item.data.GetModule<ItemModuleBinoculars>();
body = this.GetComponent<Rigidbody>();
// setup item events
item.OnGrabEvent += OnGrabEvent;
item.OnUngrabEvent += OnUngrabEvent;
item.OnHeldActionEvent += OnHeldAction;
scopeL = item.GetCustomReference(module.leftScopeID).GetComponent<Renderer>();
scopeR = item.GetCustomReference(module.rightScopeID).GetComponent<Renderer>();
SetupScope(item.GetCustomReference(module.leftScopeCameraID).GetComponent<Camera>(), scopeMaterialInstanceL, ref scopeCameraL, ref renderScopeTextureL);
SetupScope(item.GetCustomReference(module.rightScopeCameraID).GetComponent<Camera>(), scopeMaterialInstanceR, ref scopeCameraR, ref renderScopeTextureR);
if (!string.IsNullOrEmpty(module.zoomSoundsID)) zoomSounds = item.GetCustomReference(module.zoomSoundsID).GetComponents<AudioSource>();
}
protected void OnDestroy() {
if (scopeCameraL) scopeCameraL.targetTexture = null;
if (scopeCameraR) scopeCameraR.targetTexture = null;
if (renderScopeTextureL) {
if (renderScopeTextureL.IsCreated()) {
renderScopeTextureL.Release();
}
Destroy(renderScopeTextureL);
renderScopeTextureL = null;
}
if (renderScopeTextureR) {
if (renderScopeTextureR.IsCreated()) {
renderScopeTextureR.Release();
}
Destroy(renderScopeTextureR);
renderScopeTextureR = null;
}
}
void SetupScope(Camera scopeCamera, MaterialInstance scopeMaterial, ref Camera storedCamera, ref RenderTexture renderTexture) {
scopeCamera.enabled = false;
storedCamera = scopeCamera;
scopeCamera.fieldOfView = module.scopeZoom[currentScopeZoom];
renderTexture = new RenderTexture(
module.scopeResolution != null ? module.scopeResolution[0] : GlobalSettings.BlasterScopeResolution,
module.scopeResolution != null ? module.scopeResolution[1] : GlobalSettings.BlasterScopeResolution,
module.scopeDepth, RenderTextureFormat.DefaultHDR);
scopeMaterial.material.SetTexture("_RenderTexture", renderTexture);
if (GlobalSettings.BlasterScope3D) scopeMaterial.material.EnableKeyword("_3D_SCOPE"); else scopeMaterial.material.DisableKeyword("_3D_SCOPE");
}
void SetScopeRender(MaterialInstance scopeMaterial, Camera scopeCamera, bool state, ref RenderTexture renderTexture) {
if (scopeMaterial == null) return;
scopeCamera.enabled = state;
if (state) {
if (!renderTexture.IsCreated()) renderTexture.Create();
scopeCamera.targetTexture = renderTexture;
scopeMaterial.material.EnableKeyword("_SCOPE_ACTIVE");
} else {
scopeCamera.targetTexture = null;
renderTexture.Release();
scopeMaterial.material.DisableKeyword("_SCOPE_ACTIVE");
}
}
void CycleScope(RagdollHand interactor = null) {
if (scopeL == null || scopeCameraL == null || scopeR == null || scopeCameraR == null) return;
currentScopeZoom = (currentScopeZoom >= module.scopeZoom.Length - 1) ? -1 : currentScopeZoom;
currentScopeZoom++;
scopeCameraL.fieldOfView = module.scopeZoom[currentScopeZoom];
scopeCameraR.fieldOfView = module.scopeZoom[currentScopeZoom];
if (zoomSounds != null) Utils.PlayRandomSound(zoomSounds);
Utils.PlayHaptic(interactor, Utils.HapticIntensity.Minor);
}
void CycleScopeBack(RagdollHand interactor = null) {
if (scopeL == null || scopeCameraL == null || scopeR == null || scopeCameraR == null) return;
currentScopeZoom = (currentScopeZoom <= 0) ? module.scopeZoom.Length : currentScopeZoom;
currentScopeZoom--;
scopeCameraL.fieldOfView = module.scopeZoom[currentScopeZoom];
scopeCameraR.fieldOfView = module.scopeZoom[currentScopeZoom];
if (zoomSounds != null) Utils.PlayRandomSound(zoomSounds);
Utils.PlayHaptic(interactor, Utils.HapticIntensity.Minor);
}
public void ExecuteAction(string action, RagdollHand ragdollHand = null) {
if (action == "cycleScope") {
CycleScope(ragdollHand);
} else if (action == "cycleScopeBack") {
CycleScopeBack(ragdollHand);
}
}
public void OnGrabEvent(Handle handle, RagdollHand interactor) {
// toggle scope for performance reasons
if (interactor.playerHand) {
SetScopeRender(scopeMaterialInstanceL, scopeCameraL, true, ref renderScopeTextureL);
SetScopeRender(scopeMaterialInstanceR, scopeCameraR, true, ref renderScopeTextureR);
}
}
public void OnUngrabEvent(Handle handle, RagdollHand interactor, bool throwing) {
// toggle scope for performance reasons
SetScopeRender(scopeMaterialInstanceL, scopeCameraL, false, ref renderScopeTextureL);
SetScopeRender(scopeMaterialInstanceR, scopeCameraR, false, ref renderScopeTextureR);
}
public void OnHeldAction(RagdollHand interactor, Handle handle, Interactable.Action action) {
if (action == Interactable.Action.UseStart) {
if (interactor.side == Side.Right) ExecuteAction(module.rightGripPrimaryAction, interactor);
else ExecuteAction(module.leftGripPrimaryAction, interactor);
} else if (action == Interactable.Action.AlternateUseStart) {
if (interactor.side == Side.Right) ExecuteAction(module.rightGripSecondaryAction, interactor);
else ExecuteAction(module.leftGripSecondaryAction, interactor);
}
}
}
}