Example #1
0
        // parse a collection of sprite sheets from a specified sprite sheet data file
        public static SpriteSheetCollection parseFrom(String fileName, ContentManager content)
        {
            if (fileName == null || !File.Exists(fileName))
            {
                return(null);
            }

            StreamReader          instream;
            SpriteSheet           spriteSheet;
            SpriteSheetCollection spriteSheets = new SpriteSheetCollection();

            // open the sprite sheet data file and parse until either:
            // an invalid sprite sheet is encountered
            // or the end of the file is encountered
            try {
                instream = File.OpenText(fileName);

                do
                {
                    // parse the sprite sheet and store it
                    spriteSheet = SpriteSheet.parseFrom(instream, content);
                    spriteSheets.addSpriteSheet(spriteSheet);
                } while(spriteSheet != null);

                instream.Close();
            }
            catch (Exception) { return(null); }

            return(spriteSheets);
        }
Example #2
0
        public void Initialize(SpriteSheetCollection spriteSheets)
        {
            SpriteSheet spriteSheet = spriteSheets.getSpriteSheet("Tree");

            if (spriteSheet == null)
            {
                return;
            }
            for (int i = 1; i < 9; i += 1)
            {
                Sprite sprite = spriteSheet.getSprite(string.Concat("Tree ", i.ToString()));
                if (sprite == null)
                {
                    return;
                }
                //chris edited this
                sprite.m_SpriteDepth = 0.49f;
                m_lTreeSprites.Add(sprite);
            }

            for (int i = 0; i < TOTAL_STARTING_TREES; i++)
            {
                Vector2 pos = new Vector2(Firecracker.random.Next(0, Firecracker.level.dimensions.X * Firecracker.level.gridSize), Firecracker.random.Next(0, Firecracker.level.dimensions.Y * Firecracker.level.gridSize));
                if (Terrain.Instance == null || Terrain.Instance.isPositionWalkable(pos))
                {
                    int          RandomIndex = Firecracker.random.Next(0, 4);
                    StaticObject newTree     = new StaticObject(pos, m_lTreeSprites[RandomIndex]);
                    if (newTree != null)
                    {
                        Firecracker.level.addObject(newTree);
                        m_lForestList.Add(newTree);
                    }
                }
            }
        }
Example #3
0
        public static SpriteAnimationCollection readFrom(String fileName, SpriteSheetCollection spriteSheets)
        {
            if (fileName == null || spriteSheets == null || !File.Exists(fileName))
            {
                return(null);
            }

            SpriteAnimation           animation;
            SpriteAnimationCollection animations = new SpriteAnimationCollection();

            StreamReader input = null;

            try {
                input = File.OpenText(fileName);

                do
                {
                    animation = SpriteAnimation.parseFrom(input, spriteSheets);
                    animations.addAnimation(animation);
                } while(animation != null);

                input.Close();
            }
            catch (Exception) {
                return(null);
            }

            return(animations);
        }
Example #4
0
        public static Player parseFrom(StreamReader input, SpriteSheetCollection spriteSheets)
        {
            // create the object
            Player newObject = new Player();

            newObject.updateInitialValues();

            return(newObject);
        }
Example #5
0
        public static Terrain parseFrom(StreamReader input, SpriteSheetCollection spriteSheets)
        {
            if (input == null || spriteSheets == null)
            {
                return(null);
            }

            VariableSystem properties = new VariableSystem();

            // store all of the animation properties
            String   data;
            Variable property;

            do
            {
                data = input.ReadLine();
                if (data == null)
                {
                    return(null);
                }

                data = data.Trim();
                if (data.Length == 0)
                {
                    continue;
                }

                property = Variable.parseFrom(data);
                if (property == null)
                {
                    return(null);
                }

                properties.add(property);
            } while (properties.size() < 2);

            String heightmapName = properties.getValue("Heightmap Name");

            if (heightmapName == null)
            {
                return(null);
            }
            string visibleString = properties.getValue("Visible");

            if (visibleString == null)
            {
                return(null);
            }
            bool visible = visibleString.Equals("True", StringComparison.OrdinalIgnoreCase);

            // create the object
            Terrain newObject = new Terrain(visible, heightmapName);

            return(newObject);
        }
Example #6
0
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // parse and load all sprite sheets
            spriteSheets = SpriteSheetCollection.parseFrom(Content.RootDirectory + "/" + settings.spriteSheetFileName, Content);

            // parse and load all sprite animations
            animations = SpriteAnimationCollection.readFrom(Content.RootDirectory + "/" + settings.animationsFileName, spriteSheets);


            // load game content
            menu.loadContent(Content);

            console.loadContent(Content);
        }
        public static Settlement 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);
            }

            // 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
            Settlement newObject = new Settlement(newPosition);

            newObject.updateInitialValues();

            return(newObject);
        }
