Add project files.
This commit is contained in:
61
FreedomFighter/GameMap.cs
Normal file
61
FreedomFighter/GameMap.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace FreedomFighter;
|
||||
|
||||
/// <summary>
|
||||
/// Port of TOmegaMap, loading the JSON files converted from the .omap format.
|
||||
/// Only Layer1 pattern indices and triggers are used by the game.
|
||||
/// </summary>
|
||||
public class GameMap
|
||||
{
|
||||
public int WidthCount;
|
||||
public int HeightCount;
|
||||
public int TileWidth = 32;
|
||||
public int TileHeight = 32;
|
||||
public int[][] Layer1 = []; // [x][y] -> tileset pattern index, -1 = empty
|
||||
public string?[,] TriggerActions = new string?[0, 0];
|
||||
public OmegaImage? Tileset;
|
||||
|
||||
readonly GraphicsDevice _gd;
|
||||
readonly string _contentDir;
|
||||
readonly Dictionary<string, OmegaImage> _tilesetCache = new();
|
||||
|
||||
public GameMap(GraphicsDevice gd, string contentDir)
|
||||
{
|
||||
_gd = gd;
|
||||
_contentDir = contentDir;
|
||||
}
|
||||
|
||||
record TriggerJson(int x, int y, string name, string action);
|
||||
record MapJson(int widthCount, int heightCount, string oil, int tileWidth, int tileHeight,
|
||||
int[][] layer1, TriggerJson[] triggers);
|
||||
|
||||
public void LoadMap(string mapName)
|
||||
{
|
||||
string path = Path.Combine(_contentDir, "maps", Path.GetFileNameWithoutExtension(mapName) + ".json");
|
||||
var m = JsonSerializer.Deserialize<MapJson>(File.ReadAllText(path))!;
|
||||
|
||||
WidthCount = m.widthCount;
|
||||
HeightCount = m.heightCount;
|
||||
TileWidth = m.tileWidth;
|
||||
TileHeight = m.tileHeight;
|
||||
Layer1 = m.layer1;
|
||||
|
||||
TriggerActions = new string?[WidthCount, HeightCount];
|
||||
foreach (var t in m.triggers)
|
||||
TriggerActions[t.x, t.y] = t.action;
|
||||
|
||||
string tilesetFile = Path.GetFileNameWithoutExtension(m.oil).ToLowerInvariant() + "_00.png";
|
||||
if (!_tilesetCache.TryGetValue(tilesetFile, out var tileset))
|
||||
{
|
||||
tileset = OmegaImage.Load(_gd, Path.Combine(_contentDir, "tilesets", tilesetFile), TileWidth, TileHeight, m.oil);
|
||||
_tilesetCache[tilesetFile] = tileset;
|
||||
}
|
||||
Tileset = tileset;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user