/*
         * Resets the current world
         */
        public virtual void resetWorld()
        {
            Mapfile tempFile = world.file;

            resourceComponent.killWorld(world);
            resourceComponent.loadWorld(currentWorldName, tempFile);
            setWorld(currentWorldName);
        }
Example #2
0
        internal bool firstUpdate;                 //Whether or not the world has been updated previously

        /**
         * Constructor.
         *
         * @param engine the engine
         * @param file the Mapfile to load the world from
         */
        public World(MirrorEngine engine, Mapfile file) : base(engine)
        {
            this.file = file;

            if (engine.resourceComponent == null)
            {
                throw new Exception("World: ResourceComponent not initialized");
            }
        }
        /**
         * Sets the currently running world
         *
         * @param filePath the map-key to load
         * @param mapFile the mapFile to load, overrides the filePath
         */
        public virtual void setWorld(string filePath, Mapfile mapFile = null)
        {
            string worldName = ResourceComponent.getKeyFromPath(filePath);

            if (worldName != currentWorldName)
            {
                resourceComponent.discardScriptStates();
            }

            if (!resourceComponent.loadWorld(filePath, mapFile))
            {
                return;
            }
            world = resourceComponent.getWorld(worldName);

            world.firstUpdate = true;
            currentWorldName  = worldName;
        }
        /*
         * If the given key hasn't been loaded, then loads the given key.
         * If a Mapfile has been provided, then it loads the world from the map, not from disk.
         *
         * @return whether the load was successful.
         */
        public bool loadWorld(string filePath, Mapfile map = null)
        {
            //Get the world's name
            string worldName = getKeyFromPath(filePath);

            //Load world if it hasn't been already
            if (!worlds.ContainsKey(worldName) || worlds[worldName] == null)
            {
                World w;

                if (map == null) //Load from disk
                {
                    //Check if file exists
                    if (!File.Exists(getFullPathFromKey(filePath)))
                    {
                        Trace.WriteLine(filePath + " does not exist.");
                        return(false);
                    }

                    w = engine.constructGameWorld(filePath);
                }
                else //Load from Mapfile
                {
                    w = engine.constructGameWorld(map);
                }

                //Load the world
                w.initialize();
                w.loadContent();

                //Add the world
                worlds[worldName] = w;
            }

            return(true);
        }
        private void resize()
        {
            if (widthBox.text.Length == 0 || heightBox.text.Length == 0)
            {
                return;
            }

            //Get dimensions
            int width;
            int height;

            try
            {
                width  = Convert.ToInt32(widthBox.text);
                height = Convert.ToInt32(heightBox.text);
            }
            catch (Exception e) { return; }

            World   tempWorld = editor.engine.world;
            Mapfile tempFile  = tempWorld.file;

            Mapfile.TileData emptyTileData = new Mapfile.TileData("");

            //Create new arrays
            Tile[,] newTileArray = new Tile[width, height];
            Mapfile.TileData[, ,] newTileData = new Mapfile.TileData[1, width, height];

            //Transfer tiles
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    if (x < tempWorld.width && y < tempWorld.height)
                    {
                        newTileArray[x, y]   = tempWorld.tileArray[x, y];
                        newTileData[0, x, y] = tempFile.worldTileData[0, x, y];
                    }
                    else
                    {
                        newTileData[0, x, y] = emptyTileData;
                        newTileArray[x, y]   = new Tile(tempWorld, x, y, emptyTileData);
                    }
                }
            }

            //Erase actors
            List <Actor> actorsToErase = new List <Actor>();

            foreach (Actor a in tempWorld.actors)
            {
                if (tempWorld.getTileAt(a.position.x, a.position.y) == null)
                {
                    actorsToErase.Add(a);
                }
            }
            foreach (Actor a in actorsToErase)
            {
                Mapfile.ActorData w = new Mapfile.ActorData();
                w.id = (byte)editor.engine.world.actorFactory.names[a.actorName];
                w.x  = a.spawn.x;
                w.y  = a.spawn.y;
                w.z  = 0;
                editor.engine.world.file.worldActorData.Remove(w);
                editor.engine.world.actors.Remove(a);
            }

            //Overwrite
            tempFile.worldTileData = newTileData;
            tempWorld.tileArray    = newTileArray;
            tempWorld.width        = width;
            tempWorld.height       = height;
        }
 /*
  * Constructs a game world
  *
  * @param file the Mapfile to load the world from
  */
 public abstract World constructGameWorld(Mapfile file);