|
| 1 | +// ----------------------------------------------------------------------- |
| 2 | +// <copyright file="Chamber.cs" company="Exiled Team"> |
| 3 | +// Copyright (c) Exiled Team. All rights reserved. |
| 4 | +// Licensed under the CC BY-SA 3.0 license. |
| 5 | +// </copyright> |
| 6 | +// ----------------------------------------------------------------------- |
| 7 | +namespace Exiled.API.Features.Lockers |
| 8 | +{ |
| 9 | + using System.Collections.Generic; |
| 10 | + using System.Diagnostics; |
| 11 | + using System.Linq; |
| 12 | + |
| 13 | + using Exiled.API.Enums; |
| 14 | + using Exiled.API.Extensions; |
| 15 | + using Exiled.API.Features.Pickups; |
| 16 | + using Exiled.API.Interfaces; |
| 17 | + using MapGeneration.Distributors; |
| 18 | + using UnityEngine; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// A wrapper for <see cref="LockerChamber"/>. |
| 22 | + /// </summary> |
| 23 | + public class Chamber : IWrapper<LockerChamber>, IWorldSpace |
| 24 | + { |
| 25 | + /// <summary> |
| 26 | + /// <see cref="Dictionary{TKey,TValue}"/> with <see cref="LockerChamber"/> and <see cref="Chamber"/>. |
| 27 | + /// </summary> |
| 28 | + internal static readonly Dictionary<LockerChamber, Chamber> Chambers = new(); |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Initializes a new instance of the <see cref="Chamber"/> class. |
| 32 | + /// </summary> |
| 33 | + /// <param name="chamber"><see cref="LockerChamber"/> instance.</param> |
| 34 | + /// <param name="locker"><see cref="Lockers.Locker"/> where this chamber is located.</param> |
| 35 | + public Chamber(LockerChamber chamber, Locker locker) |
| 36 | + { |
| 37 | + Base = chamber; |
| 38 | + Locker = locker; |
| 39 | + |
| 40 | + Chambers.Add(chamber, this); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Gets a <see cref="IEnumerable{T}"/> of <see cref="Chamber"/> which contains all the <see cref="Chamber"/> instances. |
| 45 | + /// </summary> |
| 46 | + public static IReadOnlyCollection<Chamber> List => Chambers.Values; |
| 47 | + |
| 48 | + /// <inheritdoc/> |
| 49 | + public LockerChamber Base { get; } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets the <see cref="Lockers.Locker"/> where this chamber is located at. |
| 53 | + /// </summary> |
| 54 | + public Locker Locker { get; } |
| 55 | + |
| 56 | + /// <inheritdoc/> |
| 57 | + public Vector3 Position => Base.transform.position; |
| 58 | + |
| 59 | + /// <inheritdoc/> |
| 60 | + public Quaternion Rotation => Base.transform.rotation; |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Gets or sets all pickups that should be spawned when the door is initially opened. |
| 64 | + /// </summary> |
| 65 | + public IEnumerable<Pickup> ToBeSpawned |
| 66 | + { |
| 67 | + get => Base._toBeSpawned.Select(Pickup.Get); |
| 68 | + set |
| 69 | + { |
| 70 | + Base._toBeSpawned.Clear(); |
| 71 | + |
| 72 | + foreach (Pickup pickup in value) |
| 73 | + Base._toBeSpawned.Add(pickup.Base); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Gets or sets all spawn points. |
| 79 | + /// </summary> |
| 80 | + /// <remarks> |
| 81 | + /// Used if <see cref="UseMultipleSpawnpoints"/> is set to <see langword="true"/>. |
| 82 | + /// </remarks> |
| 83 | + public IEnumerable<Transform> Spawnpoints |
| 84 | + { |
| 85 | + get => Base._spawnpoints; |
| 86 | + set => Base._spawnpoints = value.ToArray(); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Gets or sets all the acceptable items which can be spawned in this chamber. |
| 91 | + /// </summary> |
| 92 | + public IEnumerable<ItemType> AcceptableTypes |
| 93 | + { |
| 94 | + get => Base.AcceptableItems; |
| 95 | + set => Base.AcceptableItems = value.ToArray(); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Gets or sets required permissions to open this chamber. |
| 100 | + /// </summary> |
| 101 | + public KeycardPermissions RequiredPermissions |
| 102 | + { |
| 103 | + get => (KeycardPermissions)Base.RequiredPermissions; |
| 104 | + set => Base.RequiredPermissions = (Interactables.Interobjects.DoorUtils.KeycardPermissions)value; |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Gets or sets a value indicating whether multiple spawn points should be used. |
| 109 | + /// </summary> |
| 110 | + /// <remarks> |
| 111 | + /// If <see langword="true"/>, <see cref="Spawnpoints"/> will be used over <see cref="Spawnpoint"/>. |
| 112 | + /// </remarks> |
| 113 | + public bool UseMultipleSpawnpoints |
| 114 | + { |
| 115 | + get => Base._useMultipleSpawnpoints; |
| 116 | + set => Base._useMultipleSpawnpoints = value; |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Gets or sets a spawn point for the items in the chamber. |
| 121 | + /// </summary> |
| 122 | + /// <remarks> |
| 123 | + /// Used if <see cref="UseMultipleSpawnpoints"/> is set to <see langword="false"/>. |
| 124 | + /// </remarks> |
| 125 | + public Transform Spawnpoint |
| 126 | + { |
| 127 | + get => Base._spawnpoint; |
| 128 | + set => Base._spawnpoint = value; |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Gets or sets a value indicating whether or not items should be spawned as soon as they one chamber is opened. |
| 133 | + /// </summary> |
| 134 | + public bool InitiallySpawn |
| 135 | + { |
| 136 | + get => Base._spawnOnFirstChamberOpening; |
| 137 | + set => Base._spawnOnFirstChamberOpening = value; |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Gets or sets the amount of time before a player can interact with the chamber again. |
| 142 | + /// </summary> |
| 143 | + public float Cooldown |
| 144 | + { |
| 145 | + get => Base._targetCooldown; |
| 146 | + set => Base._targetCooldown = value; |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Gets a value indicating whether the chamber is currently open. |
| 151 | + /// </summary> |
| 152 | + public bool IsOpen => Base.IsOpen; |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Gets the <see cref="Stopwatch"/> of current cooldown. |
| 156 | + /// </summary> |
| 157 | + /// <remarks>Used in <see cref="CanInteract"/> check.</remarks> |
| 158 | + public Stopwatch CurrentCooldown => Base._stopwatch; |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Gets a value indicating whether the chamber is interactable. |
| 162 | + /// </summary> |
| 163 | + public bool CanInteract => Base.CanInteract; |
| 164 | + |
| 165 | + /// <summary> |
| 166 | + /// Spawns a specified item from <see cref="AcceptableTypes"/>. |
| 167 | + /// </summary> |
| 168 | + /// <param name="type"><see cref="ItemType"/> from <see cref="AcceptableTypes"/>.</param> |
| 169 | + /// <param name="amount">Amount of items that should be spawned.</param> |
| 170 | + public void SpawnItem(ItemType type, int amount) => Base.SpawnItem(type, amount); |
| 171 | + |
| 172 | + /// <summary> |
| 173 | + /// Adds an item of the specified type to the chamber's spawn list. |
| 174 | + /// If the chamber is open and <paramref name="spawnIfIsOpen"/> is set to <see langword="true"/>, |
| 175 | + /// the item is spawned immediately at a random spawn point within the chamber. |
| 176 | + /// </summary> |
| 177 | + /// <param name="itemType">The type of item to add to the spawn list.</param> |
| 178 | + /// <param name="quantity">The number of items to add. Defaults to 1.</param> |
| 179 | + /// <param name="spawnIfIsOpen"> |
| 180 | + /// If <see langword="true"/> and the chamber is open, the item is immediately spawned at a random spawn point. |
| 181 | + /// Otherwise, the item is added to the spawn list and will spawn when the chamber is opened. |
| 182 | + /// </param> |
| 183 | + public void AddItemToSpawn(ItemType itemType, int quantity = 1, bool spawnIfIsOpen = false) |
| 184 | + { |
| 185 | + for (int i = 0; i < quantity; i++) |
| 186 | + { |
| 187 | + Pickup pickup = Pickup.Create(itemType); |
| 188 | + |
| 189 | + if (spawnIfIsOpen && IsOpen) |
| 190 | + { |
| 191 | + pickup.Position = GetRandomSpawnPoint(); |
| 192 | + pickup.Spawn(); |
| 193 | + continue; |
| 194 | + } |
| 195 | + |
| 196 | + Base._toBeSpawned.Add(pickup.Base); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + /// <summary> |
| 201 | + /// Gets a random spawn point within the chamber. |
| 202 | + /// If multiple spawn points are available and <see cref="UseMultipleSpawnpoints"/> is <see langword="true"/>, |
| 203 | + /// a random spawn point is selected from the available points. |
| 204 | + /// Otherwise, the default spawn point is used. |
| 205 | + /// </summary> |
| 206 | + /// <returns>A <see cref="Vector3"/> representing the position of the selected spawn point.</returns> |
| 207 | + public Vector3 GetRandomSpawnPoint() |
| 208 | + { |
| 209 | + if (UseMultipleSpawnpoints && Spawnpoints.Any()) |
| 210 | + { |
| 211 | + return Spawnpoints.GetRandomValue().position; |
| 212 | + } |
| 213 | + |
| 214 | + return Spawnpoint.position; |
| 215 | + } |
| 216 | + |
| 217 | + /// <summary> |
| 218 | + /// Gets the chamber by its <see cref="LockerChamber"/>. |
| 219 | + /// </summary> |
| 220 | + /// <param name="chamber"><see cref="LockerChamber"/>.</param> |
| 221 | + /// <returns><see cref="Chamber"/>.</returns> |
| 222 | + internal static Chamber Get(LockerChamber chamber) => Chambers.TryGetValue(chamber, out Chamber chmb) ? chmb : new(chamber, Locker.Get(x => x.Chambers.Any(x => x.Base == chamber)).FirstOrDefault()); |
| 223 | + } |
| 224 | +} |
0 commit comments