Пример #1
0
    //--------------------------------------------------------------------------------------
    // Methods:
    //--------------------------------------------------------------------------------------

    #region void Create(LevelXml)

    /// <summary>
    /// Create the level inside the current scene.
    /// </summary>
    /// <param name="descriptor">The level descriptor.</param>
    public void Create(LevelXml descriptor)
    {
        // Set the loaded flag to false:
        IsLoaded = false;

        // And then try to load the data in the scene:
        try {
            // Set the size of the world:
            Width  = descriptor.World.Width;
            Height = descriptor.World.Height;

            // Check that the size of the world is correct:
            var data = descriptor.World.Terrain;
            if (data.Length != Width * Height)
            {
                throw new Exception("Invalid size inside the level descriptor for the world.");
            }

            // Load all the sprites from the selected tileset:
            var sprites = CoreManager.Instance.LoadGameSprites(descriptor.Tileset);

            // Get the world node inside the scene:
            var worldObject = GameObject.Find(CoreManager.TERRAIN_GOBJ_NAME);

            // This function creates a new tile game object in the scene:
            Func <uint, GameObject> createTile = id => {
                // Create the object and set the parent:
                var victim = new GameObject(CoreManager.TILE_GOBJ_NAME);
                SceneUtil.SetParent(victim, worldObject);
                // Create the sprite renderer component:
                var spriteRenderer = victim.AddComponent <SpriteRenderer>();
                var sprite         = sprites.Where(x => x.name == descriptor.Tileset + "_" + id)
                                     .FirstOrDefault();
                spriteRenderer.sprite = sprite;
                // Return the object created:
                return(victim);
            };

            // Set some data to calculate the position of the tiles:
            int       widthInPixels  = Width * CoreManager.TILE_WIDTH;
            int       heightInPixels = Height * CoreManager.TILE_HEIGHT;
            const int PIVOT_OFFSET   = 8;
            int       startX         = PIVOT_OFFSET - (widthInPixels / 2);
            int       startY         = (heightInPixels / 2) - PIVOT_OFFSET;

            // For each id inside the terrain array of the world inside the descriptor
            // we're going to create a new tile and add it to the array of cells:
            List <CellData> cellsList = new List <CellData>();

            for (int i = 0, k = 0; i < Height; i++)
            {
                for (int j = 0; j < Width; j++, k++)
                {
                    // Create the tile and change its position:
                    var tile = createTile(data[k]);
                    SceneUtil.SetPosition(
                        tile,
                        (startX + CoreManager.TILE_WIDTH * j) / 100.0f,
                        (startY - CoreManager.TILE_HEIGHT * i) / 100.0f,
                        0.0f
                        );
                    // Create the current cell data:
                    cellsList.Add(new CellData(i, j, tile, data[k]));
                }
            }

            cells = cellsList.ToArray();

            // Get the entities node inside the scene:
            var entitiesObject = GameObject.Find(CoreManager.ENTITIES_GOBJ_NAME);

            // Create all the entities inside the world:
            foreach (var item in descriptor.Entities)
            {
                var created = PlayerController.Create(item, entitiesObject);
                if (!created)
                {
                    BoxController.Create(item, entitiesObject, descriptor.Tileset, sprites);
                }
            }

            // Finishing the load level process:
            IsLoaded = true;
        } catch (Exception e) {
            CoreManager.Instance.Log(e);
            Width  = 0;
            Height = 0;
            cells  = null;
        }
    }