Exemple #1
0
    //--------------------------------------------------------------------------------------
    // Methods (Static):
    //--------------------------------------------------------------------------------------

    #region bool Create(EntityXml, GameObject, string, Sprite[])

    /// <summary>
    /// Creates a new box entity.
    /// </summary>
    /// <param name="descriptor">The descriptor of the entity.</param>
    /// <param name="parent">The parent game object.</param>
    /// <param name="tileset">The name of the tileset to use.</param>
    /// <param name="sprites">The loaded sprites to use.</param>
    /// <returns>Returns if the entity have been created or not.</returns>
    public static bool Create(EntityXml descriptor, GameObject parent, string tileset, Sprite[] sprites)
    {
        // Check the type of the descriptor:
        var type = GetType(descriptor);

        if (type == TYPE_INVALID)
        {
            return(false);
        }

        // Check if the entity will be inside the world:
        var level = CoreManager.Instance.Level;
        var cell  = level.GetCell(descriptor.X, descriptor.Y);

        if (cell == null || cell.EntityObject != null)
        {
            return(false);
        }

        // Create the player node inside the scene:
        var victim = new GameObject(CoreManager.BOX_GOBJ_NAME);

        SceneUtil.SetParent(victim, parent);
        SceneUtil.SetPosition(victim, cell.TerrainObject);

        // Find the sprites:
        var normalSpriteName      = GetNormalSpriteName(tileset, type);
        var destinationSpriteName = GetDestinationSpriteName(tileset, type);
        var normalSprite          = sprites.Where(x => x.name == normalSpriteName).FirstOrDefault();
        var destinationSprite     = sprites.Where(x => x.name == destinationSpriteName).FirstOrDefault();

        // Create the sprite renderer component:
        var spriteRenderer = victim.AddComponent <SpriteRenderer>();

        spriteRenderer.sprite = normalSprite;

        // Create the audio source component:
        victim.AddComponent <AudioSource>();

        // Create the player behaviour component:
        var boxController = victim.AddComponent <BoxController>();

        boxController.configure(type, normalSprite, destinationSprite);
        boxController.EnterCell(cell);

        // Everything is alright:
        return(true);
    }
    //--------------------------------------------------------------------------------------
    // Methods (Static):
    //--------------------------------------------------------------------------------------

    #region bool Create(EntityXml, GameObject)

    /// <summary>
    /// Creates a new player entity.
    /// </summary>
    /// <param name="descriptor">The descriptor of the entity.</param>
    /// <param name="parent">The parent game object.</param>
    /// <returns>Returns if the entity have been created or not.</returns>
    public static bool Create(EntityXml descriptor, GameObject parent)
    {
        // Check the type of the descriptor:
        if (descriptor.Type.ToLower() != EntityType.PLAYER)
        {
            return(false);
        }

        // Check if the entity will be inside the world:
        var core  = CoreManager.Instance;
        var level = core.Level;
        var cell  = level.GetCell(descriptor.X, descriptor.Y);

        if (cell == null || cell.EntityObject != null)
        {
            return(false);
        }

        // Load all the sprites from the selected charset:
        var charset = core.CurrentProfile != null ?
                      core.CurrentProfile.Charset :
                      ProfileData.GetRandomCharset();
        var sprites = core.LoadGameSprites(ProfileData.CHARSET_FILE_BASE);

        // Create the player node inside the scene:
        var victim = new GameObject(CoreManager.PLAYER_GOBJ_NAME);

        SceneUtil.SetParent(victim, parent);
        SceneUtil.SetPosition(victim, cell.TerrainObject);

        // Create the sprite renderer component:
        var spriteRenderer = victim.AddComponent <SpriteRenderer>();
        var sprite         = sprites.Where(x => x.name == charset + IDLE_EXT).FirstOrDefault();

        spriteRenderer.sprite = sprite;

        // Create the audio source component:
        victim.AddComponent <AudioSource>();

        // Create the player behaviour component:
        var playerController = victim.AddComponent <PlayerController>();

        playerController.configure(charset, sprites);
        playerController.EnterCell(cell);

        // Everything is alright:
        return(true);
    }
