Skip to content

Commit

Permalink
Update input handling to back off when completing a warp. Fixes some …
Browse files Browse the repository at this point in the history
…issues where warping causes the character to jump about weirdly
  • Loading branch information
ethanmoffat committed Sep 20, 2022
1 parent c12bd67 commit 0d30778
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 23 deletions.
7 changes: 7 additions & 0 deletions EOLib/Domain/Map/CurrentMapStateRepository.cs
@@ -1,5 +1,6 @@
using AutomaticTypeMapper;
using Optional;
using System;
using System.Collections.Generic;

namespace EOLib.Domain.Map
Expand Down Expand Up @@ -30,6 +31,8 @@ public interface ICurrentMapStateRepository

Option<short> MapWarpID { get; set; }

Option<DateTime> MapWarpTime { get; set; }

HashSet<short> UnknownPlayerIDs { get; set; }

HashSet<byte> UnknownNPCIndexes { get; set; }
Expand Down Expand Up @@ -61,6 +64,8 @@ public interface ICurrentMapStateProvider

Option<short> MapWarpID { get; }

Option<DateTime> MapWarpTime { get; }

HashSet<short> UnknownPlayerIDs { get; }

HashSet<byte> UnknownNPCIndexes { get; }
Expand Down Expand Up @@ -93,6 +98,8 @@ public class CurrentMapStateRepository : ICurrentMapStateRepository, ICurrentMap

public Option<short> MapWarpID { get; set; }

public Option<DateTime> MapWarpTime { get; set; }

public HashSet<short> UnknownPlayerIDs { get; set; }

public HashSet<byte> UnknownNPCIndexes { get; set; }
Expand Down
6 changes: 5 additions & 1 deletion EOLib/PacketHandlers/EndPlayerWarpHandler.cs
Expand Up @@ -10,6 +10,8 @@
using EOLib.Net;
using EOLib.Net.Handlers;
using EOLib.Net.Translators;
using Optional;
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -52,6 +54,8 @@ public override bool HandlePacket(IPacket packet)

var warpAgreePacketData = _warpAgreePacketTranslator.TranslatePacket(packet);

_currentMapStateRepository.CurrentMapID = warpAgreePacketData.MapID;

var updatedMainCharacter = warpAgreePacketData.Characters.Single(MainCharacterIDMatches);

//character.renderproperties.isdead is set True by the attack handler
Expand All @@ -66,7 +70,6 @@ public override bool HandlePacket(IPacket packet)

var differentMapID = _currentMapStateRepository.CurrentMapID != warpAgreePacketData.MapID;

_currentMapStateRepository.CurrentMapID = warpAgreePacketData.MapID;
_currentMapStateRepository.Characters = warpAgreePacketData.Characters.ToDictionary(k => k.ID, v => v);
_currentMapStateRepository.NPCs = new HashSet<NPC>(warpAgreePacketData.NPCs);
_currentMapStateRepository.MapItems = new HashSet<MapItem>(warpAgreePacketData.Items);
Expand All @@ -80,6 +83,7 @@ public override bool HandlePacket(IPacket packet)
warpAnimation: warpAgreePacketData.WarpAnimation);

