-
Notifications
You must be signed in to change notification settings - Fork 0
/
GunController.cs
183 lines (158 loc) · 6.94 KB
/
GunController.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using UnityEngine;
using OWML.Common;
using UnityEngine.UI;
using System.Collections.Generic;
namespace QSBFPS;
public class GunController : MonoBehaviour
{
bool fadeReticle = false;
bool cooldownFire = false;
bool canFire = true;
bool autoFire;
float currentReticleAlpha;
float reticleFadeTime = 0.75f;
float fireCooldownTimer;
float fireCooldown = 0.2f;
int damage;
float stationaryAccuracyVar = 0.015f;
float movingAccuracyVar = 0.03f;
float autoAccuracyVarMult = 2f;
public Image hitReticle;
public GameObject gunObject;
public GameObject gunFirePoint;
public Transform particlesOffset;
private void Update()
{
if (!qsbFPS.Instance.gunHUD.enabled)
{
return;
}
//Detect button press
if (qsbFPS.inSolarSystem && canFire)
{
if (OWInput.IsNewlyPressed(InputLibrary.lockOn, InputMode.All))
{
damage = 20;
fireCooldown = 0.2f;
autoFire = false;
FireRaycast();
}
else if (OWInput.IsPressed(InputLibrary.probeLaunch, InputMode.All))
{
damage = 8;
fireCooldown = 0.08f;
autoFire = true;
FireRaycast();
}
}
//Fade out hit marker
if (fadeReticle)
{
currentReticleAlpha -= Time.deltaTime / reticleFadeTime;
hitReticle.color = new Color(hitReticle.color.r, hitReticle.color.g, hitReticle.color.b, currentReticleAlpha);
if (hitReticle.color.a <= 0)
{
//qsbFPS.Instance.ModHelper.Console.WriteLine("Reticle disabled");
hitReticle.enabled = false;
fadeReticle = false;
}
}
//Check if cooldown is finished
if (cooldownFire)
{
if (Time.time - fireCooldownTimer > fireCooldown)
{
canFire = true;
cooldownFire = false;
}
}
}
private void FireRaycast()
{
float raycastDist = 200f;
PlayerCameraController player = FindObjectOfType<PlayerCameraController>();
gunObject.GetComponentInChildren<Animator>().SetTrigger("fire");
StartCooldown();
Vector2 vector = OWInput.GetAxisValue(InputLibrary.moveXZ, InputMode.Character | InputMode.NomaiRemoteCam);
float variable;
if (vector.magnitude > 0.5f)
{
variable = movingAccuracyVar;
}
else
{
variable = stationaryAccuracyVar;
}
Vector3 randomVector = Random.insideUnitSphere * variable;
if (autoFire)
{
randomVector *= autoAccuracyVarMult;
}
GameObject fireEffect = AssetBundleUtilities.LoadPrefab("Assets/qsbfps", "Assets/qsbFPS/GunFireEffect.prefab", qsbFPS.Instance);
GameObject instantiatedFireEffect = Instantiate(fireEffect, gunFirePoint.transform.position,
Quaternion.LookRotation(gunObject.transform.GetChild(0).forward), gunObject.transform);
instantiatedFireEffect.SetActive(true);
Physics.queriesHitTriggers = false;
if (Physics.Raycast(player.transform.position, player.transform.forward + randomVector, out RaycastHit hit, raycastDist))
{
Physics.queriesHitTriggers = true;
if (!hit.collider.GetComponentInParent<PlayerCharacterController>() && hit.collider.gameObject.name == "REMOTE_PlayerDetector")
{
/*qsbFPS.Instance.ModHelper.Console.WriteLine("Shot hit another player!", MessageType.Success);
qsbFPS.qsbAPI.SendMessage("deal-damage", damage, receiveLocally: false);
qsbFPS.Instance.ModHelper.Console.WriteLine("Dictionary contains hit player: " +
qsbFPS.Instance.idToGameObjects.ContainsValue(hit.collider.GetComponentInParent<QSB.Player.RemotePlayerVelocity>().gameObject));
qsbFPS.Instance.ModHelper.Console.WriteLine("Hit player: " +
hit.collider.GetComponentInParent<QSB.Player.RemotePlayerVelocity>().gameObject);*/
qsbFPS.Instance.ModHelper.Console.WriteLine("Dictionary length: " + qsbFPS.Instance.idToGameObjects.Count);
foreach (KeyValuePair<uint, GameObject> pair in qsbFPS.Instance.idToGameObjects)
{
qsbFPS.Instance.ModHelper.Console.WriteLine("Pair object: " + pair.Value);
qsbFPS.Instance.ModHelper.Console.WriteLine("Pair ID: " + pair.Key);
if (pair.Value == hit.collider.GetComponentInParent<QSB.Player.RemotePlayerVelocity>().gameObject)
{
qsbFPS.Instance.ModHelper.Console.WriteLine("Found valid pair object!", MessageType.Success);
//new PlayerDamageMessage(pair.Key, damage).Send();
qsbFPS.qsbAPI.SendMessage("deal-damage", damage, pair.Key, false);
break;
}
}
GameObject prefab = AssetBundleUtilities.LoadPrefab("Assets/qsbfps", "Assets/qsbFPS/GunPlayerHitEffect.prefab", qsbFPS.Instance);
GameObject instantiated = Instantiate(prefab, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal), particlesOffset);
instantiated.SetActive(true);
ReticleFade();
return;
}
else
{
/*qsbFPS.Instance.ModHelper.Console.WriteLine("Shot didn't hit a player", MessageType.Info);
qsbFPS.Instance.ModHelper.Console.WriteLine("Shot reciever is my own character: " +
(hit.collider.GetComponentInParent<PlayerCharacterController>() != null));
qsbFPS.Instance.ModHelper.Console.WriteLine("Shot reciever has a player detector: " +
(hit.collider.gameObject.name == "REMOTE_PlayerDetector"));*/
}
if (hit.collider != null)
{
GameObject prefab = AssetBundleUtilities.LoadPrefab("Assets/qsbfps", "Assets/qsbFPS/GunGroundHitEffect.prefab", qsbFPS.Instance);
GameObject instantiated = Instantiate(prefab, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal), particlesOffset);
instantiated.SetActive(true);
//ReticleFade();
}
}
}
private void ReticleFade()
{
//qsbFPS.Instance.ModHelper.Console.WriteLine("Reticle enabled");
//qsbFPS.Instance.ModHelper.Console.WriteLine("Reticle object: " + hitReticle);
hitReticle.color = new Color(hitReticle.color.r, hitReticle.color.g, hitReticle.color.b, 1f);
currentReticleAlpha = 1f;
hitReticle.enabled = true;
fadeReticle = true;
}
private void StartCooldown()
{
fireCooldownTimer = Time.time;
canFire = false;
cooldownFire = true;
}
}