-
Notifications
You must be signed in to change notification settings - Fork 16
/
MapInteractionController.cs
312 lines (280 loc) · 14.9 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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.HUD.Spells;
using EndlessClient.Input;
using EndlessClient.Rendering;
using EndlessClient.Rendering.Character;
using EndlessClient.Rendering.Factories;
using EOLib.Domain.Character;
using EOLib.Domain.Extensions;
using EOLib.Domain.Interact;
using EOLib.Domain.Item;
using EOLib.Domain.Map;
using EOLib.Domain.Spells;
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 IWalkValidationActions _walkValidationActions;
private readonly IUnwalkableTileActions _unwalkableTileActions;
private readonly ICharacterAnimationActions _characterAnimationActions;
private readonly ISpellCastValidationActions _spellCastValidationActions;
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;
private readonly ISpellSlotDataRepository _spellSlotDataRepository;
public MapInteractionController(IMapActions mapActions,
ICharacterActions characterActions,
IInGameDialogActions inGameDialogActions,
IPaperdollActions paperdollActions,
IWalkValidationActions walkValidationActions,
IUnwalkableTileActions unwalkableTileActions,
ICharacterAnimationActions characterAnimationActions,
ISpellCastValidationActions spellCastValidationActions,
ICurrentMapStateProvider currentMapStateProvider,
ICharacterProvider characterProvider,
IStatusLabelSetter statusLabelSetter,
IInventorySpaceValidator inventorySpaceValidator,
IHudControlProvider hudControlProvider,
ICharacterRendererProvider characterRendererProvider,
IContextMenuRepository contextMenuRepository,
IUserInputTimeRepository userInputTimeRepository,
IEOMessageBoxFactory messageBoxFactory,
IContextMenuRendererFactory contextMenuRendererFactory,
IActiveDialogProvider activeDialogProvider,
IUserInputRepository userInputRepository,
ISpellSlotDataRepository spellSlotDataRepository)
{
_mapActions = mapActions;
_characterActions = characterActions;
_inGameDialogActions = inGameDialogActions;
_paperdollActions = paperdollActions;
_walkValidationActions = walkValidationActions;
_unwalkableTileActions = unwalkableTileActions;
_characterAnimationActions = characterAnimationActions;
_spellCastValidationActions = spellCastValidationActions;
_currentMapStateProvider = currentMapStateProvider;
_characterProvider = characterProvider;
_statusLabelSetter = statusLabelSetter;
_inventorySpaceValidator = inventorySpaceValidator;
_hudControlProvider = hudControlProvider;
_characterRendererProvider = characterRendererProvider;
_contextMenuRepository = contextMenuRepository;
_userInputTimeRepository = userInputTimeRepository;
_messageBoxFactory = messageBoxFactory;
_contextMenuRendererFactory = contextMenuRendererFactory;
_activeDialogProvider = activeDialogProvider;
_userInputRepository = userInputRepository;
_spellSlotDataRepository = spellSlotDataRepository;
}
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(MapItem.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 unwalkableActions = _unwalkableTileActions.HandleUnwalkableTile(cellState);
foreach (var unwalkableAction in unwalkableActions)
{
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
&& _walkValidationActions.IsCellStateWalkable(cellState)
&& _characterProvider.MainCharacter.RenderProperties.IsActing(CharacterActionState.Standing)
&& !_spellSlotDataRepository.SelectedSpellSlot.HasValue)
{
mouseRenderer.MatchSome(r => r.AnimateClick());
_hudControlProvider.GetComponent<ICharacterAnimator>(HudControlIdentifier.CharacterAnimator)
.StartMainCharacterWalkAnimation(Option.Some(cellState.Coordinate));
_userInputRepository.ClickHandled = true;
}
cellState.Warp.MatchSome(w =>
{
w.SomeWhen(d => d.DoorType != DoorSpec.NoDoor)
.MatchSome(d =>
{
if (_unwalkableTileActions.HandleUnwalkableTile(cellState).Any(x => x == UnwalkableTileAction.Door))
{
_mapActions.OpenDoor(d);
}
_userInputRepository.ClickHandled = true;
});
});
_spellSlotDataRepository.SelectedSpellSlot = Option.None<int>();
_userInputTimeRepository.LastInputTime = DateTime.Now;
}
public void LeftClick(ISpellTargetable spellTarget)
{
if (_spellSlotDataRepository.SpellIsPrepared)
{
_spellSlotDataRepository.SelectedSpellInfo.MatchSome(si =>
{
var result = _spellCastValidationActions.ValidateSpellCast(si.ID, spellTarget);
if (result == SpellCastValidationResult.Ok && _characterAnimationActions.PrepareMainCharacterSpell(si.ID, spellTarget))
_characterActions.PrepareCastSpell(si.ID);
else if (result == SpellCastValidationResult.CannotAttackNPC)
_statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_WARNING, EOResourceID.YOU_CANNOT_ATTACK_THIS_NPC);
else if (result == SpellCastValidationResult.ExhaustedNoTp)
_statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_WARNING, EOResourceID.ATTACK_YOU_ARE_EXHAUSTED_TP);
else if (result == SpellCastValidationResult.ExhaustedNoSp)
_statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_WARNING, EOResourceID.ATTACK_YOU_ARE_EXHAUSTED_SP);
});
_spellSlotDataRepository.SpellIsPrepared = false;
_spellSlotDataRepository.SelectedSpellSlot = Option.None<int>();
_userInputRepository.ClickHandled = true;
}
_userInputTimeRepository.LastInputTime = DateTime.Now;
}
public void RightClick(Character character)
{
if (_activeDialogProvider.ActiveDialogs.Any(x => x.HasValue))
return;
if (character == _characterProvider.MainCharacter)
{
_paperdollActions.RequestPaperdoll(_characterProvider.MainCharacter.ID);
_inGameDialogActions.ShowPaperdollDialog(_characterProvider.MainCharacter, isMainCharacter: true);
_userInputTimeRepository.LastInputTime = DateTime.Now;
}
else if (_characterRendererProvider.CharacterRenderers.ContainsKey(character.ID))
{
_contextMenuRepository.ContextMenu = _contextMenuRepository.ContextMenu.Match(
some: cmr =>
{
cmr.Dispose();
return Option.Some(_contextMenuRendererFactory.CreateContextMenuRenderer(_characterRendererProvider.CharacterRenderers[character.ID]));
},
none: () => Option.Some(_contextMenuRendererFactory.CreateContextMenuRenderer(_characterRendererProvider.CharacterRenderers[character.ID])));
_contextMenuRepository.ContextMenu.MatchSome(r => r.Initialize());
}
}
private void HandlePickupResult(ItemPickupResult pickupResult, MapItem 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 LeftClick(ISpellTargetable spellTarget);
void RightClick(Character character);
}
}