using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MLEM.Cameras; using MLEM.Extended.Extensions; using MLEM.Extended.Tiled; using MLEM.Extensions; using MLEM.Misc; using MLEM.Startup; using MLEM.Textures; using MonoGame.Extended; using MonoGame.Extended.Tiled; namespace GreatSpringGameJam { public class Map { private static readonly UniformTextureAtlas BackgroundTexture = new(MlemGame.LoadContent("Textures/Backgrounds"), 8, 8); public Point SizeInPixels => new(this.map.WidthInPixels, this.map.HeightInPixels); public Point SpawnPoint => new(this.map.Properties.GetInt("StartX"), this.map.Properties.GetInt("StartY")); public Point Size => new(this.map.Width, this.map.Height); public Vector2 TileSize => this.map.GetTileSize(); public readonly TiledMapCollisions Collisions; public readonly int TotalSnow; public readonly int TotalSeeds; public readonly int TotalGnomes; private readonly TiledMap map; private readonly IndividualTiledMapRenderer renderer; private readonly List entities = new(); public Map(TiledMap map) { this.map = map; this.renderer = new IndividualTiledMapRenderer(this.map); this.Collisions = new TiledMapCollisions(this.map); this.TotalSnow = this.GetTotalTiles("Snow"); this.TotalSeeds = this.GetTotalTiles("Seed"); foreach (var (pos, _) in this.EnumerateTiles("Gnome")) { this.entities.Add(new Gnome(this, new Vector2(pos.X, pos.Y))); this.SetTile(pos.Layer, pos.X, pos.Y, 0); } this.TotalGnomes = this.entities.OfType().Count(); } public void AddEntity(Entity entity) { this.entities.Add(entity); } public void RemoveEntity(Entity entity) { this.entities.Remove(entity); } public IEnumerable GetEntities() where T : Entity { return this.entities.OfType(); } public void Update(GameTime time) { this.renderer.UpdateAnimations(time); for (var i = this.entities.Count - 1; i >= 0; i--) this.entities[i].Update(time); } public TiledMapTile GetTile(string layer, int x, int y) { return this.map.GetTile(layer, x, y); } public void SetTile(string layer, int x, int y, int globalId) { var index = this.map.GetTileLayerIndex(layer); this.map.SetTile(layer, x, y, globalId); this.renderer.UpdateDrawInfo(index, x, y); this.Collisions.UpdateCollisionInfo(index, x, y); } public void SetTile(string layer, int x, int y, TiledMapTileset tileset, TiledMapTilesetTile tile) { this.SetTile(layer, x, y, tile.GetGlobalIdentifier(tileset, this.map)); } public TiledMapTilesetTile GetTilesetTile(TiledMapTile tile, TiledMapTileset tileset) { return tileset.GetTilesetTile(tile, this.map); } public TiledMapTileset GetTileset(TiledMapTile tile) { return tile.GetTileset(this.map); } public void Draw(GameTime time, SpriteBatch batch, Camera camera) { batch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, transformMatrix: camera.ViewMatrix); // render background const float parallaxFactor = 0.5F; var parallax = camera.Position * parallaxFactor; var (parW, parH) = this.SizeInPixels.ToVector2() * (1 - parallaxFactor) + camera.ScaledViewport * parallaxFactor; var mountains = BackgroundTexture[0, 0, 8, 4]; for (var x = 0; x < parW; x += mountains.Width * 2) { batch.Draw(mountains, parallax + new Vector2(x, parH - mountains.Height * 2), Color.White * 0.5F, 0, Vector2.Zero, 2, SpriteEffects.None, 0); } // render map etc. this.DrawLayer(batch, "Background", camera); this.DrawLayer(batch, "Ground", camera); foreach (var entity in this.entities) entity.Draw(time, batch); this.DrawLayer(batch, "Foreground", camera); this.DrawLayer(batch, "Snow", camera); batch.End(); } public int GetTotalTiles(string type) { return this.EnumerateTiles(type).Count(); } public IEnumerable<(LayerPosition, TiledMapTilesetTile)> EnumerateTiles(string type) { for (var x = 0; x < this.map.Width; x++) { for (var y = 0; y < this.map.Height; y++) { foreach (var layer in this.map.TileLayers) { var tile = layer.GetTile(x, y); if (tile.IsBlank) continue; var tilesetTile = this.GetTilesetTile(tile, this.GetTileset(tile)); if (tilesetTile.Properties.GetBool(type)) yield return (new LayerPosition(layer.Name, x, y), tilesetTile); } } } } private void DrawLayer(SpriteBatch batch, string layer, Camera camera) { var id = this.map.GetTileLayerIndex(layer); if (id >= 0) this.renderer.DrawLayer(batch, id, camera.GetVisibleRectangle().ToExtended()); } } }