-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomStateSystem.cs
More file actions
427 lines (403 loc) · 20.4 KB
/
CustomStateSystem.cs
File metadata and controls
427 lines (403 loc) · 20.4 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
using System;
using System.Collections.Generic;
using CoreLib.Submodules.ModComponent;
using CoreLib.Submodules.ModSystem;
using CoreLib.Util.Extensions;
using PugTilemap;
using Rewired;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Transforms;
using UnityEngine;
using Color = System.Drawing.Color;
using Math = Unity.Physics.Math;
using Random = UnityEngine.Random;
namespace PainMod;
public class CustomStateSystem : MonoBehaviour, IPseudoServerSystem, IStateRequester
{
public static CustomStateSystem instance;
private World serverWorld;
private EntityQuery entityQuery;
private EntityQuery markerQuery;
//private EntityQuery shamanQuery;
public const string STATE_ID = "PainMod:OctopusState1";
public static StateID octopusFirstState = SystemModule.GetModStateId(STATE_ID);
private static NativeArray<Entity> entitiesArray;
private ModComponentDataFromEntity<OctopusModdedStateCD> octopusModStateGroup;
private void Awake()
{
instance = this;
}
public void OnServerStarted(World world)
{
serverWorld = world;
entityQuery = serverWorld.EntityManager.CreateEntityQuery(
ComponentModule.ReadOnly<OctopusModdedStateCD>(),
ComponentModule.ReadWrite<Translation>());
markerQuery = serverWorld.EntityManager.CreateEntityQuery(
new EntityQueryDesc
{
All = new[]
{
ComponentModule.ReadOnly<BossSpawnLocationCD>()
},
Any = Array.Empty<ComponentType>(),
None = new[] { ComponentModule.ReadOnly<EntityDestroyedCD>() },
Options = EntityQueryOptions.IncludeDisabled
});
//shamanQuery = serverWorld.EntityManager.CreateEntity()
foreach (Entity ent in markerQuery.ToEntityArray(Allocator.Temp))
{
Plugin.logger.LogInfo("Position of the enemy is " + serverWorld.EntityManager.GetModComponentData<BossSpawnLocationCD>(ent).bossID +
" at " + serverWorld.EntityManager.GetModComponentData<Translation>(ent).Value);
if(serverWorld.EntityManager.GetModComponentData<BossSpawnLocationCD>(ent).bossID == ObjectID.OctopusBoss)
Constants.OctopusMarkerPos = serverWorld.EntityManager.GetModComponentData<Translation>(ent).Value;
else if(serverWorld.EntityManager.GetModComponentData<BossSpawnLocationCD>(ent).bossID == ObjectID.BirdBoss)
Constants.BirdMarkerPos = serverWorld.EntityManager.GetModComponentData<Translation>(ent).Value;
}
hasSpawnedInEnemies = false;
hasTeleportOutOfState = false;
}
public void OnServerStopped()
{
serverWorld = null;
}
public void OnCreate(World world)
{
octopusModStateGroup = new ModComponentDataFromEntity<OctopusModdedStateCD>(world.EntityManager);
//for real
}
public bool ShouldUpdate(Entity entity, ref StateRequestData data, ref StateRequestContainers containers)
{
return octopusModStateGroup.HasComponent(entity);
}
private EntityQuery query;
public static bool hasTeleportOutOfState = false;
public static bool hasStartedSecondState = false;
public static bool isDoingForcedTeleport = false;
public bool OnUpdate(Entity entity, EntityCommandBuffer ecb, ref StateRequestData data, ref StateRequestContainers containers,
ref StateInfoCD stateInfo)
{
// Does this entity have our custom component?
if (!octopusModStateGroup.HasComponent(entity)) return false;
// Get needed data
HealthCD healthCd = containers._healthGroup[entity];
OctopusModdedStateCD OctopusStateCd = octopusModStateGroup[entity];
DamageReductionCD damageReductionCd = containers._damageReductionGroup[entity];
OctopusBossSpawnTentaclesStateCD bossSpawnTentaclesStateCd = containers._spawnTentacleGroup[entity];
RangeAttackStateCD rangeAttackStateCd = containers._rangeStateGroup[entity];
TeleportStateCD teleportStateCd = containers._teleportStateGroup[entity];
IsInCombatCD inCombatCd = containers._isInCombatGroup[entity];
float healthPercent = healthCd.health / (float) healthCd.maxHealth;
//Plugin.logger.LogInfo("Health percent is " + healthPercent + " and ratio to enter state is " + OctopusStateCd.HpRatioToEnterState);
// If the entity has too low HP enter flee state
if (stateInfo.currentState == octopusFirstState)
{
stateInfo.newState = octopusFirstState;
if (allEnemiesDead)
{
ChatWindow_Patch.SendMessage("All enemies are dead now!", UnityEngine.Color.red);
//stateInfo.newState = StateID.Teleport;
damageReductionCd.reduction = 0;
serverWorld.EntityManager.SetModComponentData(entity, damageReductionCd);
stateInfo.LeaveState();
stateInfo.EnterState(StateID.RangeAttack);
return true;
}
if (healthCd.HasFullHealth)
{
stateInfo.LeaveState();
stateInfo.EnterState(StateID.OctopusBossLurkingBelow);
return true;
}
// By returning true here we signal that the 'stateInfo' field has changed
return true;
}
if (isDoingForcedTeleport)
teleportStateCd.targetDestination = Constants.OctopusMarkerPos;
if (stateInfo.currentState != octopusFirstState &&
healthPercent < OctopusStateCd.HpRatioToEnterState && !hasSpawnedInEnemies)
{
if (!isDoingForcedTeleport)
{
stateInfo.newState = StateID.Teleport;
isDoingForcedTeleport = true;
}
else if(stateInfo.newState!=StateID.Teleport)
{
stateInfo.newState = octopusFirstState;
isDoingForcedTeleport = false;
}
// By returning true here we signal that the 'stateInfo' field has changed
return true;
}
/*if (existingTenticleCount >= OctopusStateCd.maxTentacleCap && stateInfo.newState==StateID.OctopusBossSpawnTentacles)
{
bossSpawnTentaclesStateCd.cooldownTimer.Start(data._elapsedTime, 100000);
rangeAttackStateCd.isDisabled = false;
stateInfo.newState = StateID.RangeAttack;
stateInfo.locked = true;
rangeAttackStateCd.internalState = 1;
return true;
} */
/*
else if(inCombatCd.isInCombat)
bossSpawnTentaclesStateCd.cooldownTimer.Start(data._elapsedTime, bossSpawnTentaclesStateCd.minCooldown);
*/
if (stateInfo.newState == StateID.OctopusBossSpawnTentacles || stateInfo.currentState == StateID.OctopusBossSpawnTentacles)
{
Plugin.logger.LogInfo("Going to spawn tentacle state at " + existingTenticleCount + " tentacles");
return false;
}
if (stateInfo.currentState != octopusFirstState && healthPercent < OctopusStateCd.HpRatioToEnterState2 && !hasStartedSecondState)
{
hasSpawnedInEnemies = false;
OctopusStateCd.iteration = 2;
serverWorld.EntityManager.SetModComponentData(entity, OctopusStateCd);
hasStartedSecondState = true;
}
// Nothing changed
return false;
}
private static float3[] positionsRelative = {new float3(4,0,1), new float3(12,0, 2), new float3(13,0,9), new float3(11,0, -8), new float3(6,0,5),
new float3(-3, 0, 3), new float3(-10, 0, 1), new float3(-1, 0, -4), new float3(-13, 0, 8), new float3(-6, 0, 7), new float3(-4, 0, -8),
new float3(-4, 0, -14), new float3(-11, 0, -11), new float3(6, 0, -5), new float3(4, 0, -12)};
private static bool hasSpawnedInEnemies = false;
private EntityCommandBuffer EntityCommandBuffer;
private static bool allEnemiesDead;
private static int existingTenticleCount = 0;
private void SetDafaults(ObjectID bossID)
{
switch (bossID)
{
case ObjectID.OctopusBoss:
hasSpawnedInEnemies = false;
hasStartedSecondState = false;
isDoingForcedTeleport = false;
existingTenticleCount = 0;
allEnemiesDead = false;
var q3 = serverWorld.EntityManager.CreateEntityQuery(
ComponentModule.ReadOnly<MustBeDestroyedForOctopusLeaveStateCD>(),
ComponentModule.ReadWrite<Translation>());
foreach (var entity in q3.ToEntityArray(Allocator.Temp))
{
serverWorld.EntityManager.DestroyEntity(entity);
}
var q4 = serverWorld.EntityManager.CreateEntityQuery(
ComponentModule.ReadOnly<OctopusTenticleCounterCD>(),
ComponentModule.ReadWrite<Translation>());
foreach (var entity in q4.ToEntityArray(Allocator.Temp))
{
serverWorld.EntityManager.DestroyEntity(entity);
}
var q5 = serverWorld.EntityManager.CreateEntityQuery(
ComponentModule.ReadOnly<DoesCircleCD>(),
ComponentModule.ReadWrite<Translation>());
foreach (var entity in q5.ToEntityArray(Allocator.Temp))
{
serverWorld.EntityManager.DestroyEntity(entity);
}
var temp = serverWorld.EntityManager.CreateEntityQuery(
ComponentModule.ReadOnly<OctopusModdedStateCD>(),
ComponentModule.ReadWrite<Translation>());
foreach (var e in temp.ToEntityArray(Allocator.Temp))
{
DamageReductionCD reductionCd = serverWorld.EntityManager.GetModComponentData<DamageReductionCD>(e);
reductionCd.reduction = 0;
serverWorld.EntityManager.SetModComponentData(e, reductionCd);
}
//Plugin.logger.LogInfo("Octopus is de-spawning");
break;
}
}
private void FixedUpdate()
{
if (serverWorld == null) return;
// Execute our query and itterate the entities
entitiesArray = entityQuery.ToEntityArray(Allocator.Temp);
foreach (Entity e in entitiesArray)
{
StateInfoCD stateInfo = serverWorld.EntityManager.GetModComponentData<StateInfoCD>(e);
OctopusModdedStateCD moddedStateCd = serverWorld.EntityManager.GetModComponentData<OctopusModdedStateCD>(e);
DamageReductionCD reductionCd = serverWorld.EntityManager.GetModComponentData<DamageReductionCD>(e);
RotatingBeamCD rotatingBeamCd = serverWorld.EntityManager.GetModComponentData<RotatingBeamCD>(e);
HealthCD healthCd = serverWorld.EntityManager.GetModComponentData<HealthCD>(e);
if(healthCd.HasFullHealth)
SetDafaults(ObjectID.OctopusBoss);
//Plugin.logger.LogInfo("Current state is " + stateInfo.currentState);
if (stateInfo.currentState == octopusFirstState)
{
if(healthCd.HasFullHealth)
return;
if (!hasSpawnedInEnemies)
{
Plugin.logger.LogInfo("IN OCTOPUS MODDED STATE");
if(moddedStateCd.iteration==1)
SpawnInvincibilityEntities(4);
else if (moddedStateCd.iteration==2)
SpawnInvincibilityEntities(7);
//do invincible stuff
reductionCd.reduction = 1000000;
serverWorld.EntityManager.SetModComponentData(e, reductionCd);
hasSpawnedInEnemies = true;
if (serverWorld.EntityManager.HasModComponent<RotatingBeamCD>(e))
CreateRotatingBeams(rotatingBeamCd);
}
if(serverWorld.EntityManager.HasModComponent<RotatingBeamCD>(e))
DoRotations(rotatingBeamCd, e);
query = serverWorld.EntityManager.CreateEntityQuery(
ComponentModule.ReadOnly<MustBeDestroyedForOctopusLeaveStateCD>(),
ComponentModule.ReadWrite<Translation>());
int enemiesLeft = 0;
foreach (var ent in query.ToEntityArray(Allocator.Temp))
{
if (serverWorld.EntityManager.GetModComponentData<HealthCD>(ent).health > 0)
enemiesLeft++;
}
Plugin.logger.LogInfo("Amount of entities with this thing is " + enemiesLeft);
allEnemiesDead = enemiesLeft == 0;
EntityQuery circleQuery1= serverWorld.EntityManager.CreateEntityQuery(
ComponentModule.ReadOnly<ProjectileCD>(),
ComponentModule.ReadOnly<FactionCD>(),
ComponentModule.ReadOnly<DoesCircleCD>(),
ComponentModule.ReadOnly<OwnerCD>(),
ComponentModule.ReadOnly<DestroyTimerCD>(),
ComponentModule.ReadWrite<Translation>());
foreach (var ent in circleQuery1.ToEntityArray(Allocator.Temp))
{
Plugin.logger.LogInfo("Removing destroy timer component");
ComponentType componentType = ComponentType.FromTypeIndex(ComponentModule.GetModTypeIndex<DestroyTimerCD>());
serverWorld.EntityManager.RemoveComponent(ent, componentType);
}
if (allEnemiesDead)
{
EntityQuery circleQuery2 = serverWorld.EntityManager.CreateEntityQuery(
ComponentModule.ReadOnly<ProjectileCD>(),
ComponentModule.ReadOnly<FactionCD>(),
ComponentModule.ReadOnly<DoesCircleCD>(),
ComponentModule.ReadOnly<OwnerCD>(),
ComponentModule.ReadWrite<Translation>());
foreach (var ent in circleQuery2.ToEntityArray(Allocator.Temp))
{
Plugin.logger.LogInfo("Destroying entity");
serverWorld.EntityManager.DestroyEntity(ent);
}
}
}
else if (stateInfo.currentState == StateID.Teleport)
{
if (isDoingForcedTeleport)
{
TeleportStateCD teleportStateCd = serverWorld.EntityManager.GetModComponentData<TeleportStateCD>(e);
teleportStateCd.targetDestination = Constants.OctopusMarkerPos;
serverWorld.EntityManager.SetModComponentData(e, teleportStateCd);
}
}
else if (stateInfo.currentState == StateID.OctopusBossSpawnTentacles)
{
if (existingTenticleCount >= moddedStateCd.maxTentacleCap)
{
OctopusBossSpawnTentaclesStateCD spawnTentaclesStateCd =
serverWorld.EntityManager.GetModComponentData<OctopusBossSpawnTentaclesStateCD>(e);
if (spawnTentaclesStateCd.internalState == 2)
spawnTentaclesStateCd.internalState = 3;
serverWorld.EntityManager.SetModComponentData(e, spawnTentaclesStateCd);
}
}
EntityQuery q2 = serverWorld.EntityManager.CreateEntityQuery(
ComponentModule.ReadOnly<OctopusTenticleCounterCD>(),
ComponentModule.ReadWrite<Translation>());
int ct = 0;
foreach (var ent2 in q2.ToEntityArray(Allocator.Temp))
{
if (serverWorld.EntityManager.GetModComponentData<HealthCD>(ent2).health > 0)
ct++;
}
existingTenticleCount = ct;
}
}
private float timer;
private static int deleted = -1;
private void CreateRotatingBeams(RotatingBeamCD rotatingBeamCd)
{
timer = Random.Range(0, Mathf.PI);
EntityQuery query2 = Manager.ecs.ServerWorld.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<PugDatabase.DatabaseBankCD>());
BlobAssetReference<PugDatabase.PugDatabaseBank> bank = query2.GetSingleton<PugDatabase.DatabaseBankCD>()
.databaseBankBlob;
EntityCommandBuffer = new EntityCommandBuffer(Allocator.Temp);
List<float3> posLeft = new List<float3>(positionsRelative);
for (int i = 0; i < rotatingBeamCd.amount-2; i++)
{
Entity orb = EntityUtility.CreateEntity(EntityCommandBuffer, Constants.OctopusMarkerPos
, rotatingBeamCd.ObjectID, 1, bank, 0);
EntityCommandBuffer.AddModComponent<DoesCircleCD>(orb);
}
deleted = Random.Range(4, rotatingBeamCd.amount);
Plugin.logger.LogInfo("Deleted is " + deleted + "one");
EntityCommandBuffer.Playback(Manager.ecs.ServerWorld.EntityManager);
EntityCommandBuffer.Dispose();
}
private void DoRotations(RotatingBeamCD rotatingBeamCd, Entity owner)
{
timer += Time.fixedDeltaTime * rotatingBeamCd.speed;
EntityQuery circleQuery = serverWorld.EntityManager.CreateEntityQuery(
ComponentModule.ReadOnly<ProjectileCD>(),
ComponentModule.ReadOnly<FactionCD>(),
ComponentModule.ReadOnly<DoesCircleCD>(),
ComponentModule.ReadOnly<OwnerCD>(),
ComponentModule.ReadWrite<Translation>());
int i = 1;
foreach (var ent in circleQuery.ToEntityArray(Allocator.Temp))
{
//Plugin.logger.LogInfo("I is: " + i);
if (i == deleted-1)
{
//Plugin.logger.LogInfo("Deleted " + deleted + "and i is " + i);
i+=2;
}
OwnerCD ownerCd = serverWorld.EntityManager.GetModComponentData<OwnerCD>(ent);
ownerCd.owner = owner;
serverWorld.EntityManager.SetModComponentData(ent, ownerCd);
Translation temp = serverWorld.EntityManager.GetModComponentData<Translation>(ent);
temp.Value = Constants.OctopusMarkerPos + new float3(i * Mathf.Cos(timer), 0, i * Mathf.Sin(timer));
serverWorld.EntityManager.SetModComponentData(ent, temp);
ProjectileCD projectileCd = serverWorld.EntityManager.GetModComponentData<ProjectileCD>(ent);
projectileCd.damage = 420;
serverWorld.EntityManager.SetModComponentData(ent, projectileCd);
FactionCD factionCd = serverWorld.EntityManager.GetModComponentData<FactionCD>(ent);
factionCd.originalFaction = FactionID.SeaCreature;
factionCd.faction = FactionID.SeaCreature;
serverWorld.EntityManager.SetModComponentData(ent, factionCd);
i++;
}
}
private unsafe void SpawnInvincibilityEntities(int amount)
{
Plugin.logger.LogInfo("Testing that this works");
EntityQuery query2 = Manager.ecs.ServerWorld.EntityManager.CreateEntityQuery(ComponentType.ReadOnly<PugDatabase.DatabaseBankCD>());
BlobAssetReference<PugDatabase.PugDatabaseBank> bank = query2.GetSingleton<PugDatabase.DatabaseBankCD>()
.databaseBankBlob;
EntityCommandBuffer = new EntityCommandBuffer(Allocator.Temp);
Plugin.logger.LogInfo("Entity command buffer data null: " + (EntityCommandBuffer.m_Data == null));
List<float3> posLeft = new List<float3>(positionsRelative);
for (int i = 0; i < amount; i++)
{
//rand gen
int rand = Random.Range(0, posLeft.Count);
/*Manager._instance.player.playerCommandSystem.CreateEntity(ObjectID.CrabEnemy,
(Constants.OctopusMarkerPos + posLeft[rand]) - Manager._instance.player.WorldPosition.ToFloat3());*/
//testing with server side version (not working)
Entity orb = EntityUtility.CreateEntity(EntityCommandBuffer,Constants.OctopusMarkerPos + posLeft[rand]
, Plugin.octopusOrb, 1, bank, 0);
EntityCommandBuffer.AddModComponent<MustBeDestroyedForOctopusLeaveStateCD>(orb);
posLeft.Remove(posLeft[rand]);
}
EntityCommandBuffer.Playback(Manager.ecs.ServerWorld.EntityManager);
EntityCommandBuffer.Dispose();
}
public int priority => SystemModule.HIGHEST_PRIORITY;
//public void SetWorld(World world) {serverWorld = world;}
//public void RemoveWorld() { serverWorld = null; }
}