diff --git a/Source/Demos/JamGame/Components/Body.cs b/Source/Demos/JamGame/Components/Body.cs new file mode 100644 index 00000000..8cc1d511 --- /dev/null +++ b/Source/Demos/JamGame/Components/Body.cs @@ -0,0 +1,9 @@ +using Microsoft.Xna.Framework; + +namespace JamGame.Components +{ + public class Body + { + public Vector2 Velocity { get; set; } + } +} \ No newline at end of file diff --git a/Source/Demos/JamGame/Components/Player.cs b/Source/Demos/JamGame/Components/Player.cs index afc78530..84d2de8d 100644 --- a/Source/Demos/JamGame/Components/Player.cs +++ b/Source/Demos/JamGame/Components/Player.cs @@ -1,12 +1,6 @@ namespace JamGame.Components { - public enum Facing - { - Left, Right - } - public class Player { - private Facing Facing { get; set; } = Facing.Right; } } \ No newline at end of file diff --git a/Source/Demos/JamGame/Content/0x72_16x16DungeonTileset.v4.png b/Source/Demos/JamGame/Content/0x72_16x16DungeonTileset.v4.png index d3061e26..ea123ef5 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/Content/fireball.aseprite b/Source/Demos/JamGame/Content/fireball.aseprite new file mode 100644 index 00000000..e19c47d5 Binary files /dev/null and b/Source/Demos/JamGame/Content/fireball.aseprite differ diff --git a/Source/Demos/JamGame/Content/fireball.json b/Source/Demos/JamGame/Content/fireball.json new file mode 100644 index 00000000..f6e66d36 --- /dev/null +++ b/Source/Demos/JamGame/Content/fireball.json @@ -0,0 +1,47 @@ +{ "frames": [ + { + "filename": "fireball 0.aseprite", + "frame": { "x": 0, "y": 0, "w": 16, "h": 16 }, + "rotated": false, + "trimmed": false, + "spriteSourceSize": { "x": 0, "y": 0, "w": 16, "h": 16 }, + "sourceSize": { "w": 16, "h": 16 }, + "duration": 100 + }, + { + "filename": "fireball 1.aseprite", + "frame": { "x": 16, "y": 0, "w": 16, "h": 16 }, + "rotated": false, + "trimmed": false, + "spriteSourceSize": { "x": 0, "y": 0, "w": 16, "h": 16 }, + "sourceSize": { "w": 16, "h": 16 }, + "duration": 100 + }, + { + "filename": "fireball 2.aseprite", + "frame": { "x": 32, "y": 0, "w": 16, "h": 16 }, + "rotated": false, + "trimmed": false, + "spriteSourceSize": { "x": 0, "y": 0, "w": 16, "h": 16 }, + "sourceSize": { "w": 16, "h": 16 }, + "duration": 100 + }, + { + "filename": "fireball 3.aseprite", + "frame": { "x": 48, "y": 0, "w": 16, "h": 16 }, + "rotated": false, + "trimmed": false, + "spriteSourceSize": { "x": 0, "y": 0, "w": 16, "h": 16 }, + "sourceSize": { "w": 16, "h": 16 }, + "duration": 100 + } + ], + "meta": { + "app": "http://www.aseprite.org/", + "version": "1.2.9-x64", + "image": "D:\\Github\\MonoGame.Extended\\Source\\Demos\\JamGame\\Content\\fireball.png", + "format": "I8", + "size": { "w": 64, "h": 16 }, + "scale": "1" + } +} diff --git a/Source/Demos/JamGame/Content/fireball.png b/Source/Demos/JamGame/Content/fireball.png new file mode 100644 index 00000000..3b91d460 Binary files /dev/null and b/Source/Demos/JamGame/Content/fireball.png differ diff --git a/Source/Demos/JamGame/EntityFactory.cs b/Source/Demos/JamGame/EntityFactory.cs new file mode 100644 index 00000000..0c19c7b1 --- /dev/null +++ b/Source/Demos/JamGame/EntityFactory.cs @@ -0,0 +1,58 @@ +using JamGame.Components; +using Microsoft.Xna.Framework; +using MonoGame.Extended; +using MonoGame.Extended.Entities; +using MonoGame.Extended.Sprites; + +namespace JamGame +{ + public class EntityFactory + { + private readonly Tileset _tileset; + + public EntityFactory(Tileset tileset) + { + _tileset = tileset; + } + + public World World { get; set; } + + public void SpawnPlayer(float x, float y) + { + var entity = World.CreateEntity(); + entity.Attach(new Sprite(_tileset.GetTile(232))); + entity.Attach(new Transform2(x, y)); + entity.Attach(new Player()); + } + + public void SpawnSkeleton(float x, float y) + { + var entity = World.CreateEntity(); + entity.Attach(new Sprite(_tileset.GetTile(145))); + entity.Attach(new Transform2(x, y)); + } + + public void SpawnZombie(float x, float y) + { + var entity = World.CreateEntity(); + entity.Attach(new Sprite(_tileset.GetTile(165))); + entity.Attach(new Transform2(x, y)); + } + + public void SpawnPurpleThing(float x, float y) + { + var entity = World.CreateEntity(); + entity.Attach(new Sprite(_tileset.GetTile(213))); + entity.Attach(new Transform2(x, y)); + } + + public Entity SpawnFireball(float x, float y) + { + var entity = World.CreateEntity(); + entity.Attach(new Sprite(_tileset.GetTile(240))); + entity.Attach(new Transform2(x, y) { Scale = Vector2.One * 0.75f }); + entity.Attach(new Body()); + return entity; + } + } +} \ No newline at end of file diff --git a/Source/Demos/JamGame/GameMain.cs b/Source/Demos/JamGame/GameMain.cs index 2e69c4ca..c0253a7b 100644 --- a/Source/Demos/JamGame/GameMain.cs +++ b/Source/Demos/JamGame/GameMain.cs @@ -1,12 +1,9 @@ -using JamGame.Components; -using JamGame.Systems; +using JamGame.Systems; 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; -using MonoGame.Extended.Sprites; namespace JamGame { @@ -37,20 +34,23 @@ namespace JamGame var font = Content.Load("Sensation"); var texture = Content.Load("0x72_16x16DungeonTileset.v4"); var tileset = new Tileset(texture, 16, 16); + var entityFactory = new EntityFactory(tileset); _spriteBatch = new SpriteBatch(GraphicsDevice); _world = new WorldBuilder() .AddSystem(new MapRenderingSystem(GraphicsDevice, tileset)) .AddSystem(new SpriteRenderingSystem(GraphicsDevice, texture)) .AddSystem(new HudSystem(this, GraphicsDevice, font, tileset)) - .AddSystem(new PlayerMovementSystem(this)) + .AddSystem(new PlayerControlSystem(this, entityFactory)) + .AddSystem(new BodyMovementSystem()) .Build(); - var player = _world.CreateEntity(); - player.Attach(new Sprite(tileset.GetTile(232))); - player.Attach(new Transform2(100, 100)); - player.Attach(new Player()); + entityFactory.World = _world; + entityFactory.SpawnPlayer(100, 100); + entityFactory.SpawnZombie(200, 80); + entityFactory.SpawnSkeleton(200, 100); + entityFactory.SpawnPurpleThing(200, 120); } protected override void UnloadContent() diff --git a/Source/Demos/JamGame/README.md b/Source/Demos/JamGame/README.md new file mode 100644 index 00000000..5cd75402 --- /dev/null +++ b/Source/Demos/JamGame/README.md @@ -0,0 +1,5 @@ + +## Assets Used + + - [16x16 Dungeon Tileset](https://0x72.itch.io/16x16-dungeon-tileset) + - [Kyrise's Free 16x16 RPG Icon Pack](https://kyrise.itch.io/kyrises-free-16x16-rpg-icon-pack) diff --git a/Source/Demos/JamGame/Systems/BodyMovementSystem.cs b/Source/Demos/JamGame/Systems/BodyMovementSystem.cs new file mode 100644 index 00000000..9d6fefc0 --- /dev/null +++ b/Source/Demos/JamGame/Systems/BodyMovementSystem.cs @@ -0,0 +1,38 @@ +using JamGame.Components; +using Microsoft.Xna.Framework; +using MonoGame.Extended; +using MonoGame.Extended.Entities; +using MonoGame.Extended.Entities.Systems; + +namespace JamGame.Systems +{ + public class BodyMovementSystem : EntityUpdateSystem + { + private ComponentMapper _transformMapper; + private ComponentMapper _bodyMapper; + + public BodyMovementSystem() + : base(Aspect.All(typeof(Transform2), typeof(Body))) + { + } + + public override void Initialize(IComponentMapperService mapperService) + { + _transformMapper = mapperService.GetMapper(); + _bodyMapper = mapperService.GetMapper(); + } + + public override void Update(GameTime gameTime) + { + var elapsedSeconds = gameTime.GetElapsedSeconds(); + + foreach (var entity in ActiveEntities) + { + var transform = _transformMapper.Get(entity); + var body = _bodyMapper.Get(entity); + + transform.Position += body.Velocity * elapsedSeconds; + } + } + } +} \ No newline at end of file diff --git a/Source/Demos/JamGame/Systems/PlayerMovementSystem.cs b/Source/Demos/JamGame/Systems/PlayerControlSystem.cs similarity index 64% rename from Source/Demos/JamGame/Systems/PlayerMovementSystem.cs rename to Source/Demos/JamGame/Systems/PlayerControlSystem.cs index 65d34e93..2a7dff03 100644 --- a/Source/Demos/JamGame/Systems/PlayerMovementSystem.cs +++ b/Source/Demos/JamGame/Systems/PlayerControlSystem.cs @@ -9,17 +9,22 @@ using MonoGame.Extended.Sprites; namespace JamGame.Systems { - public class PlayerMovementSystem : EntityUpdateSystem + public class PlayerControlSystem : EntityUpdateSystem { private readonly MainGame _mainGame; + private readonly EntityFactory _entityFactory; private ComponentMapper _transformMapper; private ComponentMapper _spriteMapper; private const int _playerSpeed = 64; + private KeyboardState _previousKeyboardState; - public PlayerMovementSystem(MainGame mainGame) + private Entity _spawningFireball; + + public PlayerControlSystem(MainGame mainGame, EntityFactory entityFactory) : base(Aspect.All(typeof(Player), typeof(Transform2), typeof(Sprite))) { _mainGame = mainGame; + _entityFactory = entityFactory; } public override void Initialize(IComponentMapperService mapperService) @@ -54,7 +59,21 @@ namespace JamGame.Systems if (_mainGame.KeyboardState.IsKeyDown(Keys.Down)) transform.Position += new Vector2(0, _playerSpeed) * elapsedSeconds; + + 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)) + { + _spawningFireball.Get().Velocity = new Vector2(100, 0); + _spawningFireball = null; + } + + if(_spawningFireball != null) + _spawningFireball.Get().Position = new Vector2(transform.Position.X + 16, transform.Position.Y); } + + _previousKeyboardState = _mainGame.KeyboardState; } } } \ No newline at end of file