-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSprayPaintItemExt.cs
166 lines (150 loc) · 6.29 KB
/
SprayPaintItemExt.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
using System;
using System.Collections.Generic;
using BetterSprayPaint;
using BetterSprayPaint.Ngo;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
public class SprayPaintItemExt: MonoBehaviour {
public SprayPaintItem instance = null!;
public SprayPaintItemNetExt net = null!;
public DecalProjector previewDecal = null!;
List<Action> cleanupActions = new List<Action>();
public bool ItemActive() => net.HeldByLocalPlayer && net.InActiveSlot && Patches.CanUseItem(instance.playerHeldBy);
public void Awake() {
instance = GetComponent<SprayPaintItem>();
net = instance.NetExt();
var go = UnityEngine.Object.Instantiate(instance.sprayPaintPrefab);
previewDecal = go.GetComponent<DecalProjector>();
GameObject.DontDestroyOnLoad(go);
previewDecal.material = new Material(net.baseDecalMaterial);
previewDecal.enabled = true;
go.name = "PreviewDecal";
go.SetActive(true);
var actions = new ActionSubscriptionBuilder(cleanupActions, () => ItemActive());
actions.Subscribe(
Plugin.inputActions.SprayPaintEraseModifier,
onStart: delegate {
if (SessionData.AllowErasing) { net.IsErasing = true; }
}
);
actions.Subscribe(
Plugin.inputActions.SprayPaintErase,
onStart: delegate {
if (SessionData.AllowErasing) { net.IsErasing = true; }
instance.UseItemOnClient(true);
},
onStop: delegate {
instance.UseItemOnClient(false);
}
);
actions.Subscribe(
Plugin.inputActions.SprayPaintNextColor,
delegate {
if (!SessionData.AllowColorChange) return;
var idx = net.ColorPalette.FindIndex((Color color) => color == net.CurrentColor);
idx = net.posmod(++idx, net.ColorPalette.Count);
StartCoroutine(net.ChangeColorCoroutine(net.ColorPalette[idx]));
}
);
actions.Subscribe(
Plugin.inputActions.SprayPaintPreviousColor,
delegate {
if (!SessionData.AllowColorChange) return;
var idx = net.ColorPalette.FindIndex((Color color) => color == net.CurrentColor);
idx = net.posmod(--idx, net.ColorPalette.Count);
StartCoroutine(net.ChangeColorCoroutine(net.ColorPalette[idx]));
}
);
actions.Subscribe(
Plugin.inputActions.SprayPaintColor1,
delegate {
if (!SessionData.AllowColorChange) return;
var idx = net.posmod(0, net.ColorPalette.Count);
StartCoroutine(net.ChangeColorCoroutine(net.ColorPalette[idx]));
}
);
actions.Subscribe(
Plugin.inputActions.SprayPaintColor2,
delegate {
if (!SessionData.AllowColorChange) return;
var idx = net.posmod(1, net.ColorPalette.Count);
StartCoroutine(net.ChangeColorCoroutine(net.ColorPalette[idx]));
}
);
actions.Subscribe(
Plugin.inputActions.SprayPaintColor3,
delegate {
if (!SessionData.AllowColorChange) return;
var idx = net.posmod(2, net.ColorPalette.Count);
StartCoroutine(net.ChangeColorCoroutine(net.ColorPalette[idx]));
}
);
actions.Subscribe(
Plugin.inputActions.SprayPaintColor4,
delegate {
if (!SessionData.AllowColorChange) return;
var idx = net.posmod(3, net.ColorPalette.Count);
StartCoroutine(net.ChangeColorCoroutine(net.ColorPalette[idx]));
}
);
actions.Subscribe(
Plugin.inputActions.SprayPaintIncreaseSize,
(_, _) => StartCoroutine(net.ChangeSizeCoroutine()),
(_, _, coroutine) => StopCoroutine(coroutine)
);
actions.Subscribe(
Plugin.inputActions.SprayPaintDecreaseSize,
(_, _) => StartCoroutine(net.ChangeSizeCoroutine()),
(_, _, coroutine) => StopCoroutine(coroutine)
);
actions.Subscribe(
Plugin.inputActions.SprayPaintSize01,
delegate {
net.PaintSize = 0.1f;
}
);
actions.Subscribe(
Plugin.inputActions.SprayPaintSize1,
delegate {
net.PaintSize = 1.0f;
}
);
actions.Subscribe(
Plugin.inputActions.SprayPaintSize2,
delegate {
net.PaintSize = 2.0f;
}
);
}
float previewFadeFactor = 1f;
Vector3 previewOriginalScale = Vector3.oneVector;
public void Update() {
var active = false;
if (ItemActive()) {
var sprayPos = instance.playerHeldBy.gameplayCamera.transform.position;
var sprayRot = instance.playerHeldBy.gameplayCamera.transform.forward;
Ray ray = new Ray(sprayPos, sprayRot);
if (Patches.RaycastCustom(ray, out var sprayHit, SessionData.Range, net.sprayPaintMask, QueryTriggerInteraction.Collide, instance)) {
Patches.PositionSprayPaint(instance, previewDecal.gameObject, sprayHit, setColor: false);
previewOriginalScale = previewDecal.transform.localScale;
active = true;
}
}
var c = net.CurrentColor;
var factor = (Mathf.Sin(Time.timeSinceLevelLoad * 6f) + 1f) * 0.2f + 0.2f;
previewFadeFactor = Utils.Lexp(previewFadeFactor, active ? 1f : 0f, 15f * Time.deltaTime);
if (previewDecal?.gameObject != null && previewDecal.material != null) {
previewDecal.material.color = new Color(
Mathf.Lerp(c.r, Math.Min(c.r + 0.35f, 1f), factor),
Mathf.Lerp(c.g, Math.Min(c.g + 0.35f, 1f), factor),
Mathf.Lerp(c.b, Math.Min(c.b + 0.35f, 1f), factor),
Mathf.Clamp(Plugin.SprayPreviewOpacity, 0f, 1f) * previewFadeFactor
);
previewDecal.transform.localScale = previewOriginalScale * previewFadeFactor;
}
}
public void OnDestroy() {
foreach (var a in cleanupActions) { a(); }
Destroy(previewDecal);
}
}