forked from SubnauticaNitrox/Nitrox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent entity spawning from stopping when it errors. Implement rando…
…mly-generated-on-the-fly entities
- Loading branch information
1 parent
31f9848
commit 61f2a7c
Showing
20 changed files
with
219 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
NitroxClient/GameLogic/Spawning/WorldEntities/PrefabPlaceholderEntitySpawner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Collections; | ||
using NitroxModel.DataStructures.GameLogic.Entities; | ||
using NitroxModel.DataStructures.Util; | ||
using NitroxModel_Subnautica.DataStructures; | ||
using UnityEngine; | ||
|
||
namespace NitroxClient.GameLogic.Spawning.WorldEntities; | ||
|
||
public class PrefabPlaceholderEntitySpawner : IWorldEntitySpawner, IWorldEntitySyncSpawner | ||
{ | ||
private readonly DefaultWorldEntitySpawner defaultEntitySpawner; | ||
|
||
public PrefabPlaceholderEntitySpawner(DefaultWorldEntitySpawner defaultEntitySpawner) | ||
{ | ||
this.defaultEntitySpawner = defaultEntitySpawner; | ||
} | ||
|
||
// TODO: Clean the spawners (move to to Setup() and IsValidOrError() after rebase) | ||
public IEnumerator SpawnAsync(WorldEntity entity, Optional<GameObject> parent, EntityCell cellRoot, TaskResult<Optional<GameObject>> result) | ||
{ | ||
if (entity is not PrefabPlaceholderEntity prefabEntity) | ||
{ | ||
yield break; | ||
} | ||
if (!parent.Value || !parent.Value.TryGetComponent(out PrefabPlaceholdersGroup group)) | ||
{ | ||
Log.Error($"[{nameof(PrefabPlaceholderEntity)}] Can't find a {nameof(PrefabPlaceholdersGroup)} on parent for {entity.Id}"); | ||
yield break; | ||
} | ||
PrefabPlaceholder placeholder = group.prefabPlaceholders[prefabEntity.ComponentIndex]; | ||
yield return defaultEntitySpawner.SpawnAsync(entity, placeholder.transform.parent.gameObject, cellRoot, result); | ||
if (!result.value.HasValue) | ||
{ | ||
yield break; | ||
} | ||
SetupObject(entity, result.value.Value); | ||
} | ||
|
||
public bool SpawnsOwnChildren() => false; | ||
|
||
public bool SpawnSync(WorldEntity entity, Optional<GameObject> parent, EntityCell cellRoot, TaskResult<Optional<GameObject>> result) | ||
{ | ||
if (entity is not PrefabPlaceholderEntity prefabEntity) | ||
{ | ||
return true; | ||
} | ||
if (!parent.Value || !parent.Value.TryGetComponent(out PrefabPlaceholdersGroup group)) | ||
{ | ||
Log.Error($"[{nameof(PrefabPlaceholderEntity)}] Can't find a {nameof(PrefabPlaceholdersGroup)} on parent for {entity.Id}"); | ||
return true; | ||
} | ||
PrefabPlaceholder placeholder = group.prefabPlaceholders[prefabEntity.ComponentIndex]; | ||
if (!defaultEntitySpawner.SpawnSync(entity, placeholder.transform.parent.gameObject, cellRoot, result)) | ||
{ | ||
return false; | ||
} | ||
SetupObject(entity, result.value.Value); | ||
return true; | ||
} | ||
|
||
private void SetupObject(WorldEntity entity, GameObject gameObject) | ||
{ | ||
gameObject.transform.localPosition = entity.Transform.LocalPosition.ToUnity(); | ||
gameObject.transform.localRotation = entity.Transform.LocalRotation.ToUnity(); | ||
gameObject.transform.localScale = entity.Transform.LocalScale.ToUnity(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,3 @@ | ||
namespace NitroxModel.DataStructures.GameLogic.Entities; | ||
|
||
public record struct UwePrefab(string ClassId, int Count, float Probability, bool IsFragment) | ||
{ | ||
public string ClassId { get; } = ClassId; | ||
public int Count { get; } = Count; | ||
public float Probability { get; set; } = Probability; | ||
public bool IsFragment { get; } = IsFragment; | ||
} | ||
public readonly record struct UwePrefab(string ClassId, int Count, float Probability, bool IsFragment); |
11 changes: 5 additions & 6 deletions
11
NitroxModel/DataStructures/GameLogic/Entities/UwePrefabFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.Generic; | ||
|
||
namespace NitroxModel.DataStructures.GameLogic.Entities | ||
namespace NitroxModel.DataStructures.GameLogic.Entities; | ||
|
||
public interface IUwePrefabFactory | ||
{ | ||
public abstract class UwePrefabFactory | ||
{ | ||
public abstract List<UwePrefab> GetPossiblePrefabs(string biomeType); | ||
} | ||
public abstract List<UwePrefab> GetPossiblePrefabs(string biomeType); | ||
} |
9 changes: 4 additions & 5 deletions
9
NitroxModel/DataStructures/GameLogic/Entities/UweWorldEntityFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
namespace NitroxModel.DataStructures.GameLogic.Entities | ||
namespace NitroxModel.DataStructures.GameLogic.Entities; | ||
|
||
public interface IUweWorldEntityFactory | ||
{ | ||
public abstract class UweWorldEntityFactory | ||
{ | ||
public abstract bool TryFind(string classId, out UweWorldEntity uweWorldEntity); | ||
} | ||
public abstract bool TryFind(string classId, out UweWorldEntity uweWorldEntity); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.