| 1 | namespace NewMoon.Samples.NPC; |
| 2 | |
| 3 | /// <summary> |
| 4 | /// An example gamemode that registers one custom NPC entity, <c>npc_bot_newmoon</c>, which fronts <c>base_boss</c>. |
| 5 | /// </summary> |
| 6 | [GameMode("Custom NPC Sample", "custom-npc-sample", Description = "Registers a custom NPC entity as an example.")] |
| 7 | public sealed partial class NewMoonSampleNPC : FGamePlugin |
| 8 | { |
| 9 | /// <inheritdoc/> |
| 10 | protected override void OnPluginStart() => PLogger.LogStarted(); |
| 11 | |
| 12 | /// <inheritdoc/> |
| 13 | protected override void OnPluginEnd() => PLogger.LogEnded(); |
| 14 | } |
| 15 |
screwdriver / gist:f99d5bf9d8f14fe2a9a3bf56d4ac0c66
Last active 1 hour ago
Revision ba3eacc011de05ba336297ae4d690aa214bc526c
| 1 | using JetBrains.Annotations; |
| 2 | using Microsoft.Extensions.Logging; |
| 3 | |
| 4 | #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member |
| 5 | |
| 6 | namespace NewMoon.Samples.NPC; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// Source-generated log messages. |
| 10 | /// </summary> |
| 11 | [UsedImplicitly] |
| 12 | public static partial class NewMoonSampleNPCLogger |
| 13 | { |
| 14 | [LoggerMessage(1, LogLevel.Information, "Plugin started")] |
| 15 | public static partial void LogStarted(this ILogger logger); |
| 16 | |
| 17 | [LoggerMessage(2, LogLevel.Information, "Plugin ended")] |
| 18 | public static partial void LogEnded(this ILogger logger); |
| 19 | |
| 20 | [LoggerMessage(3, LogLevel.Information, "npc_bot_newmoon OnSpawn {Index}")] |
| 21 | public static partial void LogNPCSpawn(this ILogger logger, int index); |
| 22 | |
| 23 | [LoggerMessage(4, LogLevel.Information, "npc_bot_newmoon OnActivate {Index}")] |
| 24 | public static partial void LogNPCActivate(this ILogger logger, int index); |
| 25 | |
| 26 | [LoggerMessage(5, LogLevel.Information, "npc_bot_newmoon OnRemoved {Index}")] |
| 27 | public static partial void LogNPCRemoved(this ILogger logger, int index); |
| 28 | } |
| 29 |
| 1 | using Microsoft.Extensions.Logging; |
| 2 | |
| 3 | namespace NewMoon.Samples.NPC; |
| 4 | |
| 5 | /// <summary> |
| 6 | /// The custom NPC: a <c>npc_bot_newmoon</c> classname that is a factory alias of <c>base_boss</c>. Spawning it yields a real |
| 7 | /// <c>base_boss</c> that carries the new classname, and the registry calls the lifecycle overrides below. |
| 8 | /// </summary> |
| 9 | /// <remarks> |
| 10 | /// The class only logs, through the <see cref="FCustomEntity.PLogger"/> the registry supplies. The spawner supplies <c>health</c> |
| 11 | /// and <c>model</c>, for example <c>ent_create npc_bot_newmoon health 500 model models/bots/headless_hatman.mdl</c>. The registry |
| 12 | /// constructs the class through dependency injection, so it needs a public constructor; any constructor parameters are resolved |
| 13 | /// from the plugin's services. The plugin registers it automatically at start-up. |
| 14 | /// </remarks> |
| 15 | [CustomEntity("npc_bot_newmoon", "base_boss", PrecacheModels = ["models/bots/headless_hatman.mdl"])] |
| 16 | internal sealed class NPCBotNewMoon : FCustomEntity |
| 17 | { |
| 18 | // Captured at spawn so the remove log never depends on the view still resolving while the instance is torn down. |
| 19 | private int _index = -1; |
| 20 | |
| 21 | /// <inheritdoc/> |
| 22 | protected override void OnSpawn() |
| 23 | { |
| 24 | _index = Entity.Index; |
| 25 | PLogger.LogNPCSpawn(_index); |
| 26 | } |
| 27 | |
| 28 | /// <inheritdoc/> |
| 29 | protected override void OnActivate() => PLogger.LogNPCActivate(_index); |
| 30 | |
| 31 | /// <inheritdoc/> |
| 32 | protected override void OnRemoved() => PLogger.LogNPCRemoved(_index); |
| 33 | } |
| 34 |