2018-12-30 18:00:05 +08:00
|
|
|
using CustomNPCFramework.Framework.Enums;
|
2018-03-04 23:53:55 +08:00
|
|
|
using CustomNPCFramework.Framework.NPCS;
|
2018-02-24 15:16:10 +08:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using StardewValley;
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
namespace CustomNPCFramework.Framework.ModularNpcs
|
2018-02-24 15:16:10 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Used to hold all of the sprites for a single asset such as hair or bodies.</summary>
|
2018-02-24 15:16:10 +08:00
|
|
|
public class AnimatedSpriteCollection
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>The left sprite for this sprite asset part.</summary>
|
2018-02-24 17:04:35 +08:00
|
|
|
AnimatedSpriteExtended leftSprite;
|
2018-12-30 18:00:05 +08:00
|
|
|
|
|
|
|
/// <summary>The right sprite for this sprite asset part.</summary>
|
2018-02-24 17:04:35 +08:00
|
|
|
AnimatedSpriteExtended rightSprite;
|
2018-12-30 18:00:05 +08:00
|
|
|
|
|
|
|
/// <summary>The up sprite for this sprite asset part.</summary>
|
2018-02-24 17:04:35 +08:00
|
|
|
AnimatedSpriteExtended upSprite;
|
2018-12-30 18:00:05 +08:00
|
|
|
|
|
|
|
/// <summary>The down sprite for this sprite asset part.</summary>
|
2018-02-24 17:04:35 +08:00
|
|
|
AnimatedSpriteExtended downSprite;
|
2018-02-24 15:16:10 +08:00
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>The current sprite for this sprite collection. This is one of the four directions for this collection.</summary>
|
2018-02-24 17:04:35 +08:00
|
|
|
public AnimatedSpriteExtended currentSprite;
|
2018-02-24 15:16:10 +08:00
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Construct an instance.</summary>
|
2018-02-24 15:16:10 +08:00
|
|
|
/// <param name="LeftSprite">Left animated sprite for this piece.</param>
|
|
|
|
/// <param name="RightSprite">Right animated sprite for this piece.</param>
|
|
|
|
/// <param name="UpSprite">Up animated sprite for this piece.</param>
|
|
|
|
/// <param name="DownSprite">Down animated sprite for this piece.</param>
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <param name="startingSpriteDirection">The sprite's initial facing direction.</param>
|
|
|
|
public AnimatedSpriteCollection(AnimatedSpriteExtended LeftSprite, AnimatedSpriteExtended RightSprite, AnimatedSpriteExtended UpSprite, AnimatedSpriteExtended DownSprite, Direction startingSpriteDirection)
|
2018-02-24 15:16:10 +08:00
|
|
|
{
|
|
|
|
this.leftSprite = LeftSprite;
|
|
|
|
this.rightSprite = RightSprite;
|
|
|
|
this.upSprite = UpSprite;
|
|
|
|
this.downSprite = DownSprite;
|
2018-12-30 18:00:05 +08:00
|
|
|
switch (startingSpriteDirection)
|
2018-02-24 15:16:10 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
case Direction.down:
|
|
|
|
this.setDown();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Direction.left:
|
|
|
|
this.setLeft();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Direction.right:
|
|
|
|
this.setRight();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Direction.up:
|
|
|
|
this.setUp();
|
|
|
|
break;
|
2018-02-24 15:16:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Reloads all of the directional textures for this texture collection.</summary>
|
2018-02-24 17:04:35 +08:00
|
|
|
public virtual void reload()
|
|
|
|
{
|
|
|
|
this.leftSprite.reload();
|
|
|
|
this.rightSprite.reload();
|
|
|
|
this.upSprite.reload();
|
|
|
|
this.downSprite.reload();
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Sets the current sprite direction to face left.</summary>
|
2018-02-24 15:16:10 +08:00
|
|
|
public void setLeft()
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
this.currentSprite = this.leftSprite;
|
2018-02-24 15:16:10 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Sets the current sprite direction to face right.</summary>
|
2018-02-24 15:16:10 +08:00
|
|
|
public void setRight()
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
this.currentSprite = this.rightSprite;
|
2018-02-24 15:16:10 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Sets the current sprite direction to face down.</summary>
|
2018-02-24 15:16:10 +08:00
|
|
|
public void setDown()
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
this.currentSprite = this.downSprite;
|
2018-02-24 15:16:10 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Sets the current sprite direction to face up.</summary>
|
2018-02-24 15:16:10 +08:00
|
|
|
public void setUp()
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
this.currentSprite = this.upSprite;
|
2018-02-24 15:16:10 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Used to draw the sprite to the screen.</summary>
|
2018-02-24 15:16:10 +08:00
|
|
|
public void draw(SpriteBatch b, Vector2 screenPosition, float layerDepth)
|
|
|
|
{
|
2018-02-24 17:04:35 +08:00
|
|
|
b.Draw(this.currentSprite.sprite.Texture, screenPosition, new Rectangle?(this.currentSprite.sprite.sourceRect), Color.White, 0.0f, Vector2.Zero, (float)Game1.pixelZoom, this.currentSprite.sprite.currentAnimation == null || !this.currentSprite.sprite.currentAnimation[this.currentSprite.sprite.currentAnimationIndex].flip ? SpriteEffects.None : SpriteEffects.FlipHorizontally, layerDepth);
|
2018-02-24 15:16:10 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Used to draw the sprite to the screen.</summary>
|
2018-02-24 15:16:10 +08:00
|
|
|
public void draw(SpriteBatch b, Vector2 screenPosition, float layerDepth, int xOffset, int yOffset, Color c, bool flip = false, float scale = 1f, float rotation = 0.0f, bool characterSourceRectOffset = false)
|
|
|
|
{
|
2018-05-01 09:21:31 +08:00
|
|
|
b.Draw(this.currentSprite.sprite.Texture, screenPosition, new Rectangle?(new Rectangle(this.currentSprite.sprite.sourceRect.X + xOffset, this.currentSprite.sprite.sourceRect.Y + yOffset, this.currentSprite.sprite.sourceRect.Width, this.currentSprite.sprite.sourceRect.Height)), c, rotation, characterSourceRectOffset ? new Vector2((float)(this.currentSprite.sprite.SpriteWidth / 2), (float)((double)this.currentSprite.sprite.SpriteHeight * 3.0 / 4.0)) : Vector2.Zero, scale, flip || this.currentSprite.sprite.currentAnimation != null && this.currentSprite.sprite.currentAnimation[this.currentSprite.sprite.currentAnimationIndex].flip ? SpriteEffects.FlipHorizontally : SpriteEffects.None, layerDepth);
|
2018-02-24 15:16:10 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>A very verbose asset drawer.</summary>
|
|
|
|
public void draw(SpriteBatch b, ExtendedNpc npc, Vector2 position, Rectangle sourceRectangle, Color color, float alpha, Vector2 origin, float scale, SpriteEffects effects, float layerDepth)
|
2018-02-24 15:16:10 +08:00
|
|
|
{
|
2018-03-17 19:18:35 +08:00
|
|
|
//DEFINITELY FIX THIS PART. Something is wrong with how these two functions handle the drawing of my npc to the scene.
|
2018-03-18 12:49:22 +08:00
|
|
|
//this.draw(b, position, layerDepth);
|
2018-12-30 18:00:05 +08:00
|
|
|
b.Draw(this.currentSprite.sprite.Texture, position, this.currentSprite.sprite.sourceRect, color, 0.0f, origin, scale, effects, layerDepth);
|
2018-02-24 15:16:10 +08:00
|
|
|
//b.Draw(this.Sprite.Texture, npc.getLocalPosition(Game1.viewport) + new Vector2((float)(this.sprite.spriteWidth * Game1.pixelZoom / 2), (float)(this.GetBoundingBox().Height / 2)) + (this.shakeTimer > 0 ? new Vector2((float)Game1.random.Next(-1, 2), (float)Game1.random.Next(-1, 2)) : Vector2.Zero), new Microsoft.Xna.Framework.Rectangle?(this.Sprite.SourceRect), Color.White * alpha, this.rotation, new Vector2((float)(this.sprite.spriteWidth / 2), (float)((double)this.sprite.spriteHeight * 3.0 / 4.0)), Math.Max(0.2f, this.scale) * (float)Game1.pixelZoom, this.flip || this.sprite.currentAnimation != null && this.sprite.currentAnimation[this.sprite.currentAnimationIndex].flip ? SpriteEffects.FlipHorizontally : SpriteEffects.None, Math.Max(0.0f, this.drawOnTop ? 0.991f : (float)this.getStandingY() / 10000f));
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Animate the current sprite. Theoreticlly works from index offset to how many frames</summary>
|
2018-03-20 14:01:43 +08:00
|
|
|
/// <param name="intervalDelay">The delay in milliseconds between frames.</param>
|
2018-12-30 18:00:05 +08:00
|
|
|
public void Animate(float intervalDelay, bool loop = true)
|
2018-02-24 15:16:10 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
this.Animate(Game1.currentGameTime, 0, 2, intervalDelay, this.currentSprite.sprite, loop);
|
2018-03-19 07:06:49 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Animate the current sprite.</summary>
|
2018-03-20 14:01:43 +08:00
|
|
|
/// <param name="gameTime">The game time from Monogames/XNA</param>
|
|
|
|
/// <param name="startFrame">The starting frame of the animation on the sprite sheet.</param>
|
|
|
|
/// <param name="numberOfFrames">The number of frames to animate the sprite.</param>
|
|
|
|
/// <param name="interval">The delay between frames in milliseconds.</param>
|
|
|
|
/// <param name="sprite">The animated sprite from the npc.</param>
|
|
|
|
/// <param name="loop">If true, the animation plays over and over again.</param>
|
2018-12-30 18:00:05 +08:00
|
|
|
public virtual bool Animate(GameTime gameTime, int startFrame, int numberOfFrames, float interval, AnimatedSprite sprite, bool loop = true)
|
2018-03-19 07:06:49 +08:00
|
|
|
{
|
|
|
|
if (sprite.CurrentFrame >= startFrame + numberOfFrames + 1 || sprite.CurrentFrame < startFrame)
|
|
|
|
sprite.CurrentFrame = startFrame + sprite.CurrentFrame % numberOfFrames;
|
|
|
|
sprite.timer = sprite.timer + (float)gameTime.ElapsedGameTime.TotalMilliseconds;
|
|
|
|
if ((double)sprite.timer > (double)interval)
|
|
|
|
{
|
|
|
|
sprite.CurrentFrame = sprite.CurrentFrame + 1;
|
|
|
|
sprite.timer = 0.0f;
|
2018-05-01 09:21:31 +08:00
|
|
|
if (sprite.CurrentFrame == startFrame + numberOfFrames + 1 || sprite.currentFrame * sprite.SpriteWidth >= sprite.Texture.Width)
|
2018-03-19 07:06:49 +08:00
|
|
|
{
|
|
|
|
if (loop)
|
|
|
|
sprite.CurrentFrame = startFrame;
|
|
|
|
sprite.UpdateSourceRect();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.UpdateSourceRect(sprite);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Update the source rectangle on the sprite sheet. Needed for animation.</summary>
|
2018-03-19 07:06:49 +08:00
|
|
|
public virtual void UpdateSourceRect(AnimatedSprite sprite)
|
|
|
|
{
|
|
|
|
if (sprite.ignoreSourceRectUpdates)
|
|
|
|
return;
|
2018-05-01 09:21:31 +08:00
|
|
|
sprite.sourceRect.X = sprite.CurrentFrame * sprite.SpriteWidth;
|
2018-03-19 07:06:49 +08:00
|
|
|
sprite.sourceRect.Y = 0;
|
|
|
|
//sprite.SourceRect = new Rectangle(, 0, sprite.spriteWidth, sprite.spriteHeight);
|
2018-02-24 17:04:35 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Animate the current sprite. Theoreticlly works from index offset to how many frames</summary>
|
|
|
|
public void Animate(float intervalFromCharacter, int startFrame, int endFrame, bool loop)
|
2018-02-24 17:04:35 +08:00
|
|
|
{
|
2018-03-19 07:06:49 +08:00
|
|
|
this.currentSprite.sprite.loop = loop;
|
2018-02-24 17:04:35 +08:00
|
|
|
this.currentSprite.sprite.Animate(Game1.currentGameTime, startFrame, endFrame, intervalFromCharacter);
|
2018-02-24 15:16:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|