Cat and ghosts moving

master
BraydonKains 5 years ago
parent 4be05cff2d
commit 0229f9f055

File diff suppressed because one or more lines are too long

@ -1,68 +1,139 @@
{
"name": "cat",
"image": "catsandghosts",
"image": "Master-no-background-01",
"width": 32,
"height": 32,
"animations": [
{
"id": 1,
"name": "Idle",
"name": "Walk Left",
"frames": [
{
"id": 1,
"x": 32,
"y": 64,
"timer": 8
},
{
"id": 2,
"x": 64,
"y": 64,
"timer": 8
},
{
"id": 3,
"x": 0,
"y": 32,
"timer": 0
"y": 64,
"timer": 8
}
]
},
{
"id": 2,
"name": "Walk X",
"name": "Walk Right",
"frames": [
{
"id": 1,
"x": 64,
"y": 32,
"x": 32,
"y": 96,
"timer": 8
},
{
"id": 2,
"x": 32,
"y": 32,
"x": 64,
"y": 96,
"timer": 8
},
{
"id": 3,
"x": 0,
"y": 32,
"y": 96,
"timer": 8
}
]
},
{
"id": 3,
"name": "Walk Y",
"name": "Walk Up",
"frames": [
{
"id": 1,
"x": 32,
"y": 64,
"timer": 8
"y": 128,
"timer": 6
},
{
"id": 2,
"x": 64,
"y": 64,
"timer": 8
"x": 0,
"y": 128,
"timer": 6
}
]
},
{
"id": 4,
"name": "Scared",
"name": "Walk Down",
"frames": [
{
"id": 1,
"x": 32,
"y": 160,
"timer": 6
},
{
"id": 2,
"x": 0,
"y": 160,
"timer": 6
}
]
},
{
"id": 5,
"name": "Scared Left",
"frames": [
{
"id": 1,
"x": 96,
"y": 64,
"timer": 0
}
]
},
{
"id": 6,
"name": "Scared Right",
"frames": [
{
"id": 1,
"x": 96,
"y": 96,
"timer": 0
}
]
},
{
"id": 7,
"name": "Scared Up",
"frames": [
{
"id": 1,
"x": 64,
"y": 128,
"timer": 0
}
]
},
{
"id": 8,
"name": "Scared Down",
"frames": [
{
"id": 1,
"x": 64,
"y": 160,
"timer": 0
}
]
}
]

@ -0,0 +1,80 @@
{
"name": "ghost",
"image": "Master-no-background-01",
"width": 32,
"height": 32,
"animations": [
{
"id": 1,
"name": "Walk Left",
"frames": [
{
"id": 1,
"x": 0,
"y": 32,
"timer": 6
},
{
"id": 2,
"x": 32,
"y": 32,
"timer": 6
}
]
},
{
"id": 2,
"name": "Walk Right",
"frames": [
{
"id": 1,
"x": 0,
"y": 0,
"timer": 6
},
{
"id": 2,
"x": 32,
"y": 0,
"timer": 6
}
]
},
{
"id": 3,
"name": "Walk Up",
"frames": [
{
"id": 1,
"x": 64,
"y": 0,
"timer": 6
},
{
"id": 2,
"x": 64,
"y": 32,
"timer": 6
}
]
},
{
"id": 4,
"name": "Walk Down",
"frames": [
{
"id": 1,
"x": 96,
"y": 0,
"timer": 6
},
{
"id": 2,
"x": 96,
"y": 32,
"timer": 6
}
]
}
]
}

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
@ -7,12 +8,16 @@ namespace SeeNoEvil.Character {
public string Image {get; private set;}
public int Width {get; private set;}
public int Height {get; private set;}
public bool Idle;
public Dictionary<int, Animation> Animations {get; private set;}
private Animation CurrentAnimation;
public Frame CurrentFrame =>
CurrentAnimation.GetFrame();
public bool IsDefault =>
CurrentAnimation.Id == 1;
public Frame CurrentFrame {
get {
if(Idle) return CurrentAnimation.CurrentFrame;
else return CurrentAnimation.GetFrame();
}
}
public AnimationController(AnimationSetModel model) {
Name = model.Name;
@ -25,6 +30,7 @@ namespace SeeNoEvil.Character {
return animations;
});
ChangeAnimation(1);
Idle = true;
}
public void ChangeAnimation(int animationId) {
@ -37,8 +43,8 @@ namespace SeeNoEvil.Character {
public int Id {get; set;}
public string Name {get; set;}
public FrameCollection Frames {get; set;}
public Frame CurrentFrame;
private int TotalFrames;
private Frame CurrentFrame;
private int CurrentFrameId;
public Animation(AnimationModel model) {

@ -7,6 +7,21 @@ namespace SeeNoEvil.Character {
Width = AnimationController.Width;
Height = AnimationController.Height;
Facing = facing;
//FIXME Can probably share this code
switch(Facing) {
case Direction.Up:
AnimationController.ChangeAnimation(3);
break;
case Direction.Down:
AnimationController.ChangeAnimation(4);
break;
case Direction.Left:
AnimationController.ChangeAnimation(1);
break;
case Direction.Right:
AnimationController.ChangeAnimation(2);
break;
}
}
public override void Move(Direction direction) {
@ -16,10 +31,10 @@ namespace SeeNoEvil.Character {
AnimationController.ChangeAnimation(3);
break;
case Direction.Down:
AnimationController.ChangeAnimation(3);
AnimationController.ChangeAnimation(4);
break;
case Direction.Left:
AnimationController.ChangeAnimation(2);
AnimationController.ChangeAnimation(1);
break;
case Direction.Right:
AnimationController.ChangeAnimation(2);

@ -1,18 +1,20 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using SeeNoEvil.Level;
namespace SeeNoEvil.Character {
public class Character {
private Vector2 Destination {get; set;}
private Vector2 Velocity {get; set;}
private Vector2 Position {get; set;}
public Vector2 Position {get; private set;}
private Texture2D SpriteSheet;
protected AnimationController AnimationController;
protected int Width;
protected int Height;
protected Direction Facing;
protected PlayField Field;
protected bool Moving =>
!Destination.Equals(Vector2.Zero) &&
@ -25,16 +27,18 @@ namespace SeeNoEvil.Character {
Velocity = Vector2.Zero;
}
public void Load(ContentManager content) {
public void Load(ContentManager content, PlayField playField) {
SpriteSheet = content.Load<Texture2D>(AnimationController.Image);
Field = playField;
}
// TODO Do I want to move every frame?
public void Update() {
if(Moving)
if(Moving) {
Position = Vector2.Add(Position, Velocity);
else if(!AnimationController.IsDefault)
AnimationController.ChangeAnimation(1);
AnimationController.Idle = false;
}
else AnimationController.Idle = true;
}
public void Draw(SpriteBatch spriteBatch) {
@ -44,11 +48,11 @@ namespace SeeNoEvil.Character {
AnimationController.Width,
AnimationController.Height);
spriteBatch.Draw(SpriteSheet, Position, srcRectangle, Color.White);
// spriteBatch.Draw(SpriteSheet, Position, Color.White);
}
public virtual void Move(Direction direction) {
if(!Moving) {
int velocity = 1;
int x = 0, y = 0;
switch(direction) {
case Direction.Up:
@ -64,8 +68,11 @@ namespace SeeNoEvil.Character {
x = 1;
break;
}
Destination = Vector2.Add(Position, new Vector2(Width*x, Height*y));
Velocity = new Vector2(x, y);
var tryPosition = Vector2.Add(Position, new Vector2(Width*x, Height*y));
if(Field.TryWalk(tryPosition)) {
Destination = Vector2.Add(Position, new Vector2(Width*x, Height*y));
Velocity = new Vector2(x*velocity, y*velocity);
}
}
}
}

@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using SeeNoEvil.Level;
using SeeNoEvil.Tiled;
namespace SeeNoEvil.Character {
public class GhostController {
public List<Ghost> Ghosts {get; set;}
public GhostController(IEnumerable<ObjectCoordinate> coordinates) {
Ghosts = coordinates.Select(
coord => new Ghost(new Vector2(coord.X, coord.Y))
).ToList();
}
public void LoadAll(ContentManager content, PlayField playField) {
Ghosts.ForEach(ghost => ghost.Load(content, playField));
}
public void DrawAll(SpriteBatch spriteBatch) {
Ghosts.ForEach(ghost => ghost.Draw(spriteBatch));
}
public void UpdateAll() {
Ghosts.ForEach(ghost => ghost.Update());
}
public void MoveGhosts() {
Ghosts.ForEach(ghost => ghost.DecideMove());
}
}
public class Ghost : Character {
public Ghost(Vector2 position) : base(position) {
AnimationController = new AnimationController(AnimationParser.ReadAnimationJson("SeeNoEvil/Animation/ghost.json"));
Width = AnimationController.Width;
Height = AnimationController.Height;
Facing = Direction.Down;
}
public void DecideMove() {
Array values = Enum.GetValues(typeof(Direction));
Random random = new Random();
Direction randomDirection = (Direction)values.GetValue(random.Next(values.Length));
switch(randomDirection) {
case Direction.Up:
AnimationController.ChangeAnimation(3);
break;
case Direction.Down:
AnimationController.ChangeAnimation(4);
break;
case Direction.Left:
AnimationController.ChangeAnimation(1);
break;
case Direction.Right:
AnimationController.ChangeAnimation(2);
break;
}
base.Move(randomDirection);
}
}
}

@ -10,7 +10,7 @@ namespace SeeNoEvil.Level {
private Vector3 TranslationVector =>
new Vector3(-Centre.X + (Viewport.Width / 2),
-Centre.Y + (Viewport.Height / 2),
0);
1);
public Camera(Viewport _viewport) {
Viewport = _viewport;
@ -18,8 +18,9 @@ namespace SeeNoEvil.Level {
Centre = new Vector2(Viewport.Width / 2, Viewport.Height / 2);
}
//FIXME Don't need velocity anymore?
public void Update(Vector2 position, Vector2 velocity) {
Centre = Vector2.Add(Centre, velocity);
Centre = position;
Transform = Matrix.CreateTranslation(TranslationVector);
}
}

@ -1,10 +1,12 @@
using System.Collections.Generic;
using System.Linq;
using SeeNoEvil.Tiled;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using SeeNoEvil.Tiled;
namespace SeeNoEvil.Level {
public class TilemapLevel {
private readonly string MapName;
@ -18,6 +20,7 @@ namespace SeeNoEvil.Level {
public void LoadMap(ContentManager content) {
Map = new TiledMap(TiledParser.ReadMapJson(MapName));
Map.LoadView();
Map.LoadObjects();
TilesetTextures = Map.GetTilesetNames().Aggregate(new Dictionary<string, Texture2D>(),
(textures, contentName) => {
textures.Add(contentName, content.Load<Texture2D>(contentName));
@ -25,8 +28,24 @@ namespace SeeNoEvil.Level {
});
}
private IEnumerable<string> GetTilesetNames() {
return Map.GetTilesetNames();
private IEnumerable<string> GetTilesetNames() => Map.GetTilesetNames();
public Vector2 GetPlayerPosition() {
//FIXME This fuckin sucks
Map.Objects.TryGetValue("Cat", out List<ObjectCoordinate> catCoords);
ObjectCoordinate catCoord = catCoords.First();
return new Vector2(catCoord.X, catCoord.Y);
}
public PlayField GetPlayField() {
Map.View.TryGetValue("Ground", out List<TileLocation> ground);
return new PlayField(ground.Where(tile => tile.tile.gid != 0));
}
public IEnumerable<ObjectCoordinate> GetGhostCoordinates() {
// FIXME this fuckin sucks too I think?
Map.Objects.TryGetValue("Ghosts", out List<ObjectCoordinate> ghosts);
return ghosts;
}
public void Draw(SpriteBatch spriteBatch, string layer) {
@ -36,7 +55,7 @@ namespace SeeNoEvil.Level {
Texture2D layerTexture;
if(tile.tile.gid > 0 && TilesetTextures.TryGetValue(tile.tile.setName, out layerTexture)) {
spriteBatch.Draw(layerTexture, tile.location, tile.tile.srcRectangle, Color.White);
}
}
});
}
}

@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using SeeNoEvil.Tiled;
namespace SeeNoEvil.Level {
public class PlayField {
private IEnumerable<TileLocation> Tiles {get; set;}
public PlayField(IEnumerable<TileLocation> tiles) {
Tiles = tiles;
}
public bool TryWalk(Vector2 newLocation) =>
Tiles.Any(tile => tile.location.Equals(newLocation));
}
}

@ -17,7 +17,9 @@ namespace SeeNoEvil
SpriteBatch spriteBatch;
TilemapLevel level;
Camera camera;
Cat cat;
Cat player;
GhostController enemyController;
PlayField playField;
public SeeNoEvilGame()
{
@ -28,10 +30,9 @@ namespace SeeNoEvil
protected override void Initialize()
{
// TODO: Add your initialization logic here
level = new TilemapLevel("./Maps/MagicLandCsv.json");
// level = new TilemapLevel("./Maps/MagicLandCsv.json");
// level = new TilemapLevel("./Maps/test.json");
cat = new Cat(Vector2.Zero, Direction.Right);
level = new TilemapLevel("./Maps/level.json");
base.Initialize();
}
@ -40,7 +41,11 @@ namespace SeeNoEvil
{
spriteBatch = new SpriteBatch(GraphicsDevice);
level.LoadMap(Content);
cat.Load(Content);
playField = level.GetPlayField();
player = new Cat(level.GetPlayerPosition(), Direction.Right);
player.Load(Content, playField);
enemyController = new GhostController(level.GetGhostCoordinates());
enemyController.LoadAll(Content, playField);
camera = new Camera(GraphicsDevice.Viewport);
}
@ -49,26 +54,25 @@ namespace SeeNoEvil
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
int xVelocity = 0, yVelocity = 0;
// if(Keyboard.GetState().IsKeyDown(Keys.Down))
// yVelocity = 1;
// if(Keyboard.GetState().IsKeyDown(Keys.Up))
// yVelocity = -1;
// if(Keyboard.GetState().IsKeyDown(Keys.Left))
// xVelocity = -1;
// if(Keyboard.GetState().IsKeyDown(Keys.Right))
// xVelocity = 1;
camera.Update(Vector2.Zero, new Vector2(xVelocity, yVelocity));
if(Keyboard.GetState().IsKeyDown(Keys.Down))
cat.Move(Direction.Down);
if(Keyboard.GetState().IsKeyDown(Keys.Up))
cat.Move(Direction.Up);
if(Keyboard.GetState().IsKeyDown(Keys.Left))
cat.Move(Direction.Left);
if(Keyboard.GetState().IsKeyDown(Keys.Right))
cat.Move(Direction.Right);
cat.Update();
camera.Update(player.Position, Vector2.Zero);
if(Keyboard.GetState().IsKeyDown(Keys.Down)) {
player.Move(Direction.Down);
enemyController.MoveGhosts();
}
if(Keyboard.GetState().IsKeyDown(Keys.Up)) {
player.Move(Direction.Up);
enemyController.MoveGhosts();
}
if(Keyboard.GetState().IsKeyDown(Keys.Left)) {
player.Move(Direction.Left);
enemyController.MoveGhosts();
}
if(Keyboard.GetState().IsKeyDown(Keys.Right)) {
player.Move(Direction.Right);
enemyController.MoveGhosts();
}
player.Update();
enemyController.UpdateAll();
base.Update(gameTime);
}
@ -80,8 +84,11 @@ namespace SeeNoEvil
spriteBatch.Begin(
transformMatrix: camera.Transform
);
// level.Draw(spriteBatch, "background");
cat.Draw(spriteBatch);
level.Draw(spriteBatch, "Sky");
level.Draw(spriteBatch, "Ground");
level.Draw(spriteBatch, "Bushes");
player.Draw(spriteBatch);
enemyController.DrawAll(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);

@ -4,7 +4,7 @@ using System.Linq;
using SeeNoEvil.Level;
namespace SeeNoEvil.Tiled {
public class MapLayer {
public class TileLayer {
public string Type {get; private set;}
public string Name {get; private set;}
@ -14,7 +14,7 @@ namespace SeeNoEvil.Tiled {
private int Height;
private List<uint> Data;
public MapLayer(MapLayerModel model) {
public TileLayer(MapLayerModel model) {
Name = model.Name;
Width = model.Width;
Height = model.Height;

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
//TODO Do all these need to be public?
namespace SeeNoEvil.Tiled {
//TODO Can probably be a struct?
public class ObjectLayer {
public IEnumerable<ObjectCoordinate> Objects {get; private set;}
public string Name {get; private set;}
public ObjectLayer(MapLayerModel model) {
Name = model.Name;
Objects = model.Objects.Select(o => new ObjectCoordinate(o.Id,
o.X,
o.Y));
}
}
public struct ObjectCoordinate {
public ObjectCoordinate(int id, float x, float y) {
X = (int)Math.Round(x / 32)*32;
Y = (int)Math.Round(y / 32)*32;
Id = id;
}
public int X {get; set;}
public int Y {get; set;}
public int Id {get; set;}
}
}

@ -6,8 +6,10 @@ using SeeNoEvil.Level;
namespace SeeNoEvil.Tiled {
public class TiledMap {
public Dictionary<string, List<TileLocation>> View {get; private set;}
public Dictionary<string, List<ObjectCoordinate>> Objects {get; private set;}
private bool Infinite;
private List<MapLayer> Layers;
private List<TileLayer> TileLayers;
private List<ObjectLayer> ObjectLayers;
private List<TileSet> TileSets;
private int TileHeight;
private int TileWidth;
@ -18,7 +20,10 @@ namespace SeeNoEvil.Tiled {
public TiledMap(TiledModel model) {
Infinite = model.Infinite;
Layers = model.Layers.Select(model => new MapLayer(model)).ToList();
TileLayers = model.Layers.Where(model => model.Type == "tilelayer")
.Select(model => new TileLayer(model)).ToList();
ObjectLayers = model.Layers.Where(model => model.Type == "objectgroup")
.Select(model => new ObjectLayer(model)).ToList();
TileSets = model.TileSets.Select(model => new TileSet(model)).ToList();
TileHeight = model.TileHeight;
TileWidth = model.TileWidth;
@ -35,7 +40,7 @@ namespace SeeNoEvil.Tiled {
public void LoadView() {
// Get all tilelayers
Dictionary<string, List<DataCoordinate>> layerData = new Dictionary<string, List<DataCoordinate>>();
Layers.Where(layer => layer.Type == "tilelayer").ToList()
TileLayers.Where(layer => layer.Type == "tilelayer").ToList()
.ForEach(layer => {
layerData.Add(layer.Name, layer.DataCoordinates);
});
@ -75,6 +80,17 @@ namespace SeeNoEvil.Tiled {
});
}
// Load objects on the level
public void LoadObjects() {
Objects = ObjectLayers.Aggregate(new Dictionary<string, List<ObjectCoordinate>>(),
// FIXME This is stupid naming
(layers, objects) => {
layers.Add(objects.Name, objects.Objects.ToList());
return layers;
}
);
}
// Load tiles into cache if they don't exist
private void LoadTiles(HashSet<uint> tiles) {
foreach(uint gid in tiles) {
@ -86,4 +102,5 @@ namespace SeeNoEvil.Tiled {
}
}
}
}

@ -24,12 +24,18 @@ namespace SeeNoEvil.Tiled {
public int TileCount {get; set;}
}
public struct MapLayerModel {
public string Name {get; set;}
public int Width {get; set;}
public int Height {get; set;}
public IEnumerable<uint> Data {get; set;}
public string Type {get; set;}
public IEnumerable<TiledObjectModel> Objects {get; set;}
}
public struct TiledObjectModel {
public int Id {get; set;}
public float X {get; set;}
public float Y {get; set;}
}
}
Loading…
Cancel
Save