Skip to content

Commit

Permalink
Load empty character info panels are part of content loading. Reduces…
Browse files Browse the repository at this point in the history
… delay when switching to LoggedIn game state
  • Loading branch information
ethanmoffat committed Mar 25, 2022
1 parent 4c822df commit 5ee2f46
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 32 deletions.
6 changes: 6 additions & 0 deletions EOLib/Domain/Character/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public class Character : ICharacter

public bool NoWall { get; private set; }

public Character()
{
RenderProperties = new CharacterRenderProperties();
Stats = new CharacterStats();
}

public ICharacter WithID(int id)
{
var character = MakeCopy(this);
Expand Down
7 changes: 6 additions & 1 deletion EndlessClient/ControlSets/ControlSetFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using EndlessClient.Input;
using EndlessClient.UIControls;
using EOLib.Config;
using EOLib.Domain.Login;
using EOLib.Graphics;

namespace EndlessClient.ControlSets
Expand All @@ -22,6 +23,7 @@ public class ControlSetFactory : IControlSetFactory
private readonly IKeyboardDispatcherProvider _keyboardDispatcherProvider;
private readonly IConfigurationProvider _configProvider;
private readonly ICharacterInfoPanelFactory _characterInfoPanelFactory;
private readonly ICharacterSelectorProvider _characterSelectorProvider;
private IMainButtonController _mainButtonController;
private IAccountController _accountController;
private ILoginController _loginController;
Expand All @@ -33,7 +35,8 @@ public ControlSetFactory(INativeGraphicsManager nativeGraphicsManager,
IContentProvider contentProvider,
IKeyboardDispatcherProvider keyboardDispatcherProvider,
IConfigurationProvider configProvider,
ICharacterInfoPanelFactory characterInfoPanelFactory)
ICharacterInfoPanelFactory characterInfoPanelFactory,
ICharacterSelectorProvider characterSelectorProvider)
{
_nativeGraphicsManager = nativeGraphicsManager;
_messageBoxFactory = messageBoxFactory;
Expand All @@ -42,6 +45,7 @@ public ControlSetFactory(INativeGraphicsManager nativeGraphicsManager,
_keyboardDispatcherProvider = keyboardDispatcherProvider;
_configProvider = configProvider;
_characterInfoPanelFactory = characterInfoPanelFactory;
_characterSelectorProvider = characterSelectorProvider;
}

public IControlSet CreateControlsForState(GameStates newState, IControlSet currentControlSet)
Expand Down Expand Up @@ -89,6 +93,7 @@ private IControlSet GetSetBasedOnState(GameStates newState)
_keyboardDispatcherProvider.Dispatcher,
_mainButtonController,
_characterInfoPanelFactory,
_characterSelectorProvider,
_characterManagementController,
_accountController);
case GameStates.PlayingTheGame:
Expand Down
6 changes: 5 additions & 1 deletion EndlessClient/ControlSets/LoggedInControlSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using EndlessClient.Controllers;
using EndlessClient.GameExecution;
using EndlessClient.UIControls;
using EOLib.Domain.Login;
using Microsoft.Xna.Framework;
using XNAControls;

