コード例 #1
0
ファイル: Tile.cs プロジェクト: oconnerj/TyrantsQuest
        public Tile(float offX, float offY, Tileset mySet)
        {
            this.ParentTileset = mySet;
            this.OffsetX = offX * (float)ParentTileset.GetTex().Width;
            this.OffsetY = offY * (float)ParentTileset.GetTex().Height;

            Rect = new Rectangle((int)OffsetX, (int)OffsetY, 32, 32);

            // Determine collision type based on location in the tileset
            int tX = (int)(OffsetX / 32.0f);
            int tY = (int)(OffsetY / 32.0f);
            CollisionType = ParentTileset.GetCollision(tX, tY);
        }
コード例 #2
0
ファイル: Level.cs プロジェクト: oconnerj/TyrantsQuest
        /// <summary>
        /// Given the full path to a level file, loads the level and its related resources
        /// </summary>
        /// <param name="fullPath"></param>
        public void LoadLevel(string fullPath)
        {
            int     nBuffer;
            float   fBuffer;
            bool    bBuffer;

            if (!File.Exists(fullPath))
                throw new Exception("Level file " + fullPath + " doesn't exist. Cannot continue.");

            try
            {
                // Get a stream for the file
                using (var fStream = GetLevelFileStream(fullPath))
                {
                    // Now that got a stream to the file, set up a binary reader with that stream
                    BinaryReader file = new BinaryReader(fStream);

                    // WARNING: The game will only correctly load version 0.05 levels.
                    // JKO 11/26/2010 - I have added a check to ensure that we're using the right levels.

                    #region Format Spec
                    /*
                Format Spec v0.05:
                Signature (3 ints)
                Version (float)
                Level Name (string)
                Level Width (int) (pixels)
                Level Height (int) (pixels)
                Level Parallax (float)
                Level's Music Index (int)
                Level's Background Index (int)
                Level's Weather Index (int)
                Current Layer (int)
                Current Tileset (int)
                ScrollX (float)
                ScrollY (float)
                Editor Mode (int)
                Tool Mode (int)

                For Each Layer...
                    Layer State (bool)
                End For

                Number of Tilesets (int)

                For Each Tileset...
                    Tileset's Name (string)
                    Tileset's Width (int)
                    Tileset's Height (int)
                    (the two sizes are for creation of a temp texture in case of failure)
                End For

                For Each Layer...
                    For Each Tile That Exists...
                        Array Position (int) (this will be read as -1 if end of layer)
                        Tile's X (float)
                        Tile's Y (float)
                        Index of Tileset (int)
                    End For
                End For

                Number of Entities (int)

                For Each Entity...
                    Entity's name (string)
                    Entity's width (int)
                    Entity's height (int)
                    Entity's x-position (int)
                    Entity's y-position (int)
                End For

                For now, that's it.
                */
                    #endregion

                    // Read signature info
                    nBuffer = file.ReadInt32();
                    nBuffer = file.ReadInt32();
                    nBuffer = file.ReadInt32();
                    fBuffer = file.ReadSingle();

                    if (fBuffer > 0.050002 || fBuffer < 0.049998)
                        throw new Exception("The game attempted to load a version " + fBuffer.ToString() + " level. Only v0.05 is supported.");

                    name = file.ReadString();
                    width = file.ReadInt32();
                    height = file.ReadInt32();
                    parallax = file.ReadSingle();
                    indexMusic = file.ReadInt32();
                    indexBackground = file.ReadInt32();
                    indexWeather = file.ReadInt32();

                    // Read and discard editor info
                    nBuffer = file.ReadInt32();
                    nBuffer = file.ReadInt32();
                    fBuffer = file.ReadSingle();
                    fBuffer = file.ReadSingle();
                    nBuffer = file.ReadInt32();
                    nBuffer = file.ReadInt32();
                    for (int i = 0; i < 5; i++)
                        bBuffer = file.ReadBoolean();

                    // Read number of tilesets and create array
                    nBuffer = file.ReadInt32();
                    tilesets = new Tileset[nBuffer];

                    for (int i = 0; i < tilesets.Length; i++)
                    {
                        // Creating a new tileset object loads the image
                        tilesets[i] = new Tileset(file.ReadString(), game.Content);

                        // Read editor info and discard
                        nBuffer = file.ReadInt32();
                        nBuffer = file.ReadInt32();
                    }

                    // Load tiles
                    int tileIndex;
                    bool readTiles;
                    float oX, oY;
                    int setIndex;

                    // Create the tile array
                    // Each sub-dimension of the tiles array is as big as the level
                    tiles = new Tile[5, (width / 32) * (height / 32)];

                    for (int i = 0; i < 5; i++)
                    {
                        tileIndex = 0;
                        readTiles = true;
                        while (readTiles)
                        {
                            nBuffer = file.ReadInt32();
                            if (nBuffer < 0)
                                readTiles = false;

                            if (readTiles)
                            {
                                oX = file.ReadSingle();
                                oY = file.ReadSingle();
                                setIndex = file.ReadInt32();

                                tiles[i, nBuffer] = new Tile(oX, oY, tilesets[setIndex]);
                                tileIndex++;
                            }
                        }

                    }

                    // Load entities
                    int numEnts = file.ReadInt32();
                    string entName;
                    int entX, entY;

                    for (int i = 0; i < numEnts; i++)
                    {
                        entName = file.ReadString();

                        // Discard width and height info, they're not useful here
                        nBuffer = file.ReadInt32();
                        nBuffer = file.ReadInt32();

                        entX = file.ReadInt32();
                        entY = file.ReadInt32();
                        LoadEntity(entName, entX, entY);
                    }
                }

                // Level has been loaded.
            }
            catch (IOException e)
            {
                Debug.Fail("Level " + fullPath + " was unable to be loaded. Check permissions, etc.", e.ToString());
            }
        }