Browse Source

Add Sprite class & load sprite metadata from JSON.

master
Colin McMillen 4 years ago
parent
commit
9672fabcfd
  1. 10
      Shared/NPC.cs
  2. 1
      Shared/Shared.projitems
  3. 2
      Shared/SneakGame.cs
  4. 89
      Shared/Sprites.cs

10
Shared/NPC.cs

@ -25,14 +25,10 @@ namespace SemiColinGames {
position.X += (int) (120 * Facing * modelTime);
}
private int SpriteIndex() {
int frameNum = (int) Clock.ModelTime.TotalMilliseconds / 125 % 4;
return 35 + frameNum;
}
public void Draw(SpriteBatch spriteBatch) {
int index = SpriteIndex();
Rectangle textureSource = new Rectangle(index * spriteWidth, 0, spriteWidth, spriteHeight);
Rectangle textureSource = Sprites.Executioner.GetTextureSource(
"run", (int) Clock.ModelTime.TotalMilliseconds);
// TODO: move this into Sprite metadata.
Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset);
SpriteEffects effect = Facing == 1 ?
SpriteEffects.None : SpriteEffects.FlipHorizontally;

1
Shared/Shared.projitems

@ -13,6 +13,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ExtensionMethods.cs" />
<Compile Include="$(MSBuildThisFileDirectory)NPC.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SoundEffects.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Sprites.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Text.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Textures.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Clock.cs" />

2
Shared/SneakGame.cs

@ -61,6 +61,8 @@ namespace SemiColinGames {
base.LoadContent();
SoundEffects.Load(Content);
Textures.Load(Content);
Sprites.Load(Content);
// TODO: move this into World.
linesOfSight = new LinesOfSight(GraphicsDevice);
LoadLevel();
}

89
Shared/Sprites.cs

@ -0,0 +1,89 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
namespace SemiColinGames {
struct SpriteAnimation {
public readonly int Start;
public readonly int End;
public readonly int DurationMs;
public SpriteAnimation(int start, int end, int durationMs) {
Start = start;
End = end;
DurationMs = durationMs;
}
}
struct Frame {
public readonly Rectangle Source;
public readonly int DurationMs;
public Frame(Rectangle source, int durationMs) {
Source = source;
DurationMs = durationMs;
}
}
class Sprite {
public readonly TextureRef Texture;
private readonly Dictionary<string, SpriteAnimation> animations;
private readonly List<Frame> frames;
public Sprite(TextureRef texture, string metadataJson) {
Texture = texture;
animations = new Dictionary<string, SpriteAnimation>();
JObject json = JObject.Parse(metadataJson);
frames = new List<Frame>();
foreach (JToken child in json.SelectToken("frames").Children()) {
Rectangle source = new Rectangle(
child.SelectToken("frame.x").Value<int>(),
child.SelectToken("frame.y").Value<int>(),
child.SelectToken("frame.w").Value<int>(),
child.SelectToken("frame.h").Value<int>());
int durationMs = child.SelectToken("duration").Value<int>();
frames.Add(new Frame(source, durationMs));
}
// TODO: handle ping-pong animations.
JToken frameTags = json.SelectToken("meta.frameTags");
foreach (JToken child in frameTags.Children()) {
string name = child.SelectToken("name").Value<string>();
int start = child.SelectToken("from").Value<int>();
int end = child.SelectToken("to").Value<int>();
int durationMs = 0;
for (int i = start; i <= end; i++) {
durationMs += frames[i].DurationMs;
}
animations[name] = new SpriteAnimation(start, end, durationMs);
}
}
public Rectangle GetTextureSource(string animationName, int timeMs) {
SpriteAnimation animation = animations[animationName];
int time = timeMs % animation.DurationMs;
for (int i = animation.Start; i <= animation.End; i++) {
int frameTime = frames[i].DurationMs;
if (time < frameTime) {
return frames[i].Source;
}
time -= frameTime;
}
// We shouldn't get here, but if we did, the last frame is a fine thing to return.
return frames[animation.End].Source;
}
}
static class Sprites {
public static Sprite Executioner;
public static void Load(ContentManager content) {
Executioner = new Sprite(
Textures.Executioner, content.Load<string>("sprites/ccg/executioner_female_json"));
}
}
}
Loading…
Cancel
Save