Expand All @@ -12,6 +13,7 @@ namespace EndlessClient.ControlSets
public class LoggedInControlSet : IntermediateControlSet
{
private readonly ICharacterInfoPanelFactory _characterInfoPanelFactory;
private readonly ICharacterSelectorProvider _characterSelectorProvider;
private readonly ICharacterManagementController _characterManagementController;
private readonly IAccountController _accountController;
private readonly List<CharacterInfoPanel> _characterInfoPanels;
Expand All @@ -25,11 +27,13 @@ public class LoggedInControlSet : IntermediateControlSet
public LoggedInControlSet(KeyboardDispatcher dispatcher,
IMainButtonController mainButtonController,
ICharacterInfoPanelFactory characterInfoPanelFactory,
ICharacterSelectorProvider characterSelectorProvider,
ICharacterManagementController characterManagementController,
IAccountController accountController)
: base(dispatcher, mainButtonController)
{
_characterInfoPanelFactory = characterInfoPanelFactory;
_characterSelectorProvider = characterSelectorProvider;
_characterManagementController = characterManagementController;
_accountController = accountController;
_characterInfoPanels = new List<CharacterInfoPanel>();
Expand All @@ -40,7 +44,7 @@ protected override void InitializeControlsHelper(IControlSet currentControlSet)
base.InitializeControlsHelper(currentControlSet);

_changePasswordButton = GetControl(currentControlSet, GameControlIdentifier.ChangePasswordButton, GetPasswordButton);
_characterInfoPanels.AddRange(_characterInfoPanelFactory.CreatePanels());
_characterInfoPanels.AddRange(_characterInfoPanelFactory.CreatePanels(_characterSelectorProvider.Characters));

_allComponents.Add(_changePasswordButton);
_allComponents.AddRange(_characterInfoPanels);
Expand Down
16 changes: 15 additions & 1 deletion EndlessClient/GameExecution/EndlessGame.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using AutomaticTypeMapper;
using EndlessClient.Content;
using EndlessClient.ControlSets;
using EndlessClient.Rendering;
using EndlessClient.Rendering.Chat;
using EndlessClient.Test;
using EndlessClient.UIControls;
using EOLib.Domain.Character;
using EOLib.Graphics;
using EOLib.IO;
using EOLib.IO.Actions;
Expand All @@ -29,6 +32,7 @@ public class EndlessGame : Game, IEndlessGame
private readonly ILoggerProvider _loggerProvider;
private readonly IChatBubbleTextureProvider _chatBubbleTextureProvider;
private readonly IShaderRepository _shaderRepository;
private readonly ICharacterInfoPanelFactory _characterInfoPanelFactory;
private GraphicsDeviceManager _graphicsDeviceManager;

private KeyboardState _previousKeyState;
Expand All @@ -42,7 +46,8 @@ public EndlessGame(IClientWindowSizeProvider windowSizeProvider,
IPubFileLoadActions pubFileLoadActions,
ILoggerProvider loggerProvider,
IChatBubbleTextureProvider chatBubbleTextureProvider,
IShaderRepository shaderRepository)
IShaderRepository shaderRepository,
ICharacterInfoPanelFactory characterInfoPanelFactory)
{
_windowSizeProvider = windowSizeProvider;
_contentProvider = contentProvider;
Expand All @@ -54,6 +59,7 @@ public EndlessGame(IClientWindowSizeProvider windowSizeProvider,
_loggerProvider = loggerProvider;
_chatBubbleTextureProvider = chatBubbleTextureProvider;
_shaderRepository = shaderRepository;
_characterInfoPanelFactory = characterInfoPanelFactory;

_graphicsDeviceManager = new GraphicsDeviceManager(this);

Expand Down Expand Up @@ -97,6 +103,14 @@ protected override void LoadContent()
_shaderRepository.Shaders[ShaderRepository.HairClip] = new Effect(GraphicsDevice, shaderBytes);
}

// for some reason initializing these and then killing them speeds up transition from Login -> LoggedIn state
// TODO: figure out why this happens????
foreach (var panel in _characterInfoPanelFactory.CreatePanels(Enumerable.Repeat(new Character(), 3)))
{
panel.Initialize();
panel.Dispose();
}

SetUpInitialControlSet();

base.LoadContent();
Expand Down
53 changes: 31 additions & 22 deletions EndlessClient/Rendering/Character/CharacterRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,26 +117,31 @@ public override void Initialize()
_charRenderTarget = _renderTargetFactory.CreateRenderTarget();
_sb = new SpriteBatch(Game.GraphicsDevice);

_nameLabel = new BlinkingLabel(Constants.FontSize08pt5)
if (_gameStateProvider.CurrentState == GameStates.PlayingTheGame)
{
Visible = _gameStateProvider.CurrentState == GameStates.PlayingTheGame,
TextWidth = 89,
TextAlign = LabelAlignment.MiddleCenter,
ForeColor = Color.White,
AutoSize = true,
Text = _character?.Name ?? string.Empty,
DrawOrder = 30,
KeepInClientWindowBounds = false,
};
_nameLabel.Initialize();

if (!_nameLabel.Game.Components.Contains(_nameLabel))
_nameLabel.Game.Components.Add(_nameLabel);
_nameLabel = new BlinkingLabel(Constants.FontSize08pt5)
{
Visible = true,
TextWidth = 89,
TextAlign = LabelAlignment.MiddleCenter,
ForeColor = Color.White,
AutoSize = true,
Text = _character?.Name ?? string.Empty,
DrawOrder = 30,
KeepInClientWindowBounds = false,
};
_nameLabel.Initialize();

if (!_nameLabel.Game.Components.Contains(_nameLabel))
_nameLabel.Game.Components.Add(_nameLabel);

_nameLabel.DrawPosition = GetNameLabelPosition();

_healthBarRenderer = _healthBarRendererFactory.CreateHealthBarRenderer(this);
}

_nameLabel.DrawPosition = GetNameLabelPosition();
_previousMouseState = _currentMouseState = Mouse.GetState();

_healthBarRenderer = _healthBarRendererFactory.CreateHealthBarRenderer(this);

base.Initialize();
}
Expand Down Expand Up @@ -184,9 +189,9 @@ public override void Update(GameTime gameTime)
{
_mapInteractionController.RightClick(new MapCellState { Character = Option.Some(Character) });
}
}

