Пример #1
0
        public static Level readFrom(String fileName)
        {
            if (fileName == null || fileName.Length == 0)
            {
                return(null);
            }
            hasAnyLevelBeenLoaded = true;

            Level     level;
            LevelType type                   = DEFAULT_LEVEL_TYPE;
            int       gridSize               = DEFAULT_GRID_SIZE;
            Point     dimensions             = Point.Zero;
            int       numberOfCollisionEdges = 0;
            int       numberOfObjects        = 0;

            StreamReader input = null;

            try {
                input = File.OpenText(fileName);
            }
            catch (Exception) {
                return(null);
            }

            String data;

            // read initial header
            while ((data = input.ReadLine()) != null)
            {
                data = data.Trim();
                if (data.Length == 0)
                {
                    continue;
                }

                // read and verify level type
                String[] versionHeader = data.Split(':');
                if (versionHeader.Length != 2)
                {
                    return(null);
                }

                String typeString = versionHeader[0] = versionHeader[0].Trim();
                if (typeString.Equals(CARTESIAN_TYPE, StringComparison.OrdinalIgnoreCase))
                {
                    type = LevelType.Cartesian;
                }
                else if (typeString.Equals(ISOMETRIC_TYPE, StringComparison.OrdinalIgnoreCase))
                {
                    type = LevelType.Isometric;
                }
                else
                {
                    return(null);
                }

                // read and verify level version
                String   versionString = versionHeader[1].Trim();
                String[] versionData   = versionString.Split(' ');
                if (versionData.Length != 2)
                {
                    return(null);
                }
                if (!versionData[0].Equals("Version", StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }
                try {
                    float version = float.Parse(versionData[1]);

                    if (version != LEVEL_VERSION)
                    {
                        return(null);
                    }
                }
                catch (Exception) { return(null); }

                break;
            }

            // read grid size
            while ((data = input.ReadLine()) != null)
            {
                data = data.Trim();
                if (data.Length == 0)
                {
                    continue;
                }

                // read and store grid size
                String[] gridHeader = data.Split(':');
                if (gridHeader.Length != 2)
                {
                    return(null);
                }
                if (!gridHeader[0].Trim().Equals("Grid Size", StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }
                try {
                    gridSize = Int32.Parse(gridHeader[1].Trim());
                }
                catch (Exception) { return(null); }

                break;
            }

            // read map dimensions
            while ((data = input.ReadLine()) != null)
            {
                data = data.Trim();
                if (data.Length == 0)
                {
                    continue;
                }

                // read and store grid size
                String[] dimensionHeader = data.Split(':');
                if (dimensionHeader.Length != 2)
                {
                    return(null);
                }
                if (!dimensionHeader[0].Trim().Equals("Dimensions", StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }
                String[] dimensionData = dimensionHeader[1].Split(',');
                if (dimensionData.Length != 2)
                {
                    return(null);
                }
                try {
                    dimensions.X = Int32.Parse(dimensionData[0].Trim());
                    dimensions.Y = Int32.Parse(dimensionData[1].Trim());
                }
                catch (Exception) { return(null); }

                break;
            }

            // read collision data header
            while ((data = input.ReadLine()) != null)
            {
                data = data.Trim();
                if (data.Length == 0)
                {
                    continue;
                }

                // verify header and parse number of collision edges
                String[] collisionHeader = data.Split(':');
                if (collisionHeader.Length != 2)
                {
                    return(null);
                }
                if (!collisionHeader[0].Trim().Equals("Collision Edges", StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }
                try {
                    numberOfCollisionEdges = Int32.Parse(collisionHeader[1].Trim());
                }
                catch (Exception) { return(null); }

                break;
            }

            level = new Level(type, gridSize, dimensions);

            level.collisionData = Graph.parseFrom(input, numberOfCollisionEdges);

            // read objects header and number of objects
            while ((data = input.ReadLine()) != null)
            {
                String objectsHeader = data.Trim();
                if (objectsHeader.Length == 0)
                {
                    continue;
                }

                // separate header data
                String[] headerData = objectsHeader.Split(':');
                if (headerData.Length != 2)
                {
                    return(null);
                }

                // verify the objects header
                if (!headerData[0].Trim().Equals("Objects", StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }

                // parse the number of objects
                try {
                    numberOfObjects = Int32.Parse(headerData[1].Trim());
                }
                catch (Exception) { return(null); }

                break;
            }

            // load the objects
            int currentObject = 0;

            while (currentObject < numberOfObjects)
            {
                data = input.ReadLine();
                String objectHeader = data.Trim();
                if (objectHeader.Length == 0)
                {
                    continue;
                }

                // parse object type
                String objectType;
                if (objectHeader[objectHeader.Length - 1] == ':')
                {
                    objectType = objectHeader.Substring(0, objectHeader.Length - 1);
                }
                else
                {
                    return(null);
                }

                // parse the object based on its type
                GameObject newObject = null;
                if (objectType.Equals("Static Object", StringComparison.OrdinalIgnoreCase))
                {
                    newObject = StaticObject.parseFrom(input, Firecracker.spriteSheets);
                }
                else if (objectType.Equals("Game Tile", StringComparison.OrdinalIgnoreCase))
                {
                    newObject = GameTile.parseFrom(input, Firecracker.spriteSheets);
                }
                else if (objectType.Equals("NPCObject", StringComparison.OrdinalIgnoreCase))
                {
                    newObject = NPCObject.parseFrom(input, Firecracker.spriteSheets);
                }
                else if (objectType.Equals("Player", StringComparison.OrdinalIgnoreCase))
                {
                    newObject = Player.parseFrom(input, Firecracker.spriteSheets);
                }
                else if (objectType.Equals("Settlement", StringComparison.OrdinalIgnoreCase))
                {
                    newObject = Settlement.parseFrom(input, Firecracker.spriteSheets);
                }
                else if (objectType.Equals("Terrain", StringComparison.OrdinalIgnoreCase))
                {
                    newObject = Terrain.parseFrom(input, Firecracker.spriteSheets);
                }
                // verify that the object was successfully parsed
                if (newObject != null)
                {
                    currentObject++;
                    level.addObject(newObject);
                }
                else
                {
                    return(null);
                }
            }

            input.Close();

            PopupNotification.instance.ShowNotification("Objective", "Destroy all of the humans before they multiply and acquire\n" +
                                                        "the technology to leave the island. The more you destroy,\n" +
                                                        "the more devastating abilities you will be able to use.", false);

            return(level);
        }
Пример #2
0
        public static StaticObject parseFrom(StreamReader input, SpriteSheetCollection spriteSheets)
        {
            if (input == null || spriteSheets == null)
            {
                return(null);
            }

            // get the object's position
            Variable position = Variable.parseFrom(input.ReadLine());

            if (position == null || !position.id.Equals("Position", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            // Get the layer depth of this sprite
            Variable layerDepth = Variable.parseFrom(input.ReadLine());

            if (layerDepth == null || !layerDepth.id.Equals("LayerDepth", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            // get the sprite's name
            Variable spriteName = Variable.parseFrom(input.ReadLine());

            if (spriteName == null || !spriteName.id.Equals("Sprite Name", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            // get the name of the spritesheet in which the sprite is found
            Variable spriteSheetName = Variable.parseFrom(input.ReadLine());

            if (spriteSheetName == null || !spriteSheetName.id.Equals("SpriteSheet Name", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            // get the object's sprite
            SpriteSheet spriteSheet = spriteSheets.getSpriteSheet(spriteSheetName.value);

            if (spriteSheet == null)
            {
                return(null);
            }
            Sprite sprite = spriteSheet.getSprite(spriteName.value);

            if (sprite == null)
            {
                return(null);
            }

            // parse the sprite's position
            String[] positionData = position.value.Split(',');
            if (positionData.Length != 2)
            {
                return(null);
            }

            Vector2 newPosition;

            try {
                newPosition.X = Int32.Parse(positionData[0]);
                newPosition.Y = Int32.Parse(positionData[1]);
            }
            catch (Exception) { return(null); }

            // create the object
            StaticObject newObject = new StaticObject(newPosition, sprite);

            newObject.sprite.m_SpriteDepth = float.Parse(layerDepth.value);
            newObject.updateInitialValues();

            return(newObject);
        }