forked from UrFriendKen/PlateUpDecorOnDemand
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnRequestView.cs
More file actions
167 lines (152 loc) · 6.11 KB
/
Copy pathSpawnRequestView.cs
File metadata and controls
167 lines (152 loc) · 6.11 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
using Controllers;
using Kitchen;
using KitchenData;
using KitchenMods;
using MessagePack;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Unity.Collections;
using Unity.Entities;
using UnityEngine;
namespace KitchenApplianceShop
{
[StructLayout(LayoutKind.Sequential, Size = 1)]
public struct SSpawnRequestView : IComponentData, IModComponent { }
public class SpawnRequestView : ResponsiveObjectView<SpawnRequestView.ViewData, SpawnRequestView.ResponseData>
{
public class UpdateView : ResponsiveViewSystemBase<ViewData, ResponseData>, IModSystem
{
EntityQuery Views;
protected override void Initialise()
{
base.Initialise();
Views = GetEntityQuery(typeof(CLinkedView), typeof(SSpawnRequestView));
}
protected override void OnUpdate()
{
EnsureView();
using NativeArray<CLinkedView> linkedViews = Views.ToComponentDataArray<CLinkedView>(Allocator.Temp);
foreach (CLinkedView view in linkedViews)
{
ApplyUpdates(view.Identifier, PerformUpdateWithResponse, only_final_update: true);
}
}
private void PerformUpdateWithResponse(ResponseData data)
{
if (!Main.PrefManager.Get<bool>(Main.HOST_ONLY_ID) || data.InputIdentifier == InputSourceIdentifier.Identifier)
{
Action<int, SpawnPositionType, int, SpawnApplianceMode> spawnMethod = null;
switch (data.SpawnType)
{
case SpawnType.Appliance:
spawnMethod = SpawnRequestSystem.Request<Appliance>;
break;
case SpawnType.Decor:
spawnMethod = SpawnRequestSystem.Request<Decor>;
break;
}
if (spawnMethod == null)
return;
if (!Enum.TryParse(Main.PrefManager.Get<string>(Main.APPLIANCE_SPAWN_AS_ID), out SpawnApplianceMode spawnApplianceMode))
{
spawnApplianceMode = default;
}
spawnMethod(data.GdoId, data.PositionType, data.InputIdentifier, spawnApplianceMode);
}
}
private void EnsureView()
{
if (!TryGetSingletonEntity<SSpawnRequestView>(out Entity entity))
{
entity = EntityManager.CreateEntity();
Set<SSpawnRequestView>(entity);
Set(entity, new CPersistThroughSceneChanges());
Set(entity, new CDoNotPersist());
Set(entity, new CRequiresView()
{
ViewMode = ViewMode.Screen,
Type = Main.SpawnRequestViewType,
PhysicsDriven = false
});
}
}
}
[MessagePackObject(false)]
public class ViewData : IViewData, IViewResponseData, IViewData.ICheckForChanges<ViewData>
{
public IUpdatableObject GetRelevantSubview(IObjectView view)
{
return view.GetSubView<SpawnRequestView>();
}
public bool IsChangedFrom(ViewData check)
{
return false;
}
}
[MessagePackObject(false)]
public class ResponseData : IResponseData, IViewResponseData
{
[Key(0)] public int InputIdentifier;
[Key(1)] public int GdoId;
[Key(2)] public SpawnType SpawnType;
[Key(3)] public SpawnPositionType PositionType;
[Key(4)] public SpawnApplianceMode SpawnApplianceMode;
}
private Queue<GameDataObject> _requestedGDO = new Queue<GameDataObject>();
private static Dictionary<Type, SpawnType> _typeMap = new Dictionary<Type, SpawnType>()
{
{ typeof(Decor), SpawnType.Decor },
{ typeof(Appliance), SpawnType.Appliance }
};
protected override void UpdateData(ViewData data)
{
}
public override bool HasStateUpdate(out IResponseData state)
{
ActiveSpawnRequestView activeViewMarker = gameObject.GetComponent<ActiveSpawnRequestView>();
if (activeViewMarker == null)
{
activeViewMarker = gameObject.AddComponent<ActiveSpawnRequestView>();
}
activeViewMarker.LinkedView = this;
if (_requestedGDO.Count > 0)
{
if (!Enum.TryParse(Main.PrefManager.Get<string>(Main.SPAWN_AT_ID), out SpawnPositionType positionType))
{
positionType = default;
}
if (!Enum.TryParse(Main.PrefManager.Get<string>(Main.APPLIANCE_SPAWN_AS_ID), out SpawnApplianceMode spawnApplianceMode))
{
spawnApplianceMode = default;
}
GameDataObject gdo = _requestedGDO.Dequeue();
if (_typeMap.TryGetValue(gdo.GetType(), out SpawnType spawnType))
{
state = new ResponseData()
{
InputIdentifier = InputSourceIdentifier.Identifier,
GdoId = gdo.ID,
SpawnType = spawnType,
PositionType = positionType,
SpawnApplianceMode = spawnApplianceMode
};
return true;
}
}
state = default;
return false;
}
public void Request<T>(int gdoID) where T : GameDataObject, new()
{
if (GameData.Main.TryGet(gdoID, out T gdo, warn_if_fail: true))
{
_requestedGDO.Enqueue(gdo);
}
}
}
public class ActiveSpawnRequestView : MonoBehaviour
{
public SpawnRequestView LinkedView;
}
}