Exemple #3
0
    /// <summary>
    /// Gets the type of box entity.
    /// </summary>
    /// <param name="descriptor">The descriptor of the entity.</param>
    /// <returns>The type of box if valid, otherwise -1.</returns>
    public static int GetType(EntityXml descriptor)
    {
        var type = descriptor.Type.ToLower();

        if (type == EntityType.BOX)
        {
            return(TYPE_BOX);
        }
        else if (type.StartsWith(EntityType.BOX))
        {
            try {
                var boxId = int.Parse(type.Substring(EntityType.BOX.Length));
                return(TYPE_BOX + boxId);
            } catch (Exception) {
            }
        }
        return(-1);
    }
Exemple #4
0
        public static void ExportAll(string directoryName = "../../Help")
        {
            bool?replace = null;

            foreach (var ah in Database.Query <AppendixHelpEntity>())
            {
                string path = Path.Combine(directoryName, ah.Culture.Name, AppendicesDirectory, "{0}.{1}.help".FormatWith(RemoveInvalid(ah.UniqueName), ah.Culture.Name));

                FileTools.CreateParentDirectory(path);

                if (!File.Exists(path) || SafeConsole.Ask(ref replace, "Overwrite {0}?".FormatWith(path)))
                {
                    AppendixXml.ToXDocument(ah).Save(path);
                }
            }

            foreach (var nh in Database.Query <NamespaceHelpEntity>())
            {
                string path = Path.Combine(directoryName, nh.Culture.Name, NamespacesDirectory, "{0}.{1}.help".FormatWith(RemoveInvalid(nh.Name), nh.Culture.Name));

                FileTools.CreateParentDirectory(path);

                if (!File.Exists(path) || SafeConsole.Ask(ref replace, "Overwrite {0}?".FormatWith(path)))
                {
                    NamespaceXml.ToXDocument(nh).Save(path);
                }
            }

            foreach (var eh in Database.Query <EntityHelpEntity>())
            {
                string path = Path.Combine(directoryName, eh.Culture.Name, EntitiesDirectory, "{0}.{1}.help".FormatWith(RemoveInvalid(eh.Type.CleanName), eh.Culture.Name));

                FileTools.CreateParentDirectory(path);

                if (!File.Exists(path) || SafeConsole.Ask(ref replace, "Overwrite {0}?".FormatWith(path)))
                {
                    EntityXml.ToXDocument(eh).Save(path);
                }
            }

            foreach (var qh in Database.Query <QueryHelpEntity>())
            {
                string path = Path.Combine(directoryName, qh.Culture.Name, QueriesDirectory, "{0}.{1}.help".FormatWith(RemoveInvalid(qh.Query.Key), qh.Culture.Name));

                FileTools.CreateParentDirectory(path);

                if (!File.Exists(path) || SafeConsole.Ask(ref replace, "Overwrite {0}?".FormatWith(path)))
                {
                    QueryXml.ToXDocument(qh).Save(path);
                }
            }

            foreach (var qh in Database.Query <OperationHelpEntity>())
            {
                string path = Path.Combine(directoryName, qh.Culture.Name, OperationsDirectory, "{0}.{1}.help".FormatWith(RemoveInvalid(qh.Operation.Key), qh.Culture.Name));

                FileTools.CreateParentDirectory(path);

                if (!File.Exists(path) || SafeConsole.Ask(ref replace, "Overwrite {0}?".FormatWith(path)))
                {
                    OperationXml.ToXDocument(qh).Save(path);
                }
            }
        }
