//
// 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;
///
/// State of the .
///
public class PathfinderState : IStatefulEntity
{
private ConcurrentDictionary _entities;
///
/// Initializes a new instance of the class.
///
public PathfinderState()
{
_entities = new ConcurrentDictionary();
Character = new EntityState();
}
///
/// Gets the entities.
///
internal IReadOnlyDictionary Entities => _entities;
///
/// Gets the character entity state.
///
internal EntityState Character { get; private set; }
///
/// Gets or sets the current map id.
///
internal int? MapId { get; set; }
///
/// Gets or sets the current map information.
///
internal IMapInfo? MapInfo { get; set; }
///
/// Sets the id of the character entity.
///
/// The character id.
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()
);
}
///
/// Add the given entity to the list.
///
/// The id of the entity.
/// The x coordinate of the entity.
/// The y coordinate of the entity.
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());
}
///
/// Remove all entities from the list.
///
internal void ClearEntities()
{
_entities.Clear();
SetCharacterId(Character.Id);
}
}