From 56f70cfaa20b2ee7f3aacf5211552ae13e7e2b5b Mon Sep 17 00:00:00 2001 From: Extremelyd1 <10898310+Extremelyd1@users.noreply.github.com> Date: Fri, 12 Jul 2024 18:21:41 +0200 Subject: [PATCH] Fix challenge prompt for Mantis Lords --- .../Component/ChallengePromptComponent.cs | 55 +++++++++++++++++++ .../Entity/Component/ComponentFactory.cs | 2 + .../Entity/Component/EntityComponent.cs | 5 +- HKMP/Networking/Packet/Data/EntityUpdate.cs | 4 +- HKMP/Resource/entity-registry.json | 3 + 5 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 HKMP/Game/Client/Entity/Component/ChallengePromptComponent.cs diff --git a/HKMP/Game/Client/Entity/Component/ChallengePromptComponent.cs b/HKMP/Game/Client/Entity/Component/ChallengePromptComponent.cs new file mode 100644 index 0000000..ddd7f75 --- /dev/null +++ b/HKMP/Game/Client/Entity/Component/ChallengePromptComponent.cs @@ -0,0 +1,55 @@ +using Hkmp.Networking.Client; +using Hkmp.Networking.Packet.Data; +using Hkmp.Util; +using UnityEngine; + +namespace Hkmp.Game.Client.Entity.Component; + +/// +/// This component manages the challenge prompt that appears for Mantis Lords. +internal class ChallengePromptComponent : EntityComponent { + /// + /// The game object that handles the challenge prompt pop-up. + /// + private readonly GameObject _promptObj; + + public ChallengePromptComponent( + NetClient netClient, + ushort entityId, + HostClientPair gameObject + ) : base(netClient, entityId, gameObject) { + var hostObj = gameObject.Host; + var parent = hostObj.transform.parent; + _promptObj = parent.Find("Challenge Prompt").gameObject; + + var promptFsm = _promptObj.LocateMyFSM("Challenge Start"); + promptFsm.InsertMethod("Take Control", 6, () => { + var data = new EntityNetworkData { + Type = EntityComponentType.ChallengePrompt + }; + data.Packet.Write(true); + + SendData(data); + }); + } + + /// + public override void InitializeHost() { + } + + /// + public override void Update(EntityNetworkData data) { + var destroyPrompt = data.Packet.ReadBool(); + if (!destroyPrompt) { + return; + } + + if (_promptObj != null) { + Object.Destroy(_promptObj); + } + } + + /// + public override void Destroy() { + } +} diff --git a/HKMP/Game/Client/Entity/Component/ComponentFactory.cs b/HKMP/Game/Client/Entity/Component/ComponentFactory.cs index 497a850..bc9c06e 100644 --- a/HKMP/Game/Client/Entity/Component/ComponentFactory.cs +++ b/HKMP/Game/Client/Entity/Component/ComponentFactory.cs @@ -63,6 +63,8 @@ HostClientPair objects Client = spriteRendererClient, Host = spriteRendererHost }); + case EntityComponentType.ChallengePrompt: + return new ChallengePromptComponent(netClient, entityId, objects); default: throw new ArgumentOutOfRangeException(nameof(type), type, $"Could not instantiate entity component for type: {type}"); } diff --git a/HKMP/Game/Client/Entity/Component/EntityComponent.cs b/HKMP/Game/Client/Entity/Component/EntityComponent.cs index 8b72e63..236b00f 100644 --- a/HKMP/Game/Client/Entity/Component/EntityComponent.cs +++ b/HKMP/Game/Client/Entity/Component/EntityComponent.cs @@ -69,7 +69,7 @@ protected void SendData(EntityNetworkData data) { /// Enum for data types. /// [JsonConverter(typeof(StringEnumConverter))] -internal enum EntityComponentType : byte { +internal enum EntityComponentType : ushort { Fsm = 0, Death, Invincibility, @@ -85,4 +85,5 @@ internal enum EntityComponentType : byte { ChildrenActivation, SpawnJar, SpriteRenderer, -} \ No newline at end of file + ChallengePrompt +} diff --git a/HKMP/Networking/Packet/Data/EntityUpdate.cs b/HKMP/Networking/Packet/Data/EntityUpdate.cs index 1057103..f6b91db 100644 --- a/HKMP/Networking/Packet/Data/EntityUpdate.cs +++ b/HKMP/Networking/Packet/Data/EntityUpdate.cs @@ -545,7 +545,7 @@ public EntityNetworkData() { /// public void WriteData(IPacket packet) { - packet.Write((byte) Type); + packet.Write((ushort) Type); var data = Packet.ToArray(); @@ -563,7 +563,7 @@ public void WriteData(IPacket packet) { /// public void ReadData(IPacket packet) { - Type = (EntityComponentType) packet.ReadByte(); + Type = (EntityComponentType) packet.ReadUShort(); var length = packet.ReadUShort(); var data = new byte[length]; diff --git a/HKMP/Resource/entity-registry.json b/HKMP/Resource/entity-registry.json index cbc9bef..5bcf820 100644 --- a/HKMP/Resource/entity-registry.json +++ b/HKMP/Resource/entity-registry.json @@ -395,6 +395,9 @@ "type": "MantisLord", "fsm_name": "Mantis Lord" } + ], + "components": [ + "ChallengePrompt" ] }, {