-
Notifications
You must be signed in to change notification settings - Fork 16
/
MapInteractionController.cs
251 lines (227 loc) · 11.6 KB
/
MapInteractionController.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using AutomaticTypeMapper;
using EndlessClient.ControlSets;
using EndlessClient.Dialogs;
using EndlessClient.Dialogs.Actions;
using EndlessClient.Dialogs.Factories;
using EndlessClient.HUD;
using EndlessClient.HUD.Controls;
using EndlessClient.HUD.Inventory;
using EndlessClient.HUD.Panels;
using EndlessClient.Input;
using EndlessClient.Rendering;
using EndlessClient.Rendering.Character;
using EndlessClient.Rendering.Factories;
using EOLib.Domain.Character;
using EOLib.Domain.Interact;
using EOLib.Domain.Item;
using EOLib.Domain.Map;
using EOLib.IO.Map;
using EOLib.Localization;
using Optional;
using Optional.Collections;
using System;
using System.Linq;
namespace EndlessClient.Controllers
{
[AutoMappedType]
public class MapInteractionController : IMapInteractionController
{
private readonly IMapActions _mapActions;
private readonly ICharacterActions _characterActions;
private readonly IInGameDialogActions _inGameDialogActions;
private readonly IPaperdollActions _paperdollActions;
private readonly IUnwalkableTileActions _unwalkableTileActions;
private readonly ICurrentMapStateProvider _currentMapStateProvider;
private readonly ICharacterProvider _characterProvider;
private readonly IStatusLabelSetter _statusLabelSetter;
private readonly IInventorySpaceValidator _inventorySpaceValidator;
private readonly IHudControlProvider _hudControlProvider;
private readonly ICharacterRendererProvider _characterRendererProvider;
private readonly IContextMenuRepository _contextMenuRepository;
private readonly IUserInputTimeRepository _userInputTimeRepository;
private readonly IEOMessageBoxFactory _messageBoxFactory;
private readonly IContextMenuRendererFactory _contextMenuRendererFactory;
private readonly IActiveDialogProvider _activeDialogProvider;
private readonly IUserInputRepository _userInputRepository;
public MapInteractionController(IMapActions mapActions,
ICharacterActions characterActions,
IInGameDialogActions inGameDialogActions,
IPaperdollActions paperdollActions,
IUnwalkableTileActions unwalkableTileActions,
ICurrentMapStateProvider currentMapStateProvider,
ICharacterProvider characterProvider,
IStatusLabelSetter statusLabelSetter,
IInventorySpaceValidator inventorySpaceValidator,
IHudControlProvider hudControlProvider,
ICharacterRendererProvider characterRendererProvider,
IContextMenuRepository contextMenuRepository,
IUserInputTimeRepository userInputTimeRepository,
IEOMessageBoxFactory messageBoxFactory,
IContextMenuRendererFactory contextMenuRendererFactory,
IActiveDialogProvider activeDialogProvider,
IUserInputRepository userInputRepository)
{
_mapActions = mapActions;
_characterActions = characterActions;
_inGameDialogActions = inGameDialogActions;
_paperdollActions = paperdollActions;
_unwalkableTileActions = unwalkableTileActions;
_currentMapStateProvider = currentMapStateProvider;
_characterProvider = characterProvider;
_statusLabelSetter = statusLabelSetter;
_inventorySpaceValidator = inventorySpaceValidator;
_hudControlProvider = hudControlProvider;
_characterRendererProvider = characterRendererProvider;
_contextMenuRepository = contextMenuRepository;
_userInputTimeRepository = userInputTimeRepository;
_messageBoxFactory = messageBoxFactory;
_contextMenuRendererFactory = contextMenuRendererFactory;
_activeDialogProvider = activeDialogProvider;
_userInputRepository = userInputRepository;
}
public void LeftClick(IMapCellState cellState, Option<IMouseCursorRenderer> mouseRenderer)
{
if (!InventoryPanel.NoItemsDragging() || _activeDialogProvider.ActiveDialogs.Any(x => x.HasValue))
return;
var optionalItem = cellState.Items.FirstOrNone();
if (optionalItem.HasValue)
{
var item = optionalItem.ValueOr(Item.None);
if (!_inventorySpaceValidator.ItemFits(item))
_statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_INFORMATION, EOResourceID.STATUS_LABEL_ITEM_PICKUP_NO_SPACE_LEFT);
else
HandlePickupResult(_mapActions.PickUpItem(item), item);
_userInputRepository.ClickHandled = true;
}
else if (cellState.Sign.HasValue)
{
var sign = cellState.Sign.ValueOr(Sign.None);
var messageBox = _messageBoxFactory.CreateMessageBox(sign.Message, sign.Title);
messageBox.ShowDialog();
_userInputRepository.ClickHandled = true;
}
else if (_characterProvider.MainCharacter.RenderProperties.SitState != SitState.Standing)
{
_characterActions.ToggleSit();
_userInputRepository.ClickHandled = true;
}
else if (InteractableTileSpec(cellState.TileSpec) && CharacterIsCloseEnough(cellState.Coordinate))
{
var unwalkableAction = _unwalkableTileActions.HandleUnwalkableTile(cellState);
switch (cellState.TileSpec)
{
// todo: implement for other clickable tile specs (board, jukebox, etc)
case TileSpec.Chest:
if (unwalkableAction == UnwalkableTileAction.Chest)
{
_mapActions.OpenChest((byte)cellState.Coordinate.X, (byte)cellState.Coordinate.Y);
_inGameDialogActions.ShowChestDialog();
_userInputRepository.ClickHandled = true;
}
break;
case TileSpec.BankVault:
if (unwalkableAction == UnwalkableTileAction.Locker)
{
_mapActions.OpenLocker((byte)cellState.Coordinate.X, (byte)cellState.Coordinate.Y);
_inGameDialogActions.ShowLockerDialog();
_userInputRepository.ClickHandled = true;
}
break;
}
}
else if (cellState.InBounds && !cellState.Character.HasValue && !cellState.NPC.HasValue)
{
mouseRenderer.MatchSome(r => r.AnimateClick());
_hudControlProvider.GetComponent<ICharacterAnimator>(HudControlIdentifier.CharacterAnimator)
.StartMainCharacterWalkAnimation(Option.Some(cellState.Coordinate));
_userInputRepository.ClickHandled = true;
}
_userInputTimeRepository.LastInputTime = DateTime.Now;
}
// todo: move to new controller for character interaction
public void RightClick(IMapCellState cellState)
{
if (!cellState.Character.HasValue || _activeDialogProvider.ActiveDialogs.Any(x => x.HasValue))
return;
cellState.Character.MatchSome(c =>
{
if (c == _characterProvider.MainCharacter)
{
_paperdollActions.RequestPaperdoll(_characterProvider.MainCharacter.ID);
_inGameDialogActions.ShowPaperdollDialog(_characterProvider.MainCharacter, isMainCharacter: true);
_userInputTimeRepository.LastInputTime = DateTime.Now;
}
else if (_characterRendererProvider.CharacterRenderers.ContainsKey(c.ID))
{
_contextMenuRepository.ContextMenu = _contextMenuRepository.ContextMenu.Match(
some: cmr =>
{
cmr.Dispose();
return Option.Some(_contextMenuRendererFactory.CreateContextMenuRenderer(_characterRendererProvider.CharacterRenderers[c.ID]));
},
none: () => Option.Some(_contextMenuRendererFactory.CreateContextMenuRenderer(_characterRendererProvider.CharacterRenderers[c.ID])));
_contextMenuRepository.ContextMenu.MatchSome(r => r.Initialize());
}
});
}
private void HandlePickupResult(ItemPickupResult pickupResult, IItem item)
{
switch (pickupResult)
{
case ItemPickupResult.DropProtection:
var message = EOResourceID.STATUS_LABEL_ITEM_PICKUP_PROTECTED;
var extra = string.Empty;
item.OwningPlayerID.MatchSome(playerId =>
{
message = EOResourceID.STATUS_LABEL_ITEM_PICKUP_PROTECTED_BY;
if (_currentMapStateProvider.Characters.ContainsKey(playerId))
{
extra = _currentMapStateProvider.Characters[playerId].Name;
}
});
_statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_INFORMATION, message, extra);
break;
case ItemPickupResult.TooHeavy:
_statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_WARNING, EOResourceID.DIALOG_ITS_TOO_HEAVY_WEIGHT);
break;
case ItemPickupResult.TooFar:
case ItemPickupResult.Ok: break;
default: throw new ArgumentOutOfRangeException(nameof(pickupResult), pickupResult, null);
}
}
private InventoryPanel InventoryPanel => _hudControlProvider.GetComponent<InventoryPanel>(HudControlIdentifier.InventoryPanel);
private static bool InteractableTileSpec(TileSpec tileSpec)
{
switch (tileSpec)
{
case TileSpec.Chest:
case TileSpec.BankVault:
case TileSpec.Board1:
case TileSpec.Board2:
case TileSpec.Board3:
case TileSpec.Board4:
case TileSpec.Board5:
case TileSpec.Board6:
case TileSpec.Board7:
case TileSpec.Board8:
case TileSpec.Jukebox:
return true;
default:
return false;
}
}
private bool CharacterIsCloseEnough(MapCoordinate coordinate)
{
var x = _characterProvider.MainCharacter.RenderProperties.MapX;
var y = _characterProvider.MainCharacter.RenderProperties.MapY;
var withinOneUnit = Math.Max(Math.Abs(x - coordinate.X), Math.Abs(y - coordinate.Y)) <= 1;
var sameXOrY = x == coordinate.X || y == coordinate.Y;
return withinOneUnit && sameXOrY;
}
}
public interface IMapInteractionController
{
void LeftClick(IMapCellState cellState, Option<IMouseCursorRenderer> mouseRenderer);
void RightClick(IMapCellState cellState);
}
}