Пример #1
0
        public MapBlockData LoadFile(String path)
        {
            MapBlockData retval = null;

            if (path == null)
            {
                throw new NullReferenceException("Parameter path cannot be null");
            }
            FileStream   stream = null;
            BinaryReader reader = null;

            try {
                stream = File.OpenRead(path);
                reader = new BinaryReader(stream);

                int fileVersion = reader.ReadInt32();
                if (fileVersion > maploaders.Length || fileVersion < 0)
                {
                    //TODO: display fileversion in exception
                    throw new Exception("File Version {0} is unsupported");
                }

                retval = maploaders[fileVersion].LoadFile(reader);
            }catch (Exception)
            {
                //TODO: log exceptions
            }finally {
                if (reader != null)
                {
                    reader.Close();
                    reader = null;
                }
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                    stream = null;
                }
            }
            return(retval);
        }
Пример #2
0
        public void SaveFile(MapBlockData mapData, String path)
        {
            if (mapData == null)
            {
                throw new NullReferenceException("Parameter mapData cannot be null");
            }
            if (path == null)
            {
                throw new NullReferenceException("Parameter path cannot be null");
            }
            // Create folder structure or saving will fail
            if (!File.Exists(path))
            {
                string dirname = Path.GetDirectoryName(path);
                Directory.CreateDirectory(dirname);
            }

            FileStream   stream = null;
            BinaryWriter writer = null;

            try {
                stream = File.OpenWrite(path);
                writer = new BinaryWriter(stream);

                writer.Write(maploaders.Length - 1);
                maploaders[maploaders.Length - 1].SaveFile(mapData, writer);
            }finally {
                if (writer != null)
                {
                    writer.Close();
                    writer = null;
                }
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                    stream = null;
                }
            }
        }
