forked from UrFriendKen/PlateUpDecorOnDemand
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnHandlerSystem.cs
More file actions
201 lines (183 loc) · 8.78 KB
/
SpawnHandlerSystem.cs
File metadata and controls
201 lines (183 loc) · 8.78 KB
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
using Kitchen;
using KitchenData;
using KitchenMods;
using System;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using UnityEngine;
using System.Linq;
namespace KitchenApplianceShop
{
public enum SpawnType
{
Decor,
Appliance
}
public enum SpawnPositionType
{
Door,
Player
}
public abstract class SpawnHandlerSystemBase : GenericSystemBase
{
protected abstract Type GDOType { get; }
protected virtual bool UseFallbackTile => false;
EntityQuery Players;
protected override void Initialise()
{
base.Initialise();
Players = GetEntityQuery(typeof(CPlayer), typeof(CPosition));
Main.EntityManager = EntityManager;
}
internal static int GetKnownPrice(EntityManager entityManager, int applianceID)
{
var buyPrice = GameData.Main.Get<Appliance>(applianceID).PurchaseCost;
// We look for CApplianceInfo with CImmovable
// These are our price trackers
// Why? Because this mod should easily be removable, if we use entities without views, they will never get one, easy!
var query = entityManager.CreateEntityQuery(new QueryHelper().All(typeof(CApplianceInfo), typeof(CImmovable)).None(typeof(CLinkedView), typeof(CRequiresView)).Build());
var appliances = query.ToComponentDataArray<CApplianceInfo>(Allocator.Temp);
try
{
foreach (var appliance in appliances.Where(appliance => appliance.ID == applianceID))
{
buyPrice = Math.Min(buyPrice, appliance.Price);
}
}
finally
{
appliances.Dispose();
}
return buyPrice;
}
protected override void OnUpdate()
{
if (GDOType != null && !SpawnRequestSystem.IsHandled && GDOType == SpawnRequestSystem.Current?.GDO?.GetType())
{
if (HasSingleton<SMoney>())
{
int thisPurchaseCost = 0;
float costMultipler = Main.PrefManager.Get<float>(Main.APPLIANCE_BLUEPRINT_COST_ID);
var appliancePrice = GetKnownPrice(EntityManager, SpawnRequestSystem.Current.GDO.ID);
SMoney money = GetSingleton<SMoney>();
if (SpawnRequestSystem.Current.GDO.ID == Main.saleApplianceID)
thisPurchaseCost = (int)(money.Amount * Main.PrefManager.Get<float>(Main.SHOP_TAX_ID))
+ (int)(appliancePrice * costMultipler * Main.salePrices);
else
{
thisPurchaseCost = (int)(money.Amount * Main.PrefManager.Get<float>(Main.SHOP_TAX_ID))
+ (int)(Main.PrefManager.Get<float>(Main.SHOP_SERVICEFEE_ID))
+ (int)(appliancePrice * costMultipler);
if (Main.PrefManager.Get<SpawnApplianceMode>(Main.APPLIANCE_SPAWN_AS_ID) == SpawnApplianceMode.Blueprint)
thisPurchaseCost -= appliancePrice;
if (thisPurchaseCost < 0) thisPurchaseCost = 0;
Main.LogInfo($"Appliance Price:{appliancePrice}");
Main.LogInfo($"Tax: {money.Amount * Main.PrefManager.Get<float>(Main.SHOP_TAX_ID)}");
Main.LogInfo($"Fee: {Main.PrefManager.Get<float>(Main.SHOP_SERVICEFEE_ID)}");
Main.LogInfo($"Money: {money.Amount}");
}
if (SpawnRequestSystem.Current.IsFree) thisPurchaseCost = 0;
if (
money.Amount - thisPurchaseCost
< 0
//|| money.Amount < 250
)
{
string description = string.Format("Too expensive.\nAppliance cost = ${0}\nTax and Fees = ${1}\nTotal = ${2}"
, (int)(appliancePrice * Main.PrefManager.Get<float>(Main.APPLIANCE_BLUEPRINT_COST_ID))
, (int)(money.Amount * Main.PrefManager.Get<float>(Main.SHOP_TAX_ID)
+ Main.PrefManager.Get<float>(Main.SHOP_SERVICEFEE_ID))
, thisPurchaseCost);
if (SpawnRequestSystem.Current.GDO.ID == Main.saleApplianceID) description =
string.Format("Too expensive.\nAppliance cost = ${0}(SALE)\nTax = ${1}\nTotal = ${2}"
, (int)(appliancePrice * costMultipler * Main.salePrices)
, (int)(money.Amount * Main.PrefManager.Get<float>(Main.SHOP_TAX_ID))
, thisPurchaseCost);
ApplianceShopSystem.SelectedApplianceCost = thisPurchaseCost;
KitchenLib.UI.GenericPopupManager.CreatePopup("Appliance Shop", description);
SpawnRequestSystem.RequestHandled();
return;
}
money.Amount -= thisPurchaseCost;
if (money.Amount < 0)
money.Amount = 0;
Main.AppliancePurchases += thisPurchaseCost;
SetSingleton(money);
}
Vector3 position = UseFallbackTile ? GetFallbackTile() : GetFrontDoor(get_external_tile: true);
using NativeArray<CPlayer> players = Players.ToComponentDataArray<CPlayer>(Allocator.Temp);
using NativeArray<CPosition> playerPositions = Players.ToComponentDataArray<CPosition>(Allocator.Temp);
switch (SpawnRequestSystem.Current.PositionType)
{
case SpawnPositionType.Player:
bool positionSet = false;
for (int i = 0; i < players.Length; i++)
{
CPlayer player = players[i];
CPosition playerPosition = playerPositions[i];
bool match = player.InputSource == SpawnRequestSystem.Current.InputIdentifier;
if (!positionSet || match)
{
positionSet = true;
position = playerPosition;
}
if (match)
break;
}
break;
case SpawnPositionType.Door:
default:
break;
}
if (SpawnRequestSystem.Current.GDO.ID == Main.saleApplianceID) Main.saleApplianceID = -1;
SpawnRequestSystem.RequestHandled();
Spawn(SpawnRequestSystem.Current.GDO, position, SpawnRequestSystem.Current.SpawnApplianceMode);
}
}
protected abstract void Spawn(GameDataObject gameDataObject, Vector3 position, SpawnApplianceMode spawnApplianceMode);
}
public class SpawnRequest
{
public GameDataObject GDO;
public SpawnPositionType PositionType;
public int InputIdentifier;
public SpawnApplianceMode SpawnApplianceMode;
public bool IsFree;
}
public class SpawnRequestSystem : GenericSystemBase, IModSystem
{
static Queue<SpawnRequest> requests = new Queue<SpawnRequest>();
public static SpawnRequest Current { get; private set; } = null;
public static bool IsHandled { get; private set; } = false;
protected override void OnUpdate()
{
if (requests.Count > 0)
{
IsHandled = false;
Current = requests.Dequeue();
return;
}
Current = null;
IsHandled = true;
}
public static void Request<T>(int gdoID, SpawnPositionType positionType, int inputIdentifier = 0, SpawnApplianceMode spawnApplianceMode = default, bool isFree = false) where T : GameDataObject, new()
{
if (gdoID != 0 && GameInfo.CurrentScene == SceneType.Kitchen && GameData.Main.TryGet(gdoID, out T gdo, warn_if_fail: true))
{
requests.Enqueue(new SpawnRequest()
{
GDO = gdo,
PositionType = positionType,
InputIdentifier = inputIdentifier,
SpawnApplianceMode = spawnApplianceMode,
IsFree = isFree
});
}
}
public static void RequestHandled()
{
IsHandled = true;
}
}
}