-
Notifications
You must be signed in to change notification settings - Fork 1
/
ItemShop.cs
295 lines (244 loc) · 12.1 KB
/
ItemShop.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using UnityEngine;
using MSCLoader;
using HutongGames.PlayMaker;
using HutongGames.PlayMaker.Actions;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace UniversalShoppingSystem
{
public class ItemShop : MonoBehaviour
{
public delegate void ShopEvent();
// Settings to change in Unity
[Header("Shop Settings")]
public string ShopID = "Unique ID for Save/Load management";
public string ItemName = "Shop Item Name";
public float ItemPrice;
[Header("Relative to store_inside")]
public Vector3 TriggerPosition;
public Vector3 TriggerRotation;
public GameObject ItemPrefab;
public bool SpawnInBag;
// FOLLOWING NOT NEEDED FOR UNITY SETUP; THEREFORE NOT INCLUDED IN MINI DLL
public List<GameObject> BoughtItems = new List<GameObject>(); // Used for saving, no need to change in inspector!
public event ShopEvent OnBuy;
public event ShopEvent OnRestock;
public int Stock;
public int Cart;
private Vector3 bigItemSpawnPosition = new Vector3(-1551.303f, 4.88f, 1181.904f);
private int itemsBought;
private PlayMakerFSM register; // Required to hook paying mechanics
private PlayMakerFSM registerData; // Required to hook bag creation mechanics
private GameObject vanillaShopInventory; // Required to hook restock mechanics
public void SaveShop(Mod mod)
{
// SHOP SAVING
List<bool> shopItemsActive = new List<bool>();
for (int i = 0; i < transform.childCount; i++) shopItemsActive.Add(transform.GetChild(i).gameObject.activeInHierarchy);
SaveLoad.WriteValue(mod, $"USS_{ShopID}_shopItemsActive", shopItemsActive);
SaveLoad.WriteValue(mod, $"USS_{ShopID}_stock", Stock + Cart);
SaveLoad.WriteValue(mod, $"USS_{ShopID}_itemsBought", itemsBought);
// BOUGHT ITEMS SAVING
List<Vector3> pos = new List<Vector3>();
List<Quaternion> rot = new List<Quaternion>();
List<bool> inBag = new List<bool>();
List<string> bagID = new List<string>();
List<float> condition = new List<float>();
foreach (GameObject obj in BoughtItems)
{
USSItem itm = obj.GetComponent<USSItem>();
pos.Add(obj.transform.position);
rot.Add(obj.transform.rotation);
inBag.Add(itm.InBag);
bagID.Add(itm.BagID);
condition.Add(itm.Condition);
}
SaveLoad.WriteValue<Vector3>(mod, $"USS_{ShopID}_pos", pos);
SaveLoad.WriteValue<Quaternion>(mod, $"USS_{ShopID}_rot", rot);
SaveLoad.WriteValue<bool>(mod, $"USS_{ShopID}_inBag", inBag);
SaveLoad.WriteValue<string>(mod, $"USS_{ShopID}_bagID", bagID);
SaveLoad.WriteValue<float>(mod, $"USS_{ShopID}_condition", condition);
}
public void LoadShop(Mod mod)
{
if (SaveLoad.ValueExists(mod, $"USS_{ShopID}_stock"))
{
// SHOP LOADING
List<bool> shopItemsActive = SaveLoad.ReadValueAsList<bool>(mod, $"USS_{ShopID}_shopItemsActive");
for (int i = 0; i < transform.childCount; i++) transform.GetChild(i).gameObject.SetActive(shopItemsActive[i]);
Stock = SaveLoad.ReadValue<int>(mod, $"USS_{ShopID}_stock");
itemsBought = SaveLoad.ReadValue<int>(mod, $"USS_{ShopID}_itemsBought");
// BOUGHT ITEMS LOADING
List<Vector3> pos = SaveLoad.ReadValueAsList<Vector3>(mod, $"USS_{ShopID}_pos");
List<Quaternion> rot = SaveLoad.ReadValueAsList<Quaternion>(mod, $"USS_{ShopID}_rot");
List<bool> inBag = SaveLoad.ReadValueAsList<bool>(mod, $"USS_{ShopID}_inBag");
List<string> bagID = SaveLoad.ReadValueAsList<string>(mod, $"USS_{ShopID}_bagID");
List<float> condition = SaveLoad.ReadValueAsList<float>(mod, $"USS_{ShopID}_condition");
List<GameObject> shoppingBags = (from gameObject in UnityEngine.Resources.FindObjectsOfTypeAll<GameObject>()
where gameObject.name.Contains("shopping bag") && gameObject.GetComponent<PlayMakerFSM>() != null
select gameObject).ToList();
for (int i = 0; i < pos.Count; i++)
{
GameObject obj = GameObject.Instantiate(ItemPrefab);
USSItem itm = obj.GetComponent<USSItem>();
obj.transform.position = pos[i];
obj.transform.rotation = rot[i];
itm.InBag = inBag[i];
itm.BagID = bagID[i];
itm.Condition = condition[i];
obj.SetActive(false);
if (itm.InBag)
{
GameObject bag = shoppingBags.FirstOrDefault((GameObject select) => select.GetComponent<PlayMakerFSM>().FsmVariables.FindFsmString("ID").Value == itm.BagID);
if (bag == null)
{
ModConsole.LogWarning("UniversalShoppingSystem: Couldnt find bag; Spawning item outside.");
obj.SetActive(true);
itm.StartSpoiling();
continue;
}
USSBagInventory bagBagInventory = bag.GetComponent<USSBagInventory>();
if (bagBagInventory == null)
{
bagBagInventory = bag.AddComponent<USSBagInventory>();
bagBagInventory.BagContent = new List<GameObject> { obj };
USSBagSetupOpenAction act = bag.AddComponent<USSBagSetupOpenAction>();
act.Bag = bag;
act.BagInventory = bagBagInventory;
}
else bagBagInventory.BagContent.Add(obj);
}
else
{
obj.SetActive(true);
itm.StartSpoiling();
}
BoughtItems.Add(obj); // Keep the item tracked for next save
}
}
}
public class CreateBagAction : FsmStateAction
{
public ItemShop Shop;
public FsmInt Check;
public override void OnEnter()
{
if (Shop.Cart > 0) if (Check.Value == 0) Fsm.Event("FINISHED");
Finish();
}
}
public class RestockAction : FsmStateAction
{
public ItemShop shop;
public override void OnEnter()
{
shop.Restock();
Finish();
}
}
private void Awake()
{
if (ShopID == "Unique ID for Save/Load management") ModConsole.Error("UniversalShoppingSystem: ShopID of " + ItemName + " is still default!");
if (!GameObject.Find("PLAYER").GetComponent<ItemShopRaycast>()) GameObject.Find("PLAYER").AddComponent<ItemShopRaycast>();
GameObject.Find("PLAYER").GetComponent<ItemShopRaycast>().Shops.Add(this); // Add shop for commands, unique id check etc.
register = GameObject.Find("STORE/StoreCashRegister/Register").GetComponent<PlayMakerFSM>();
vanillaShopInventory = GameObject.Find("STORE/Inventory");
registerData = GameObject.Find("StoreCashRegister").transform.GetChild(2).GetPlayMaker("Data");
registerData.InitializeFSM();
GameHook.InjectStateHook(register.gameObject, "Purchase", () => { Pay(); });
vanillaShopInventory.GetPlayMaker("Logic").GetState("Items").InsertAction(0, new RestockAction { shop = this }); // Inject paying and restock mechanics
transform.SetParent(GameObject.Find("STORE").transform.Find("LOD").transform.Find("GFX_Store").transform.Find("store_inside"), false);
transform.localEulerAngles = TriggerRotation;
transform.localPosition = TriggerPosition;
transform.SetParent(null, true); // Needs to be parented to root in order to stay active all the time to not cause problems with restocking
Stock = transform.childCount;
Cart = itemsBought = 0;
Restock(); // Fully restock, save data overrides the values
if (SpawnInBag) // Only setup the whole stuff when items should spawn in bags
{
// Bag Spawning Setup
GameObject store = GameObject.Find("STORE");
PlayMakerFSM fsm = store.transform.Find("LOD/ShopFunctions/BagCreator").GetPlayMaker("Create");
fsm.InitializeFSM();
fsm.GetState("Copy contents").InsertAction(0, new USSBagSetupAction
{
Bag = (fsm.GetState("Copy contents").Actions.First(action => action is ArrayListCopyTo) as ArrayListCopyTo).gameObjectTarget.GameObject,
Shop = this
});
// Abusing the oil filter shop for our purposes
registerData.GetState("Oil filter").InsertAction(0, new CreateBagAction
{
Shop = this,
Check = registerData.FsmVariables.FindFsmInt("QOilfilter")
});
}
}
private void Restock()
{
StartCoroutine(RestockShop());
itemsBought = 0;
Stock = this.gameObject.transform.childCount;
OnRestock?.Invoke(); // Run user-provided actions
}
private IEnumerator RestockShop()
{
for (int i = 0; i < this.gameObject.transform.childCount; i++) this.transform.GetChild(i).gameObject.SetActive(true);
yield break;
}
public void SpawnBag(USSBagInventory BagInventory)
{
if (this.SpawnInBag) StartCoroutine(BagSpawner(BagInventory));
}
private IEnumerator BagSpawner(USSBagInventory BagInventoryent)
{
yield return new WaitForSeconds(0.31f); // wait slightly longer than expandedshop
itemsBought += Cart;
while (Cart > 0)
{
Cart--;
GameObject item = GameObject.Instantiate(ItemPrefab);
item.SetActive(false);
BagInventoryent.BagContent.Add(item);
BoughtItems.Add(item); // Track item for saving/loading
USSItem ussitm = item.GetComponent<USSItem>();
ussitm.InBag = true;
ussitm.OriginShop = this;
yield return null;
}
yield break;
}
public void Buy() // Put item in Cart
{
Stock--;
Cart++;
this.transform.GetChild(Cart - 1 + itemsBought).gameObject.SetActive(false);
register.FsmVariables.GetFsmFloat("PriceTotal").Value += ItemPrice;
register.SendEvent("PURCHASE");
}
public void Unbuy() // Take item back out of Cart
{
this.transform.GetChild(Cart - 1 + itemsBought).gameObject.SetActive(true);
Stock++;
Cart--;
register.FsmVariables.GetFsmFloat("PriceTotal").Value -= ItemPrice;
register.SendEvent("PURCHASE");
}
private void Pay() // Called on checkout at register
{
if (this.Cart > 0) OnBuy?.Invoke();
if (!this.SpawnInBag) // When its a big item run this, if it spawns in bag SpawnBag gets called
{
for (int i = 0; i < Cart; i++)
{
GameObject item = GameObject.Instantiate(ItemPrefab);
BoughtItems.Add(item);
item.transform.position = bigItemSpawnPosition;
item.SetActive(true);
}
itemsBought += Cart;
Cart = 0;
}
}
}
}