diff --git a/Intersect (Core)/ColorF.cs b/Intersect (Core)/ColorF.cs index f910c8af4f..696dcb9fcc 100644 --- a/Intersect (Core)/ColorF.cs +++ b/Intersect (Core)/ColorF.cs @@ -1,8 +1,9 @@ namespace Intersect; -public partial class ColorF +public partial class ColorF : IEquatable { + private const float Tolerance = 1.0f / 256.0f; public ColorF() { @@ -32,25 +33,25 @@ public ColorF(float r, float g, float b) public float B { get; set; } - public static ColorF White => new ColorF(255, 255, 255, 255); + public static ColorF White => new(255, 255, 255, 255); - public static ColorF Black => new ColorF(255, 0, 0, 0); + public static ColorF Black => new(255, 0, 0, 0); - public static ColorF Transparent => new ColorF(0, 0, 0, 0); + public static ColorF Transparent => new(0, 0, 0, 0); - public static ColorF Red => new ColorF(255, 255, 0, 0); + public static ColorF Red => new(255, 255, 0, 0); - public static ColorF Green => new ColorF(255, 0, 255, 0); + public static ColorF Green => new(255, 0, 255, 0); - public static ColorF Blue => new ColorF(255, 0, 0, 255); + public static ColorF Blue => new(255, 0, 0, 255); - public static ColorF Yellow => new ColorF(255, 255, 255, 0); + public static ColorF Yellow => new(255, 255, 255, 0); - public static ColorF LightCoral => new ColorF(255, 240, 128, 128); + public static ColorF LightCoral => new(255, 240, 128, 128); - public static ColorF ForestGreen => new ColorF(255, 34, 139, 34); + public static ColorF ForestGreen => new(255, 34, 139, 34); - public static ColorF Magenta => new ColorF(255, 255, 0, 255); + public static ColorF Magenta => new(255, 255, 0, 255); public byte GetHue() { @@ -62,4 +63,8 @@ public static ColorF FromArgb(float a, float r, float g, float b) return new ColorF(a, r, g, b); } + public override bool Equals(object other) => other is ColorF color && Equals(color); + + public bool Equals(ColorF other) => Math.Abs(R - other.R) < Tolerance && Math.Abs(G - other.G) < Tolerance && + Math.Abs(B - other.B) < Tolerance && Math.Abs(A - other.A) < Tolerance; } diff --git a/Intersect.Client.Core/Core/Graphics.cs b/Intersect.Client.Core/Core/Graphics.cs index 5293757089..5c77291c4b 100644 --- a/Intersect.Client.Core/Core/Graphics.cs +++ b/Intersect.Client.Core/Core/Graphics.cs @@ -36,7 +36,7 @@ public static FloatRect CurrentView Renderer?.SetView(_currentView); } } - + public static FloatRect WorldViewport => new(CurrentView.Position, CurrentView.Size / (Globals.Database?.WorldZoom ?? 1)); public static GameShader? DefaultShader; @@ -123,13 +123,15 @@ public static GameFont FindFont(string font) { var size = 8; - if (font.IndexOf(',') > -1) + if (font.IndexOf(',') < 0) { - var parts = font.Split(','); - font = parts[0]; - _ = int.TryParse(parts[1], out size); + return sContentManager.GetFont(font, size); } + var parts = font.Split(','); + font = parts[0]; + _ = int.TryParse(parts[1], out size); + return sContentManager.GetFont(font, size); } @@ -147,14 +149,16 @@ public static void InitInGame() public static void DrawIntro() { - var imageTex = sContentManager.GetTexture( - Framework.Content.TextureType.Image, ClientConfiguration.Instance.IntroImages[Globals.IntroIndex] - ); - - if (imageTex != null) + if (!sContentManager.TryGetTexture( + TextureType.Image, + ClientConfiguration.Instance.IntroImages[Globals.IntroIndex], + out var texture + )) { - DrawFullScreenTextureFitMinimum(imageTex); + return; } + + DrawFullScreenTextureFitMinimum(texture); } private static void DrawMenu() @@ -609,8 +613,7 @@ public static void Render(TimeSpan deltaTime, TimeSpan _) private static void DrawMap(Guid mapId, int layer = 0) { - var map = MapInstance.Get(mapId); - if (map == null) + if (!MapInstance.TryGet(mapId, out var map)) { return; } @@ -631,18 +634,24 @@ private static void DrawMap(Guid mapId, int layer = 0) private static void DrawMapPanorama(Guid mapId) { - var map = MapInstance.Get(mapId); - if (map != null) + if (!MapInstance.TryGet(mapId, out var map)) { - if (!new FloatRect( - map.GetX(), map.GetY(), Options.TileWidth * Options.MapWidth, Options.TileHeight * Options.MapHeight - ).IntersectsWith(WorldViewport)) - { - return; - } + return; + } + + var mapBounds = new FloatRect( + map.GetX(), + map.GetY(), + Options.TileWidth * Options.MapWidth, + Options.TileHeight * Options.MapHeight + ); - map.DrawPanorama(); + if (!mapBounds.IntersectsWith(WorldViewport)) + { + return; } + + map.DrawPanorama(); } public static void DrawOverlay() @@ -652,8 +661,7 @@ public static void DrawOverlay() return; } - var map = MapInstance.Get(Globals.Me?.MapId ?? Guid.Empty); - if (map != null) + if (MapInstance.TryGet(Globals.Me?.MapId ?? default, out var map)) { float ecTime = Timing.Global.MillisecondsUtc - sOverlayUpdate; @@ -1027,14 +1035,28 @@ private static void ClearDarknessTexture() private static void GenerateLightMap() { - // If we're not allowed to draw lighting, exit out. - if (!Globals.Database.EnableLighting || Renderer == default || Globals.Me == default) + if (Renderer == default) + { + return; + } + + if (sDarknessTexture == default) + { + return; + } + + var mapId = Globals.Me?.MapId ?? default; + if (mapId == default) + { + return; + } + + if (!Globals.Database.EnableLighting) { return; } - var map = MapInstance.Get(Globals.Me.MapId); - if (map == null || sDarknessTexture == null) + if (!MapInstance.TryGet(mapId, out var map)) { return; } @@ -1159,8 +1181,7 @@ public static void UpdatePlayerLight() } //Draw Light Around Player - var map = MapInstance.Get(Globals.Me.MapId); - if (map != null) + if (MapInstance.TryGet(Globals.Me.MapId, out var map)) { float ecTime = Timing.Global.MillisecondsUtc - sLightUpdate; var valChange = 255 * ecTime / 2000f; diff --git a/Intersect.Client.Core/MonoGame/File Management/MonoContentManager.cs b/Intersect.Client.Core/MonoGame/File Management/MonoContentManager.cs index 790fb4bd2f..207108777d 100644 --- a/Intersect.Client.Core/MonoGame/File Management/MonoContentManager.cs +++ b/Intersect.Client.Core/MonoGame/File Management/MonoContentManager.cs @@ -18,8 +18,6 @@ public partial class MonoContentManager : GameContentManager public MonoContentManager() { - Init(this); - var rootPath = Path.GetFullPath(ClientConfiguration.ResourcesDirectory); if (!Directory.Exists(rootPath)) @@ -288,7 +286,7 @@ public override void LoadSounds() } } } - + } public override void LoadMusic() diff --git a/Intersect.Client.Framework/File Management/GameContentManager.cs b/Intersect.Client.Framework/File Management/GameContentManager.cs index 633fe83412..e786d41692 100644 --- a/Intersect.Client.Framework/File Management/GameContentManager.cs +++ b/Intersect.Client.Framework/File Management/GameContentManager.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Intersect.Async; using Intersect.Client.Framework.Audio; using Intersect.Client.Framework.Content; @@ -26,37 +27,43 @@ public enum UI Debug, } - public static GameContentManager Current; + public static GameContentManager Current { get; private set; } - protected Dictionary mAnimationDict = new Dictionary(); + protected readonly Dictionary mAnimationDict = []; - protected Dictionary mEntityDict = new Dictionary(); + protected readonly Dictionary mEntityDict = []; - protected Dictionary mFaceDict = new Dictionary(); + protected readonly Dictionary mFaceDict = []; - protected Dictionary mFogDict = new Dictionary(); + protected readonly Dictionary mFogDict = []; - protected List mFontDict = new List(); + protected readonly List mFontDict = []; - protected Dictionary mGuiDict = new Dictionary(); + protected readonly Dictionary mGuiDict = []; - protected Dictionary mImageDict = new Dictionary(); + protected readonly Dictionary mImageDict = []; - protected Dictionary mItemDict = new Dictionary(); + protected readonly Dictionary mItemDict = []; - protected Dictionary mMiscDict = new Dictionary(); + protected readonly Dictionary mMiscDict = []; - protected Dictionary mMusicDict = new Dictionary(); + protected readonly Dictionary mMusicDict = []; - protected Dictionary mPaperdollDict = new Dictionary(); + protected readonly Dictionary mPaperdollDict = []; - protected Dictionary mResourceDict = new Dictionary(); + protected readonly Dictionary mResourceDict = []; - protected Dictionary mShaderDict = new Dictionary(); + protected readonly Dictionary mShaderDict = []; - protected Dictionary mSoundDict = new Dictionary(); + protected readonly Dictionary mSoundDict = []; - protected Dictionary, string> mUiDict = new Dictionary, string>(); + protected readonly Dictionary, string> mUiDict = []; + + protected readonly Dictionary mSpellDict = []; + + protected readonly Dictionary mTexturePackDict = []; + + protected readonly Dictionary mTilesetDict = []; /// /// Contains all indexed files and their caches from sound pack files. @@ -68,20 +75,13 @@ public enum UI /// public AssetPacker MusicPacks { get; set; } - protected Dictionary mSpellDict = new Dictionary(); - - protected Dictionary mTexturePackDict = new Dictionary(); - - //Game Content - protected Dictionary mTilesetDict = new Dictionary(); - public bool TilesetsLoaded = false; public ContentWatcher ContentWatcher { get; protected set; } - public void Init(GameContentManager manager) + protected GameContentManager() { - Current = manager; + Current = this; } //Content Loading @@ -199,8 +199,14 @@ public string[] GetTextureNames(TextureType type) return null; } + public bool TryGetTexture(TextureType textureType, string textureName, [NotNullWhen(true)] out GameTexture? texture) + { + texture = GetTexture(textureType, textureName); + return texture != default; + } + //Content Getters - public virtual GameTexture GetTexture(TextureType type, string name) + public virtual GameTexture? GetTexture(TextureType type, string name) { if (string.IsNullOrEmpty(name)) { @@ -282,19 +288,20 @@ public virtual GameTexture GetTexture(TextureType type, string name) return textureDict.TryGetValue(name.ToLower(), out var asset) ? asset as GameTexture : default; } - public virtual GameShader GetShader(string name) + public bool TryGetShader(string shaderName, [NotNullWhen(true)] out GameShader? shader) { - if (string.IsNullOrEmpty(name)) - { - return null; - } + shader = GetShader(shaderName); + return shader != default; + } - if (mShaderDict == null) + public virtual GameShader? GetShader(string name) + { + if (string.IsNullOrEmpty(name)) { return null; } - return mShaderDict.TryGetValue(name.ToLower(), out var effect) ? effect : null; + return mShaderDict?.GetValueOrDefault(name.ToLower()); } public virtual GameFont GetFont(string name, int size)