Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OnSpawned callback to Coordinated Spawns SpawnInfos #551

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) { }
Metious marked this conversation as resolved.
Show resolved Hide resolved

[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
Loading