-
Notifications
You must be signed in to change notification settings - Fork 16
/
MapCellStateProvider.cs
79 lines (69 loc) · 3.21 KB
/
MapCellStateProvider.cs
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
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using EOLib.Domain.Extensions;
using EOLib.Domain.NPC;
using EOLib.IO.Map;
using EOLib.IO.Repositories;
using Optional;
using Optional.Collections;
using System.Linq;
namespace EOLib.Domain.Map
{
[AutoMappedType]
public class MapCellStateProvider : IMapCellStateProvider
{
private readonly ICurrentMapStateProvider _mapStateProvider;
private readonly ICharacterProvider _characterProvider;
private readonly IMapFileProvider _mapFileProvider;
public MapCellStateProvider(ICurrentMapStateProvider mapStateProvider,
ICharacterProvider characterProvider,
IMapFileProvider mapFileProvider)
{
_mapStateProvider = mapStateProvider;
_characterProvider = characterProvider;
_mapFileProvider = mapFileProvider;
}
public IMapCellState GetCellStateAt(int x, int y)
{
if (x < 0 || y < 0 || x > CurrentMap.Properties.Width || y > CurrentMap.Properties.Height)
return new MapCellState { InBounds = false, TileSpec = TileSpec.MapEdge };
var tileSpec = CurrentMap.Tiles[y, x];
var warp = CurrentMap.Warps[y, x];
var chest = CurrentMap.Chests.Where(c => c.X == x && c.Y == y && c.Key != ChestKey.None).Select(c => c.Key).FirstOrDefault();
var sign = CurrentMap.Signs.FirstOrDefault(s => s.X == x && s.Y == y);
var character = _mapStateProvider.Characters.Values.Concat(new[] { _characterProvider.MainCharacter })
.FirstOrNone(c => CharacterAtCoordinates(c, x, y));
var npc = _mapStateProvider.NPCs.FirstOrNone(n => NPCAtCoordinates(n, x, y));
var items = _mapStateProvider.MapItems.Where(i => i.X == x && i.Y == y);
return new MapCellState
{
InBounds = true,
Coordinate = new MapCoordinate(x, y),
Items = items.ToList(),
TileSpec = tileSpec,
Warp = warp.SomeNotNull().Map<IWarp>(w => new Warp(w)),
ChestKey = chest.SomeNotNull(),
Sign = sign.SomeNotNull().Map<ISign>(s => new Sign(s)),
Character = character,
NPC = npc
};
}
private static bool CharacterAtCoordinates(ICharacter character, int x, int y)
{
return character.RenderProperties.IsActing(CharacterActionState.Walking)
? character.RenderProperties.GetDestinationX() == x && character.RenderProperties.GetDestinationY() == y
: character.RenderProperties.MapX == x && character.RenderProperties.MapY == y;
}
private static bool NPCAtCoordinates(INPC npc, int x, int y)
{
return npc.IsActing(NPCActionState.Walking)
? npc.GetDestinationX() == x && npc.GetDestinationY() == y
: npc.X == x && npc.Y == y;
}
private IMapFile CurrentMap => _mapFileProvider.MapFiles[_mapStateProvider.CurrentMapID];
}
public interface IMapCellStateProvider
{
IMapCellState GetCellStateAt(int x, int y);
}
}