Skip to content

Commit

Permalink
Reduce character SP when attaccking or casting spells
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Apr 18, 2022
1 parent aab32ba commit d9f835b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 59 deletions.
26 changes: 17 additions & 9 deletions EOLib/Domain/Character/CharacterActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ namespace EOLib.Domain.Character
public class CharacterActions : ICharacterActions
{
private readonly IPacketSendService _packetSendService;
private readonly ICharacterProvider _characterProvider;
private readonly ICharacterRepository _characterRepository;
private readonly IESFFileProvider _spellFileProvider;

public CharacterActions(IPacketSendService packetSendService,
ICharacterProvider characterProvider,
ICharacterRepository characterRepository,
IESFFileProvider spellFileProvider)
{
_packetSendService = packetSendService;
_characterProvider = characterProvider;
_characterRepository = characterRepository;
_spellFileProvider = spellFileProvider;
}

Expand All @@ -37,9 +37,9 @@ public void Face(EODirection direction)

public void Walk()
{
var admin = _characterProvider.MainCharacter.NoWall &&
_characterProvider.MainCharacter.AdminLevel != AdminLevel.Player;
var renderProperties = _characterProvider.MainCharacter.RenderProperties;
var admin = _characterRepository.MainCharacter.NoWall &&
_characterRepository.MainCharacter.AdminLevel != AdminLevel.Player;
var renderProperties = _characterRepository.MainCharacter.RenderProperties;

var packet = new PacketBuilder(PacketFamily.Walk, admin ? PacketAction.Admin : PacketAction.Player)
.AddChar((byte) renderProperties.Direction)
Expand All @@ -53,8 +53,12 @@ public void Walk()

public void Attack()
{
var c = _characterRepository.MainCharacter;
var sp = Math.Max(0, c.Stats[CharacterStat.SP] - 1);
_characterRepository.MainCharacter = c.WithStats(c.Stats.WithNewStat(CharacterStat.SP, sp));

var packet = new PacketBuilder(PacketFamily.Attack, PacketAction.Use)
.AddChar((byte) _characterProvider.MainCharacter.RenderProperties.Direction)
.AddChar((byte) _characterRepository.MainCharacter.RenderProperties.Direction)
.AddThree(DateTime.Now.ToEOTimeStamp())
.Build();

Expand All @@ -63,7 +67,7 @@ public void Attack()

public void ToggleSit()
{
var renderProperties = _characterProvider.MainCharacter.RenderProperties;
var renderProperties = _characterRepository.MainCharacter.RenderProperties;
var sitAction = renderProperties.SitState == SitState.Standing
? SitAction.Sit
: SitAction.Stand;
Expand All @@ -81,7 +85,7 @@ public void ToggleSit()

public void SitInChair()
{
var rp = _characterProvider.MainCharacter.RenderProperties;
var rp = _characterRepository.MainCharacter.RenderProperties;
var action = rp.SitState == SitState.Chair ? SitAction.Stand : SitAction.Sit;
var packet = new PacketBuilder(PacketFamily.Chair, PacketAction.Request)
.AddChar((byte)action)
Expand All @@ -106,6 +110,10 @@ public void CastSpell(int spellId, ISpellTargetable target)
{
var data = _spellFileProvider.ESFFile.Single(x => x.ID == spellId);

var c = _characterRepository.MainCharacter;
var sp = Math.Max(0, c.Stats[CharacterStat.SP] - data.SP);
_characterRepository.MainCharacter = c.WithStats(c.Stats.WithNewStat(CharacterStat.SP, sp));

var action = data.Target == IO.SpellTarget.Self
? PacketAction.TargetSelf
: data.Target == IO.SpellTarget.Normal
Expand Down
1 change: 0 additions & 1 deletion EndlessClient/Controllers/ControlKeyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ private void AttemptAttack()

if (showAnimationAnyway)
{
//todo: lower SP for character when attacking
_characterActions.Attack();
_characterAnimationActions.StartAttacking();
}
Expand Down
2 changes: 1 addition & 1 deletion EndlessClient/HUD/Controls/HudControlIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public enum HudControlIdentifier
ClockLabel,

//not displayed
UsageTracker,
PeriodicStatUpdater,
UserInputHandler,
CharacterAnimator,
NPCAnimator,
Expand Down
6 changes: 3 additions & 3 deletions EndlessClient/HUD/Controls/HudControlsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public IReadOnlyDictionary<HudControlIdentifier, IGameComponent> CreateHud()
{HudControlIdentifier.ClockLabel, CreateClockLabel()},
{HudControlIdentifier.StatusLabel, CreateStatusLabel()},

{HudControlIdentifier.UsageTracker, CreateUsageTracker()},
{HudControlIdentifier.PeriodicStatUpdater, CreatePeriodicStatUpdater()},
{HudControlIdentifier.UserInputHandler, CreateUserInputHandler()},
{HudControlIdentifier.CharacterAnimator, characterAnimator},
{HudControlIdentifier.NPCAnimator, CreateNPCAnimator()},
Expand Down Expand Up @@ -400,9 +400,9 @@ private TimeLabel CreateClockLabel()
return new TimeLabel(_clientWindowSizeProvider) { DrawOrder = HUD_CONTROL_LAYER };
}

private UsageTrackerComponent CreateUsageTracker()
private PeriodicStatUpdaterComponent CreatePeriodicStatUpdater()
{
return new UsageTrackerComponent(_endlessGameProvider, _characterRepository);
return new PeriodicStatUpdaterComponent(_endlessGameProvider, _characterRepository);
}

private UnknownEntitiesRequester CreateUnknownEntitiesRequester()
Expand Down
71 changes: 71 additions & 0 deletions EndlessClient/HUD/Controls/PeriodicStatUpdaterComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Diagnostics;
using EndlessClient.GameExecution;
using EOLib.Domain.Character;
using Microsoft.Xna.Framework;
using Optional;

namespace EndlessClient.HUD.Controls
{
public class PeriodicStatUpdaterComponent : GameComponent
{
private readonly IEndlessGameProvider _gameProvider;
private readonly ICharacterRepository _characterRepository;

private DateTime _lastUsageUpdateTime;
private Option<Stopwatch> _lastSpUpdateTime;

public PeriodicStatUpdaterComponent(IEndlessGameProvider gameProvider,
ICharacterRepository characterRepository)
: base((Game)gameProvider.Game)
{
_gameProvider = gameProvider;
_characterRepository = characterRepository;
_lastUsageUpdateTime = DateTime.Now;
_lastSpUpdateTime = Option.None<Stopwatch>();
}

public override void Initialize()
{
if (!_gameProvider.Game.Components.Contains(this))
_gameProvider.Game.Components.Add(this);

base.Initialize();
}

public override void Update(GameTime gameTime)
{
var stats = _characterRepository.MainCharacter.Stats;

if ((DateTime.Now - _lastUsageUpdateTime).TotalMinutes >= 1)
{
stats = stats.WithNewStat(CharacterStat.Usage, stats.Stats[CharacterStat.Usage] + 1);
_characterRepository.MainCharacter = _characterRepository.MainCharacter.WithStats(stats);
_lastUsageUpdateTime = DateTime.Now;
}

if (stats[CharacterStat.SP] < stats[CharacterStat.MaxSP])
{
_lastSpUpdateTime.Match(
some: t =>
{
// this seems close to 2 but is probably based on level and/or one of the stats
if (t.ElapsedMilliseconds > 2000)
{
var sp = Math.Min(stats[CharacterStat.SP] + 2, stats[CharacterStat.MaxSP]);
stats = stats.WithNewStat(CharacterStat.SP, sp);
_characterRepository.MainCharacter = _characterRepository.MainCharacter.WithStats(stats);

if (stats[CharacterStat.SP] == stats[CharacterStat.MaxSP])
_lastSpUpdateTime = Option.None<Stopwatch>();
else
_lastSpUpdateTime = Option.Some(Stopwatch.StartNew());
}
},
none: () => _lastSpUpdateTime = Option.Some(Stopwatch.StartNew()));
}

base.Update(gameTime);
}
}
}
45 changes: 0 additions & 45 deletions EndlessClient/HUD/Controls/UsageTrackerComponent.cs

This file was deleted.

0 comments on commit d9f835b

Please sign in to comment.