Skip to content

Commit

Permalink
Fix challenge prompt for Mantis Lords
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Jul 12, 2024
1 parent 8e9cc13 commit 56f70cf
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
55 changes: 55 additions & 0 deletions HKMP/Game/Client/Entity/Component/ChallengePromptComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Hkmp.Networking.Client;
using Hkmp.Networking.Packet.Data;
using Hkmp.Util;
using UnityEngine;

namespace Hkmp.Game.Client.Entity.Component;

/// <inheritdoc />
/// This component manages the challenge prompt that appears for Mantis Lords.
internal class ChallengePromptComponent : EntityComponent {
/// <summary>
/// The game object that handles the challenge prompt pop-up.
/// </summary>
private readonly GameObject _promptObj;

public ChallengePromptComponent(
NetClient netClient,
ushort entityId,
HostClientPair<GameObject> 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);
});
}

/// <inheritdoc />
public override void InitializeHost() {
}

/// <inheritdoc />
public override void Update(EntityNetworkData data) {
var destroyPrompt = data.Packet.ReadBool();
if (!destroyPrompt) {
return;
}

if (_promptObj != null) {
Object.Destroy(_promptObj);
}
}

/// <inheritdoc />
public override void Destroy() {
}
}
2 changes: 2 additions & 0 deletions HKMP/Game/Client/Entity/Component/ComponentFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ HostClientPair<GameObject> 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}");
}
Expand Down
5 changes: 3 additions & 2 deletions HKMP/Game/Client/Entity/Component/EntityComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected void SendData(EntityNetworkData data) {
/// Enum for data types.
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
internal enum EntityComponentType : byte {
internal enum EntityComponentType : ushort {
Fsm = 0,
Death,
Invincibility,
Expand All @@ -85,4 +85,5 @@ internal enum EntityComponentType : byte {
ChildrenActivation,
SpawnJar,
SpriteRenderer,
}
ChallengePrompt
}
4 changes: 2 additions & 2 deletions HKMP/Networking/Packet/Data/EntityUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ public EntityNetworkData() {

/// <inheritdoc cref="IPacketData.WriteData" />
public void WriteData(IPacket packet) {
packet.Write((byte) Type);
packet.Write((ushort) Type);

var data = Packet.ToArray();

Expand All @@ -563,7 +563,7 @@ public void WriteData(IPacket packet) {

/// <inheritdoc cref="IPacketData.ReadData" />
public void ReadData(IPacket packet) {
Type = (EntityComponentType) packet.ReadByte();
Type = (EntityComponentType) packet.ReadUShort();

var length = packet.ReadUShort();
var data = new byte[length];
Expand Down
3 changes: 3 additions & 0 deletions HKMP/Resource/entity-registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@
"type": "MantisLord",
"fsm_name": "Mantis Lord"
}
],
"components": [
"ChallengePrompt"
]
},
{
Expand Down

0 comments on commit 56f70cf

Please sign in to comment.