Skip to content

Commit

Permalink
Prevent main character from trying to walk to an unwalkable tile
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Mar 7, 2022
1 parent bcc1a70 commit 114e6e4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
7 changes: 5 additions & 2 deletions EndlessClient/HUD/Controls/HudControlsFactory.cs
Expand Up @@ -51,6 +51,7 @@ public class HudControlsFactory : IHudControlsFactory
private readonly IArrowKeyController _arrowKeyController;
private readonly IPathFinder _pathFinder;
private readonly ICharacterActions _characterActions;
private readonly IWalkValidationActions _walkValidationActions;
private IChatController _chatController;

public HudControlsFactory(IHudButtonController hudButtonController,
Expand All @@ -73,7 +74,8 @@ public class HudControlsFactory : IHudControlsFactory
IExperienceTableProvider experienceTableProvider,
IArrowKeyController arrowKeyController,
IPathFinder pathFinder,
ICharacterActions characterActions)
ICharacterActions characterActions,
IWalkValidationActions walkValidationActions)
{
_hudButtonController = hudButtonController;
_hudPanelFactory = hudPanelFactory;
Expand All @@ -96,6 +98,7 @@ public class HudControlsFactory : IHudControlsFactory
_arrowKeyController = arrowKeyController;
_pathFinder = pathFinder;
_characterActions = characterActions;
_walkValidationActions = walkValidationActions;
}

public void InjectChatController(IChatController chatController)
Expand Down Expand Up @@ -338,7 +341,7 @@ private IUserInputHandler CreateUserInputHandler()

private ICharacterAnimator CreateCharacterAnimator()
{
return new CharacterAnimator(_endlessGameProvider, _characterRepository, _currentMapStateRepository, _currentMapProvider, _characterActions);
return new CharacterAnimator(_endlessGameProvider, _characterRepository, _currentMapStateRepository, _currentMapProvider, _characterActions, _walkValidationActions);
}

private INPCAnimator CreateNPCAnimator()
Expand Down
32 changes: 21 additions & 11 deletions EndlessClient/Rendering/Character/CharacterAnimator.cs
Expand Up @@ -19,7 +19,7 @@ public class CharacterAnimator : GameComponent, ICharacterAnimator
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly ICurrentMapProvider _currentMapProvider;
private readonly ICharacterActions _characterActions;

private readonly IWalkValidationActions _walkValidationActions;
private readonly Dictionary<int, EODirection> _queuedDirections;
private readonly Dictionary<int, MapCoordinate> _queuedPositions;
private readonly Dictionary<int, RenderFrameActionTime> _otherPlayerStartWalkingTimes;
Expand All @@ -30,14 +30,15 @@ public class CharacterAnimator : GameComponent, ICharacterAnimator
ICharacterRepository characterRepository,
ICurrentMapStateRepository currentMapStateRepository,
ICurrentMapProvider currentMapProvider,
ICharacterActions characterActions)
ICharacterActions characterActions,
IWalkValidationActions walkValidationActions)
: base((Game) gameProvider.Game)
{
_characterRepository = characterRepository;
_currentMapStateRepository = currentMapStateRepository;
_currentMapProvider = currentMapProvider;
_characterActions = characterActions;

_walkValidationActions = walkValidationActions;
_queuedDirections = new Dictionary<int, EODirection>();
_queuedPositions = new Dictionary<int, MapCoordinate>();
_otherPlayerStartWalkingTimes = new Dictionary<int, RenderFrameActionTime>();
Expand Down Expand Up @@ -85,7 +86,6 @@ public void StartMainCharacterWalkAnimation()

public void StartMainCharacterAttackAnimation()
{
// todo: queue attack if walking
if (_otherPlayerStartAttackingTimes.ContainsKey(_characterRepository.MainCharacter.ID))
{
_otherPlayerStartAttackingTimes[_characterRepository.MainCharacter.ID].Replay = true;
Expand Down Expand Up @@ -179,15 +179,25 @@ private void AnimateCharacterWalking()
{
if (pair.Replay)
{
// send the walk packet after the game state has been updated so the correct coordinates are sent
sendWalk = currentCharacter == _characterRepository.MainCharacter;
nextFrameRenderProperties = AnimateOneWalkFrame(nextFrameRenderProperties.ResetAnimationFrames());
pair.Replay = false;
var isMainCharacter = currentCharacter == _characterRepository.MainCharacter;

if (_queuedDirections.ContainsKey(pair.UniqueID))
if (isMainCharacter && _walkValidationActions.CanMoveToCoordinates(nextFrameRenderProperties.GetDestinationX(), nextFrameRenderProperties.GetDestinationY()))
{
// send the walk packet after the game state has been updated so the correct coordinates are sent
sendWalk = currentCharacter == _characterRepository.MainCharacter;
nextFrameRenderProperties = AnimateOneWalkFrame(nextFrameRenderProperties.ResetAnimationFrames());
pair.Replay = false;

if (_queuedDirections.ContainsKey(pair.UniqueID))
{
nextFrameRenderProperties = nextFrameRenderProperties.WithDirection(_queuedDirections[pair.UniqueID]);
_queuedDirections.Remove(pair.UniqueID);
}
}
else
{
nextFrameRenderProperties = nextFrameRenderProperties.WithDirection(_queuedDirections[pair.UniqueID]);
_queuedDirections.Remove(pair.UniqueID);
// tried to replay but the new destination position is not walkable
playersDoneWalking.Add(pair.UniqueID);
}
}
else
Expand Down

0 comments on commit 114e6e4

Please sign in to comment.