Skip to content

Commit

Permalink
Add handling for F11 sit and rendering of sit state
Browse files Browse the repository at this point in the history
Rendering offsets WIP
  • Loading branch information
ethanmoffat committed Jan 27, 2021
1 parent d4fde4d commit c89193d
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 3 deletions.
15 changes: 15 additions & 0 deletions EOLib/Domain/Character/CharacterActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ public void Attack()

_packetSendService.SendPacket(packet);
}

public void Sit()
{
var sitAction = _characterProvider.MainCharacter.RenderProperties.SitState == SitState.Standing
? SitAction.Sit
: SitAction.Stand;

var packet = new PacketBuilder(PacketFamily.Sit, PacketAction.Request)
.AddChar((byte)sitAction)
.Build();

_packetSendService.SendPacket(packet);
}
}

public interface ICharacterActions
Expand All @@ -62,5 +75,7 @@ public interface ICharacterActions
void Walk();

void Attack();

void Sit();
}
}
8 changes: 8 additions & 0 deletions EOLib/Domain/Character/SitAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace EOLib.Domain.Character
{
public enum SitAction
{
Sit = 1,
Stand = 2
}
}
3 changes: 3 additions & 0 deletions EOLib/EOLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="Domain\Character\CharacterStat.cs" />
<Compile Include="Domain\Character\CharacterStats.cs" />
<Compile Include="Domain\Character\ExperienceTableProvider.cs" />
<Compile Include="Domain\Character\SitAction.cs" />
<Compile Include="Domain\Character\StatTrainingActions.cs" />
<Compile Include="Domain\Character\TrainType.cs" />
<Compile Include="Domain\Character\WalkValidationActions.cs" />
Expand Down Expand Up @@ -257,9 +258,11 @@
<Compile Include="PacketHandlers\Effects\MapQuakeHandler.cs" />
<Compile Include="PacketHandlers\EndPlayerWarpHandler.cs" />
<Compile Include="PacketHandlers\ItemPickupHandler.cs" />
<Compile Include="PacketHandlers\PlayerSitHandler.cs" />
<Compile Include="PacketHandlers\MainPlayerWalkHandler.cs" />
<Compile Include="PacketHandlers\Effects\MapHpDrainHandler.cs" />
<Compile Include="PacketHandlers\NPCTakeDamageHandler.cs" />
<Compile Include="PacketHandlers\PlayerStandHandler.cs" />
<Compile Include="PacketHandlers\PlayerTargetOtherSpellHandler.cs" />
<Compile Include="PacketHandlers\PlayerSelfTargetSpellHandler.cs" />
<Compile Include="PacketHandlers\Effects\PlayerSpikeDamageHandler.cs" />
Expand Down
86 changes: 86 additions & 0 deletions EOLib/PacketHandlers/PlayerSitHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using EOLib.Domain.Login;
using EOLib.Domain.Map;
using EOLib.Net;
using EOLib.Net.Handlers;
using System.Linq;

namespace EOLib.PacketHandlers
{
public abstract class PlayerSitHandler : InGameOnlyPacketHandler
{
private readonly ICharacterRepository _characterRepository;
private readonly ICurrentMapStateRepository _currentMapStateRepository;

public override PacketAction Action => PacketAction.Player;

public PlayerSitHandler(IPlayerInfoProvider playerInfoProvider,
ICharacterRepository characterRepository,
ICurrentMapStateRepository currentMapStateRepository)
: base(playerInfoProvider)
{
_characterRepository = characterRepository;
_currentMapStateRepository = currentMapStateRepository;
}

public override bool HandlePacket(IPacket packet)
{
var playerId = packet.ReadShort();
var x = packet.ReadChar();
var y = packet.ReadChar();
var direction = (EODirection)packet.ReadChar();

var sitState = Family == PacketFamily.Sit ? SitState.Floor : SitState.Chair;

if (packet.ReadChar() != 0)
return false;

if (playerId == _characterRepository.MainCharacter.ID)
{
var renderProperties = _characterRepository.MainCharacter.RenderProperties;
var updatedRenderProperties = renderProperties.WithSitState(sitState)
.WithMapX(x)
.WithMapY(y)
.WithDirection(direction);
_characterRepository.MainCharacter = _characterRepository.MainCharacter.WithRenderProperties(updatedRenderProperties);
}
else
{
var oldCharacter = _currentMapStateRepository.Characters.Single(c => c.ID == playerId);
var renderProperties = oldCharacter.RenderProperties.WithSitState(sitState)
.WithMapX(x)
.WithMapY(y)
.WithDirection(direction);
var newCharacter = oldCharacter.WithRenderProperties(renderProperties);

_currentMapStateRepository.Characters.Remove(oldCharacter);
_currentMapStateRepository.Characters.Add(newCharacter);
}

return true;
}
}

[AutoMappedType]
public class PlayerSitFloorHandler : PlayerSitHandler
{
public override PacketFamily Family => PacketFamily.Sit;

public PlayerSitFloorHandler(IPlayerInfoProvider playerInfoProvider,
ICharacterRepository characterRepository,
ICurrentMapStateRepository currentMapStateRepository)
: base(playerInfoProvider, characterRepository, currentMapStateRepository) { }
}

[AutoMappedType]
public class PlayerSitChairHandler : PlayerSitHandler
{
public override PacketFamily Family => PacketFamily.Chair;

public PlayerSitChairHandler(IPlayerInfoProvider playerInfoProvider,
ICharacterRepository characterRepository,
ICurrentMapStateRepository currentMapStateRepository)
: base(playerInfoProvider, characterRepository, currentMapStateRepository) { }
}
}
78 changes: 78 additions & 0 deletions EOLib/PacketHandlers/PlayerStandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using EOLib.Domain.Login;
using EOLib.Domain.Map;
using EOLib.Net;
using EOLib.Net.Handlers;
using System.Linq;

