From defeac7b977fa16bddfef0f77449908df9781a24 Mon Sep 17 00:00:00 2001 From: Dylan Wilson Date: Mon, 30 Jul 2018 23:06:00 +1000 Subject: [PATCH] adhoc map generation --- Source/Demos/JamGame/EntityFactory.cs | 4 +- .../Systems/CollisionResponseSystem.cs | 9 +--- .../JamGame/Systems/MapRenderingSystem.cs | 45 +++++++++++++++++-- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/Source/Demos/JamGame/EntityFactory.cs b/Source/Demos/JamGame/EntityFactory.cs index 88162a00..d31b6261 100644 --- a/Source/Demos/JamGame/EntityFactory.cs +++ b/Source/Demos/JamGame/EntityFactory.cs @@ -40,7 +40,7 @@ namespace JamGame var entity = World.CreateEntity(); entity.Attach(new Sprite(_tileset.GetTile(165))); 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(-4, 0) }); entity.Attach(new Enemy()); } @@ -58,7 +58,7 @@ namespace JamGame 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 { Size = new Size2(8, 8) }); + entity.Attach(new Body { Size = new Size2(16, 16) }); entity.Attach(new Projectile()); return entity; } diff --git a/Source/Demos/JamGame/Systems/CollisionResponseSystem.cs b/Source/Demos/JamGame/Systems/CollisionResponseSystem.cs index 82277564..ffdf5f04 100644 --- a/Source/Demos/JamGame/Systems/CollisionResponseSystem.cs +++ b/Source/Demos/JamGame/Systems/CollisionResponseSystem.cs @@ -1,5 +1,4 @@ -using System.Diagnostics; -using JamGame.Components; +using JamGame.Components; using Microsoft.Xna.Framework; using MonoGame.Extended.Entities; using MonoGame.Extended.Entities.Systems; @@ -28,12 +27,6 @@ namespace JamGame.Systems { var entity = GetEntity(entityId); - var hasProjectile = entity.Has(); - var hasEnemy = entity.Has(); - - if (hasProjectile && hasEnemy) - Debug.Fail("Wtf"); - if (entity.Has()) entity.Destroy(); diff --git a/Source/Demos/JamGame/Systems/MapRenderingSystem.cs b/Source/Demos/JamGame/Systems/MapRenderingSystem.cs index b929c63b..f323cacf 100644 --- a/Source/Demos/JamGame/Systems/MapRenderingSystem.cs +++ b/Source/Demos/JamGame/Systems/MapRenderingSystem.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended; using MonoGame.Extended.Entities.Systems; using MonoGame.Extended.TextureAtlases; @@ -10,12 +11,40 @@ namespace JamGame.Systems private readonly GraphicsDevice _graphicsDevice; private readonly Tileset _tileset; private readonly SpriteBatch _spriteBatch; + private readonly FastRandom _random = new FastRandom(); + + private const int _mapWidth = 30; + private const int _mapHeight = 30; + private readonly int[,] _mapData = new int[_mapWidth, _mapHeight]; public MapRenderingSystem(GraphicsDevice graphicsDevice, Tileset tileset) { _graphicsDevice = graphicsDevice; _tileset = tileset; _spriteBatch = new SpriteBatch(graphicsDevice); + + for (var x = 0; x < _mapWidth; x++) + { + for (var y = 0; y < _mapHeight; y++) + _mapData[x, y] = GetTileIndexAt(x, y); + } + } + + private int GetTileIndexAt(int x, int y) + { + if (x < 3 || x > 19) + return -1; + + if (y < 2 || y > 19) + return -1; + + switch (y) + { + case 2: return x == 3 ? 0 : (x == 19 ? 1 : 2); + case 3: return x == 3 ? 16 : (x == 19 ? 18 : 17); + case 4: return _random.Next(32, 34); + default: return 50; + } } public override void Draw(GameTime gameTime) @@ -23,10 +52,20 @@ namespace JamGame.Systems _graphicsDevice.Clear(Color.DarkBlue * 0.2f); _spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: Matrix.CreateScale(2)); - for (var x = 3; x < 20; x++) + for (var x = 0; x < _mapWidth; x++) { - for(var y = 3; y < 20; y++) - _spriteBatch.Draw(_tileset.GetTile(50), new Vector2(x * _tileset.TileWidth, y * _tileset.TileHeight), Color.White); + for (var y = 0; y < _mapHeight; y++) + { + var tileIndex = _mapData[x, y]; + + if (tileIndex >= 0) + { + var tile = _tileset.GetTile(tileIndex); + var position = new Vector2(x * _tileset.TileWidth, y * _tileset.TileHeight); + + _spriteBatch.Draw(tile, position, Color.White); + } + } } _spriteBatch.End();