コード例 #1
0
        public ProjectileSystem(SpriteSheetCollection spriteSheets, GameSettings settings, ContentManager content)
        {
            m_projectiles = new List <Projectile>();

            // load the projectile sprites and create an animation from them
            if (spriteSheets != null)
            {
                m_projectileSprites = spriteSheets.getSpriteSheet("Projectiles");
            }

            if (m_projectileSprites != null)
            {
                m_projectileAnimations = new SpriteAnimation[4];

                for (int i = 0; i < 4; i++)
                {
                    m_projectileAnimations[i] = new SpriteAnimation(0.18f, SpriteAnimationType.Loop);
                    m_projectileAnimations[i].addSprite(m_projectileSprites.getSprite(i * 2));
                    m_projectileAnimations[i].addSprite(m_projectileSprites.getSprite(i * 2 + 1));
                }
            }

            // load the laser sound
            if (content != null)
            {
                m_laserSound = content.Load <SoundEffect>("Laser");
            }

            m_settings = settings;
        }
コード例 #2
0
        // // add a collection of frames to the animation from a sprite sheet
        public void addSprites(SpriteSheet spriteSheet)
        {
            if (spriteSheet == null)
            {
                return;
            }

            for (int i = 0; i < spriteSheet.size(); i++)
            {
                addSprite(spriteSheet.getSprite(i));
            }
        }
コード例 #3
0
ファイル: SpaceShip.cs プロジェクト: nitro404/Asteroids
        public SpaceShip(PlayerIndex playerNumber, Vector2 position, float rotation, SpriteSheet spriteSheet, ProjectileSystem projectileSystem, GameSettings settings, ContentManager content)
        {
            // instantiate the player's number with a player index
            m_playerNumber = playerNumber;
            if (m_playerNumber == PlayerIndex.One)
            {
                m_colour = SpaceShipColour.Red;
            }
            else if (m_playerNumber == PlayerIndex.Two)
            {
                m_colour = SpaceShipColour.Blue;
            }
            else if (m_playerNumber == PlayerIndex.Three)
            {
                m_colour = SpaceShipColour.Green;
            }
            else if (m_playerNumber == PlayerIndex.Four)
            {
                m_colour = SpaceShipColour.Yellow;
            }

            // instantiate the player's input device type
            m_inputDevice = settings.getInputDevice(playerNumber);
            if (m_inputDevice == InputDevice.Controller1)
            {
                m_controllerNumber = PlayerIndex.One;
            }
            else if (m_inputDevice == InputDevice.Controller2)
            {
                m_controllerNumber = PlayerIndex.Two;
            }
            else if (m_inputDevice == InputDevice.Controller3)
            {
                m_controllerNumber = PlayerIndex.Three;
            }
            else if (m_inputDevice == InputDevice.Controller4)
            {
                m_controllerNumber = PlayerIndex.Four;
            }
            else
            {
                m_controllerNumber = PlayerIndex.One;
            }

            // instantiate local variables
            this.position = position;
            this.rotation = rotation;
            m_scale       = new Vector2(1, 1);

            m_maximumVelocity = 5.8f;
            m_acceleration    = 0.048f;
            m_rotationSpeed   = 3.1f;

            m_settings         = settings;
            m_spriteSheet      = spriteSheet;
            m_projectileSystem = projectileSystem;

            if (m_spriteSheet == null)
            {
                return;
            }

            // initialize sprite / sprite animation arrays
            m_idleSprite      = new Sprite[3];
            m_movingAnimation = new SpriteAnimation[3];

            // set the space ship sprite sheet offset based on the space ship colour
            int spriteOffset = 0;

            if (m_colour == SpaceShipColour.Red)
            {
                spriteOffset = 0;
            }
            else if (m_colour == SpaceShipColour.Blue)
            {
                spriteOffset = 18;
            }
            else if (m_colour == SpaceShipColour.Green)
            {
                spriteOffset = 3;
            }
            else if (m_colour == SpaceShipColour.Yellow)
            {
                spriteOffset = 21;
            }

            // load the idle sprites for each colour
            for (int i = 0; i < 3; i++)
            {
                m_idleSprite[i] = m_spriteSheet.getSprite(i + spriteOffset);
            }
            // create and store the movement animations for each colour
            for (int i = 0; i < 3; i++)
            {
                m_movingAnimation[i] = new SpriteAnimation(0.16f, SpriteAnimationType.Loop);
                m_movingAnimation[i].addSprite(m_spriteSheet.getSprite(i + spriteOffset + 6));
                m_movingAnimation[i].addSprite(m_spriteSheet.getSprite(i + spriteOffset + 12));
            }

            m_offset = new Vector2(m_idleSprite[1].xOffset * 0.9f, m_idleSprite[1].yOffset * 0.9f);
            m_size   = new Vector2(m_offset.X * 2.0f, m_offset.Y * 2.0f);

            updateInitialValues();
        }