namespace EOLib.PacketHandlers
{
public abstract class PlayerStandHandler : InGameOnlyPacketHandler
{
private readonly ICharacterRepository _characterRepository;
private readonly ICurrentMapStateRepository _currentMapStateRepository;

public override PacketFamily Family => PacketFamily.Sit;

public PlayerStandHandler(IPlayerInfoProvider playerInfoProvider,
ICharacterRepository characterRepository,
ICurrentMapStateRepository currentMapStateRepository)
: base(playerInfoProvider)
{
_characterRepository = characterRepository;
_currentMapStateRepository = currentMapStateRepository;
}

public override bool HandlePacket(IPacket packet)
{
var playerId = packet.ReadShort();
var x = packet.ReadChar();
var y = packet.ReadChar();

if (playerId == _characterRepository.MainCharacter.ID)
{
var renderProperties = _characterRepository.MainCharacter.RenderProperties;
var updatedRenderProperties = renderProperties.WithSitState(SitState.Standing)
.WithMapX(x)
.WithMapY(y);
_characterRepository.MainCharacter = _characterRepository.MainCharacter.WithRenderProperties(updatedRenderProperties);
}
else
{
var oldCharacter = _currentMapStateRepository.Characters.Single(c => c.ID == playerId);
var renderProperties = oldCharacter.RenderProperties.WithSitState(SitState.Standing)
.WithMapX(x)
.WithMapY(y);
var newCharacter = oldCharacter.WithRenderProperties(renderProperties);

_currentMapStateRepository.Characters.Remove(oldCharacter);
_currentMapStateRepository.Characters.Add(newCharacter);
}

return true;
}
}

[AutoMappedType]
public class OtherPlayerStandHandler : PlayerStandHandler
{
public override PacketAction Action => PacketAction.Remove;

public OtherPlayerStandHandler(IPlayerInfoProvider playerInfoProvider,
ICharacterRepository characterRepository,
ICurrentMapStateRepository currentMapStateRepository)
: base(playerInfoProvider, characterRepository, currentMapStateRepository) { }
}

[AutoMappedType]
public class MainPlayerStandHandler : PlayerStandHandler
{
public override PacketAction Action => PacketAction.Close;

public MainPlayerStandHandler(IPlayerInfoProvider playerInfoProvider,
ICharacterRepository characterRepository,
ICurrentMapStateRepository currentMapStateRepository)
: base(playerInfoProvider, characterRepository, currentMapStateRepository) { }
}
}
14 changes: 13 additions & 1 deletion EndlessClient/Controllers/FunctionKeyController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using EOLib.Domain.Map;

namespace EndlessClient.Controllers
Expand All @@ -7,10 +8,19 @@ namespace EndlessClient.Controllers
public class FunctionKeyController : IFunctionKeyController
{
private readonly IMapActions _mapActions;
private readonly ICharacterActions _characterActions;

public FunctionKeyController(IMapActions mapActions)
public FunctionKeyController(IMapActions mapActions,
ICharacterActions characterActions)
{
_mapActions = mapActions;
_characterActions = characterActions;
}

public bool Sit()
{
_characterActions.Sit();
return true;
}

public bool RefreshMapState()
Expand All @@ -22,6 +32,8 @@ public bool RefreshMapState()

public interface IFunctionKeyController
{
bool Sit();

bool RefreshMapState();
}
}
5 changes: 4 additions & 1 deletion EndlessClient/Input/FunctionKeyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public class FunctionKeyHandler : InputHandlerBase

protected override Optional<Keys> HandleInput()
{
if (IsKeyHeld(Keys.F12) && _functionKeyController.RefreshMapState())
if (IsKeyPressedOnce(Keys.F11) && _functionKeyController.Sit())
return Keys.F11;

if (IsKeyPressedOnce(Keys.F12) && _functionKeyController.RefreshMapState())
return Keys.F12;

// todo: spell selection
Expand Down
5 changes: 5 additions & 0 deletions EndlessClient/Input/InputHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ protected bool IsKeyHeld(params Keys[] keys)
{
return keys.Any(key => CurrentState.IsKeyHeld(PreviousState, key));
}

protected bool IsKeyPressedOnce(params Keys[] keys)
{
return keys.Any(key => CurrentState.IsKeyPressedOnce(PreviousState, key));
}
}

public interface IInputHandler
Expand Down
5 changes: 4 additions & 1 deletion EndlessClient/Rendering/Character/CharacterRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected override void LoadContent()
if (_gameStateProvider.CurrentState == GameStates.None)
{
_outline = new Texture2D(GraphicsDevice, 1, 1);
_outline.SetData(new[] { Color.Black });
_outline.SetData(new[] { Color.White });
}

base.LoadContent();
Expand Down Expand Up @@ -267,6 +267,9 @@ private void DrawToRenderTarget()
_sb.Draw(_outline, DrawArea.WithPosition(new Vector2(DrawArea.X + DrawArea.Width, DrawArea.Y)).WithSize(1, DrawArea.Height), Color.Black);
_sb.Draw(_outline, DrawArea.WithPosition(new Vector2(DrawArea.X, DrawArea.Y + DrawArea.Height)).WithSize(DrawArea.Width, 1), Color.Black);
_sb.Draw(_outline, DrawArea.WithSize(1, DrawArea.Height), Color.Black);

_sb.Draw(_outline, DrawArea, Color.FromNonPremultiplied(255, 0, 0, 64));
_sb.Draw(_outline, MapProjectedDrawArea, Color.FromNonPremultiplied(0, 255, 0, 64));
}

_sb.End();
Expand Down

0 comments on commit c89193d

Please sign in to comment.