~ruther/NosSmooth

ref: 95574fd3b93fcf508043ff5104efeedcbd8d0fba NosSmooth/Core/NosSmooth.Game/PacketHandlers/Entities/PtctlResponder.cs -rw-r--r-- 1.6 KiB
95574fd3 — NotKappa Add PtctlResponder 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
//  PtctlResponder.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.Core.Packets;
using NosSmooth.Game.Data.Entities;
using NosSmooth.Game.Data.Info;
using NosSmooth.Packets.Client.Mates;
using Remora.Results;

namespace NosSmooth.Game.PacketHandlers.Entities;

/// <summary>
/// Responds to Ptctl packet.
/// </summary>
public class PtctlResponder : IPacketResponder<PtctlPacket>
{
    private readonly Game _game;

    /// <summary>
    /// Initializes a new instance of the <see cref="PtctlResponder"/> class.
    /// </summary>
    /// <param name="game">The nostale game.</param>
    public PtctlResponder(Game game)
    {
        _game = game;
    }

    /// <inheritdoc />
    public Task<Result> Respond(PacketEventArgs<PtctlPacket> packetArgs, CancellationToken ct = default)
    {
        var map = _game.CurrentMap;

        if (map is null)
        {
            return Task.FromResult(Result.FromSuccess());
        }

        var packet = packetArgs.Packet;

        foreach (var control in packet.Controls)
        {
            var entity = map.Entities.GetEntity<ILivingEntity>(control.MateTransportId);
            if (entity is not null)
            {
                entity.Position = new Position(control.PositionX, control.PositionY);

                if (packet.EntityId == control.MateTransportId)
                {
                    entity.Speed = packet.Speed;
                }
            }
        }

        return Task.FromResult(Result.FromSuccess());
    }
}
Do not follow this link