コード例 #4
0
ファイル: SpriteSheet.cs プロジェクト: nitro404/Asteroids
        // parse and create a sprite sheet from a text file input stream
        public static SpriteSheet parseFrom(StreamReader instream, ContentManager content)
        {
            if (instream == null || content == null)
            {
                return(null);
            }

            string input = null;
            string data  = null;

            Variable        v;
            VariableSystem  properties = new VariableSystem();
            string          spriteSheetName, spriteSheetFileName = null;
            SpriteSheetType spriteSheetType  = SpriteSheetType.Invalid;
            Sprite          spriteSheetImage = null;
            SpriteSheet     spriteSheet      = null;

            try {
                while ((input = instream.ReadLine()) != null)
                {
                    // trim the current line being read
                    data = input.Trim();

                    // if the line is blank, discard it
                    if (data.Length == 0)
                    {
                        continue;
                    }
                    // otherwise, if it contains information
                    else
                    {
                        // attempt to parse a variable from the data and store it
                        v = Variable.parseFrom(data);

                        // if successful, store it
                        if (v != null)
                        {
                            properties.add(v);
                        }

                        // if the variable is a specification of the number of sprites or
                        // the number of grids to be parsed, begin doing so
                        if (v != null && (v.id.Equals("Number of Sprites", StringComparison.OrdinalIgnoreCase) ||
                                          v.id.Equals("Number of Grids", StringComparison.OrdinalIgnoreCase)))
                        {
                            // get the sprite sheet name and verify it exists
                            spriteSheetName = properties.getValue("SpriteSheet Name");
                            if (spriteSheetName == null)
                            {
                                return(null);
                            }

                            // parse the sprite sheet type and ensure it is valid
                            spriteSheetType = parseType(properties.getValue("SpriteSheet Type"));
                            if (spriteSheetType == SpriteSheetType.Invalid)
                            {
                                return(null);
                            }

                            // get the sprite sheet file name
                            spriteSheetFileName = properties.getValue("File Name");
                            if (spriteSheetFileName == null)
                            {
                                return(null);
                            }

                            // load the sprite to be parsed by the sprite sheet system
                            spriteSheetImage      = new Sprite(spriteSheetFileName, content);
                            spriteSheetImage.type = SpriteType.Sheet;

                            // ===============================================================================
                            // Arbitrary Offset SpriteSheet ==================================================
                            // ===============================================================================
                            if (spriteSheetType == SpriteSheetType.ArbitraryOffsets)
                            {
                                VariableSystem spriteAttributes = new VariableSystem();
                                int            numberOfSprites;

                                // verify that the number of sprites is valid
                                try { numberOfSprites = int.Parse(v.value); }
                                catch (Exception) { return(null); }
                                if (numberOfSprites <= 0)
                                {
                                    return(null);
                                }

                                int          spriteIndex;
                                string       spriteName, spriteType;
                                Rectangle    grid;
                                Rectangle[]  offsets     = new Rectangle[numberOfSprites];
                                string[]     spriteNames = new string[numberOfSprites];
                                SpriteType[] spriteTypes = new SpriteType[numberOfSprites];
                                for (int i = 0; i < numberOfSprites; i++)
                                {
                                    spriteNames[i] = null;
                                    spriteTypes[i] = SpriteType.Unknown;
                                    offsets[i]     = new Rectangle(0, 0, 0, 0);
                                }
                                // loop through and collect the attributes for each sprite
                                for (int i = 0; i < numberOfSprites; i++)
                                {
                                    spriteAttributes.clear();
                                    // get the 5 sprite attributes (index, name, type, offset, size)
                                    for (int j = 0; j < 5; j++)
                                    {
                                        spriteAttributes.add(Variable.parseFrom(instream.ReadLine()));
                                    }

                                    // store the sprite index value and validate it
                                    try { spriteIndex = int.Parse(spriteAttributes.getValue("Sprite")); }
                                    catch (Exception) { return(null); }
                                    if (spriteIndex < 0 || spriteIndex >= numberOfSprites)
                                    {
                                        return(null);
                                    }

                                    // get the sprite name
                                    spriteName = spriteAttributes.getValue("Name");
                                    if (spriteName == null)
                                    {
                                        return(null);
                                    }

                                    // get the sprite type (as a string)
                                    spriteType = spriteAttributes.getValue("Type");
                                    if (spriteType == null)
                                    {
                                        return(null);
                                    }

                                    // parse the sprite's offset in its parent sprite sheet
                                    string temp = spriteAttributes.getValue("Offset");
                                    if (temp == null)
                                    {
                                        return(null);
                                    }
                                    string[] offsetValues = temp.Split(',');
                                    if (offsetValues.Length != 2)
                                    {
                                        return(null);
                                    }

                                    // parse the size of the sprite
                                    temp = spriteAttributes.getValue("Size");
                                    if (temp == null)
                                    {
                                        return(null);
                                    }
                                    string[] sizeValues = temp.Split(',');
                                    if (sizeValues.Length != 2)
                                    {
                                        return(null);
                                    }

                                    // attempt to create a source grid based off of these specifications
                                    try { grid = new Rectangle(int.Parse(offsetValues[0]),
                                                               int.Parse(offsetValues[1]),
                                                               int.Parse(sizeValues[0]),
                                                               int.Parse(sizeValues[1])); }
                                    catch (Exception) { return(null); }

                                    // assign the values for temporary storage
                                    spriteNames[spriteIndex] = spriteName;
                                    spriteTypes[spriteIndex] = Sprite.parseType(spriteType);
                                    offsets[spriteIndex]     = new Rectangle(grid.X, grid.Y, grid.Width, grid.Height);
                                }

                                // once the attributes and offsets for all of the sprites have been collected, create the sprite sheet
                                spriteSheet      = new SpriteSheet(spriteSheetImage, offsets);
                                spriteSheet.name = spriteSheetName;

                                // loop through the temporarily stored sprite attributes and assign them to each corresponding sprite
                                for (int i = 0; i < numberOfSprites; i++)
                                {
                                    spriteSheet.getSprite(i).name       = spriteNames[i];
                                    spriteSheet.getSprite(i).index      = i;
                                    spriteSheet.getSprite(i).parentName = spriteSheetName;
                                    spriteSheet.getSprite(i).type       = spriteTypes[i];
                                }
                            }

                            // ===============================================================================
                            // Single Grid SpriteSheet =======================================================
                            // ===============================================================================
                            else if (spriteSheetType == SpriteSheetType.SingleGrid)
                            {
                                VariableSystem spriteAttributes = new VariableSystem();
                                Rectangle      grid;
                                Axis           direction;
                                int            numberOfRows, numberOfColumns, numberOfSprites;

                                // get the number of sprites to obtain attributes for
                                try { numberOfSprites = int.Parse(v.value); }
                                catch (Exception) { return(null); }

                                // get the grid offset
                                string temp = properties.getValue("Offset");
                                if (temp == null)
                                {
                                    return(null);
                                }
                                string[] offsetValues = temp.Split(',');
                                if (offsetValues.Length != 2)
                                {
                                    return(null);
                                }

                                // get the size of each cell within the grid
                                temp = properties.getValue("Size");
                                if (temp == null)
                                {
                                    return(null);
                                }
                                string[] sizeValues = temp.Split(',');
                                if (sizeValues.Length != 2)
                                {
                                    return(null);
                                }

                                // convert the offset and cell size to a rectangle
                                try { grid = new Rectangle(int.Parse(offsetValues[0]),
                                                           int.Parse(offsetValues[1]),
                                                           int.Parse(sizeValues[0]),
                                                           int.Parse(sizeValues[1])); }
                                catch (Exception) { return(null); }

                                // get the direction to parse the sprites in
                                direction = parseAxis(properties.getValue("Direction"));

                                // get the number of rows in the grid
                                try { numberOfRows = int.Parse(properties.getValue("Number of Rows")); }
                                catch (Exception) { return(null); }

                                // get the number of columns in the grid
                                try { numberOfColumns = int.Parse(properties.getValue("Number of Columns")); }
                                catch (Exception) { return(null); }

                                // create the sprite sheet based on the corresponding specifications
                                spriteSheet = new SpriteSheet(spriteSheetImage,
                                                              grid,
                                                              direction,
                                                              numberOfRows,
                                                              numberOfColumns);
                                spriteSheet.name = spriteSheetName;

                                int    spriteIndex;
                                string spriteName, spriteType;
                                // loop through the sprite sheet specifications
                                for (int i = 0; i < numberOfSprites; i++)
                                {
                                    // obtain the 3 specifications for the current sprite (index, name, type)
                                    for (int j = 0; j < 3; j++)
                                    {
                                        spriteAttributes.add(Variable.parseFrom(instream.ReadLine()));
                                    }

                                    // get the sprite index and verify it
                                    try { spriteIndex = int.Parse(spriteAttributes.getValue("Sprite")); }
                                    catch (Exception) { return(null); }
                                    if (spriteIndex < 0 || spriteIndex >= spriteSheet.size())
                                    {
                                        return(null);
                                    }

                                    // get the sprite name
                                    spriteName = spriteAttributes.getValue("Name");
                                    if (spriteName == null)
                                    {
                                        return(null);
                                    }

                                    // get the sprite type (as a string)
                                    spriteType = spriteAttributes.getValue("Type");
                                    if (spriteType == null)
                                    {
                                        return(null);
                                    }

                                    // assign the attributes to the corresponding sprite
                                    spriteSheet.getSprite(spriteIndex).name       = spriteName;
                                    spriteSheet.getSprite(spriteIndex).index      = spriteIndex;
                                    spriteSheet.getSprite(spriteIndex).parentName = spriteSheetName;
                                    spriteSheet.getSprite(spriteIndex).type       = Sprite.parseType(spriteType);

                                    // clear the list of attributes presently associated with the current sprite
                                    spriteAttributes.clear();
                                }
                            }

                            // ===============================================================================
                            // Multiple Grids SpriteSheet ====================================================
                            // ===============================================================================
                            else if (spriteSheetType == SpriteSheetType.MultipleGrids)
                            {
                                int numberOfGrids;
                                int spriteIndexOffset = 0;

                                // verify that the number of grids to parse is specified and valid
                                try { numberOfGrids = int.Parse(v.value); }
                                catch (Exception) { return(null); }
                                if (numberOfGrids < 1)
                                {
                                    return(null);
                                }

                                // create an empty sprite sheet
                                spriteSheet       = new SpriteSheet();
                                spriteSheet.name  = spriteSheetName;
                                spriteSheet.image = spriteSheetImage;

                                // temporary storage
                                VariableSystem gridAttributes  = new VariableSystem();
                                int[]          numberOfSprites = new int[numberOfGrids];
                                Rectangle[]    grid            = new Rectangle[numberOfGrids];
                                Axis[]         direction       = new Axis[numberOfGrids];
                                int[]          numberOfRows    = new int[numberOfGrids];
                                int[]          numberOfColumns = new int[numberOfGrids];
                                for (int i = 0; i < numberOfGrids; i++)
                                {
                                    numberOfSprites[i] = -1;
                                    grid[i]            = new Rectangle(0, 0, 0, 0);
                                    direction[i]       = Axis.Horizontal;
                                    numberOfRows[i]    = -1;
                                    numberOfColumns[i] = -1;
                                }

                                // loop through all of the grids
                                for (int i = 0; i < numberOfGrids; i++)
                                {
                                    // obtain the 7 attributes for each grid (index, offset, size, parse direction, number of rows, number of columns, number of sprite attributes)
                                    for (int j = 0; j < 7; j++)
                                    {
                                        gridAttributes.add(Variable.parseFrom(instream.ReadLine()));
                                    }

                                    // get the index of the current grid
                                    int gridIndex;
                                    try { gridIndex = int.Parse(gridAttributes.getValue("Grid")); }
                                    catch (Exception) { return(null); }
                                    if (gridIndex < 0 || gridIndex >= numberOfGrids)
                                    {
                                        return(null);
                                    }

                                    // get the number of sprites within the current grid and validate it
                                    try { numberOfSprites[gridIndex] = int.Parse(gridAttributes.getValue("Number of Sprites")); }
                                    catch (Exception) { return(null); }

                                    // get the offset of the current grid
                                    string temp = gridAttributes.getValue("Offset");
                                    if (temp == null)
                                    {
                                        return(null);
                                    }
                                    string[] offsetValues = temp.Split(',');
                                    if (offsetValues.Length != 2)
                                    {
                                        return(null);
                                    }

                                    // get the size of each cell within the current grid
                                    temp = gridAttributes.getValue("Size");
                                    if (temp == null)
                                    {
                                        return(null);
                                    }
                                    string[] sizeValues = temp.Split(',');
                                    if (sizeValues.Length != 2)
                                    {
                                        return(null);
                                    }

                                    // create a rectangle representing the first cell in the current grid
                                    try { grid[gridIndex] = new Rectangle(int.Parse(offsetValues[0]),
                                                                          int.Parse(offsetValues[1]),
                                                                          int.Parse(sizeValues[0]),
                                                                          int.Parse(sizeValues[1])); }
                                    catch (Exception) { return(null); }

                                    // obtain the direction in which to parse the sprites from the current sprite sheet grid
                                    direction[gridIndex] = parseAxis(gridAttributes.getValue("Direction"));

                                    // get the number of rows in the current grid
                                    try { numberOfRows[gridIndex] = int.Parse(gridAttributes.getValue("Number of Rows")); }
                                    catch (Exception) { return(null); }

                                    // get the number of columns in the current grid
                                    try { numberOfColumns[gridIndex] = int.Parse(gridAttributes.getValue("Number of Columns")); }
                                    catch (Exception) { return(null); }

                                    // start parsing at the initial offset
                                    // loop through all of the rows and columns incrementing the x position and y position as appropriate
                                    // parse each sprite according to the current offset and size and store it in the main sprite collection
                                    int xPos = grid[gridIndex].X;
                                    int yPos = grid[gridIndex].Y;
                                    for (int j = 0; j < numberOfRows[gridIndex]; j++)
                                    {
                                        for (int k = 0; k < numberOfColumns[gridIndex]; k++)
                                        {
                                            spriteSheet.addSprite(new Sprite(spriteSheetImage, new Rectangle(xPos, yPos, grid[gridIndex].Width, grid[gridIndex].Height)));
                                            if (direction[gridIndex] == Axis.Horizontal)
                                            {
                                                xPos += grid[gridIndex].Width + 2;
                                            }
                                            else
                                            {
                                                yPos += grid[gridIndex].Height + 2;
                                            }
                                        }
                                        if (direction[gridIndex] == Axis.Horizontal)
                                        {
                                            yPos += grid[gridIndex].Height + 2; xPos = grid[gridIndex].X;
                                        }
                                        else
                                        {
                                            xPos += grid[gridIndex].Width + 2; yPos = grid[gridIndex].Y;
                                        }
                                    }

                                    int            spriteIndex;
                                    string         spriteName, spriteType;
                                    VariableSystem spriteAttributes = new VariableSystem();
                                    // loop through the sprite attribute specifications for the current grid
                                    for (int j = 0; j < numberOfSprites[gridIndex]; j++)
                                    {
                                        // read in the 3 sprite attributes (index, name, type) and store them
                                        for (int k = 0; k < 3; k++)
                                        {
                                            spriteAttributes.add(Variable.parseFrom(instream.ReadLine()));
                                        }

                                        // get the sprite index and validate it
                                        try { spriteIndex = spriteIndexOffset + int.Parse(spriteAttributes.getValue("Sprite")); }
                                        catch (Exception) { return(null); }
                                        if (spriteIndex < spriteIndexOffset || spriteIndex >= spriteSheet.size())
                                        {
                                            return(null);
                                        }

                                        // get the sprite name
                                        spriteName = spriteAttributes.getValue("Name");
                                        if (spriteName == null)
                                        {
                                            return(null);
                                        }

                                        // get the sprite type
                                        spriteType = spriteAttributes.getValue("Type");
                                        if (spriteType == null)
                                        {
                                            return(null);
                                        }

                                        // assign the current set of attributes to the corresponding sprite
                                        spriteSheet.getSprite(spriteIndex).name       = spriteName;
                                        spriteSheet.getSprite(spriteIndex).index      = spriteIndex;
                                        spriteSheet.getSprite(spriteIndex).parentName = spriteSheetName;
                                        spriteSheet.getSprite(spriteIndex).type       = Sprite.parseType(spriteType);

                                        // clear the current collection of sprite attributes
                                        spriteAttributes.clear();
                                    }

                                    // clear the current collection of grid attributes and update the Sprite index offset (current position in the main SpriteSheet collection)
                                    gridAttributes.clear();
                                    spriteIndexOffset = spriteSheet.size();
                                }
                            }

                            // return null if sprite sheet type is invalid
                            else
                            {
                                return(null);
                            }

                            // return the current sprite sheet
                            return(spriteSheet);
                        }
                    }
                }
            }
            catch (Exception) { }

            return(null);
        }
