diff --git a/Source/Demos/JamGame/Content/0x72_16x16DungeonTileset.v4.png b/Source/Demos/JamGame/Content/0x72_16x16DungeonTileset.v4.png index ea123ef5..ad97407b 100644 Binary files a/Source/Demos/JamGame/Content/0x72_16x16DungeonTileset.v4.png and b/Source/Demos/JamGame/Content/0x72_16x16DungeonTileset.v4.png differ diff --git a/Source/Demos/JamGame/EntityFactory.cs b/Source/Demos/JamGame/EntityFactory.cs index d31b6261..fb17553a 100644 --- a/Source/Demos/JamGame/EntityFactory.cs +++ b/Source/Demos/JamGame/EntityFactory.cs @@ -1,6 +1,9 @@ -using JamGame.Components; +using System.Linq; +using JamGame.Components; using Microsoft.Xna.Framework; using MonoGame.Extended; +using MonoGame.Extended.Animations; +using MonoGame.Extended.Animations.SpriteSheets; using MonoGame.Extended.Entities; using MonoGame.Extended.Sprites; @@ -31,7 +34,7 @@ namespace JamGame var entity = World.CreateEntity(); entity.Attach(new Sprite(_tileset.GetTile(145))); entity.Attach(new Transform2(x, y)); - entity.Attach(new Body { Size = new Size2(16, 16) }); + entity.Attach(new Body { Size = new Size2(16, 16), Velocity = new Vector2(-8, 0) }); entity.Attach(new Enemy()); } @@ -49,7 +52,7 @@ namespace JamGame var entity = World.CreateEntity(); entity.Attach(new Sprite(_tileset.GetTile(213))); entity.Attach(new Transform2(x, y)); - entity.Attach(new Body { Size = new Size2(16, 16) }); + entity.Attach(new Body { Size = new Size2(16, 16), Velocity = new Vector2(-2, 0) }); entity.Attach(new Enemy()); } @@ -62,5 +65,18 @@ namespace JamGame entity.Attach(new Projectile()); return entity; } + + public void SpawnExplosion(Vector2 position, Vector2 velocity) + { + var frames = new[] {256, 257, 258, 259, 260, 261, 262, 263, 264}; + var animationFactory = new SpriteSheetAnimationFactory(frames.Select(f => _tileset.GetTile(f))); + animationFactory.Add("explode", new SpriteSheetAnimationData(new []{0,1,2,3,4,5,6,7,8}, isLooping: false, frameDuration: 1f / 32f)); + var entity = World.CreateEntity(); + var animatedSprite = new AnimatedSprite(animationFactory); + entity.Attach(animatedSprite); + entity.Attach(new Transform2(position)); + entity.Attach(new Body {Velocity = velocity}); + animatedSprite.Play("explode", () => entity.Destroy()); + } } } \ No newline at end of file diff --git a/Source/Demos/JamGame/GameMain.cs b/Source/Demos/JamGame/GameMain.cs index aac501ec..566ca580 100644 --- a/Source/Demos/JamGame/GameMain.cs +++ b/Source/Demos/JamGame/GameMain.cs @@ -2,6 +2,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; +using MonoGame.Extended; using MonoGame.Extended.BitmapFonts; using MonoGame.Extended.Entities; @@ -44,15 +45,20 @@ namespace JamGame .AddSystem(new PlayerControlSystem(this, entityFactory)) .AddSystem(new BodyMovementSystem()) .AddSystem(new CollisionSystem()) - .AddSystem(new CollisionResponseSystem()) + .AddSystem(new CollisionResponseSystem(entityFactory)) .Build(); entityFactory.World = _world; entityFactory.SpawnPlayer(100, 100); - entityFactory.SpawnZombie(200, 80); - entityFactory.SpawnSkeleton(200, 100); - entityFactory.SpawnPurpleThing(200, 120); + var random = new FastRandom(); + + for (var i = 0; i < 20; i++) + { + entityFactory.SpawnZombie(random.Next(150, 800), random.Next(0, 480)); + //entityFactory.SpawnSkeleton(random.Next(150, 250), random.Next(110, 250)); + //entityFactory.SpawnPurpleThing(random.Next(150, 250), random.Next(110, 250)); + } } protected override void UnloadContent() diff --git a/Source/Demos/JamGame/Systems/CollisionResponseSystem.cs b/Source/Demos/JamGame/Systems/CollisionResponseSystem.cs index ffdf5f04..37abcab8 100644 --- a/Source/Demos/JamGame/Systems/CollisionResponseSystem.cs +++ b/Source/Demos/JamGame/Systems/CollisionResponseSystem.cs @@ -1,5 +1,6 @@ using JamGame.Components; using Microsoft.Xna.Framework; +using MonoGame.Extended; using MonoGame.Extended.Entities; using MonoGame.Extended.Entities.Systems; @@ -7,11 +8,13 @@ namespace JamGame.Systems { public class CollisionResponseSystem : EntityProcessingSystem { + private readonly EntityFactory _entityFactory; private ComponentMapper _bodyMapper; - public CollisionResponseSystem() - : base(Aspect.All(typeof(Body))) + public CollisionResponseSystem(EntityFactory entityFactory) + : base(Aspect.All(typeof(Body), typeof(Transform2))) { + _entityFactory = entityFactory; } public override void Initialize(IComponentMapperService mapperService) @@ -28,10 +31,17 @@ namespace JamGame.Systems var entity = GetEntity(entityId); if (entity.Has()) + { + _entityFactory.SpawnExplosion(entity.Get().Position, body.Velocity / 2f); entity.Destroy(); + } + if (entity.Has()) + { + _entityFactory.SpawnExplosion(entity.Get().Position, body.Velocity / 2f); entity.Destroy(); + } } } } diff --git a/Source/Demos/JamGame/Systems/PlayerControlSystem.cs b/Source/Demos/JamGame/Systems/PlayerControlSystem.cs index fb3ba8e9..7d6b8f71 100644 --- a/Source/Demos/JamGame/Systems/PlayerControlSystem.cs +++ b/Source/Demos/JamGame/Systems/PlayerControlSystem.cs @@ -63,14 +63,17 @@ namespace JamGame.Systems if (_previousKeyboardState.IsKeyUp(Keys.Space) && _mainGame.KeyboardState.IsKeyDown(Keys.Space)) _spawningFireball = _entityFactory.SpawnFireball(transform.Position.X + 16, transform.Position.Y); - if (_spawningFireball != null && _previousKeyboardState.IsKeyDown(Keys.Space) && _mainGame.KeyboardState.IsKeyUp(Keys.Space)) + // Kinda hacky but it works for now. + var spawningFireballBody = _spawningFireball?.Get(); + if (spawningFireballBody != null && _previousKeyboardState.IsKeyDown(Keys.Space) && _mainGame.KeyboardState.IsKeyUp(Keys.Space)) { - _spawningFireball.Get().Velocity = new Vector2(200, 0); + spawningFireballBody.Velocity = new Vector2(200, 0); _spawningFireball = null; } - if(_spawningFireball != null) - _spawningFireball.Get().Position = new Vector2(transform.Position.X + 16, transform.Position.Y); + var spawningFireballTransform = _spawningFireball?.Get(); + if(spawningFireballTransform != null) + spawningFireballTransform.Position = new Vector2(transform.Position.X + 16, transform.Position.Y); } _previousKeyboardState = _mainGame.KeyboardState; diff --git a/Source/Demos/JamGame/Systems/SpriteRenderingSystem.cs b/Source/Demos/JamGame/Systems/SpriteRenderingSystem.cs index 121090cf..9c30c706 100644 --- a/Source/Demos/JamGame/Systems/SpriteRenderingSystem.cs +++ b/Source/Demos/JamGame/Systems/SpriteRenderingSystem.cs @@ -1,6 +1,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MonoGame.Extended; +using MonoGame.Extended.Animations; using MonoGame.Extended.Entities; using MonoGame.Extended.Entities.Systems; using MonoGame.Extended.Sprites; @@ -13,9 +14,10 @@ namespace JamGame.Systems private ComponentMapper _transformMapper; private ComponentMapper _spriteMapper; + private ComponentMapper _animatedSpriteMapper; public SpriteRenderingSystem(GraphicsDevice graphicsDevice, Texture2D texture) - : base(Aspect.All(typeof(Transform2), typeof(Sprite))) + : base(Aspect.All(typeof(Transform2)).One(typeof(Sprite), typeof(AnimatedSprite))) { _spriteBatch = new SpriteBatch(graphicsDevice); } @@ -24,6 +26,7 @@ namespace JamGame.Systems { _transformMapper = mapperService.GetMapper(); _spriteMapper = mapperService.GetMapper(); + _animatedSpriteMapper = mapperService.GetMapper(); } public override void Draw(GameTime gameTime) @@ -33,9 +36,12 @@ namespace JamGame.Systems foreach (var entity in ActiveEntities) { var transform = _transformMapper.Get(entity); + var animatedSprite = _animatedSpriteMapper.Get(entity); var sprite = _spriteMapper.Get(entity); - _spriteBatch.Draw(sprite, transform); + animatedSprite?.Update(gameTime); + + _spriteBatch.Draw(sprite ?? animatedSprite, transform); } _spriteBatch.End(); diff --git a/Source/MonoGame.Extended.Animations/SpriteSheets/SpriteSheetAnimationFactory.cs b/Source/MonoGame.Extended.Animations/SpriteSheets/SpriteSheetAnimationFactory.cs index 9615f4df..8c28223c 100644 --- a/Source/MonoGame.Extended.Animations/SpriteSheets/SpriteSheetAnimationFactory.cs +++ b/Source/MonoGame.Extended.Animations/SpriteSheets/SpriteSheetAnimationFactory.cs @@ -34,9 +34,7 @@ namespace MonoGame.Extended.Animations.SpriteSheets public SpriteSheetAnimation Create(string name) { - SpriteSheetAnimationData data; - - if (_animationDataDictionary.TryGetValue(name, out data)) + if (_animationDataDictionary.TryGetValue(name, out var data)) { var keyFrames = data.FrameIndicies .Select(i => Frames[i]) diff --git a/Source/MonoGame.Extended/Collections/Bag.cs b/Source/MonoGame.Extended/Collections/Bag.cs index e2884486..cfa59689 100644 --- a/Source/MonoGame.Extended/Collections/Bag.cs +++ b/Source/MonoGame.Extended/Collections/Bag.cs @@ -57,7 +57,7 @@ namespace MonoGame.Extended.Collections public T this[int index] { - get => _items[index]; + get => index >= _items.Length ? default(T) : _items[index]; set { EnsureCapacity(index + 1);