コード例 #1
0
        public void Init(string levelName, BattleManager battlemanager, GridNavigator gridNavigator)
        {
            this.gridNavigator = gridNavigator;
            this.battlemanager = battlemanager;
            battlemanager.OnPlayerTurnEnded += OnPlayerTurnEnded;

            var levelText = Resources.Load <TextAsset>($"Levels/{levelName}").text;
            var rows      = levelText.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            var width     = int.Parse(rows[0]);
            var height    = int.Parse(rows[1]);

            GridSize = new Vector2Int(width, height);

            LevelData = new LevelData
            {
                Width         = width,
                Height        = height,
                Tiles         = new TileView[width, height],
                Entities      = new List <Entity>(),
                TilesEntities = new Entity[width, height]
            };

            // Ground
            for (int y = 0; y < height; y++)
            {
                var row = rows[3 + y];

                for (int x = 0; x < width; x++)
                {
                    var tile = int.Parse(row[x].ToString()) - 1;
                    InstantiateTile(x, y, tile);
                }
            }

            // Entities
            Sprite entitySprite;

            for (int y = 0; y < height; y++)
            {
                var row = rows[4 + height + y];

                for (int x = 0; x < width; x++)
                {
                    Vector2Int gridPosition = new Vector2Int(x, y);
                    switch (row[x])
                    {
                    case 'e':
                        entitySprite = entitySprites[UnityEngine.Random.Range(0, 5)];
                        InstantiateEntity(gridPosition, entitySprite, EntityType.Character, EntityFaction.Enemy);
                        break;

                    case 'p':
                        entitySprite = entitySprites[UnityEngine.Random.Range(5, 10)];
                        InstantiateEntity(gridPosition, entitySprite, EntityType.Character, EntityFaction.Player);
                        break;

                    case '#':
                        entitySprite = tileSprites[49];
                        InstantiateEntity(gridPosition, entitySprite, EntityType.Obstacle, EntityFaction.Neutral);
                        break;
                    }
                }
            }

            gridNavigator.Init(this);
            CenterCamera(height);
        }