Skip to content

Commit

Permalink
QoL
Browse files Browse the repository at this point in the history
* Added ``RoomSpawnPoint`` for spawning things in a room with a offset property.

* Now Items will be spawned in the MapGenerated event instead of RoundStart avoiding the micro-log for spawning to many pickups in one frame.

* YES YAMATO I USE NULLEABLE
  • Loading branch information
SrLicht committed Aug 24, 2024
1 parent 5fc1b14 commit db60910
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
58 changes: 58 additions & 0 deletions EXILED/Exiled.API/Features/Spawn/RoomSpawnPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// -----------------------------------------------------------------------
// <copyright file="RoomSpawnPoint.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------
namespace Exiled.API.Features.Spawn
{
using System;

using Exiled.API.Enums;

using UnityEngine;

using YamlDotNet.Serialization;

/// <summary>
/// Represents a spawn point within a specific room in the game.
/// </summary>
public class RoomSpawnPoint : SpawnPoint
{
/// <summary>
/// Gets or sets the room type used for this spawn.
/// </summary>
public RoomType Room { get; set; }

/// <summary>
/// Gets or sets the offset position within the room where the spawn point is located, relative to the room's origin.
/// </summary>
public Vector3? Offset { get; set; }

/// <inheritdoc/>
public override float Chance { get; set; }

/// <inheritdoc/>
[YamlIgnore]
public override string Name
{
get => Room.ToString();
set => throw new InvalidOperationException("The name of this type of SpawnPoint cannot be changed.");
}

/// <inheritdoc/>
[YamlIgnore]
public override Vector3 Position
{
get
{
Room roomInstance = Features.Room.Get(Room) ?? throw new InvalidOperationException("The room instance could not be found.");

return Offset.HasValue
? (roomInstance.Type == RoomType.Surface ? Offset.Value : roomInstance.transform.TransformPoint(Offset.Value))
: roomInstance.Position;
}
set => throw new InvalidOperationException("The position of this type of SpawnPoint cannot be changed.");
}
}
}
5 changes: 5 additions & 0 deletions EXILED/Exiled.API/Features/Spawn/SpawnProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class SpawnProperties
/// </summary>
public List<RoleSpawnPoint> RoleSpawnPoints { get; set; } = new();

/// <summary>
/// Gets or sets a <see cref="List{T}"/> of possible room-based spawn points.
/// </summary>
public List<RoomSpawnPoint> RoomSpawnPoints { get; set; } = new();

/// <summary>
/// Counts how many spawn points are in this instance.
/// </summary>
Expand Down
8 changes: 5 additions & 3 deletions EXILED/Exiled.CustomItems/API/Features/CustomItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -694,17 +694,19 @@ public virtual void SpawnAll()
if (SpawnProperties is null)
return;

// This will go over each spawn property type (static, dynamic and role) to try and spawn the item.
// It will attempt to spawn in role-based locations, and then dynamic ones, and finally static.
// This will go over each spawn property type (static, dynamic, role-based, and room-based) to try and spawn the item.
// It will attempt to spawn in role-based locations, then dynamic ones, followed by room-based, and finally static.
// Math.Min is used here to ensure that our recursive Spawn() calls do not result in exceeding the spawn limit config.
// This is the same as:
// int spawned = 0;
// spawned += Spawn(SpawnProperties.RoleSpawnPoints, SpawnProperties.Limit);
// if (spawned < SpawnProperties.Limit)
// spawned += Spawn(SpawnProperties.DynamicSpawnPoints, SpawnProperties.Limit - spawned);
// if (spawned < SpawnProperties.Limit)
// spawned += Spawn(SpawnProperties.RoomSpawnPoints, SpawnProperties.Limit - spawned);
// if (spawned < SpawnProperties.Limit)
// Spawn(SpawnProperties.StaticSpawnPoints, SpawnProperties.Limit - spawned);
Spawn(SpawnProperties.StaticSpawnPoints, Math.Min(0, SpawnProperties.Limit - Math.Min(0, Spawn(SpawnProperties.DynamicSpawnPoints, SpawnProperties.Limit) - Spawn(SpawnProperties.RoleSpawnPoints, SpawnProperties.Limit))));
Spawn(SpawnProperties.StaticSpawnPoints, Math.Min(SpawnProperties.Limit, SpawnProperties.Limit - Math.Min(Spawn(SpawnProperties.DynamicSpawnPoints, SpawnProperties.Limit), SpawnProperties.Limit - Math.Min(Spawn(SpawnProperties.RoleSpawnPoints, SpawnProperties.Limit), SpawnProperties.Limit - Spawn(SpawnProperties.RoomSpawnPoints, SpawnProperties.Limit)))));
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions EXILED/Exiled.CustomItems/CustomItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override void OnEnabled()
roundHandler = new RoundHandler();
playerHandler = new PlayerHandler();

Exiled.Events.Handlers.Server.RoundStarted += roundHandler.OnRoundStarted;
Exiled.Events.Handlers.Map.Generated += roundHandler.OnMapGenerated;

Exiled.Events.Handlers.Player.ChangingItem += playerHandler.OnChangingItem;

Expand All @@ -50,7 +50,7 @@ public override void OnEnabled()
/// <inheritdoc />
public override void OnDisabled()
{
Exiled.Events.Handlers.Server.RoundStarted -= roundHandler!.OnRoundStarted;
Exiled.Events.Handlers.Map.Generated -= roundHandler!.OnMapGenerated;

Exiled.Events.Handlers.Player.ChangingItem -= playerHandler!.OnChangingItem;

Expand Down
4 changes: 2 additions & 2 deletions EXILED/Exiled.CustomItems/Events/RoundHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace Exiled.CustomItems.Events
/// </summary>
internal sealed class RoundHandler
{
/// <inheritdoc cref="Exiled.Events.Handlers.Server.OnRoundStarted"/>
public void OnRoundStarted()
/// <inheritdoc cref="Exiled.Events.Handlers.Map.Generated"/>
public void OnMapGenerated()
{
foreach (CustomItem customItem in CustomItem.Registered)
customItem?.SpawnAll();
Expand Down

0 comments on commit db60910

Please sign in to comment.