using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CustomNPCFramework.Framework.Graphics { /// /// Used to handle loading different textures and handling opperations on those textures. /// public class AssetSheet { public Texture2D texture; public AssetInfo assetInfo; public string path; public int index; private int widthIndex; private int heightIndex; private int widthIndexMax; private int heightIndexMax; public Rectangle currentAsset; public AssetSheet(AssetInfo info,string path) { this.assetInfo = info; this.texture = Class1.ModHelper.Content.Load(Class1.getShortenedDirectory(Path.Combine(path,info.name+".png")).Remove(0,1)); this.widthIndexMax = this.texture.Width / (int)this.assetInfo.assetSize.X; this.heightIndexMax = this.texture.Width / (int)this.assetInfo.assetSize.Y; this.index = 0; if (this.assetInfo.randomizeUponLoad == false) { this.widthIndex = 0; this.heightIndex = 0; } else { getRandomAssetIndicies(); setIndex(); } this.currentAsset = new Rectangle(widthIndex * (int)this.assetInfo.assetSize.X, heightIndex * (int)this.assetInfo.assetSize.Y, (int)this.assetInfo.assetSize.X, (int)this.assetInfo.assetSize.Y); } /// /// Get the next graphic from the texture. /// public void getNext() { //If I can still iterate through my list but my width is maxed, increment height. if (this.widthIndex == this.widthIndexMax - 1 && this.heightIndex != this.heightIndexMax) { this.widthIndex -= 0; this.heightIndex++; } //If I reached the end of my image loop to 0; else if (this.heightIndex == this.heightIndexMax && this.widthIndex == this.widthIndexMax - 1) { this.heightIndex = 0; this.widthIndex = 0; } else { //If I can still iterate through my list do so. widthIndex++; } this.setIndex(); this.setAsset(); } /// /// Get the last graphic from my texture. /// public void getPrevious() { //If my width index is 0 and my height index isn't decrement my height index and set the width index to the far right. if (this.widthIndex == 0 && this.heightIndex != 0) { this.heightIndex--; this.widthIndex = this.widthIndexMax - 1; } //If both my height and width indicies are 0, loop to the bottom right of the texture. else if (this.widthIndex == 0 && this.heightIndex == 0) { this.widthIndex = this.widthIndexMax - 1; this.heightIndex = this.heightIndexMax - 1; } else { //Just decrement my width index by 1. this.widthIndex--; } this.setIndex(); this.setAsset(); } /// /// sets the current positioning for the rectangle index; /// private void setAsset() { this.currentAsset.X = widthIndex * (int)this.assetInfo.assetSize.X; this.currentAsset.Y = heightIndex * (int)this.assetInfo.assetSize.Y; } /// /// Used mainly for display purposes and length purposes. /// public void setIndex() { this.index = heightIndex * widthIndexMax + widthIndex; } /// /// Sets the asset index to a random value. /// public void getRandomAssetIndicies() { Random r = new Random(DateTime.Now.Millisecond); this.widthIndex = r.Next(0, this.widthIndexMax); this.widthIndex = r.Next(0, this.heightIndexMax); setIndex(); setAsset(); } /// /// Used just to get a copy of this asset sheet. /// public void clone() { var asset = new AssetSheet(this.assetInfo,(string)this.path.Clone()); } } }