Пример #3
0
        public IEnumerator Initialize(int blockX, int blockY, MapBlockData mapBlockData, String mapPath, YieldDirection yieldDirection, ResourceManager resourceManager, AsciiMapScript asciiMapScript)
        {
            this.blockX          = blockX;
            this.blockY          = blockY;
            this.mapBlockData    = mapBlockData;
            this.mapPath         = mapPath;
            this.resourceManager = resourceManager;
            this.asciiMapScript  = asciiMapScript;
            this.mapfile         = new MapFile();
            if (this.prefabWall == null)
            {
                this.prefabWall = resourceManager.getGameObject("Main/Wall");
            }
            if (this.prefabWorldWall == null)
            {
                this.prefabWorldWall = resourceManager.getGameObject("Main/worldwall");
            }

            if (mapBlockData == null)
            {
                // Empty map blocks will display as walls
                CreateMapObject((MapRows / 2), (MapCols / 2), prefabWorldWall, this.gameObject, "Main/WorldWall", 6, MapLayer.Floor);
            }
            else
            {
                if (yieldDirection == YieldDirection.YieldRight)
                {
                    for (int x = 0; x < mapBlockData.getRows(); x++)
                    {
                        if (this != null)
                        {
                            for (int y = 0; y < mapBlockData.getCols(); y++)
                            {
                                if (x < 0 || x == mapBlockData.getRows() || y < 0 || y == mapBlockData.getCols())
                                {
                                    CreateMapObject(x, y, prefabWall, this.gameObject, "Main/Wall", 6, MapLayer.Floor);
                                }
                                else
                                {
                                    //GameObject floorObject = mapData.getFloor (x, y);
                                    GameObject floorObject = resourceManager.getGameObject(mapBlockData.getFloorResource(x, y));
                                    if (floorObject != null)
                                    {
                                        CreateMapObject(x, y, floorObject, this.gameObject, mapBlockData.getFloorResource(x, y), mapBlockData.getFloorInt(x, y), MapLayer.Floor);
                                    }
                                    //GameObject mainObject = mapData.getMain (x, y);
                                    GameObject mainObject = resourceManager.getGameObject(mapBlockData.getMainResource(x, y));
                                    if (mainObject != null)
                                    {
                                        CreateMapObject(x, y, mainObject, this.gameObject, mapBlockData.getMainResource(x, y), mapBlockData.getMainInt(x, y), MapLayer.Main);
                                    }
                                }
                            }
                        }
                        yield return(null);
                    }
                }
                else if (yieldDirection == YieldDirection.YieldLeft)
                {
                    for (int x = mapBlockData.getRows() - 1; x >= 0; x--)
                    {
                        if (this != null)
                        {
                            for (int y = 0; y < mapBlockData.getCols(); y++)
                            {
                                if (x < 0 || x == mapBlockData.getRows() || y < 0 || y == mapBlockData.getCols())
                                {
                                    CreateMapObject(x, y, prefabWall, this.gameObject, "Main/Wall", 6, MapLayer.Floor);
                                }
                                else
                                {
                                    //GameObject floorObject = mapData.getFloor (x, y);
                                    GameObject floorObject = resourceManager.getGameObject(mapBlockData.getFloorResource(x, y));
                                    if (floorObject != null)
                                    {
                                        CreateMapObject(x, y, floorObject, this.gameObject, mapBlockData.getFloorResource(x, y), mapBlockData.getFloorInt(x, y), MapLayer.Floor);
                                    }
                                    //GameObject mainObject = mapData.getMain (x, y);
                                    GameObject mainObject = resourceManager.getGameObject(mapBlockData.getMainResource(x, y));
                                    if (mainObject != null)
                                    {
                                        CreateMapObject(x, y, mainObject, this.gameObject, mapBlockData.getMainResource(x, y), mapBlockData.getMainInt(x, y), MapLayer.Main);
                                    }
                                }
                            }
                        }
                        yield return(null);
                    }
                }
                else if (yieldDirection == YieldDirection.YieldDown)
                {
                    if (this != null)
                    {
                        for (int y = 0; y < mapBlockData.getCols(); y++)
                        {
                            for (int x = 0; x < mapBlockData.getRows(); x++)
                            {
                                if (x < 0 || x == mapBlockData.getRows() || y < 0 || y == mapBlockData.getCols())
                                {
                                    CreateMapObject(x, y, prefabWall, this.gameObject, "Main/Wall", 6, MapLayer.Floor);
                                }
                                else
                                {
                                    //GameObject floorObject = mapData.getFloor (x, y);
                                    GameObject floorObject = resourceManager.getGameObject(mapBlockData.getFloorResource(x, y));
                                    if (floorObject != null)
                                    {
                                        CreateMapObject(x, y, floorObject, this.gameObject, mapBlockData.getFloorResource(x, y), mapBlockData.getFloorInt(x, y), MapLayer.Floor);
                                    }
                                    //GameObject mainObject = mapData.getMain (x, y);
                                    GameObject mainObject = resourceManager.getGameObject(mapBlockData.getMainResource(x, y));
                                    if (mainObject != null)
                                    {
                                        CreateMapObject(x, y, mainObject, this.gameObject, mapBlockData.getMainResource(x, y), mapBlockData.getMainInt(x, y), MapLayer.Main);
                                    }
                                }
                            }
                        }
                        yield return(null);
                    }
                }
                else if (yieldDirection == YieldDirection.YieldUp)
                {
                    for (int y = mapBlockData.getCols() - 1; y >= 0; y--)
                    {
                        if (this != null)
                        {
                            for (int x = 0; x < mapBlockData.getRows(); x++)
                            {
                                if (x < 0 || x == mapBlockData.getRows() || y < 0 || y == mapBlockData.getCols())
                                {
                                    CreateMapObject(x, y, prefabWall, this.gameObject, "Main/Wall", 6, MapLayer.Floor);
                                }
                                else
                                {
                                    //GameObject floorObject = mapData.getFloor (x, y);
                                    GameObject floorObject = resourceManager.getGameObject(mapBlockData.getFloorResource(x, y));
                                    if (floorObject != null)
                                    {
                                        CreateMapObject(x, y, floorObject, this.gameObject, mapBlockData.getFloorResource(x, y), mapBlockData.getFloorInt(x, y), MapLayer.Floor);
                                    }
                                    //GameObject mainObject = mapData.getMain (x, y);
                                    GameObject mainObject = resourceManager.getGameObject(mapBlockData.getMainResource(x, y));
                                    if (mainObject != null)
                                    {
                                        CreateMapObject(x, y, mainObject, this.gameObject, mapBlockData.getMainResource(x, y), mapBlockData.getMainInt(x, y), MapLayer.Main);
                                    }
                                }
                            }
                        }
                        yield return(null);
                    }
                }
                else
                {
                    for (int x = 0; x < mapBlockData.getRows(); x++)
                    {
                        if (this != null)
                        {
                            for (int y = 0; y < mapBlockData.getCols(); y++)
                            {
                                if (x < 0 || x == mapBlockData.getRows() || y < 0 || y == mapBlockData.getCols())
                                {
                                    CreateMapObject(x, y, prefabWall, this.gameObject, "Main/Wall", 6, MapLayer.Floor);
                                }
                                else
                                {
                                    //GameObject floorObject = mapData.getFloor (x, y);
                                    GameObject floorObject = resourceManager.getGameObject(mapBlockData.getFloorResource(x, y));
                                    if (floorObject != null)
                                    {
                                        CreateMapObject(x, y, floorObject, this.gameObject, mapBlockData.getFloorResource(x, y), mapBlockData.getFloorInt(x, y), MapLayer.Floor);
                                    }
                                    //GameObject mainObject = mapData.getMain (x, y);
                                    GameObject mainObject = resourceManager.getGameObject(mapBlockData.getMainResource(x, y));
                                    if (mainObject != null)
                                    {
                                        CreateMapObject(x, y, mainObject, this.gameObject, mapBlockData.getMainResource(x, y), mapBlockData.getMainInt(x, y), MapLayer.Main);
                                    }
                                }
                            }
                        }
                        yield return(null);
                    }
                }
            }
            isInitialized = true;
            // Spawn a zombie
            if (blockX == 3 && blockY == 3 && this != null)
            {
                CreateMapObject(5, 10, resourceManager.getGameObject("main/zombie"), this.gameObject, "main/zombie", 7, MapLayer.Main);
                //	CreateMapObject (5, 5, resourceManager.getGameObject ("main/npc"), this.gameObject, "main/npc", 7, MapLayer.Main);
            }
        }