Exemple #5
0
        //--------------------------------------------------------------------------------------
        // Methods:
        //--------------------------------------------------------------------------------------

        /// <summary>
        /// Converts a TMX file to an XML level file.
        /// </summary>
        /// <param name="inputFileName">The input file name.</param>
        /// <param name="outputFileName">The output file name.</param>
        public static void Convert(string inputFileName, string outputFileName)
        {
            Shared.ShowTitle(inputFileName, outputFileName);

            // Get the basic data of the level:
            var level = new LevelXml();
            var map   = XmlFile.LoadXml <Tiled.Map>(inputFileName);

            level.Name        = findProperty(map, "Name", Shared.DEFAULT_NAME);
            level.Description = findProperty(map, "Description", Shared.DEFAULT_DESCRIPTION);
            level.Tileset     = findTileset(map);

            Console.WriteLine("Basic level data converted...");

            // Get the terrain data of the level:
            var layers = map.Items.Select(x => x as Tiled.Layer)
                         .Where(x => x != null).ToArray();
            var terrainLayer = findTerrainLayer(layers);

            level.World         = new WorldXml();
            level.World.Width   = terrainLayer.Width;
            level.World.Height  = terrainLayer.Height;
            level.World.Content = "\n";

            int currentProgress = 0;
            int totalProgress   = level.World.Width * level.World.Height;

            Console.Write("\rTerrain information into game level format... [" +
                          (currentProgress * 100 / totalProgress) + "%]");

            for (int y = 0, k = 0; y < level.World.Height; y++)
            {
                for (int x = 0; x < level.World.Width; x++, k++)
                {
                    var id = terrainLayer.Data.Tiles[k].Gid;
                    if (id > 0)
                    {
                        id--;
                    }
                    level.World.Content += id.ToString("0000");
                    level.World.Content += " ";

                    Console.Write("\rTerrain information into game level format... [" +
                                  (currentProgress * 100 / totalProgress) + "%]");
                }
                level.World.Content += "\n";
            }

            Console.WriteLine("\rTerrain information into game level format... [100%]");

            // Get the entities data of the level:
            var entitiesLayer = findEntitiesLayer(layers);
            var entities      = new List <EntityXml>();

            currentProgress = 0;
            Console.Write("\rEntities information into game level format... [" +
                          (currentProgress * 100 / totalProgress) + "%]");
            for (int y = 0, k = 0; y < entitiesLayer.Height; y++)
            {
                for (int x = 0; x < entitiesLayer.Width; x++, k++)
                {
                    var id = entitiesLayer.Data.Tiles[k].Gid;
                    if (id > 0)
                    {
                        id--;
                        string type = "";
                        if (Shared.TERRAIN_ID_FIRST_BOX <= id && id <= Shared.TERRAIN_ID_LAST_BOX)
                        {
                            var boxId = id - Shared.TERRAIN_ID_FIRST_BOX;
                            type = EntityType.BOX;
                            if (boxId > 0)
                            {
                                type += boxId;
                            }
                        }
                        else if (Shared.TERRAIN_ID_FIRST_DBOX <= id && id <= Shared.TERRAIN_ID_LAST_DBOX)
                        {
                            var boxId = id - Shared.TERRAIN_ID_FIRST_DBOX;
                            type = EntityType.BOX;
                            if (boxId > 0)
                            {
                                type += boxId;
                            }
                        }
                        else if (Shared.TERRAIN_ID_LAST_DBOX < id)
                        {
                            type = EntityType.PLAYER;
                        }
                        if (!string.IsNullOrEmpty(type))
                        {
                            var victim = new EntityXml();
                            victim.Type = type;
                            victim.X    = x; victim.Y = y;
                            entities.Add(victim);
                        }
                    }

                    Console.Write("\rEntities information into game level format... [" +
                                  (currentProgress * 100 / totalProgress) + "%]");
                }
            }
            level.Entities = entities.ToArray();

            Console.WriteLine("\rEntities information into game level format... [100%]");

            // Save the level data in a file:
            XmlFile.Save(outputFileName, level);
            Console.WriteLine(outputFileName + " generated & saved...\n");
            Shared.ShowEnd();
        }