Example #8
0
        public static GameTile 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
            GameTile newObject = new GameTile(newPosition, sprite);

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

            return(newObject);
        }
Example #9
0
        public static NPCObject 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);
            }

            // 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); }


            // 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 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);
            }


            // create the object
            NPCObject newObject = new NPCObject(newPosition, (Sprite)null);

            // get the sprite's name
            newObject.AnimNameN = Variable.parseFrom(input.ReadLine());
            if (newObject.AnimNameN == null || !newObject.AnimNameN.id.Equals("Anim Name N", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }
            newObject.AnimNameE = Variable.parseFrom(input.ReadLine());
            if (newObject.AnimNameE == null || !newObject.AnimNameE.id.Equals("Anim Name E", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }
            newObject.AnimNameS = Variable.parseFrom(input.ReadLine());
            if (newObject.AnimNameS == null || !newObject.AnimNameS.id.Equals("Anim Name S", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }
            newObject.AnimNameW = Variable.parseFrom(input.ReadLine());
            if (newObject.AnimNameW == null || !newObject.AnimNameW.id.Equals("Anim Name W", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            Variable isKillable = Variable.parseFrom(input.ReadLine());

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


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

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


            newObject.m_bKillable = bool.Parse(isKillable.value);
            //newObject.sprite.m_SpriteDepth = float.Parse(layerDepth.value);
            newObject.SetIsDirectionBased(true);
            newObject.SetDirAnimation(AnimDirection.DIR_N, newObject.AnimNameN.value);
            newObject.SetDirAnimation(AnimDirection.DIR_E, newObject.AnimNameE.value);
            newObject.SetDirAnimation(AnimDirection.DIR_S, newObject.AnimNameS.value);
            newObject.SetDirAnimation(AnimDirection.DIR_W, newObject.AnimNameW.value);
            newObject.updateInitialValues();

            return(newObject);
        }
Example #10
0
        public static SpriteAnimation parseFrom(StreamReader input, SpriteSheetCollection spriteSheets)
        {
            if (input == null || spriteSheets == null)
            {
                return(null);
            }

            SpriteAnimation animation;
            VariableSystem  animationProperties = new VariableSystem();

            // store all of the animation properties
            String   data;
            Variable property;

            do
            {
                data = input.ReadLine();
                if (data == null)
                {
                    return(null);
                }

                data = data.Trim();
                if (data.Length == 0)
                {
                    continue;
                }

                property = Variable.parseFrom(data);
                if (property == null)
                {
                    return(null);
                }

                animationProperties.add(property);
            } while(animationProperties.size() < 7);

            // parse the animation name
            String animationName = animationProperties.getValue("Animation Name");

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

            // parse the animation type
            SpriteAnimationType animationType = parseType(animationProperties.getValue("Animation Type"));

            if (animationType == SpriteAnimationType.Invalid)
            {
                return(null);
            }

            // parse the duration of the animation
            float duration;

            try {
                duration = float.Parse(animationProperties.getValue("Animation Duration"));
            }
            catch (Exception) { return(null); }
            if (duration <= 0)
            {
                return(null);
            }

            // parse the number of frames in the animation
            int numberOfFrames;

            try {
                numberOfFrames = int.Parse(animationProperties.getValue("Number of Frames"));
            }
            catch (Exception) { return(null); }
            if (numberOfFrames <= 0)
            {
                return(null);
            }

            // get the sprite sheet name
            String spriteSheetName = animationProperties.getValue("SpriteSheet Name");

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

            // get the sprite name
            String spriteName = animationProperties.getValue("Sprite Name");

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

            string startIndex  = animationProperties.getValue("Start Index");
            int    iStartIndex = 1;

            if (startIndex != null)
            {
                iStartIndex = int.Parse(startIndex);
            }

            animation = new SpriteAnimation(animationName, duration, animationType);

            // get the sprite sheet which contains the animation
            SpriteSheet spriteSheet = spriteSheets.getSpriteSheet(spriteSheetName);

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

            // obtain and add the sprites to the sprite animation
            Sprite sprite;

            for (int i = 0; i < numberOfFrames; i++)
            {
                sprite = spriteSheet.getSprite(spriteName + " " + (i + iStartIndex));
                if (sprite == null)
                {
                    return(null);
                }

                animation.addSprite(sprite);
            }

            return(animation);
        }