-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCoffee_Patch.cs
247 lines (227 loc) · 10.9 KB
/
Coffee_Patch.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
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
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using UnityEngine;
using static ErrorMessage;
using System.Runtime.CompilerServices;
namespace Tweaks_Fixes
{
class Coffee_Patch
{
public static BoxCollider vendMachCol = null;
//public static ConditionalWeakTable<GameObject, Rigidbody> objectsRBs = new ConditionalWeakTable<GameObject, Rigidbody>();
public static ConditionalWeakTable<Pickupable, CoffeeVendingMachine> spawnedCoffeeM = new ConditionalWeakTable<Pickupable, CoffeeVendingMachine>();
public static ConditionalWeakTable<CoffeeVendingMachine, Pickupable> spawnedCoffee = new ConditionalWeakTable<CoffeeVendingMachine, Pickupable>();
public static ConditionalWeakTable<CoffeeVendingMachine, Pickupable> spawnedCoffeeRight = new ConditionalWeakTable<CoffeeVendingMachine, Pickupable>();
//public static Dictionary<CoffeeVendingMachine, BoxCollider> shiftedColliders = new Dictionary<CoffeeVendingMachine, BoxCollider>();
public static Dictionary<Eatable, float> spawnedCoffeeTime = new Dictionary<Eatable, float>();
public static float pourCoffeeTime = 10f;
public static bool HasCoffee(CoffeeVendingMachine cvm)
{
Pickupable pickupable = null;
if (spawnedCoffee.TryGetValue(cvm, out pickupable))
{
if (pickupable)
return true;
}
else if (spawnedCoffeeRight.TryGetValue(cvm, out pickupable))
{
if (pickupable)
return true;
}
return false;
}
public static void ShiftCollider(CoffeeVendingMachine __instance, bool down = false)
{
BoxCollider collider = __instance.GetComponentInChildren<BoxCollider>();
Vector3 colPos = collider.transform.position;
if (down)
{
//AddDebug(" ShiftCollider down");
//shiftedColliders[__instance] = null;
collider.transform.position = new Vector3(colPos.x, colPos.y - .4f, colPos.z);
}
else
{ // allow to pick up coffee
//AddDebug(" ShiftCollider up");
//shiftedColliders[__instance] = collider;
collider.transform.position = new Vector3(colPos.x, colPos.y + .4f, colPos.z);
}
}
[HarmonyPatch(typeof(CoffeeVendingMachine), "Start")]
class CoffeeVendingMachine_Start_Patch
{
public static bool Prefix(CoffeeVendingMachine __instance)
{
__instance.powerRelay = PowerSource.FindRelay(__instance.transform);
if (Main.gameLoaded)
__instance.idleSound.Play();
return false;
}
}
[HarmonyPatch(typeof(Eatable), "Awake")]
class Eatable_Awake_Patch
{
public static void Postfix(Eatable __instance)
{ // cyclops physics go insane if there is coffee in cvm when game loads
if (Main.gameLoaded || __instance.transform.parent == null)
return;
if (__instance.transform.parent.GetComponent<CoffeeVendingMachine>() && CraftData.GetTechType(__instance.gameObject) == TechType.Coffee)
{
//AddDebug("Coffee awake");
UnityEngine.Object.Destroy(__instance.gameObject);
//Collider myCol = __instance.GetComponent<CapsuleCollider>();
//Collider parentCol = __instance.transform.parent.GetComponentInChildren<BoxCollider>();
//if (myCol)
// AddDebug("myCol");
//if (parentCol)
// AddDebug("parentCol");
//Physics.IgnoreCollision(myCol, parentCol);
}
}
}
[HarmonyPatch(typeof(CoffeeVendingMachine), "OnMachineUse")]
class CoffeeVendingMachine_OnMachineUse_Patch
{
public static bool Prefix(CoffeeVendingMachine __instance)
{
if (!__instance.enabled || __instance.powerRelay == null || !__instance.powerRelay.IsPowered())
return false;
//AddDebug(" OnMachineUse spawnedCoffee " + spawnedCoffee.ContainsKey(__instance));
//AddDebug(" OnMachineUse spawnedCoffeeRight " + spawnedCoffeeRight.ContainsKey(__instance));
//if (!spawnedCoffee.ContainsKey(__instance) || spawnedCoffee[__instance] == null)
Pickupable pickupable = null;
if (!spawnedCoffee.TryGetValue(__instance, out pickupable) || pickupable == null)
{
__instance.vfxController.Play(0);
__instance.waterSoundSlot1.Play();
//__instance.timeLastUseSlot1 = Time.time;
__instance.StartCoroutine(SpawnCoffee(__instance));
//SpawnCoffee(__instance);
//if (HasCoffee(__instance))
{
//AddDebug(" ShiftCollider ");
//ShiftCollider(__instance);
}
}
else if (!spawnedCoffeeRight.TryGetValue(__instance, out pickupable) || pickupable == null)
{
__instance.vfxController.Play(1);
__instance.waterSoundSlot2.Play();
//__instance.timeLastUseSlot2 = Time.time;
//SpawnCoffee(__instance, true);
__instance.StartCoroutine(SpawnCoffee(__instance, true));
}
return false;
}
}
public static IEnumerator SpawnCoffee(CoffeeVendingMachine cvm, bool right = false)
{
//pourCoffeeTime = __instance.spawnDelay;
TaskResult<GameObject> result = new TaskResult<GameObject>();
yield return CraftData.InstantiateFromPrefabAsync(TechType.Coffee, result);
GameObject coffee = result.Get();
//AddDebug("SpawnCoffee coffee " + coffee.name);
ShiftCollider(cvm);
coffee.GetComponent<Rigidbody>().isKinematic = true;
coffee.transform.localScale = new Vector3(.7f, .7f, .7f);
//Vector3 pos = __instance.transform.position;
coffee.transform.rotation = cvm.transform.rotation;
//int rndFood = Main.rndm.Next(minFood, maxFood);
//float randomAngle = Main.rndm.Next((int)coffee.transform.rotation.y, (int)coffee.transform.rotation.y + 180);
//coffee.transform.Rotate(0f, randomAngle, 0f);
//coffee.transform.position = new Vector3(pos.x, pos.y, pos.z);
coffee.transform.position = cvm.transform.position;
coffee.transform.position += coffee.transform.forward * .29f;
if (right)
coffee.transform.position -= coffee.transform.right * .08f;
else
coffee.transform.position += coffee.transform.right * .08f;
coffee.transform.position += coffee.transform.up * .06f;
Pickupable pickupable = coffee.GetComponent<Pickupable>();
if (right)
spawnedCoffeeRight.Add(cvm, pickupable);
else
spawnedCoffee.Add(cvm, pickupable);
spawnedCoffeeM.Add(pickupable, cvm);
coffee.transform.SetParent(cvm.transform, true);
spawnedCoffeeTime[coffee.GetComponent<Eatable>()] = DayNightCycle.main.timePassedAsFloat;
//coffee.transform.position += coffee.transform. * .29f;
}
[HarmonyPatch(typeof(Pickupable), "Pickup")]
class Pickupable_Pickup_Patch
{
public static void Postfix(Pickupable __instance)
{
if (CraftData.GetTechType(__instance.gameObject) == TechType.Coffee)
{
//AddDebug(" Pickup Coffee");
Eatable eatable = __instance.GetComponent<Eatable>();
if (spawnedCoffeeTime.ContainsKey(eatable))
{
if (spawnedCoffeeTime[eatable] + pourCoffeeTime > DayNightCycle.main.timePassedAsFloat)
{
float pouredCoffee = DayNightCycle.main.timePassedAsFloat - spawnedCoffeeTime[eatable];
eatable.waterValue *= pouredCoffee * .1f;
//AddDebug(" picked up too soon " + pouredCoffee);
}
spawnedCoffeeTime.Remove(eatable);
}
//AddDebug("waterValue " + eatable.waterValue);
CoffeeVendingMachine cvm = null;
if (spawnedCoffeeM.TryGetValue(__instance, out cvm))
{
if (cvm == null)
return;
//if (cvm)
{
Pickupable p;
if (spawnedCoffee.TryGetValue(cvm , out p))
{
cvm.waterSoundSlot1.Stop();
spawnedCoffee.Remove(cvm);
}
else if (spawnedCoffeeRight.TryGetValue(cvm, out p))
{
cvm.waterSoundSlot2.Stop();
spawnedCoffeeRight.Remove(cvm);
}
if (!HasCoffee(cvm))
{
//AddDebug(" ShiftCollider down ");
ShiftCollider(cvm, true);
}
}
}
//foreach (var kv in spawnedCoffee)
//{
// if (kv.Value == __instance)
// cvm = kv.Key;
//}
//if (cvm)
//{
// spawnedCoffee[cvm] = null;
// cvm.waterSoundSlot1.Stop();
//}
//bool found = false;
//foreach (var kv in spawnedCoffeeRight)
//{
// if (kv.Value == __instance)
// {
// cvm = kv.Key;
// found = true;
// }
//}
//if (found && cvm)
//{
// spawnedCoffeeRight[cvm] = null;
// cvm.waterSoundSlot2.Stop();
//AddDebug(" spawnedCoffees_ " + spawnedCoffees_[cvm]);
//}
}
}
}
}
}