Skip to content

Commit

Permalink
feat: Add OnSpawned callback to Coordinated Spawns SpawnInfos (Subnau…
Browse files Browse the repository at this point in the history
…ticaModding#551)

Add OnSpawned callback to coordinated SpawnInfo
  • Loading branch information
LeeTwentyThree authored Jun 9, 2024
1 parent 39b5499 commit adc1bb3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
46 changes: 35 additions & 11 deletions Nautilus/Handlers/CoordinatedSpawnsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,23 @@ public struct SpawnInfo : IEquatable<SpawnInfo>
internal Vector3 Scale { get; }
// For the sake of backwards compatibility, a scale of 0x0x0 is automatically converted to 1x1x1. Sorry, no 0x scale entities allowed.
internal Vector3 ActualScale => Scale == default ? Vector3.one : Scale;
internal Action<GameObject> OnSpawned { get; }

/// <summary>
/// Initializes a new <see cref="SpawnInfo"/>.
/// </summary>
/// <param name="techType">TechType to spawn.</param>
/// <param name="spawnPosition">Position to spawn into.</param>
public SpawnInfo(TechType techType, Vector3 spawnPosition)
: this(default, techType, spawnPosition, Quaternion.identity, Vector3.one) { }
: this(default, techType, spawnPosition, Quaternion.identity, Vector3.one, null) { }

/// <summary>
/// Initializes a new <see cref="SpawnInfo"/>.
/// </summary>
/// <param name="classId">ClassID to spawn.</param>
/// <param name="spawnPosition">Position to spawn into.</param>
public SpawnInfo(string classId, Vector3 spawnPosition)
: this(classId, default, spawnPosition, Quaternion.identity, Vector3.one) { }
: this(classId, default, spawnPosition, Quaternion.identity, Vector3.one, null) { }

/// <summary>
/// Initializes a new <see cref="SpawnInfo"/>.
Expand All @@ -101,7 +102,7 @@ public SpawnInfo(string classId, Vector3 spawnPosition)
/// <param name="spawnPosition">Position to spawn into.</param>
/// <param name="rotation">Rotation to spawn at.</param>
public SpawnInfo(TechType techType, Vector3 spawnPosition, Quaternion rotation)
: this(default, techType, spawnPosition, rotation, Vector3.one) { }
: this(default, techType, spawnPosition, rotation, Vector3.one, null) { }

/// <summary>
/// Initializes a new <see cref="SpawnInfo"/>.
Expand All @@ -110,7 +111,7 @@ public SpawnInfo(TechType techType, Vector3 spawnPosition, Quaternion rotation)
/// <param name="spawnPosition">Position to spawn into.</param>
/// <param name="rotation">Rotation to spawn at.</param>
public SpawnInfo(string classId, Vector3 spawnPosition, Quaternion rotation)
: this(classId, default, spawnPosition, rotation, Vector3.one) { }
: this(classId, default, spawnPosition, rotation, Vector3.one, null) { }

/// <summary>
/// Initializes a new <see cref="SpawnInfo"/>.
Expand All @@ -120,7 +121,7 @@ public SpawnInfo(string classId, Vector3 spawnPosition, Quaternion rotation)
/// <param name="rotation">Rotation to spawn at.</param>
/// <param name="scale">Scale to spawn with.</param>
public SpawnInfo(TechType techType, Vector3 spawnPosition, Quaternion rotation, Vector3 scale)
: this(default, techType, spawnPosition, rotation, scale) { }
: this(default, techType, spawnPosition, rotation, scale, null) { }

/// <summary>
/// Initializes a new <see cref="SpawnInfo"/>.
Expand All @@ -130,7 +131,7 @@ public SpawnInfo(TechType techType, Vector3 spawnPosition, Quaternion rotation,
/// <param name="rotation">Rotation to spawn at.</param>
/// <param name="scale">Scale to spawn with.</param>
public SpawnInfo(string classId, Vector3 spawnPosition, Quaternion rotation, Vector3 scale)
: this(classId, default, spawnPosition, rotation, scale) { }
: this(classId, default, spawnPosition, rotation, scale, null) { }

/// <summary>
/// Initializes a new <see cref="SpawnInfo"/>.
Expand All @@ -139,7 +140,7 @@ public SpawnInfo(string classId, Vector3 spawnPosition, Quaternion rotation, Vec
/// <param name="spawnPosition">Position to spawn into.</param>
/// <param name="rotation">Rotation to spawn at.</param>
public SpawnInfo(TechType techType, Vector3 spawnPosition, Vector3 rotation)
: this(default, techType, spawnPosition, Quaternion.Euler(rotation), Vector3.one) { }
: this(default, techType, spawnPosition, Quaternion.Euler(rotation), Vector3.one, null) { }

/// <summary>
/// Initializes a new <see cref="SpawnInfo"/>.
Expand All @@ -148,7 +149,7 @@ public SpawnInfo(TechType techType, Vector3 spawnPosition, Vector3 rotation)
/// <param name="spawnPosition">Position to spawn into.</param>
/// <param name="rotation">Rotation to spawn at.</param>
public SpawnInfo(string classId, Vector3 spawnPosition, Vector3 rotation)
: this(classId, default, spawnPosition, Quaternion.Euler(rotation), Vector3.one) { }
: this(classId, default, spawnPosition, Quaternion.Euler(rotation), Vector3.one, null) { }

/// <summary>
/// Initializes a new <see cref="SpawnInfo"/>.
Expand All @@ -158,7 +159,7 @@ public SpawnInfo(string classId, Vector3 spawnPosition, Vector3 rotation)
/// <param name="rotation">Rotation to spawn at.</param>
/// <param name="scale">Scale to spawn with.</param>
public SpawnInfo(TechType techType, Vector3 spawnPosition, Vector3 rotation, Vector3 scale)
: this(default, techType, spawnPosition, Quaternion.Euler(rotation), scale) { }
: this(default, techType, spawnPosition, Quaternion.Euler(rotation), scale, null) { }

/// <summary>
/// Initializes a new <see cref="SpawnInfo"/>.
Expand All @@ -168,10 +169,32 @@ public SpawnInfo(TechType techType, Vector3 spawnPosition, Vector3 rotation, Vec
/// <param name="rotation">Rotation to spawn at.</param>
/// <param name="scale">Scale to spawn with.</param>
public SpawnInfo(string classId, Vector3 spawnPosition, Vector3 rotation, Vector3 scale)
: this(classId, default, spawnPosition, Quaternion.Euler(rotation), scale) { }
: this(classId, default, spawnPosition, Quaternion.Euler(rotation), scale, null) { }

/// <summary>
/// Initializes a new <see cref="SpawnInfo"/>.
/// </summary>
/// <param name="techType">TechType to spawn.</param>
/// <param name="spawnPosition">Position to spawn into.</param>
/// <param name="rotation">Rotation to spawn at.</param>
/// <param name="scale">Scale to spawn with.</param>
/// <param name="onSpawned">Callback that is used when the object is successfully spawned.</param>
public SpawnInfo(TechType techType, Vector3 spawnPosition, Quaternion rotation, Vector3 scale, Action<GameObject> onSpawned)
: this(default, techType, spawnPosition, rotation, scale, onSpawned) { }

/// <summary>
/// Initializes a new <see cref="SpawnInfo"/>.
/// </summary>
/// <param name="classId">ClassID to spawn.</param>
/// <param name="spawnPosition">Position to spawn into.</param>
/// <param name="rotation">Rotation to spawn at.</param>
/// <param name="scale">Scale to spawn with.</param>
/// <param name="onSpawned">Callback that is used when the object is successfully spawned.</param>
public SpawnInfo(string classId, Vector3 spawnPosition, Quaternion rotation, Vector3 scale, Action<GameObject> onSpawned)
: this(classId, default, spawnPosition, rotation, scale, onSpawned) { }

[JsonConstructor]
internal SpawnInfo(string classId, TechType techType, Vector3 spawnPosition, Quaternion rotation, Vector3 scale)
internal SpawnInfo(string classId, TechType techType, Vector3 spawnPosition, Quaternion rotation, Vector3 scale, Action<GameObject> onSpawned)
{
ClassId = classId;
TechType = techType;
Expand All @@ -183,6 +206,7 @@ internal SpawnInfo(string classId, TechType techType, Vector3 spawnPosition, Qua
_ => SpawnType.TechType
};
Scale = scale;
OnSpawned = onSpawned;
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions Nautilus/MonoBehaviours/EntitySpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ private IEnumerator SpawnAsync()

obj.SetActive(true);

spawnInfo.OnSpawned?.Invoke(obj);

LargeWorldEntity.Register(obj);

LargeWorldStreamerPatcher.SavedSpawnInfos.Add(spawnInfo);
Expand Down

0 comments on commit adc1bb3

Please sign in to comment.