From 4d5c6d1f0ac10186c76eef780df89561449c4b63 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sat, 12 Feb 2022 17:22:32 +0100 Subject: [PATCH] feat(game): use entity stunned event instead of character stunned --- .../Events/Characters/CharacterStunEvent.cs | 13 ----------- .../Events/Entities/EntityStunnedEvent.cs | 18 +++++++++++++++ .../Entities/CondPacketResponder.cs | 23 +++++++++++++++---- 3 files changed, 36 insertions(+), 18 deletions(-) delete mode 100644 Core/NosSmooth.Game/Events/Characters/CharacterStunEvent.cs create mode 100644 Core/NosSmooth.Game/Events/Entities/EntityStunnedEvent.cs diff --git a/Core/NosSmooth.Game/Events/Characters/CharacterStunEvent.cs b/Core/NosSmooth.Game/Events/Characters/CharacterStunEvent.cs deleted file mode 100644 index 4e72279..0000000 --- a/Core/NosSmooth.Game/Events/Characters/CharacterStunEvent.cs +++ /dev/null @@ -1,13 +0,0 @@ -// -// CharacterStunEvent.cs -// -// Copyright (c) František Boháček. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace NosSmooth.Game.Events.Characters; - -/// -/// The character has been stunned or unstunned. -/// -/// Whether the character is stunned. -public record CharacterStunEvent(bool Stunned); \ No newline at end of file diff --git a/Core/NosSmooth.Game/Events/Entities/EntityStunnedEvent.cs b/Core/NosSmooth.Game/Events/Entities/EntityStunnedEvent.cs new file mode 100644 index 0000000..e14fd9d --- /dev/null +++ b/Core/NosSmooth.Game/Events/Entities/EntityStunnedEvent.cs @@ -0,0 +1,18 @@ +// +// EntityStunnedEvent.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using NosSmooth.Game.Data.Entities; + +namespace NosSmooth.Game.Events.Entities; + +/// +/// The given entity has been stunned or unstunned. +/// +/// The entity. +/// Whether the entity cannot move. +/// Whether the entity cannot attack. +public record EntityStunnedEvent(ILivingEntity Entity, bool CantMove, bool CantAttack) + : IGameEvent; \ No newline at end of file diff --git a/Core/NosSmooth.Game/PacketHandlers/Entities/CondPacketResponder.cs b/Core/NosSmooth.Game/PacketHandlers/Entities/CondPacketResponder.cs index 829a63c..461e7d2 100644 --- a/Core/NosSmooth.Game/PacketHandlers/Entities/CondPacketResponder.cs +++ b/Core/NosSmooth.Game/PacketHandlers/Entities/CondPacketResponder.cs @@ -6,6 +6,8 @@ using NosSmooth.Core.Packets; using NosSmooth.Game.Data.Entities; +using NosSmooth.Game.Events.Core; +using NosSmooth.Game.Events.Entities; using NosSmooth.Packets.Server.Entities; using Remora.Results; @@ -17,23 +19,26 @@ namespace NosSmooth.Game.PacketHandlers.Entities; public class CondPacketResponder : IPacketResponder { private readonly Game _game; + private readonly EventDispatcher _eventDispatcher; /// /// Initializes a new instance of the class. /// /// The game. - public CondPacketResponder(Game game) + /// The event dispatcher. + public CondPacketResponder(Game game, EventDispatcher eventDispatcher) { _game = game; + _eventDispatcher = eventDispatcher; } /// - public Task Respond(PacketEventArgs packetArgs, CancellationToken ct = default) + public async Task Respond(PacketEventArgs packetArgs, CancellationToken ct = default) { var map = _game.CurrentMap; if (map is null) { - return Task.FromResult(Result.FromSuccess()); + return Result.FromSuccess(); } var packet = packetArgs.Packet; @@ -41,13 +46,21 @@ public class CondPacketResponder : IPacketResponder if (entity is null) { - return Task.FromResult(Result.FromSuccess()); + return Result.FromSuccess(); } + bool cantMove = entity.CantMove; + bool cantAttack = entity.CantAttack; + entity.Speed = packet.Speed; entity.CantAttack = packet.CantAttack; entity.CantMove = packet.CantMove; - return Task.FromResult(Result.FromSuccess()); + if (cantMove != packet.CantMove || cantAttack != packet.CantAttack) + { + return await _eventDispatcher.DispatchEvent(new EntityStunnedEvent(entity, packet.CantMove, packet.CantAttack), ct); + } + + return Result.FromSuccess(); } } \ No newline at end of file -- 2.48.1