コード例 #5
0
ファイル: AsteroidSystem.cs プロジェクト: nitro404/Asteroids
        // spawn a random asteroid at a random location with random attributes
        public void spawnAsteroid()
        {
            int    asteroidIndex;
            bool   bigAsteroid;
            Random rand = new Random();

            if (rand.Next(0, 99) < 30)
            {
                // spawn small asteroid (30% chance)
                asteroidIndex = rand.Next(0, 4) + 2;
                bigAsteroid   = false;
            }
            else
            {
                // spawn big asteroid (70% chance)
                asteroidIndex = rand.Next(0, 2) % 2;
                bigAsteroid   = true;
            }

            // if there are too many asteroids of this type, do not spawn it
            if (bigAsteroid && m_numberOfBigAsteroids >= m_maxBigAsteroids)
            {
                return;
            }
            if (!bigAsteroid && m_numberOfSmallAsteroids >= m_maxSmallAsteroids)
            {
                return;
            }

            bool     isInsideOfAsteroid;
            bool     isTooCloseToShip;
            Asteroid newAsteroid = new Asteroid(m_asteroidSprites.getSprite(asteroidIndex), bigAsteroid, m_settings);

            do
            {
                // make sure that the new asteroid is not too close to a space ship
                isTooCloseToShip = false;
                for (int i = 0; i < m_spaceShipSystem.size(); i++)
                {
                    if (m_spaceShipSystem.getSpaceShip(i).checkExtendedCollision(newAsteroid))
                    {
                        isTooCloseToShip = true;
                    }
                }

                // also make sure that the asteroid is not stuck inside of another asteroid
                isInsideOfAsteroid = false;
                if (!isTooCloseToShip)
                {
                    for (int i = 0; i < m_asteroids.Count(); i++)
                    {
                        if (m_asteroids[i].checkCollision(newAsteroid))
                        {
                            isInsideOfAsteroid = true;
                        }
                    }
                }

                // if it is too close to a ship or inside of another asteroid, then re-randomize its position until it isn't
                if (isTooCloseToShip || isInsideOfAsteroid)
                {
                    newAsteroid.randomizePosition();
                }
            } while(isTooCloseToShip || isInsideOfAsteroid);

            // store the asteroid
            if (newAsteroid.bigAsteroid)
            {
                m_numberOfBigAsteroids++;
            }
            else
            {
                m_numberOfSmallAsteroids++;
            }
            m_asteroids.Add(newAsteroid);
        }
