コード例 #1
0
ファイル: MapEditorScreen.cs プロジェクト: EricSnow/tbs
        public MapEditorScreen(string map)
        {
            TransitionOnTime  = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);

            UnitCreator.Initialize();

            _nullCursor = new Vector2(-1, -1);
            _terrains   = new[]
            {
                new Terrain("Plains", false, 1, 1, 1, 2, 1, 1, 1, -1, -1),
                new Terrain("Road", false, 0, 1, 1, 1, 1, 1, 1, -1, -1),
                new Terrain("Wood", true, 3, 1, 1, 3, 3, 2, 1, -1, -1),
                new Terrain("Mountain", false, 4, 2, 1, -1, -1, -1, 1, -1, -1),
                new Terrain("Wasteland", false, 2, 1, 1, 3, 3, 2, 1, -1, -1),
                new Terrain("Ruins", true, 1, 1, 1, 2, 1, 1, 1, -1, -1),
                new Terrain("Sea", false, 0, -1, -1, -1, -1, -1, 1, 1, 1),
                new Terrain("BridgeSea", false, 0, 1, 1, 1, 1, 1, 1, 1, 1),
                new Terrain("BridgeRiver", false, 0, 1, 1, 1, 1, 1, 1, 1, 1),
                new Terrain("River", false, 0, 2, 1, -1, -1, -1, 1, -1, -1),
                new Terrain("Beach", false, 0, 1, 1, 2, 2, 1, 1, -1, 1),
                new Terrain("Rough Sea", false, 0, -1, -1, -1, -1, -1, 1, 2, 2),
                new Terrain("Mist", true, 0, -1, -1, -1, -1, -1, 1, 1, 1),
                new Terrain("Reef", true, 0, -1, -1, -1, -1, -1, 1, 2, 2)
            };

            // Load map data from TXT file
            var flines = File.ReadLines("Content/Maps/" + map + ".txt")
                         .Where(l => l.Length != 0 && !l.StartsWith("#"))
                         .ToList();

            //_mapName = flines[0].Trim();
            _mapWidth  = Convert.ToInt32(flines[1].Trim());
            _mapHeight = Convert.ToInt32(flines[2].Trim());
            var plyers = flines.GetRange(3, 4).Where(l => !l.StartsWith("0")).ToArray();

            _players = new Player[plyers.Length];
            for (var i = 0; i < _players.Length; ++i)
            {
                _players[i] = new Player(i + 1, Convert.ToInt32(plyers[i].Trim()));
            }
            var terrainLines = new string[_mapHeight];
            var read         = 0;

            for (var i = 7; i < _mapHeight + 7; ++i)
            {
                var line = flines[i].Trim();
                terrainLines[read++] = line;
            }
            var buildings = flines;

            buildings.RemoveRange(0, _mapHeight + 7);

            // Generate terrain according to map data
            _mapTerrains = new Terrain[_mapHeight, _mapWidth];
            for (var y = 0; y < _mapHeight; ++y)
            {
                for (var x = 0; x < _mapWidth; ++x)
                {
                    var type = terrainLines[y][x] >= '0' && terrainLines[y][x] <= '9'
                                                ? terrainLines[y][x] - '0'
                                                : terrainLines[y][x] - 'a' + 10;
                    _mapTerrains[y, x] = _terrains[type];
                }
            }

            var bOrder = new[] { "Headquarter", "City", "Factory", "Port", "Airport" };

            _mapBuildings = new Building[_mapHeight, _mapWidth];
            _units        = new List <Unit>();
            foreach (var data in buildings.Select(b => b.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)))
            {
                var p = Convert.ToInt32(data[2]);
                if (data.Length == 5)
                {
                    var u = UnitCreator.Unit(
                        data[3],
                        _players[p - 1],
                        new Vector2(
                            Convert.ToInt32(data[1]),
                            Convert.ToInt32(data[0])));
                    u.Life = Convert.ToInt32(data[4]) * 10;
                    _units.Add(u);
                }
                else if (data.Length == 4)
                {
                    var x = Convert.ToInt32(data[1]);
                    var y = Convert.ToInt32(data[0]);
                    _mapBuildings[y, x] = new Building(bOrder[Convert.ToInt32(data[3])], p == 0 ? null : _players[p - 1], new Vector2(x, y));
                }
            }

            _camera = new Vector2(-50, -80);
        }