Skip to content

Commit

Permalink
Fix other character walk animations
Browse files Browse the repository at this point in the history
Pass target x/y coordinate and direction to animation notifier(s) instead of updating state in the packet handler directly. Relies on animator to control where the character is positioned.
  • Loading branch information
ethanmoffat committed Mar 6, 2022
1 parent 2ceeff5 commit 8734c3a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 40 deletions.
3 changes: 2 additions & 1 deletion EOBot/Program.cs
@@ -1,5 +1,6 @@
using AutomaticTypeMapper;
using EOBot.Interpreter;
using EOLib;
using EOLib.Domain.Character;
using EOLib.Domain.Extensions;
using EOLib.Domain.Map;
Expand Down Expand Up @@ -151,7 +152,7 @@ public void NotifySelfSpellCast(short playerId, short spellId, int spellHp, byte
public void NotifyStartSpellCast(short playerId, short spellId) { }
public void NotifyTargetOtherSpellCast(short sourcePlayerID, short targetPlayerID, short spellId, int recoveredHP, byte targetPercentHealth) { }
public void StartOtherCharacterAttackAnimation(int characterID) { }
public void StartOtherCharacterWalkAnimation(int characterID) { }
public void StartOtherCharacterWalkAnimation(int characterID, byte destinationX, byte destinationY, EODirection direction) { }
}

static async Task<int> Main(string[] args)
Expand Down
4 changes: 2 additions & 2 deletions EOLib/Domain/Notifiers/IOtherCharacterAnimationNotifier.cs
Expand Up @@ -4,7 +4,7 @@ namespace EOLib.Domain.Notifiers
{
public interface IOtherCharacterAnimationNotifier
{
void StartOtherCharacterWalkAnimation(int characterID);
void StartOtherCharacterWalkAnimation(int characterID, byte destinationX, byte destinationY, EODirection direction);

void StartOtherCharacterAttackAnimation(int characterID);

Expand All @@ -18,7 +18,7 @@ public interface IOtherCharacterAnimationNotifier
[AutoMappedType]
public class NoOpOtherCharacterAnimationNotifier : IOtherCharacterAnimationNotifier
{
public void StartOtherCharacterWalkAnimation(int characterID) { }
public void StartOtherCharacterWalkAnimation(int characterID, byte destinationX, byte destinationY, EODirection direction) { }

public void StartOtherCharacterAttackAnimation(int characterID) { }

Expand Down
28 changes: 1 addition & 27 deletions EOLib/PacketHandlers/PlayerWalkHandler.cs
Expand Up @@ -38,36 +38,10 @@ public override bool HandlePacket(IPacket packet)
var x = packet.ReadChar();
var y = packet.ReadChar();

ICharacter character;
try
{
character = _currentMapStateRepository.Characters.Single(cc => cc.ID == characterID);
}
catch (InvalidOperationException) { return false; }

var renderProperties = character.RenderProperties.WithDirection(dir);
renderProperties = EnsureCorrectXAndY(renderProperties, x, y);
var newCharacter = character.WithRenderProperties(renderProperties);

_currentMapStateRepository.Characters.Remove(character);
_currentMapStateRepository.Characters.Add(newCharacter);

foreach (var notifier in _otherCharacterAnimationNotifiers)
notifier.StartOtherCharacterWalkAnimation(characterID);
notifier.StartOtherCharacterWalkAnimation(characterID, x, y, dir);

return true;
}

private static ICharacterRenderProperties EnsureCorrectXAndY(ICharacterRenderProperties renderProperties, byte x, byte y)
{
var opposite = renderProperties.Direction.Opposite();
var tempRenderProperties = renderProperties
.WithDirection(opposite)
.WithMapX(x)
.WithMapY(y);
return renderProperties
.WithMapX(tempRenderProperties.GetDestinationX())
.WithMapY(tempRenderProperties.GetDestinationY());
}
}
}
Expand Up @@ -81,12 +81,13 @@ public void StartAttacking()
_characterRendererProvider.MainCharacterRenderer);
}