コード例 #6
0
ファイル: SpaceShip.cs プロジェクト: nitro404/Asteroids
        public SpaceShip(PlayerIndex playerNumber, Vector2 position, float rotation, SpriteSheet spriteSheet, ProjectileSystem projectileSystem, GameSettings settings, ContentManager content)
        {
            // instantiate the player's number with a player index
            m_playerNumber = playerNumber;
                 if(m_playerNumber == PlayerIndex.One)   { m_colour = SpaceShipColour.Red; }
            else if(m_playerNumber == PlayerIndex.Two)   { m_colour = SpaceShipColour.Blue; }
            else if(m_playerNumber == PlayerIndex.Three) { m_colour = SpaceShipColour.Green; }
            else if(m_playerNumber == PlayerIndex.Four)  { m_colour = SpaceShipColour.Yellow; }

            // instantiate the player's input device type
            m_inputDevice = settings.getInputDevice(playerNumber);
                 if(m_inputDevice == InputDevice.Controller1) { m_controllerNumber = PlayerIndex.One; }
            else if(m_inputDevice == InputDevice.Controller2) { m_controllerNumber = PlayerIndex.Two; }
            else if(m_inputDevice == InputDevice.Controller3) { m_controllerNumber = PlayerIndex.Three; }
            else if(m_inputDevice == InputDevice.Controller4) { m_controllerNumber = PlayerIndex.Four; }
            else											  { m_controllerNumber = PlayerIndex.One; }

            // instantiate local variables
            this.position = position;
            this.rotation = rotation;
            m_scale = new Vector2(1, 1);

            m_maximumVelocity = 5.8f;
            m_acceleration = 0.048f;
            m_rotationSpeed = 3.1f;

            m_settings = settings;
            m_spriteSheet = spriteSheet;
            m_projectileSystem = projectileSystem;

            if(m_spriteSheet == null) { return; }

            // initialize sprite / sprite animation arrays
            m_idleSprite = new Sprite[3];
            m_movingAnimation = new SpriteAnimation[3];

            // set the space ship sprite sheet offset based on the space ship colour
            int spriteOffset = 0;
                 if(m_colour == SpaceShipColour.Red)    { spriteOffset = 0; }
            else if(m_colour == SpaceShipColour.Blue)   { spriteOffset = 18; }
            else if(m_colour == SpaceShipColour.Green)  { spriteOffset = 3; }
            else if(m_colour == SpaceShipColour.Yellow) { spriteOffset = 21; }

            // load the idle sprites for each colour
            for(int i=0;i<3;i++) {
                m_idleSprite[i] = m_spriteSheet.getSprite(i + spriteOffset);
            }
            // create and store the movement animations for each colour
            for(int i=0;i<3;i++) {
                m_movingAnimation[i] = new SpriteAnimation(0.16f, SpriteAnimationType.Loop);
                m_movingAnimation[i].addSprite(m_spriteSheet.getSprite(i + spriteOffset + 6));
                m_movingAnimation[i].addSprite(m_spriteSheet.getSprite(i + spriteOffset + 12));
            }

            m_offset = new Vector2(m_idleSprite[1].xOffset * 0.9f, m_idleSprite[1].yOffset * 0.9f);
            m_size = new Vector2(m_offset.X * 2.0f, m_offset.Y * 2.0f);

            updateInitialValues();
        }
