using CustomNPCFramework.Framework.NPCS;
using Microsoft.Xna.Framework.Graphics;
using StardewValley;
namespace CustomNPCFramework.Framework.ModularNpcs
{
/// Used as a wrapper for the npcs to hold sprite information.
public class Sprite
{
/// The actual sprite to draw for the npc.
public AnimatedSprite sprite;
/// The path to the texture to use for the animated sprite.
public string relativePath;
/// Construct an instance.
/// The relative path to the file.
public Sprite(string relativePath)
{
this.relativePath = relativePath;
try
{
this.sprite = new AnimatedSprite();
Texture2D text = Class1.ModHelper.Content.Load(this.relativePath);
var reflect = Class1.ModHelper.Reflection.GetField(this.sprite, "Texture", true);
reflect.SetValue(text);
}
catch
{
this.sprite = new AnimatedSprite();
Texture2D text = Class1.ModHelper.Content.Load(this.relativePath);
var reflect = Class1.ModHelper.Reflection.GetField(this.sprite, "Texture", true);
reflect.SetValue(text);
}
this.sprite.SpriteWidth = this.sprite.Texture.Width;
this.sprite.SpriteHeight = this.sprite.Texture.Height;
}
/// Construct an instance.
/// Used to hold the path to the asset.
/// Used to assign the texture to the sprite from a pre-loaded asset.
public Sprite(string path, string texture)
{
this.relativePath = path;
this.sprite = new AnimatedSprite(texture);
this.sprite.SpriteWidth = this.sprite.Texture.Width;
this.sprite.SpriteHeight = this.sprite.Texture.Height;
}
/// Sets the npc's portrait to be this portrait texture.
public void setCharacterSpriteFromThis(ExtendedNpc npc)
{
npc.Sprite = this.sprite;
}
/// Reloads the texture for the NPC portrait.
public void reload()
{
var text = Class1.ModHelper.Reflection.GetField(this.sprite.Texture, "Texture", true);
Texture2D loaded = Class1.ModHelper.Content.Load(this.relativePath);
text.SetValue(loaded);
}
/// Set's the npc's sprites to face left IF and only if there is a non-null modular Renderer attached to the npc.
public void setLeft(ExtendedNpc npc)
{
npc.characterRenderer?.setLeft();
}
/// Set's the npc's sprites to face left IF and only if there is a non-null modular Renderer attached to the npc.
public void setRight(ExtendedNpc npc)
{
npc.characterRenderer?.setRight();
}
/// Set's the npc's sprites to face left IF and only if there is a non-null modular Renderer attached to the npc.
public void setDown(ExtendedNpc npc)
{
npc.characterRenderer?.setDown();
}
/// Set's the npc's sprites to face left IF and only if there is a non-null modular Renderer attached to the npc.
public void setUp(ExtendedNpc npc)
{
npc.characterRenderer?.setUp();
}
}
}