Exemple #6
0
        //--------------------------------------------------------------------------------------
        // Methods (Convert):
        //--------------------------------------------------------------------------------------

        /// <summary>
        /// Converts a text map level into a level xml descriptor.
        /// </summary>
        /// <param name="mapLines">The text map.</param>
        /// <returns>The converted map.</returns>
        public static LevelXml ConvertFromTextMap(string[] mapLines)
        {
            // Validate the input data:
            if (mapLines == null || mapLines.Length <= 0)
            {
                throw new Exception("No input map lines to convert!");
            }
            if (mapLines.Length > 1 && string.IsNullOrEmpty(mapLines[mapLines.Length - 1]))
            {
                var ml = new string[mapLines.Length - 1];
                for (int i = 0; i < ml.Length; i++)
                {
                    ml[i] = mapLines[i];
                }
                mapLines = ml;
            }
            if (mapLines.Select(l => string.IsNullOrEmpty(l)).Aggregate((cur, nxt) => cur || nxt))
            {
                throw new Exception("Some of the map lines are empty!");
            }

            // Transform the input data into something more cleaner:
            ShowMessageInBox("Raw input map", '|', '-');
            Console.WriteLine();
            ShowTextMap(mapLines);
            mapLines = transform(mapLines);
            ShowMessageInBox("Final input map", '|', '-');
            Console.WriteLine();
            ShowTextMap(mapLines);

            // Initialize the basic data of the level:
            var level = new LevelXml();

            level.Name        = DEFAULT_NAME;
            level.Description = DEFAULT_DESCRIPTION;
            level.Tileset     = DEFAULT_TILESET;

            // Set the terrain & entities data of the level:
            level.World         = new WorldXml();
            level.World.Width   = mapLines[0].Length;
            level.World.Height  = mapLines.Length;
            level.World.Content = "\n";

            int             x = 0, y = 0;
            var             entities  = new List <EntityXml>();
            Action <string> addEntity = (type) => {
                var victim = new EntityXml();
                victim.Type = type;
                victim.X    = x; victim.Y = y;
                entities.Add(victim);
            };

            int currentProgress = 0;
            int totalProgress   = level.World.Width * level.World.Height;

            Console.Write("\rText map into game level format... [" +
                          (currentProgress * 100 / totalProgress) + "%]");

            foreach (var line in mapLines)
            {
                foreach (var c in line)
                {
                    var id = TERRAIN_ID_EMPTY;
                    switch (c)
                    {
                    case WALL:  id = TERRAIN_ID_FIRST_WALL;   break;

                    case GOAL:  id = TERRAIN_ID_FIRST_DFLOOR; break;

                    case FLOOR: id = TERRAIN_ID_FIRST_FLOOR;  break;

                    case PLAYER_ON_GOAL:
                        id = TERRAIN_ID_FIRST_DFLOOR;
                        addEntity(EntityType.PLAYER);
                        break;

                    case BOX_ON_GOAL:
                        id = TERRAIN_ID_FIRST_DFLOOR;
                        addEntity(EntityType.BOX);
                        break;

                    case PLAYER:
                        id = TERRAIN_ID_FIRST_FLOOR;
                        addEntity(EntityType.PLAYER);
                        break;

                    case BOX:
                        id = TERRAIN_ID_FIRST_FLOOR;
                        addEntity(EntityType.BOX);
                        break;
                    }
                    level.World.Content += id.ToString("0000");
                    level.World.Content += " ";
                    ++x;

                    Console.Write("\rText map into game level format... [" +
                                  (currentProgress * 100 / totalProgress) + "%]");
                }
                level.World.Content += "\n";
                x = 0;
                ++y;
            }

            Console.WriteLine("\rText map into game level format... [100%]");

            level.Entities = entities.ToArray();
            return(level);
        }