_currentMapStateRepository.MapWarpState = WarpState.None;
_currentMapStateRepository.MapWarpTime = Option.Some(DateTime.Now);

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions EndlessClient/Input/ArrowKeyHandler.cs
Expand Up @@ -14,8 +14,8 @@ public class ArrowKeyHandler : InputHandlerBase
IUserInputProvider userInputProvider,
IUserInputTimeRepository userInputTimeRepository,
IArrowKeyController arrowKeyController,
ICurrentMapStateProvider currentMapStateProvider)
: base(endlessGameProvider, userInputProvider, userInputTimeRepository, currentMapStateProvider)
ICurrentMapStateRepository currentMapStateRepository)
: base(endlessGameProvider, userInputProvider, userInputTimeRepository, currentMapStateRepository)
{
_arrowKeyController = arrowKeyController;
}
Expand Down
4 changes: 2 additions & 2 deletions EndlessClient/Input/ControlKeyHandler.cs
Expand Up @@ -14,8 +14,8 @@ public class ControlKeyHandler : InputHandlerBase
IUserInputProvider userInputProvider,
IUserInputTimeRepository userInputTimeRepository,
IControlKeyController controlKeyController,
ICurrentMapStateProvider currentMapStateProvider)
: base(endlessGameProvider, userInputProvider, userInputTimeRepository, currentMapStateProvider)
ICurrentMapStateRepository currentMapStateRepository)
: base(endlessGameProvider, userInputProvider, userInputTimeRepository, currentMapStateRepository)
{
_controlKeyController = controlKeyController;
}
Expand Down
4 changes: 2 additions & 2 deletions EndlessClient/Input/FunctionKeyHandler.cs
Expand Up @@ -14,8 +14,8 @@ public class FunctionKeyHandler : InputHandlerBase
IUserInputProvider userInputProvider,
IUserInputTimeRepository userInputTimeRepository,
IFunctionKeyController functionKeyController,
ICurrentMapStateProvider currentMapStateProvider)
: base(endlessGameProvider, userInputProvider, userInputTimeRepository, currentMapStateProvider)
ICurrentMapStateRepository currentMapStateRepository)
: base(endlessGameProvider, userInputProvider, userInputTimeRepository, currentMapStateRepository)
{
_functionKeyController = functionKeyController;
}
Expand Down
15 changes: 10 additions & 5 deletions EndlessClient/Input/InputHandlerBase.cs
Expand Up @@ -10,11 +10,12 @@ namespace EndlessClient.Input
public abstract class InputHandlerBase : IInputHandler
{
private const int INPUT_RATE_LIMIT_MILLISECONDS = 200;
private const int WARP_BACKOFF_TIME_MILLISECONDS = 300;

private readonly IEndlessGameProvider _endlessGameProvider;
private readonly IUserInputProvider _keyStateProvider;
private readonly IUserInputTimeRepository _userInputTimeRepository;
private readonly ICurrentMapStateProvider _mapStateProvider;
private readonly ICurrentMapStateRepository _currentMapStateRepository;

private KeyboardState CurrentState => _keyStateProvider.CurrentKeyState;

Expand All @@ -23,23 +24,27 @@ public abstract class InputHandlerBase : IInputHandler
protected InputHandlerBase(IEndlessGameProvider endlessGameProvider,
IUserInputProvider userInputProvider,
IUserInputTimeRepository userInputTimeRepository,
ICurrentMapStateProvider mapStateProvider)
ICurrentMapStateRepository currentMapStateRepository)
{
_endlessGameProvider = endlessGameProvider;
_keyStateProvider = userInputProvider;
_userInputTimeRepository = userInputTimeRepository;
_mapStateProvider = mapStateProvider;
_currentMapStateRepository = currentMapStateRepository;
}

public void HandleKeyboardInput(DateTime timeAtBeginningOfUpdate)
{
var millisecondsSinceLastUpdate = GetMillisecondsSinceLastUpdate(timeAtBeginningOfUpdate);
if (!_endlessGameProvider.Game.IsActive ||
millisecondsSinceLastUpdate < INPUT_RATE_LIMIT_MILLISECONDS ||
_mapStateProvider.MapWarpState != WarpState.None)
_currentMapStateRepository.MapWarpState != WarpState.None)
return;

HandleInput().MatchSome(_ => _userInputTimeRepository.LastInputTime = timeAtBeginningOfUpdate);
_currentMapStateRepository.MapWarpTime = _currentMapStateRepository.MapWarpTime.Match(
some: t => (DateTime.Now - t).TotalMilliseconds > WARP_BACKOFF_TIME_MILLISECONDS ? Option.None<DateTime>() : Option.Some(t),
none: () => Option.None<DateTime>());

_currentMapStateRepository.MapWarpTime.MatchNone(() => HandleInput().MatchSome(_ => _userInputTimeRepository.LastInputTime = timeAtBeginningOfUpdate));
}

