Files
MonoGame.Extended/Source/Demos/Platformer/EntityFactory.cs
T

75 lines
3.7 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended;
using MonoGame.Extended.Animations;
using MonoGame.Extended.Animations.SpriteSheets;
using MonoGame.Extended.Entities;
using MonoGame.Extended.Entities.Legacy;
using MonoGame.Extended.TextureAtlases;
using Platformer.Collisions;
using Platformer.Components;
namespace Platformer
{
public class EntityFactory
{
private readonly EntityManager _entityManager;
private readonly ContentManager _contentManager;
public EntityFactory(EntityManager entityManager, ContentManager contentManager)
{
_entityManager = entityManager;
_contentManager = contentManager;
}
public Entity CreatePlayer(Vector2 position)
{
var dudeTexture = _contentManager.Load<Texture2D>("hero");
var dudeAtlas = TextureAtlas.Create("dudeAtlas", dudeTexture, 16, 16);
var entity = _entityManager.CreateEntity();
var animationFactory = new SpriteSheetAnimationFactory(dudeAtlas);
animationFactory.Add("idle", new SpriteSheetAnimationData(new[] { 0, 1, 2, 1 }));
animationFactory.Add("walk", new SpriteSheetAnimationData(new[] { 6, 7, 8, 9, 10, 11 }, frameDuration: 0.1f));
animationFactory.Add("jump", new SpriteSheetAnimationData(new[] { 10, 12 }, frameDuration: 1.0f, isLooping: false));
animationFactory.Add("fall", new SpriteSheetAnimationData(new[] { 13, 14 }, frameDuration: 1.0f, isLooping: false));
animationFactory.Add("swim", new SpriteSheetAnimationData(new[] { 18, 19, 20, 21, 22, 23 }));
animationFactory.Add("kick", new SpriteSheetAnimationData(new[] { 15 }, frameDuration: 0.3f, isLooping: false));
animationFactory.Add("punch", new SpriteSheetAnimationData(new[] { 26 }, frameDuration: 0.3f, isLooping: false));
animationFactory.Add("cool", new SpriteSheetAnimationData(new[] { 17 }, frameDuration: 0.3f, isLooping: false));
entity.Attach(new AnimatedSprite(animationFactory, "idle"));
entity.Attach(new Transform2(position, 0, Vector2.One * 4));
entity.Attach(new Body { Position = position, Size = new Vector2(32, 64), BodyType = BodyType.Dynamic });
entity.Attach<Player>();
return entity;
}
public Entity CreateBlue(Vector2 position)
{
var dudeTexture = _contentManager.Load<Texture2D>("blueguy");
var dudeAtlas = TextureAtlas.Create("blueguyAtlas", dudeTexture, 16, 16);
var entity = _entityManager.CreateEntity();
var animationFactory = new SpriteSheetAnimationFactory(dudeAtlas);
animationFactory.Add("idle", new SpriteSheetAnimationData(new[] { 0, 1, 2, 3, 2, 1 }));
animationFactory.Add("walk", new SpriteSheetAnimationData(new[] { 6, 7, 8, 9, 10, 11 }, frameDuration: 0.1f));
animationFactory.Add("jump", new SpriteSheetAnimationData(new[] { 10, 12 }, frameDuration: 1.0f, isLooping: false));
entity.Attach(new AnimatedSprite(animationFactory, "idle"));
entity.Attach(new Transform2(position, 0, Vector2.One * 4));
entity.Attach(new Body { Position = position, Size = new Vector2(32, 64), BodyType = BodyType.Dynamic });
return entity;
}
public void CreateTile(int x, int y, int width, int height)
{
var entity = _entityManager.CreateEntity();
entity.Attach(new Body
{
Position = new Vector2(x * width + width * 0.5f, y * height + height * 0.5f),
Size = new Vector2(width, height),
BodyType = BodyType.Static
});
}
}
}