~ruther/NosSmooth

4d5c6d1f0ac10186c76eef780df89561449c4b63 — Rutherther 3 years ago f2ee2b7
feat(game): use entity stunned event instead of character stunned
3 files changed, 36 insertions(+), 18 deletions(-)

D Core/NosSmooth.Game/Events/Characters/CharacterStunEvent.cs
A Core/NosSmooth.Game/Events/Entities/EntityStunnedEvent.cs
M Core/NosSmooth.Game/PacketHandlers/Entities/CondPacketResponder.cs
D Core/NosSmooth.Game/Events/Characters/CharacterStunEvent.cs => Core/NosSmooth.Game/Events/Characters/CharacterStunEvent.cs +0 -13
@@ 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;

/// <summary>
/// The character has been stunned or unstunned.
/// </summary>
/// <param name="Stunned">Whether the character is stunned.</param>
public record CharacterStunEvent(bool Stunned);
\ No newline at end of file

A Core/NosSmooth.Game/Events/Entities/EntityStunnedEvent.cs => Core/NosSmooth.Game/Events/Entities/EntityStunnedEvent.cs +18 -0
@@ 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;

/// <summary>
/// The given entity has been stunned or unstunned.
/// </summary>
/// <param name="Entity">The entity.</param>
/// <param name="CantMove">Whether the entity cannot move.</param>
/// <param name="CantAttack">Whether the entity cannot attack.</param>
public record EntityStunnedEvent(ILivingEntity Entity, bool CantMove, bool CantAttack)
    : IGameEvent;
\ No newline at end of file

M Core/NosSmooth.Game/PacketHandlers/Entities/CondPacketResponder.cs => Core/NosSmooth.Game/PacketHandlers/Entities/CondPacketResponder.cs +18 -5
@@ 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<CondPacket>
{
    private readonly Game _game;
    private readonly EventDispatcher _eventDispatcher;

    /// <summary>
    /// Initializes a new instance of the <see cref="CondPacketResponder"/> class.
    /// </summary>
    /// <param name="game">The game.</param>
    public CondPacketResponder(Game game)
    /// <param name="eventDispatcher">The event dispatcher.</param>
    public CondPacketResponder(Game game, EventDispatcher eventDispatcher)
    {
        _game = game;
        _eventDispatcher = eventDispatcher;
    }

    /// <inheritdoc />
    public Task<Result> Respond(PacketEventArgs<CondPacket> packetArgs, CancellationToken ct = default)
    public async Task<Result> Respond(PacketEventArgs<CondPacket> 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<CondPacket>

        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

Do not follow this link