private double GetMillisecondsSinceLastUpdate(DateTime timeAtBeginningOfUpdate)
Expand Down
4 changes: 2 additions & 2 deletions EndlessClient/Input/NumPadHandler.cs
Expand Up @@ -15,9 +15,9 @@ public class NumPadHandler : InputHandlerBase
public NumPadHandler(IEndlessGameProvider endlessGameProvider,
IUserInputProvider userInputProvider,
IUserInputTimeRepository userInputTimeRepository,
ICurrentMapStateProvider mapStateProvider,
ICurrentMapStateRepository currentMapStateRepository,
INumPadController numPadController)
: base(endlessGameProvider, userInputProvider, userInputTimeRepository, mapStateProvider)
: base(endlessGameProvider, userInputProvider, userInputTimeRepository, currentMapStateRepository)
{
_numPadController = numPadController;
}
Expand Down
10 changes: 5 additions & 5 deletions EndlessClient/Input/UserInputHandler.cs
Expand Up @@ -22,7 +22,7 @@ public class UserInputHandler : XNAControl, IUserInputHandler
IControlKeyController controlKeyController,
IFunctionKeyController functionKeyController,
INumPadController numPadController,
ICurrentMapStateProvider currentMapStateProvider,
ICurrentMapStateRepository currentMapStateRepository,
IActiveDialogProvider activeDialogProvider)
{
_handlers = new List<IInputHandler>
Expand All @@ -31,21 +31,21 @@ public class UserInputHandler : XNAControl, IUserInputHandler
userInputProvider,
userInputTimeRepository,
arrowKeyController,
currentMapStateProvider),
currentMapStateRepository),
new ControlKeyHandler(endlessGameProvider,
userInputProvider,
userInputTimeRepository,
controlKeyController,
currentMapStateProvider),
currentMapStateRepository),
new FunctionKeyHandler(endlessGameProvider,
userInputProvider,
userInputTimeRepository,
functionKeyController,
currentMapStateProvider),
currentMapStateRepository),
new NumPadHandler(endlessGameProvider,
userInputProvider,
userInputTimeRepository,
currentMapStateProvider,
currentMapStateRepository,
numPadController),
};
_activeDialogProvider = activeDialogProvider;
Expand Down
8 changes: 4 additions & 4 deletions EndlessClient/Input/UserInputHandlerFactory.cs
Expand Up @@ -16,7 +16,7 @@ public class UserInputHandlerFactory : IUserInputHandlerFactory
private readonly IControlKeyController _controlKeyController;
private readonly IFunctionKeyController _functionKeyController;
private readonly INumPadController _numPadController;
private readonly ICurrentMapStateProvider _currentMapStateProvider;
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly IActiveDialogProvider _activeDialogProvider;

public UserInputHandlerFactory(IEndlessGameProvider endlessGameProvider,
Expand All @@ -26,7 +26,7 @@ public class UserInputHandlerFactory : IUserInputHandlerFactory
IControlKeyController controlKeyController,
IFunctionKeyController functionKeyController,
INumPadController numPadController,
ICurrentMapStateProvider currentMapStateProvider,
ICurrentMapStateRepository currentMapStateRepository,
IActiveDialogProvider activeDialogProvider)
{
_endlessGameProvider = endlessGameProvider;
Expand All @@ -36,7 +36,7 @@ public class UserInputHandlerFactory : IUserInputHandlerFactory
_controlKeyController = controlKeyController;
_functionKeyController = functionKeyController;
_numPadController = numPadController;
_currentMapStateProvider = currentMapStateProvider;
_currentMapStateRepository = currentMapStateRepository;
_activeDialogProvider = activeDialogProvider;
}

Expand All @@ -49,7 +49,7 @@ public IUserInputHandler CreateUserInputHandler()
_controlKeyController,
_functionKeyController,
_numPadController,
_currentMapStateProvider,
_currentMapStateRepository,
_activeDialogProvider);
}
}
Expand Down

0 comments on commit 0d30778

Please sign in to comment.