public void StartOtherCharacterWalkAnimation(int characterID)
public void StartOtherCharacterWalkAnimation(int characterID, byte destinationX, byte destinationY, EODirection direction)
{
if (!_hudControlProvider.IsInGame)
return;

Animator.StartOtherCharacterWalkAnimation(characterID);
Animator.StartOtherCharacterWalkAnimation(characterID, destinationX, destinationY, direction);

ShowWaterSplashiesIfNeeded(CharacterActionState.Walking,
_currentMapStateProvider.Characters.Single(x => x.ID == characterID),
_characterRendererProvider.CharacterRenderers[characterID]);
Expand Down
24 changes: 16 additions & 8 deletions EndlessClient/Rendering/Character/CharacterAnimator.cs
Expand Up @@ -23,6 +23,7 @@ public class CharacterAnimator : GameComponent, ICharacterAnimator
private readonly ICharacterActions _characterActions;

private readonly Dictionary<int, EODirection> _queuedDirections;
private readonly Dictionary<int, MapCoordinate> _queuedPositions;
private readonly Dictionary<int, RenderFrameActionTime> _otherPlayerStartWalkingTimes;
private readonly Dictionary<int, RenderFrameActionTime> _otherPlayerStartAttackingTimes;
private readonly Dictionary<int, RenderFrameActionTime> _otherPlayerStartSpellCastTimes;
Expand All @@ -40,6 +41,7 @@ public class CharacterAnimator : GameComponent, ICharacterAnimator
_characterActions = characterActions;

_queuedDirections = new Dictionary<int, EODirection>();
_queuedPositions = new Dictionary<int, MapCoordinate>();
_otherPlayerStartWalkingTimes = new Dictionary<int, RenderFrameActionTime>();
_otherPlayerStartAttackingTimes = new Dictionary<int, RenderFrameActionTime>();
_otherPlayerStartSpellCastTimes = new Dictionary<int, RenderFrameActionTime>();
Expand Down Expand Up @@ -100,16 +102,14 @@ public void StartMainCharacterAttackAnimation()
_otherPlayerStartAttackingTimes.Add(_characterRepository.MainCharacter.ID, startAttackingTime);
}

public void StartOtherCharacterWalkAnimation(int characterID)
public void StartOtherCharacterWalkAnimation(int characterID, byte destinationX, byte destinationY, EODirection direction)
{
if (_otherPlayerStartWalkingTimes.ContainsKey(characterID) ||
_otherPlayerStartSpellCastTimes.ContainsKey(characterID))
return;

if (_otherPlayerStartWalkingTimes.TryGetValue(characterID, out var _))
{
ResetCharacterAnimationFrames(characterID);
_otherPlayerStartWalkingTimes.Remove(characterID);
_otherPlayerStartWalkingTimes[characterID].Replay = true;
_queuedDirections[characterID] = direction;
_queuedPositions[characterID] = new MapCoordinate(destinationX, destinationY);
return;
}

var startWalkingTimeAndID = new RenderFrameActionTime(characterID);
Expand Down Expand Up @@ -202,6 +202,14 @@ private void AnimateCharacterWalking()
}
else
{
if (_queuedPositions.ContainsKey(pair.UniqueID))
{
nextFrameRenderProperties = nextFrameRenderProperties
.WithMapX(_queuedPositions[pair.UniqueID].X)
.WithMapY(_queuedPositions[pair.UniqueID].Y);
_queuedPositions.Remove(pair.UniqueID);
}

playersDoneWalking.Add(pair.UniqueID);
}
}
Expand Down Expand Up @@ -365,7 +373,7 @@ public interface ICharacterAnimator : IGameComponent

void StartMainCharacterAttackAnimation();

void StartOtherCharacterWalkAnimation(int characterID);
void StartOtherCharacterWalkAnimation(int characterID, byte targetX, byte targetY, EODirection direction);

void StartOtherCharacterAttackAnimation(int characterID);

Expand Down

0 comments on commit 8734c3a

Please sign in to comment.