farmlands/oldproject/Assets/Scripts/Storage/MapLoader.cs

44 lines
1.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Model;
using UnityEngine;
namespace Storage
{
public static class MapLoader
{
public static void LoadLayer(Map map, string layerName, string dataCsv)
{
// Process layer attributes
Layer layer = new Layer();
layer.Name = layerName;
// Split into substrings
string[] dataRows = dataCsv.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
for (int y = 0; y < dataRows.Length; y++)
{
string[] dataRowItems = dataRows[y].Trim().Split(',');
// Allocate cells
if (y == 0)
{
layer.Cells = new int[dataRows.Length, dataRowItems.Length];
layer.Width = dataRowItems.Length;
layer.Height = dataRows.Length;
}
// Set cells
for (int x = 0; x < layer.Width; x++)
layer.Cells[y, x] = Int32.Parse(dataRowItems[x]);
}
// Add layer
map.Layers.Add(layer);
}
}
}