~ruther/NosSmooth

ref: 419415627683d961c9a0f060e4e52d422baea40e NosSmooth/Extensions/NosSmooth.Extensions.Pathfinding/PathfinderState.cs -rw-r--r-- 2.8 KiB
41941562 — Rutherther feat(pathfinding): save mates state 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
//  PathfinderState.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 System.Collections.Concurrent;
using NosSmooth.Core.Client;
using NosSmooth.Core.Stateful;
using NosSmooth.Data.Abstractions.Infos;

namespace NosSmooth.Extensions.Pathfinding;

/// <summary>
/// State of the <see cref="Pathfinder"/>.
/// </summary>
public class PathfinderState : IStatefulEntity
{
    private ConcurrentDictionary<long, EntityState> _entities;

    /// <summary>
    /// Initializes a new instance of the <see cref="PathfinderState"/> class.
    /// </summary>
    public PathfinderState()
    {
        _entities = new ConcurrentDictionary<long, EntityState>();
        Character = new EntityState();
    }

    /// <summary>
    /// Gets the entities.
    /// </summary>
    internal IReadOnlyDictionary<long, EntityState> Entities => _entities;

    /// <summary>
    /// Gets the character entity state.
    /// </summary>
    internal EntityState Character { get; private set; }

    /// <summary>
    /// Gets or sets the current map id.
    /// </summary>
    internal int? MapId { get; set; }

    /// <summary>
    /// Gets or sets the current map information.
    /// </summary>
    internal IMapInfo? MapInfo { get; set; }

    /// <summary>
    /// Sets the id of the character entity.
    /// </summary>
    /// <param name="characterId">The character id.</param>
    internal void SetCharacterId(long characterId)
    {
        EntityState GetCharacter()
        {
            Character = new EntityState
            {
                Id = characterId,
                X = Character.X,
                Y = Character.Y
            };

            return Character;
        }

        _entities.TryRemove(Character.Id, out _);
        _entities.AddOrUpdate
        (
            characterId,
            _ => GetCharacter(),
            (_, _) => GetCharacter()
        );
    }

    /// <summary>
    /// Add the given entity to the list.
    /// </summary>
    /// <param name="entityId">The id of the entity.</param>
    /// <param name="x">The x coordinate of the entity.</param>
    /// <param name="y">The y coordinate of the entity.</param>
    internal void AddEntity
    (
        long entityId,
        short x,
        short y
    )
    {
        EntityState GetEntity()
        {
            return new EntityState
            {
                Id = entityId,
                X = x,
                Y = y
            };
        }

        _entities.AddOrUpdate(entityId, _ => GetEntity(), (_, _) => GetEntity());
    }

    /// <summary>
    /// Remove all entities from the list.
    /// </summary>
    internal void ClearEntities()
    {
        _entities.Clear();
        SetCharacterId(Character.Id);
    }
}
Do not follow this link