コード例 #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
ファイル: Explosion.cs プロジェクト: nitro404/Asteroids
        public Explosion(Vector2 position, Vector2 velocity, SpriteSheet spriteSheet)
        {
            m_explosion = new SpriteAnimation(0.17f, SpriteAnimationType.Single, spriteSheet);

            this.position = position;
            this.velocity = velocity;
        }
コード例 #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
ファイル: ExplosionSystem.cs プロジェクト: nitro404/Asteroids
 public ExplosionSystem(SpriteSheetCollection spriteSheets, ContentManager content)
 {
     m_explosions = new List<Explosion>();
     if(spriteSheets != null) {
         m_explosionSprites = spriteSheets.getSpriteSheet("Explosion");
     }
     m_explosionSound = content.Load<SoundEffect>("Explosion");
 }
コード例 #5
0
        // add a sprite sheet to the collection of sprite sheets
        public bool addSpriteSheet(SpriteSheet spriteSheet)
        {
            if(spriteSheet == null) { return false; }

            if(!m_spriteSheets.Contains(spriteSheet)) {
                m_spriteSheets.Add(spriteSheet);
                return true;
            }
            return false;
        }
コード例 #6
0
ファイル: SpriteAnimation.cs プロジェクト: nitro404/Asteroids
        public SpriteAnimation(float interval, SpriteAnimationType type, SpriteSheet spriteSheet)
        {
            m_sprites = new List<Sprite>();
            m_interval = interval;
            m_sequence = 0;
            m_end = 0;
            m_type = type;

            addSprites(spriteSheet);
        }
コード例 #7
0
ファイル: AsteroidSystem.cs プロジェクト: nitro404/Asteroids
        public AsteroidSystem(SpriteSheetCollection spriteSheets, SpaceShipSystem spaceShipSystem, GameSettings settings)
        {
            m_asteroids = new List<Asteroid>();
            if(spriteSheets != null) {
                m_asteroidSprites = spriteSheets.getSpriteSheet("Asteroids");
            }
            m_spaceShipSystem = spaceShipSystem;
            m_settings = settings;

            m_maxSmallAsteroids = 12;
            m_maxBigAsteroids = 10;
        }
コード例 #8
0
ファイル: SpaceShipSystem.cs プロジェクト: nitro404/Asteroids
        public SpaceShipSystem(int numberOfSpaceShips, SpriteSheetCollection spriteSheets, ProjectileSystem projectileSystem, GameSettings settings, ContentManager content)
        {
            this.numberOfSpaceShips = numberOfSpaceShips;
            if(spriteSheets != null) {
                m_spaceShipSprites = spriteSheets.getSpriteSheet("SpaceShip");
            }
            m_projectileSystem = projectileSystem;
            m_settings = settings;

            // initialize all space ships
            if(m_settings == null || content == null || m_spaceShipSprites == null) { return; }
            m_spaceShips = new SpaceShip[m_maxSpaceShips];
            m_spaceShips[0] = new SpaceShip(PlayerIndex.One, new Vector2(settings.screenWidth * 0.2f, settings.screenHeight * 0.2f), 135, m_spaceShipSprites, projectileSystem, settings, content);
            m_spaceShips[1] = new SpaceShip(PlayerIndex.Two, new Vector2(settings.screenWidth * 0.8f, settings.screenHeight * 0.8f), -45, m_spaceShipSprites, projectileSystem, settings, content);
            m_spaceShips[2] = new SpaceShip(PlayerIndex.Three, new Vector2(settings.screenWidth * 0.2f, settings.screenHeight * 0.8f), 45, m_spaceShipSprites, projectileSystem, settings, content);
            m_spaceShips[3] = new SpaceShip(PlayerIndex.Four, new Vector2(settings.screenWidth * 0.8f, settings.screenHeight * 0.2f), -135, m_spaceShipSprites, projectileSystem, settings, content);
        }
コード例 #9
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));
            }
        }
コード例 #10
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;
        }