GreatSpringGameJam/GreatSpringGameJam/Map.cs

108 lines
4.3 KiB
C#
Raw Normal View History

2021-03-07 22:23:51 +01:00
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Cameras;
using MLEM.Extended.Extensions;
using MLEM.Extended.Tiled;
2021-03-09 02:37:44 +01:00
using MLEM.Extensions;
2021-03-07 22:23:51 +01:00
using MLEM.Misc;
using MLEM.Startup;
2021-03-09 02:37:44 +01:00
using MLEM.Textures;
using MonoGame.Extended;
2021-03-07 22:23:51 +01:00
using MonoGame.Extended.Tiled;
namespace GreatSpringGameJam {
public class Map {
2021-03-09 02:37:44 +01:00
private static readonly UniformTextureAtlas BackgroundTexture = new(MlemGame.LoadContent<Texture2D>("Textures/Backgrounds"), 8, 8);
2021-03-07 22:23:51 +01:00
public Point SizeInPixels => new(this.map.WidthInPixels, this.map.HeightInPixels);
2021-03-09 02:37:44 +01:00
public Point SpawnPoint => new(this.map.Properties.GetInt("StartX"), this.map.Properties.GetInt("StartY"));
2021-03-09 18:29:07 +01:00
public Point Size => new(this.map.Width, this.map.Height);
2021-03-07 22:23:51 +01:00
public Vector2 TileSize => this.map.GetTileSize();
public readonly TiledMapCollisions Collisions;
2021-03-08 19:08:41 +01:00
public readonly int TotalSnow;
public readonly int TotalSeeds;
2021-03-07 22:23:51 +01:00
private readonly TiledMap map;
private readonly IndividualTiledMapRenderer renderer;
private readonly List<Entity> entities = new();
2021-03-09 18:29:07 +01:00
public Map(TiledMap map) {
this.map = map;
2021-03-07 22:23:51 +01:00
this.renderer = new IndividualTiledMapRenderer(this.map);
this.Collisions = new TiledMapCollisions(this.map);
2021-03-08 19:08:41 +01:00
this.TotalSnow = this.GetTotalTiles("Snow");
this.TotalSeeds = this.GetTotalTiles("Seed");
2021-03-07 22:23:51 +01:00
}
public void AddEntity(Entity entity) {
this.entities.Add(entity);
}
2021-03-08 02:37:40 +01:00
public void RemoveEntity(Entity entity) {
this.entities.Remove(entity);
}
2021-03-07 22:23:51 +01:00
public void Update(GameTime time) {
this.renderer.UpdateAnimations(time);
2021-03-08 02:37:40 +01:00
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 globalTile) {
var index = this.map.GetTileLayerIndex(layer);
this.map.SetTile(layer, x, y, globalTile);
this.renderer.UpdateDrawInfo(index, x, y);
this.Collisions.UpdateCollisionInfo(index, x, y);
}
public TiledMapTilesetTile GetTilesetTile(TiledMapTile tile) {
return tile.GetTilesetTile(this.map);
2021-03-07 22:23:51 +01:00
}
public void Draw(GameTime time, SpriteBatch batch, Camera camera) {
batch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, transformMatrix: camera.ViewMatrix);
2021-03-09 02:37:44 +01:00
// 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.
2021-03-08 02:37:40 +01:00
this.DrawLayer(batch, "Ground", camera);
2021-03-07 22:23:51 +01:00
foreach (var entity in this.entities)
entity.Draw(time, batch);
2021-03-08 19:08:41 +01:00
this.DrawLayer(batch, "Foreground", camera);
2021-03-08 02:37:40 +01:00
this.DrawLayer(batch, "Snow", camera);
2021-03-07 22:23:51 +01:00
batch.End();
}
2021-03-08 19:08:41 +01:00
public int GetTotalTiles(string type) {
var total = 0;
for (var x = 0; x < this.map.Width; x++) {
for (var y = 0; y < this.map.Height; y++) {
foreach (var tile in this.map.GetTiles(x, y)) {
if (tile.IsBlank)
continue;
var tilesetTile = this.GetTilesetTile(tile);
if (tilesetTile.Properties.GetBool(type))
total++;
}
}
}
return total;
}
2021-03-08 02:37:40 +01:00
private void DrawLayer(SpriteBatch batch, string layer, Camera camera) {
this.renderer.DrawLayer(batch, this.map.GetTileLayerIndex(layer), camera.GetVisibleRectangle().ToExtended());
}
2021-03-07 22:23:51 +01:00
}
}