Skip to content

Instantly share code, notes, and snippets.

@joleeee
Created October 24, 2018 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joleeee/4c636a368e2cb1ec914f55616c1dcdbb to your computer and use it in GitHub Desktop.
Save joleeee/4c636a368e2cb1ec914f55616c1dcdbb to your computer and use it in GitHub Desktop.
Aseprite importer for monogame, very crude prototype.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Linq;
using Newtonsoft.Json.Linq;
class Sprite
{
public Vector2 position;
public Texture2D texture;
public JObject json;
public Frame[] frames;
public Point size;
public Animation[] animations;
public Animation currentAnimation;
public double ms = 0;
public int currentFrame;
public Sprite(Vector2 position, Texture2D texture, string pathToJson)
{
this.position = position;
this.texture = texture;
this.json = JObject.Parse(System.IO.File.ReadAllText(pathToJson));
string s = "";
int i = 0;
frames = new Frame[json["frames"].Children().Count()];
foreach (JProperty child in json["frames"])
{
s = child.Name;
size.X = int.Parse((string)json["frames"][s]["sourceSize"]["w"]);
size.Y = int.Parse((string)json["frames"][s]["sourceSize"]["h"]);
frames[i] = new Frame(
int.Parse((string)json["frames"][s]["frame"]["x"]),
int.Parse((string)json["frames"][s]["frame"]["y"]),
int.Parse((string)json["frames"][s]["duration"])
);
i++;
}
JArray anims = (JArray)json["meta"]["frameTags"];
animations = new Animation[anims.Count];
for (int j = 0; j < anims.Count; j++)
{
Animation an = new Animation((string)anims[j]["name"], (int)anims[j]["from"], (int)anims[j]["to"]);
animations[j] = an;
}
}
public virtual void Update(GameTime gameTime)
{
ms += gameTime.ElapsedGameTime.TotalMilliseconds;
Console.WriteLine(ms);
if (ms >= frames[currentFrame].ms)
{
Console.WriteLine(true);
ms = 0;
currentFrame++;
if (currentFrame > currentAnimation.to)
currentFrame = currentAnimation.from;
}
else
{
Console.WriteLine(false);
}
}
public virtual bool SetAnimation(string name)
{
foreach (Animation animation in animations)
{
if (animation.name == name)
{
currentAnimation = animation;
ms = 0;
return true;
}
}
return false;
}
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, new Rectangle(frames[currentFrame].point, size), Color.White);
}
//public static bool Overlap(Sprite a, Sprite b, int scale = 2)
//{
// Rectangle ar = new Rectangle((a.position * scale).ToPoint(), new Point(a.current.Size.X * scale, a.current.Size.Y * scale));
// Rectangle ab = new Rectangle((b.position * scale).ToPoint(), new Point(b.current.Size.X * scale, b.current.Size.Y * scale));
// return ar.Intersects(ab);
//}
}
struct Animation
{
public string name;
public int from;
public int to;
public Animation(string name, int from, int to)
{
this.name = name;
this.from = from;
this.to = to;
}
}
struct Frame
{
public Point point;
public int ms;
public Frame(int x, int y, int ms)
{
this.point = new Point(x, y);
this.ms = ms;
}
public Frame(Point point, int ms)
{
this.point = point;
this.ms = ms;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment