Cat is moving!
parent
15612f660a
commit
830891a71e
@ -0,0 +1,69 @@
|
||||
{
|
||||
"name": "cat",
|
||||
"image": "catsandghosts",
|
||||
"width": 32,
|
||||
"height": 32,
|
||||
"animations": [
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Idle",
|
||||
"frames": [
|
||||
{
|
||||
"id": 1,
|
||||
"x": 0,
|
||||
"y": 32,
|
||||
"timer": 0
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "Walk X",
|
||||
"frames": [
|
||||
{
|
||||
"id": 1,
|
||||
"x": 64,
|
||||
"y": 32,
|
||||
"timer": 3
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"x": 32,
|
||||
"y": 32,
|
||||
"timer": 3
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"x": 0,
|
||||
"y": 32,
|
||||
"timer": 3
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"name": "Walk Y",
|
||||
"frames": [
|
||||
{
|
||||
"id": 1,
|
||||
"x": 32,
|
||||
"y": 64,
|
||||
"timer": 3
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"x": 64,
|
||||
"y": 64,
|
||||
"timer": 3
|
||||
}
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"name": "Scared",
|
||||
"frames": [
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace SeeNoEvil.Character {
|
||||
public class AnimationController {
|
||||
public string Name {get; private set;}
|
||||
public string Image {get; private set;}
|
||||
public int Width {get; private set;}
|
||||
public int Height {get; private set;}
|
||||
public Dictionary<int, Animation> Animations {get; private set;}
|
||||
private Animation CurrentAnimation;
|
||||
public Frame CurrentFrame =>
|
||||
CurrentAnimation.GetFrame();
|
||||
|
||||
public AnimationController(AnimationSetModel model) {
|
||||
Name = model.Name;
|
||||
Image = model.Image;
|
||||
Width = model.Width;
|
||||
Height = model.Height;
|
||||
Animations = model.Animations.Aggregate(new Dictionary<int, Animation>(),
|
||||
(animations, animation) => {
|
||||
animations.Add(animation.Id, new Animation(animation));
|
||||
return animations;
|
||||
});
|
||||
ChangeAnimation(1);
|
||||
}
|
||||
|
||||
public void ChangeAnimation(int animationId) {
|
||||
if(Animations.TryGetValue(animationId, out CurrentAnimation))
|
||||
CurrentAnimation.Reset();
|
||||
}
|
||||
}
|
||||
|
||||
public class Animation {
|
||||
public int Id {get; set;}
|
||||
public string Name {get; set;}
|
||||
public FrameCollection Frames {get; set;}
|
||||
private int TotalFrames;
|
||||
private Frame CurrentFrame;
|
||||
private int CurrentFrameId;
|
||||
|
||||
public Animation(AnimationModel model) {
|
||||
Id = model.Id;
|
||||
Name = model.Name;
|
||||
Frames = new FrameCollection(model.Frames);
|
||||
TotalFrames = model.Frames.Count();
|
||||
}
|
||||
|
||||
public void Reset() {
|
||||
CurrentFrameId = 1;
|
||||
CurrentFrame = Frames[CurrentFrameId];
|
||||
}
|
||||
|
||||
// TODO Is this super fuckin ugly? Seems like it
|
||||
public Frame GetFrame() {
|
||||
Frame result = CurrentFrame;
|
||||
if(CurrentFrame.Timer == 1) {
|
||||
CurrentFrameId = CurrentFrameId == TotalFrames ? 1 : CurrentFrameId + 1;
|
||||
CurrentFrame = Frames[CurrentFrameId];
|
||||
} else if(CurrentFrame.Timer > 1) CurrentFrame.Timer--;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public class FrameCollection {
|
||||
private IEnumerable<Frame> Frames;
|
||||
|
||||
public Frame this[int i] {
|
||||
get {
|
||||
return Frames.Where(item => item.Id == i).First();
|
||||
}
|
||||
}
|
||||
|
||||
public FrameCollection(IEnumerable<Frame> frames) {
|
||||
Frames = frames;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SeeNoEvil.Character {
|
||||
public struct AnimationSetModel {
|
||||
public string Name {get; set;}
|
||||
public string Image {get; set;}
|
||||
public int Width {get; set;}
|
||||
public int Height {get; set;}
|
||||
public IEnumerable<AnimationModel> Animations {get; set;}
|
||||
}
|
||||
|
||||
public struct AnimationModel {
|
||||
public int Id {get; set;}
|
||||
public string Name {get; set;}
|
||||
public IEnumerable<Frame> Frames{get; set;}
|
||||
}
|
||||
|
||||
public struct Frame {
|
||||
public int Id {get; set;}
|
||||
public int X {get; set;}
|
||||
public int Y {get; set;}
|
||||
public int Timer {get; set;}
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace SeeNoEvil.Character {
|
||||
public static class AnimationParser {
|
||||
public static AnimationSetModel ReadAnimationJson(string fileName) {
|
||||
StreamReader streamReader = File.OpenText(fileName);
|
||||
string text = streamReader.ReadToEnd();
|
||||
var options = new JsonSerializerOptions {
|
||||
PropertyNameCaseInsensitive = true,
|
||||
};
|
||||
return JsonSerializer.Deserialize<AnimationSetModel>(text, options);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace SeeNoEvil.Character {
|
||||
public enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
||||
public class Cat : Character {
|
||||
private Direction Facing;
|
||||
public Cat(Vector2 position, Direction facing) : base(position) {
|
||||
AnimationController = new AnimationController(AnimationParser.ReadAnimationJson("SeeNoEvil/Animation/cat.json"));
|
||||
Width = AnimationController.Width;
|
||||
Height = AnimationController.Height;
|
||||
Facing = facing;
|
||||
}
|
||||
|
||||
public void Move(Direction direction) {
|
||||
int x = 0, y = 0;
|
||||
switch(direction) {
|
||||
case Direction.Up:
|
||||
y = -1;
|
||||
break;
|
||||
case Direction.Down:
|
||||
y = 1;
|
||||
break;
|
||||
case Direction.Left:
|
||||
x = -1;
|
||||
break;
|
||||
case Direction.Right:
|
||||
x = 1;
|
||||
break;
|
||||
}
|
||||
Destination = Vector2.Add(Position, new Vector2(Width*x, Height*y));
|
||||
Velocity = new Vector2(x, y);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace SeeNoEvil.Character {
|
||||
public class Character {
|
||||
protected Vector2 Destination {get; set;}
|
||||
protected Vector2 Velocity {get; set;}
|
||||
protected Vector2 Position {get; private set;}
|
||||
protected bool Transform =>
|
||||
!Destination.Equals(Vector2.Zero) &&
|
||||
!Velocity.Equals(Vector2.Zero) &&
|
||||
!Position.Equals(Destination);
|
||||
|
||||
protected AnimationController AnimationController;
|
||||
protected Texture2D SpriteSheet;
|
||||
protected int Width;
|
||||
protected int Height;
|
||||
|
||||
public Character(Vector2 position) {
|
||||
Position = position;
|
||||
Destination = Vector2.Zero;
|
||||
Velocity = Vector2.Zero;
|
||||
}
|
||||
|
||||
public void Load(ContentManager content) {
|
||||
SpriteSheet = content.Load<Texture2D>(AnimationController.Image);
|
||||
}
|
||||
|
||||
// TODO Do I want to move every frame?
|
||||
public void Update() {
|
||||
if(Transform)
|
||||
Position = Vector2.Add(Position, Velocity);
|
||||
}
|
||||
|
||||
public void Draw(SpriteBatch spriteBatch) {
|
||||
Frame currentFrame = AnimationController.CurrentFrame;
|
||||
Rectangle srcRectangle = new Rectangle(currentFrame.X,
|
||||
currentFrame.Y,
|
||||
AnimationController.Width,
|
||||
AnimationController.Height);
|
||||
spriteBatch.Draw(SpriteSheet, Position, srcRectangle, Color.White);
|
||||
// spriteBatch.Draw(SpriteSheet, Position, Color.White);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue