-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpawnHandlerSystem.cs
More file actions
114 lines (107 loc) · 4.06 KB
/
SpawnHandlerSystem.cs
File metadata and controls
114 lines (107 loc) · 4.06 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
using Kitchen;
using KitchenData;
using KitchenMods;
using System;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using UnityEngine;
namespace KitchenDecorOnDemand
{
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));
}
protected override void OnUpdate()
{
if (GDOType != null && !SpawnRequestSystem.IsHandled && GDOType == SpawnRequestSystem.Current?.GDO?.GetType())
{
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;
}
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 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) 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
});
}
}
public static void RequestHandled()
{
IsHandled = true;
}
}
}