From 567c422f8cd42eba2437f9a8c2522716a1649be7 Mon Sep 17 00:00:00 2001 From: hazel Date: Mon, 26 Jan 2026 22:04:39 +0100 Subject: celesteia archive, last updated april 9th 2024 Signed-off-by: hazel --- source/resources/management/BlockManager.cs | 77 +++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 source/resources/management/BlockManager.cs (limited to 'source/resources/management/BlockManager.cs') diff --git a/source/resources/management/BlockManager.cs b/source/resources/management/BlockManager.cs new file mode 100644 index 0000000..2271c0c --- /dev/null +++ b/source/resources/management/BlockManager.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using Celesteia.Resources.Sprites; +using Celesteia.Resources.Types; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended.TextureAtlases; + +namespace Celesteia.Resources.Management { + public abstract class BlockSpriteProperties { + public const int SIZE = 8; + } + + public class BlockManager : IResourceManager { + private List Types; + private BlockType[] BakedTypes; + private Dictionary keys = new Dictionary(); + + public BlockFrames BreakAnimation; + public BlockFrames Selection; + + private void LoadBreakingAnimation(ContentManager Content) { + Debug.WriteLine($"Loading block break animation..."); + BreakAnimation = new BlockFrames(TextureAtlas.Create("blockbreak", Content.Load("sprites/blockbreak"), + BlockSpriteProperties.SIZE, + BlockSpriteProperties.SIZE + ), 0, 3); + } + + private void LoadSelector(ContentManager Content) { + Debug.WriteLine($"Loading block selector..."); + Selection = new BlockFrames(TextureAtlas.Create("selection", Content.Load("sprites/blockselection"), + BlockSpriteProperties.SIZE, + BlockSpriteProperties.SIZE + ), 0, 1); + } + + private List _collections = new List(); + public void AddCollection(IResourceCollection collection) => _collections.Add(collection); + + public void LoadContent(ContentManager Content) { + LoadBreakingAnimation(Content); + LoadSelector(Content); + + Debug.WriteLine($"Loading block types..."); + + Types = new List(); + + foreach (IResourceCollection collection in _collections) + LoadCollection(collection); + + BakedTypes = Types.ToArray(); + } + + private void LoadCollection(IResourceCollection collection) { + foreach (NamespacedKey key in collection.GetBlocks().Keys) { + AddType(key, collection.GetBlocks()[key]); + } + } + + private byte next = 0; + private void AddType(NamespacedKey key, BlockType type) { + type.SetID(next++); + keys.Add(key.Qualify(), type.GetID()); + + Types.Add(type); + } + + public BlockType GetBlock(byte id) => BakedTypes[id]; + + public IResourceType GetResource(NamespacedKey key) { + if (!keys.ContainsKey(key.Qualify())) throw new NullReferenceException(); + return BakedTypes[keys[key.Qualify()]]; + } + } +} \ No newline at end of file -- cgit v1.2.3