_healthBarRenderer.Update(gameTime);
_healthBarRenderer.Update(gameTime);
}

_previousMouseState = _currentMouseState;

Expand Down Expand Up @@ -239,7 +244,8 @@ public void DrawToSpriteBatch(SpriteBatch spriteBatch)
spriteBatch.Draw(_charRenderTarget, new Vector2(0, GetSteppingStoneOffset(Character.RenderProperties)), GetAlphaColor());
_effectRenderer.DrawInFrontOfTarget(spriteBatch);

_healthBarRenderer.DrawToSpriteBatch(spriteBatch);
if (_gameStateProvider.CurrentState == GameStates.PlayingTheGame)
_healthBarRenderer.DrawToSpriteBatch(spriteBatch);
}

#endregion
Expand Down Expand Up @@ -328,6 +334,9 @@ private int GetMainCharacterOffsetY()

private void UpdateNameLabel(GameTime gameTime)
{
if (_gameStateProvider.CurrentState != GameStates.PlayingTheGame)
return;

if (_healthBarRenderer.Visible)
{
_nameLabel.Visible = false;
Expand Down Expand Up @@ -473,10 +482,10 @@ protected override void Dispose(bool disposing)
if (disposing)
{
_outline?.Dispose();
_nameLabel.Dispose();
_nameLabel?.Dispose();

_sb.Dispose();
_charRenderTarget.Dispose();
_sb?.Dispose();
_charRenderTarget?.Dispose();
}

base.Dispose(disposing);
Expand Down
2 changes: 1 addition & 1 deletion EndlessClient/UIControls/CharacterInfoPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private Vector2 GetAdminGraphicLocation()

private static string CapitalizeName(string name)
{
return (char)(name[0] - 32) + name.Substring(1);
return string.IsNullOrEmpty(name) ? string.Empty : (char)(name[0] - 32) + name.Substring(1);
}

private ISpriteSheet CreateAdminGraphic(AdminLevel adminLevel)
Expand Down
10 changes: 5 additions & 5 deletions EndlessClient/UIControls/CharacterInfoPanelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
using EndlessClient.Dialogs.Services;
using EndlessClient.Rendering;
using EndlessClient.Rendering.Factories;
using EOLib.Domain.Character;
using EOLib.Domain.Login;
using EOLib.Graphics;

namespace EndlessClient.UIControls
{
[MappedType(BaseType = typeof(ICharacterInfoPanelFactory), IsSingleton = true)]
[AutoMappedType(IsSingleton = true)]
public class CharacterInfoPanelFactory : ICharacterInfoPanelFactory
{
private readonly ICharacterSelectorProvider _characterProvider;
Expand Down Expand Up @@ -45,16 +46,15 @@ public void InjectCharacterManagementController(ICharacterManagementController c
_characterManagementController = characterManagementController;
}

public IEnumerable<CharacterInfoPanel> CreatePanels()
public IEnumerable<CharacterInfoPanel> CreatePanels(IEnumerable<ICharacter> characters)
{
if(_loginController == null || _characterManagementController == null)
throw new InvalidOperationException("Missing controllers - the Unity container was initialized incorrectly");

int i = 0;
for (; i < _characterProvider.Characters.Count; ++i)
foreach (var character in characters)
{
var character = _characterProvider.Characters[i];
yield return new CharacterInfoPanel(i,
yield return new CharacterInfoPanel(i++,
character,
_nativeGraphicsManager,
_eoDialogButtonService,
Expand Down
3 changes: 2 additions & 1 deletion EndlessClient/UIControls/ICharacterInfoPanelFactory.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Collections.Generic;
using EndlessClient.Controllers;
using EOLib.Domain.Character;

namespace EndlessClient.UIControls
{
public interface ICharacterInfoPanelFactory
{
IEnumerable<CharacterInfoPanel> CreatePanels();
IEnumerable<CharacterInfoPanel> CreatePanels(IEnumerable<ICharacter> characters);
void InjectLoginController(ILoginController loginController);
void InjectCharacterManagementController(ICharacterManagementController characterManagementController);
}
Expand Down

0 comments on commit 5ee2f46

Please sign in to comment.