Exemple #1
0
    /*
     * FILE READING
     */
    public static Map read(string content)
    {
        /*
         * File format :
         * Height Width
         * Height *
         *  [Width * MapTileType (index)]
         */

        // Reading Information
        try{
            string[]        linesWithComments = content.Split('\n');
            List <string[]> lines             = new List <string[]>();
            // filtering comments and empty lines
            for (int i = 0; i < linesWithComments.Length; i++)
            {
                string[] lineSplitSpace = linesWithComments[i].Trim().Split(' ');
                if (lineSplitSpace.Length > 0 && lineSplitSpace[0].Length > 0 && lineSplitSpace[0][0] != '#')
                {
                    lines.Add(lineSplitSpace);
                }
            }

            int lineIt = 0;

            // initial info
            string[] infoline = lines[lineIt];
            lineIt++;
            int height = int.Parse(infoline [0]);
            int width  = int.Parse(infoline [1]);

            Map m = new Map(height, width);

            // Tiles
            for (int y = 0; y < height; y++)
            {
                string[] mapLine = lines[lineIt];
                lineIt++;
                for (int x = 0; x < width; x++)
                {
                    m.addTile(x, height - 1 - y, Map.typeIndexToType(int.Parse(mapLine[x])));
                }
            }

            // Player
            int playerSpawnerX = int.Parse(lines[lineIt][0]);
            int playerSpawnerY = int.Parse(lines[lineIt][1]);
            lineIt++;
            if (m.isUsefulPosition(playerSpawnerX, playerSpawnerY))
            {
                m.PlayerSpawnerPosition = new Vector2(playerSpawnerX, playerSpawnerY);
            }
            else
            {
                Debug.LogError("Map: incorrect player spawner position");
                throw new UnityEngine.UnityException("Map: incorrect player spawner position");
            }

            // Target
            int playerTargetX = int.Parse(lines[lineIt][0]);
            int playerTargetY = int.Parse(lines[lineIt][1]);
            lineIt++;
            if (m.isUsefulPosition(playerTargetX, playerTargetY))
            {
                m.playerTarget = new TargetElement(new Vector2(playerTargetX, playerTargetY));
            }
            else
            {
                throw new UnityEngine.UnityException("Map: incorrect player target position");
            }

            // Bridges
            int amountBridges = int.Parse(lines[lineIt][0]);
            lineIt++;
            for (int x = 0; x < amountBridges; x++)
            {
                int  bridge_length      = int.Parse(lines[lineIt][0]);
                int  bridgeX            = int.Parse(lines[lineIt][1]);
                int  bridgeY            = int.Parse(lines[lineIt][2]);
                bool bridge_closed      = int.Parse(lines[lineIt][3]) == 1;
                bool bridge_orientation = int.Parse(lines[lineIt][4]) == 1;
                lineIt++;

                if (m.isHolePosition(bridgeX, bridgeY))
                {
                    m.Bridges.Add(new BridgeElement(x, bridge_length, bridge_closed, bridge_orientation, new Vector2(bridgeX, bridgeY)));
                }
                else
                {
                    throw new UnityEngine.UnityException("Map: bridge " + x + " must be in a hole");
                }
            }

            // Bridges Buttons
            int amountBridgeButtons = int.Parse(lines[lineIt][0]);
            lineIt++;
            for (int x = 0; x < amountBridgeButtons; x++)
            {
                int bridgeButtonX = int.Parse(lines[lineIt][0]);
                int bridgeButtonY = int.Parse(lines[lineIt][1]);
                lineIt++;

                if (m.isUsefulPosition(bridgeButtonX, bridgeButtonY))
                {
                    m.BridgeButtons.Add(new BridgeButton(new Vector2(bridgeButtonX, bridgeButtonY)));
                }
                else
                {
                    throw new UnityEngine.UnityException("Map: incorrect bridge button " + x + " position");
                }
                // Associating Bridges
                int amountBridges_BridgeButtons = int.Parse(lines[lineIt][0]);
                for (int y = 1; y <= amountBridges_BridgeButtons; y++)
                {
                    int bridgeIndex = int.Parse(lines[lineIt][y]);
                    if (bridgeIndex < m.Bridges.Count)
                    {
                        m.BridgeButtons[x].addBridge(m.Bridges[bridgeIndex]);
                    }
                }
                lineIt++;
            }

            // Collectibles
            int amountCollectibles = int.Parse(lines[lineIt][0]);
            lineIt++;
            for (int x = 0; x < amountCollectibles; x++)
            {
                int collectibleX = int.Parse(lines[lineIt][0]);
                int collectibleY = int.Parse(lines[lineIt][1]);
                CollectibleElement.CollectibleType collectibleType = (CollectibleElement.CollectibleType) int.Parse(lines[lineIt][2]);
                lineIt++;

                if (m.isUsefulPosition(collectibleX, collectibleY))
                {
                    m.Collectibles.Add(new CollectibleElement(collectibleType, new Vector2(collectibleX, collectibleY)));
                }
                else
                {
                    throw new UnityEngine.UnityException("Map: incorrect collectible " + x + " position");
                }
            }

            // Barriers
            int amountBlocks = int.Parse(lines[lineIt][0]);
            lineIt++;
            for (int x = 0; x < amountBlocks; x++)
            {
                int blockX = int.Parse(lines[lineIt][0]);
                int blockY = int.Parse(lines[lineIt][1]);
                lineIt++;

                if (m.isUsefulPosition(blockX, blockY))
                {
                    m.Blocks.Add(new BlockElement(new Vector2(blockX, blockY)));
                }
                else
                {
                    throw new UnityEngine.UnityException("Map: incorrect block " + x + " position");
                }
            }

            return(m);
        }catch (System.Exception ex) {
            Debug.LogError("Error while opening the Map file : wrong format");
            Debug.LogError(ex.Message);
            return(null);
        }
    }
Exemple #2
0
 public CollectibleElement(CollectibleElement.CollectibleType type, Vector2 pos) : base(pos)
 {
     this.collectibleType = type;
     this.score           = CollectibleElement.scoreType[(int)type];
 }