Skip to content

Commit

Permalink
Add handling for PK spells
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Apr 18, 2022
1 parent 7e2809e commit ae959e8
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 6 deletions.
98 changes: 98 additions & 0 deletions EOLib/PacketHandlers/PlayerPKSpellHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using EOLib.Domain.Login;
using EOLib.Domain.Map;
using EOLib.Domain.Notifiers;
using EOLib.Net;
using EOLib.Net.Handlers;
using System.Collections.Generic;

namespace EOLib.PacketHandlers
{
[AutoMappedType]
public class PlayerPKSpellHandler : InGameOnlyPacketHandler
{
private readonly ICharacterRepository _characterRepository;
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly IEnumerable<IOtherCharacterAnimationNotifier> _animationNotifiers;

public override PacketFamily Family => PacketFamily.Avatar;

public override PacketAction Action => PacketAction.Admin;

public PlayerPKSpellHandler(IPlayerInfoProvider playerInfoProvider,
ICharacterRepository characterRepository,
ICurrentMapStateRepository currentMapStateRepository,
IEnumerable<IOtherCharacterAnimationNotifier> animationNotifiers)
: base(playerInfoProvider)
{
_characterRepository = characterRepository;
_currentMapStateRepository = currentMapStateRepository;
_animationNotifiers = animationNotifiers;
}

public override bool HandlePacket(IPacket packet)
{
var sourcePlayerId = packet.ReadShort();
var targetPlayerId = packet.ReadShort();
var damage = packet.ReadThree();
var sourcePlayerDirection = (EODirection)packet.ReadChar();
var targetPercentHealth = packet.ReadChar();
var targetIsDead = packet.ReadChar() != 0;
var spellId = packet.ReadShort();

if (sourcePlayerId == _characterRepository.MainCharacter.ID)
{
var renderProps = _characterRepository.MainCharacter.RenderProperties.WithDirection(sourcePlayerDirection);
_characterRepository.MainCharacter = _characterRepository.MainCharacter.WithRenderProperties(renderProps);
}
else if (_currentMapStateRepository.Characters.ContainsKey(sourcePlayerId))
{
var character = _currentMapStateRepository.Characters[sourcePlayerId];
var updatedCharacter = character.WithRenderProperties(character.RenderProperties.WithDirection(sourcePlayerDirection));
_currentMapStateRepository.Characters[sourcePlayerId] = updatedCharacter;
}
else
{
_currentMapStateRepository.UnknownPlayerIDs.Add(sourcePlayerId);
return true;
}

if (targetPlayerId == _characterRepository.MainCharacter.ID)
{
var renderProps = _characterRepository.MainCharacter.RenderProperties;
if (targetIsDead)
renderProps = renderProps.WithDead();

var stats = _characterRepository.MainCharacter.Stats;
stats = stats.WithNewStat(CharacterStat.HP, stats[CharacterStat.HP] - damage);
_characterRepository.MainCharacter = _characterRepository.MainCharacter
.WithStats(stats)
.WithRenderProperties(renderProps);
}
else if (_currentMapStateRepository.Characters.ContainsKey(targetPlayerId))
{
var c = _currentMapStateRepository.Characters[targetPlayerId];

var renderProps = c.RenderProperties;
if (targetIsDead)
renderProps = renderProps.WithDead();

var stats = c.Stats;
stats = stats.WithNewStat(CharacterStat.HP, stats[CharacterStat.HP] - damage);

_currentMapStateRepository.Characters[targetPlayerId] = c.WithStats(stats).WithRenderProperties(renderProps);
}
else
{
_currentMapStateRepository.UnknownPlayerIDs.Add(targetPlayerId);
return true;
}

foreach (var notifier in _animationNotifiers)
notifier.NotifyTargetOtherSpellCast(sourcePlayerId, targetPlayerId, spellId, damage, targetPercentHealth);

return true;
}
}
}
12 changes: 6 additions & 6 deletions EndlessClient/Rendering/Character/CharacterAnimationActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,6 @@ public void NotifySelfSpellCast(short playerId, short spellId, int spellHp, byte

public void NotifyTargetOtherSpellCast(short sourcePlayerID, short targetPlayerID, short spellId, int recoveredHP, byte targetPercentHealth)
{
var spellGraphic = _esfFileProvider.ESFFile[spellId].Graphic;

if (sourcePlayerID == _characterRepository.MainCharacter.ID)
{
_characterRendererProvider.MainCharacterRenderer.MatchSome(cr => cr.ShoutSpellCast());
Expand All @@ -171,18 +169,20 @@ public void NotifyTargetOtherSpellCast(short sourcePlayerID, short targetPlayerI
_characterRendererProvider.CharacterRenderers[sourcePlayerID].ShoutSpellCast();
}

var spellData = _esfFileProvider.ESFFile[spellId];

if (targetPlayerID == _characterRepository.MainCharacter.ID)
{
_characterRendererProvider.MainCharacterRenderer.MatchSome(cr =>
{
cr.ShowSpellAnimation(spellGraphic);
cr.ShowDamageCounter(recoveredHP, targetPercentHealth, isHeal: true);
cr.ShowSpellAnimation(spellData.Graphic);
cr.ShowDamageCounter(recoveredHP, targetPercentHealth, isHeal: spellData.Type == EOLib.IO.SpellType.Heal);
});
}
else
{
_characterRendererProvider.CharacterRenderers[targetPlayerID].ShowSpellAnimation(spellGraphic);
_characterRendererProvider.CharacterRenderers[targetPlayerID].ShowDamageCounter(recoveredHP, targetPercentHealth, isHeal: true);
_characterRendererProvider.CharacterRenderers[targetPlayerID].ShowSpellAnimation(spellData.Graphic);
_characterRendererProvider.CharacterRenderers[targetPlayerID].ShowDamageCounter(recoveredHP, targetPercentHealth, isHeal: spellData.Type == EOLib.IO.SpellType.Heal);
}
}

Expand Down

0 comments on commit ae959e8

Please sign in to comment.