Files
MonoGame.Extended/Source/Demos/Demo.Platformer/Entities/EntityFactory.cs
T
Dylan WilsonandGitHub b3a631aff5 Gui dialogs (#410)
* started refactoring for gui windows

* a bit more work on gui windows

* fixed some bugs and broken build stuff

* fixed some bugs in gui controls

* a little code cleanup

* got gui dialogs positioning and experimenting with named styles by type

* apply control styles directly in the constructor of controls

* got everything compiling again

* all gui controls have skinnable constructors

* rework uniform grid desired size calculations

* turns out gui layouts are complicated

* include pdb files in nuget packages

* fixed a bug in find nearest glyph

* custom control styling

* added a range constructor overload that takes one value

* rethinking control desired size behaviour

* made particle emitters more editable at runtime

* moved the auto trigger code to the particle effect instead of the emitters

* tweaks

* changed the particle modifer interface to an abstract base with an optional name

* changed the particle modifer interface to an abstract base with an optional name

* an attempt at creating a gui combo box

* rolled back the nested control experiment

* pretty good (but not perfect) combo boxes

* tweaks

* name property bindings on lists

* removed and cleaned up the hacks from the gui combo box

* minor combo box styling tweaks

* minor bug fix in gui combo box

* experiments with data binding

* fixed a mistake on the texture region service interface

* working on the ability to serialize particles

* finished particle serialization

* more robust range deserialization

* added editor browable attributes

* fixed some broken demos

* made the particle modifiers editable at runtime

* fixed some issues with particle serialization

* fixed some issues with interpolators
2017-07-22 21:22:13 +10:00

126 lines
4.9 KiB
C#

using Demo.Platformer.Entities.Components;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended;
using MonoGame.Extended.Animations.SpriteSheets;
using MonoGame.Extended.Entities;
using MonoGame.Extended.TextureAtlases;
//using AnimationComponent = Demo.Platformer.Entities.Components.AnimationComponent;
using SpriteComponent = Demo.Platformer.Entities.Components.SpriteComponent;
using TransformComponent = Demo.Platformer.Entities.Components.TransformComponent;
namespace Demo.Platformer.Entities
{
public static class Entities
{
public const string Player = "Player";
}
public class EntityFactory
{
private readonly EntityComponentSystem _ecs;
private readonly EntityManager _entityManager;
private TextureAtlas _characterTextureAtlas;
public EntityFactory(EntityComponentSystem ecs, ContentManager contentManager)
{
_ecs = ecs;
_entityManager = ecs.EntityManager;
LoadContent(contentManager);
}
private void LoadContent(ContentManager content)
{
var texture = content.Load<Texture2D>("tiny-characters");
_characterTextureAtlas = TextureAtlas.Create("tiny-characters-atlas", texture, 32, 32, 112);
}
public Entity CreatePlayer(Vector2 position)
{
var entity = _entityManager.CreateEntity();
var transform = entity.Attach<TransformComponent>();
transform.Position = position;
var textureRegion = _characterTextureAtlas[0];
var sprite = entity.Attach<SpriteComponent>();
sprite.Origin = new Vector2(textureRegion.Width / 2f, textureRegion.Height / 2f);
//var animatedSprite = entity.Attach<AnimationComponent>();
//var animationFactory = animatedSprite.AnimationFactory = new SpriteSheetAnimationFactory(_characterTextureAtlas); ;
//animationFactory.Add("idle", new SpriteSheetAnimationData(new[] { 12, 13 }, 1.0f));
//animationFactory.Add("walk", new SpriteSheetAnimationData(new[] { 0, 1, 2, 3 }));
//animationFactory.Add("jump", new SpriteSheetAnimationData(new[] { 8, 9 }, isLooping: false));
//animatedSprite.Play("idle");
var collision = entity.Attach<CollisionBodyComponent>();
collision.Size = textureRegion.Size;
collision.Origin = Vector2.One * 0.5f;
var player = entity.Attach<PlayerComponent>();
player.WalkSpeed = 220f;
player.JumpSpeed = 420f;
var health = entity.Attach<HealthComponent>();
health.Points = 20;
return entity;
}
public Entity CreateSolid(Vector2 position, Size2 size)
{
var entity = _entityManager.CreateEntity();
var transform = entity.Attach<TransformComponent>();
transform.Position = position;
var collision = entity.Attach<CollisionBodyComponent>();
collision.Size = size;
collision.IsStatic = true;
return entity;
}
public Entity CreateDeadly(Vector2 position, Size2 size)
{
var entity = _entityManager.CreateEntity();
var transform = entity.Attach<TransformComponent>();
transform.Position = position;
var collision = entity.Attach<CollisionBodyComponent>();
collision.Size = size;
collision.IsStatic = true;
return entity;
}
//public Entity CreateBloodExplosion(Vector2 position, float totalSeconds = 1.0f)
//{
// var random = new FastRandom();
// var textureRegion = _characterTextureAtlas[0];
// var entity = _ecs.CreateEntity(position);
// var profile = Profile.Spray(new Vector2(0, -1), MathHelper.Pi);
// var term = TimeSpan.FromSeconds(totalSeconds);
// var particleEmitter = new ParticleEmitter(textureRegion, 32, term, profile)
// {
// Parameters = new ParticleReleaseParameters
// {
// Speed = new Range<float>(140, 200),
// Quantity = new Range<int>(32, 64),
// Rotation = new Range<float>(-MathHelper.TwoPi, MathHelper.TwoPi)
// },
// Modifiers = new Modifier[]
// {
// new LinearGravityModifier { Direction = Vector2.UnitY, Strength = 350 },
// new OpacityFastFadeModifier(),
// new RotationModifier { RotationRate = random.NextSingle(-MathHelper.TwoPi, MathHelper.TwoPi) }
// }
// };
// entity.AttachComponent(new TransformableComponent<ParticleEmitter>(particleEmitter));
// entity.Destroy(delaySeconds: totalSeconds);
// return entity;
//}
}
}