コード例 #7
0
ファイル: SpriteAnimation.cs プロジェクト: nitro404/Asteroids
        // // add a collection of frames to the animation from a sprite sheet
        public void addSprites(SpriteSheet spriteSheet)
        {
            if(spriteSheet == null) { return; }

            for(int i=0;i<spriteSheet.size();i++) {
                addSprite(spriteSheet.getSprite(i));
            }
        }
コード例 #8
0
ファイル: SpriteSheet.cs プロジェクト: nitro404/Asteroids
        // parse and create a sprite sheet from a text file input stream
        public static SpriteSheet parseFrom(StreamReader instream, ContentManager content)
        {
            if(instream == null || content == null) { return null; }

            string input = null;
            string data = null;

            Variable v;
            VariableSystem properties = new VariableSystem();
            string spriteSheetName, spriteSheetFileName = null;
            SpriteSheetType spriteSheetType = SpriteSheetType.Invalid;
            Sprite spriteSheetImage = null;
            SpriteSheet spriteSheet = null;

            try {
                while((input = instream.ReadLine()) != null) {
                    // trim the current line being read
                    data = input.Trim();

                    // if the line is blank, discard it
                    if(data.Length == 0) {
                        continue;
                    }
                    // otherwise, if it contains information
                    else {
                        // attempt to parse a variable from the data and store it
                        v = Variable.parseFrom(data);

                        // if successful, store it
                        if(v != null) {
                            properties.add(v);
                        }

                        // if the variable is a specification of the number of sprites or
                        // the number of grids to be parsed, begin doing so
                        if(v != null && (v.id.Equals("Number of Sprites", StringComparison.OrdinalIgnoreCase) ||
                           v.id.Equals("Number of Grids", StringComparison.OrdinalIgnoreCase))) {

                            // get the sprite sheet name and verify it exists
                            spriteSheetName = properties.getValue("SpriteSheet Name");
                            if(spriteSheetName == null) { return null; }

                            // parse the sprite sheet type and ensure it is valid
                            spriteSheetType = parseType(properties.getValue("SpriteSheet Type"));
                            if(spriteSheetType == SpriteSheetType.Invalid) { return null; }

                            // get the sprite sheet file name
                            spriteSheetFileName = properties.getValue("File Name");
                            if(spriteSheetFileName == null) { return null; }

                            // load the sprite to be parsed by the sprite sheet system
                            spriteSheetImage = new Sprite(spriteSheetFileName, content);
                            spriteSheetImage.type = SpriteType.Sheet;

                            // ===============================================================================
                            // Arbitrary Offset SpriteSheet ==================================================
                            // ===============================================================================
                            if(spriteSheetType == SpriteSheetType.ArbitraryOffsets) {
                                VariableSystem spriteAttributes = new VariableSystem();
                                int numberOfSprites;

                                // verify that the number of sprites is valid
                                try { numberOfSprites = int.Parse(v.value); }
                                catch(Exception) { return null; }
                                if(numberOfSprites <= 0) { return null; }

                                int spriteIndex;
                                string spriteName, spriteType;
                                Rectangle grid;
                                Rectangle[] offsets = new Rectangle[numberOfSprites];
                                string[] spriteNames = new string[numberOfSprites];
                                SpriteType[] spriteTypes = new SpriteType[numberOfSprites];
                                for(int i=0;i<numberOfSprites;i++) {
                                    spriteNames[i] = null;
                                    spriteTypes[i] = SpriteType.Unknown;
                                    offsets[i] = new Rectangle(0, 0, 0, 0);
                                }
                                // loop through and collect the attributes for each sprite
                                for(int i=0;i<numberOfSprites;i++) {
                                    spriteAttributes.clear();
                                    // get the 5 sprite attributes (index, name, type, offset, size)
                                    for(int j=0;j<5;j++) {
                                        spriteAttributes.add(Variable.parseFrom(instream.ReadLine()));
                                    }

                                    // store the sprite index value and validate it
                                    try { spriteIndex = int.Parse(spriteAttributes.getValue("Sprite")); }
                                    catch(Exception) { return null; }
                                    if(spriteIndex < 0 || spriteIndex >= numberOfSprites) { return null; }

                                    // get the sprite name
                                    spriteName = spriteAttributes.getValue("Name");
                                    if(spriteName == null) { return null; }

                                    // get the sprite type (as a string)
                                    spriteType = spriteAttributes.getValue("Type");
                                    if(spriteType == null) { return null; }

                                    // parse the sprite's offset in its parent sprite sheet
                                    string temp = spriteAttributes.getValue("Offset");
                                    if(temp == null) { return null; }
                                    string[] offsetValues = temp.Split(',');
                                    if(offsetValues.Length != 2) { return null; }

                                    // parse the size of the sprite
                                    temp = spriteAttributes.getValue("Size");
                                    if(temp == null) { return null; }
                                    string[] sizeValues = temp.Split(',');
                                    if(sizeValues.Length != 2) { return null; }

                                    // attempt to create a source grid based off of these specifications
                                    try { grid = new Rectangle(int.Parse(offsetValues[0]),
                                                               int.Parse(offsetValues[1]),
                                                               int.Parse(sizeValues[0]),
                                                               int.Parse(sizeValues[1])); }
                                    catch(Exception) { return null; }

                                    // assign the values for temporary storage
                                    spriteNames[spriteIndex] = spriteName;
                                    spriteTypes[spriteIndex] = Sprite.parseType(spriteType);
                                    offsets[spriteIndex] = new Rectangle(grid.X, grid.Y, grid.Width, grid.Height);
                                }

                                // once the attributes and offsets for all of the sprites have been collected, create the sprite sheet
                                spriteSheet = new SpriteSheet(spriteSheetImage, offsets);
                                spriteSheet.name = spriteSheetName;

                                // loop through the temporarily stored sprite attributes and assign them to each corresponding sprite
                                for(int i=0;i<numberOfSprites;i++) {
                                    spriteSheet.getSprite(i).name = spriteNames[i];
                                    spriteSheet.getSprite(i).index = i;
                                    spriteSheet.getSprite(i).parentName = spriteSheetName;
                                    spriteSheet.getSprite(i).type = spriteTypes[i];
                                }
                            }

                            // ===============================================================================
                            // Single Grid SpriteSheet =======================================================
                            // ===============================================================================
                            else if(spriteSheetType == SpriteSheetType.SingleGrid) {
                                VariableSystem spriteAttributes = new VariableSystem();
                                Rectangle grid;
                                Axis direction;
                                int numberOfRows, numberOfColumns, numberOfSprites;

                                // get the number of sprites to obtain attributes for
                                try { numberOfSprites = int.Parse(v.value); }
                                catch(Exception) { return null; }

                                // get the grid offset
                                string temp = properties.getValue("Offset");
                                if(temp == null) { return null; }
                                string[] offsetValues = temp.Split(',');
                                if(offsetValues.Length != 2) { return null; }

                                // get the size of each cell within the grid
                                temp = properties.getValue("Size");
                                if(temp == null) { return null; }
                                string[] sizeValues = temp.Split(',');
                                if(sizeValues.Length != 2) { return null; }

                                // convert the offset and cell size to a rectangle
                                try { grid = new Rectangle(int.Parse(offsetValues[0]),
                                                           int.Parse(offsetValues[1]),
                                                           int.Parse(sizeValues[0]),
                                                           int.Parse(sizeValues[1])); }
                                catch(Exception) { return null; }

                                // get the direction to parse the sprites in
                                direction = parseAxis(properties.getValue("Direction"));

                                // get the number of rows in the grid
                                try { numberOfRows = int.Parse(properties.getValue("Number of Rows")); }
                                catch(Exception) { return null; }

                                // get the number of columns in the grid
                                try { numberOfColumns = int.Parse(properties.getValue("Number of Columns")); }
                                catch(Exception) { return null; }

                                // create the sprite sheet based on the corresponding specifications
                                spriteSheet = new SpriteSheet(spriteSheetImage,
                                                              grid,
                                                              direction,
                                                              numberOfRows,
                                                              numberOfColumns);
                                spriteSheet.name = spriteSheetName;

                                int spriteIndex;
                                string spriteName, spriteType;
                                // loop through the sprite sheet specifications
                                for(int i=0;i<numberOfSprites;i++) {
                                    // obtain the 3 specifications for the current sprite (index, name, type)
                                    for(int j=0;j<3;j++) {
                                        spriteAttributes.add(Variable.parseFrom(instream.ReadLine()));
                                    }

                                    // get the sprite index and verify it
                                    try { spriteIndex = int.Parse(spriteAttributes.getValue("Sprite")); }
                                    catch(Exception) { return null; }
                                    if(spriteIndex < 0 || spriteIndex >= spriteSheet.size()) { return null; }

                                    // get the sprite name
                                    spriteName = spriteAttributes.getValue("Name");
                                    if(spriteName == null) { return null; }

                                    // get the sprite type (as a string)
                                    spriteType = spriteAttributes.getValue("Type");
                                    if(spriteType == null) { return null; }

                                    // assign the attributes to the corresponding sprite
                                    spriteSheet.getSprite(spriteIndex).name = spriteName;
                                    spriteSheet.getSprite(spriteIndex).index = spriteIndex;
                                    spriteSheet.getSprite(spriteIndex).parentName = spriteSheetName;
                                    spriteSheet.getSprite(spriteIndex).type = Sprite.parseType(spriteType);

                                    // clear the list of attributes presently associated with the current sprite
                                    spriteAttributes.clear();
                                }
                            }

                            // ===============================================================================
                            // Multiple Grids SpriteSheet ====================================================
                            // ===============================================================================
                            else if(spriteSheetType == SpriteSheetType.MultipleGrids) {
                                int numberOfGrids;
                                int spriteIndexOffset = 0;

                                // verify that the number of grids to parse is specified and valid
                                try { numberOfGrids = int.Parse(v.value); }
                                catch(Exception) { return null; }
                                if(numberOfGrids < 1) { return null; }

                                // create an empty sprite sheet
                                spriteSheet = new SpriteSheet();
                                spriteSheet.name = spriteSheetName;
                                spriteSheet.image = spriteSheetImage;

                                // temporary storage
                                VariableSystem gridAttributes = new VariableSystem();
                                int[] numberOfSprites = new int[numberOfGrids];
                                Rectangle[] grid = new Rectangle[numberOfGrids];
                                Axis[] direction = new Axis[numberOfGrids];
                                int[] numberOfRows = new int[numberOfGrids];
                                int[] numberOfColumns = new int[numberOfGrids];
                                for(int i=0;i<numberOfGrids;i++) {
                                    numberOfSprites[i] = -1;
                                    grid[i] = new Rectangle(0, 0, 0, 0);
                                    direction[i] = Axis.Horizontal;
                                    numberOfRows[i] = -1;
                                    numberOfColumns[i] = -1;
                                }

                                // loop through all of the grids
                                for(int i=0;i<numberOfGrids;i++) {
                                    // obtain the 7 attributes for each grid (index, offset, size, parse direction, number of rows, number of columns, number of sprite attributes)
                                    for(int j=0;j<7;j++) {
                                        gridAttributes.add(Variable.parseFrom(instream.ReadLine()));
                                    }

                                    // get the index of the current grid
                                    int gridIndex;
                                    try { gridIndex = int.Parse(gridAttributes.getValue("Grid")); }
                                    catch(Exception) { return null; }
                                    if(gridIndex < 0 || gridIndex >= numberOfGrids) { return null; }

                                    // get the number of sprites within the current grid and validate it
                                    try { numberOfSprites[gridIndex] = int.Parse(gridAttributes.getValue("Number of Sprites")); }
                                    catch(Exception) { return null; }

                                    // get the offset of the current grid
                                    string temp = gridAttributes.getValue("Offset");
                                    if(temp == null) { return null; }
                                    string[] offsetValues = temp.Split(',');
                                    if(offsetValues.Length != 2) { return null; }

                                    // get the size of each cell within the current grid
                                    temp = gridAttributes.getValue("Size");
                                    if(temp == null) { return null; }
                                    string[] sizeValues = temp.Split(',');
                                    if(sizeValues.Length != 2) { return null; }

                                    // create a rectangle representing the first cell in the current grid
                                    try { grid[gridIndex] = new Rectangle(int.Parse(offsetValues[0]),
                                                                          int.Parse(offsetValues[1]),
                                                                          int.Parse(sizeValues[0]),
                                                                          int.Parse(sizeValues[1])); }
                                    catch(Exception) { return null; }

                                    // obtain the direction in which to parse the sprites from the current sprite sheet grid
                                    direction[gridIndex] = parseAxis(gridAttributes.getValue("Direction"));

                                    // get the number of rows in the current grid
                                    try { numberOfRows[gridIndex] = int.Parse(gridAttributes.getValue("Number of Rows")); }
                                    catch(Exception) { return null; }

                                    // get the number of columns in the current grid
                                    try { numberOfColumns[gridIndex] = int.Parse(gridAttributes.getValue("Number of Columns")); }
                                    catch(Exception) { return null; }

                                    // start parsing at the initial offset
                                    // loop through all of the rows and columns incrementing the x position and y position as appropriate
                                    // parse each sprite according to the current offset and size and store it in the main sprite collection
                                    int xPos = grid[gridIndex].X;
                                    int yPos = grid[gridIndex].Y;
                                    for(int j=0;j<numberOfRows[gridIndex];j++) {
                                        for(int k=0;k<numberOfColumns[gridIndex];k++) {
                                            spriteSheet.addSprite(new Sprite(spriteSheetImage, new Rectangle(xPos, yPos, grid[gridIndex].Width, grid[gridIndex].Height)));
                                            if(direction[gridIndex] == Axis.Horizontal) { xPos += grid[gridIndex].Width + 2; }
                                            else { yPos += grid[gridIndex].Height + 2; }
                                        }
                                        if(direction[gridIndex] == Axis.Horizontal) { yPos += grid[gridIndex].Height + 2; xPos = grid[gridIndex].X; }
                                        else { xPos += grid[gridIndex].Width + 2; yPos = grid[gridIndex].Y; }
                                    }

                                    int spriteIndex;
                                    string spriteName, spriteType;
                                    VariableSystem spriteAttributes = new VariableSystem();
                                    // loop through the sprite attribute specifications for the current grid
                                    for(int j=0;j<numberOfSprites[gridIndex];j++) {
                                        // read in the 3 sprite attributes (index, name, type) and store them
                                        for(int k=0;k<3;k++) {
                                            spriteAttributes.add(Variable.parseFrom(instream.ReadLine()));
                                        }

                                        // get the sprite index and validate it
                                        try { spriteIndex = spriteIndexOffset + int.Parse(spriteAttributes.getValue("Sprite")); }
                                        catch(Exception) { return null; }
                                        if(spriteIndex < spriteIndexOffset || spriteIndex >= spriteSheet.size()) {
                                            return null;
                                        }

                                        // get the sprite name
                                        spriteName = spriteAttributes.getValue("Name");
                                        if(spriteName == null) { return null; }

                                        // get the sprite type
                                        spriteType = spriteAttributes.getValue("Type");
                                        if(spriteType == null) { return null; }

                                        // assign the current set of attributes to the corresponding sprite
                                        spriteSheet.getSprite(spriteIndex).name = spriteName;
                                        spriteSheet.getSprite(spriteIndex).index = spriteIndex;
                                        spriteSheet.getSprite(spriteIndex).parentName = spriteSheetName;
                                        spriteSheet.getSprite(spriteIndex).type = Sprite.parseType(spriteType);

                                        // clear the current collection of sprite attributes
                                        spriteAttributes.clear();
                                    }

                                    // clear the current collection of grid attributes and update the Sprite index offset (current position in the main SpriteSheet collection)
                                    gridAttributes.clear();
                                    spriteIndexOffset = spriteSheet.size();
                                }
                            }

                            // return null if sprite sheet type is invalid
                            else {
                                return null;
                            }

                            // return the current sprite sheet
                            return spriteSheet;
                        }
                    }
                }
            }
            catch(Exception) { }

            return null;
        }