Пример #1
0
        private void LoadLevel(string level, string episode)
        {
            string levelPath = PathOp.Combine(DualityApp.DataDirectory, "Episodes", episode, level);

            using (Stream s = FileOp.Open(PathOp.Combine(levelPath, ".res"), FileAccessMode.Read)) {
                // ToDo: Cache parser, move JSON parsing to ContentResolver
                JsonParser json = new JsonParser();
                LevelHandler.LevelConfigJson config = json.Parse <LevelHandler.LevelConfigJson>(s);

                //if (config.Version.LayerFormat > LevelHandler.LayerFormatVersion || config.Version.EventSet > LevelHandler.EventSetVersion) {
                //    throw new NotSupportedException("Version not supported");
                //}

                Console.WriteLine("Loading level \"" + config.Description.Name + "\"...");

                defaultNextLevel    = config.Description.NextLevel;
                defaultSecretLevel  = config.Description.SecretLevel;
                ambientLightDefault = config.Description.DefaultLight;
                ambientLightCurrent = ambientLightTarget = ambientLightDefault * 0.01f;

                string tilesetPath = PathOp.Combine(DualityApp.DataDirectory, "Tilesets", config.Description.DefaultTileset);

                ColorRgba[] tileMapPalette = TileSet.LoadPalette(PathOp.Combine(tilesetPath, ".palette"));

                ContentResolver.Current.ApplyBasePalette(tileMapPalette);

                tileMap = new TileMap(this, config.Description.DefaultTileset, (config.Description.Flags & LevelHandler.LevelFlags.HasPit) != 0);

                // Read all layers
                config.Layers.Add("Sprite", new LevelHandler.LevelConfigJson.LayerSection {
                    XSpeed = 1,
                    YSpeed = 1
                });

                foreach (var layer in config.Layers.OrderBy(layer => layer.Value.Depth))
                {
                    LayerType type;
                    if (layer.Key == "Sprite")
                    {
                        type = LayerType.Sprite;
                    }
                    else if (layer.Key == "Sky")
                    {
                        type = LayerType.Sky;

                        if (layer.Value.BackgroundStyle != 0 /*Plain*/ && layer.Value.BackgroundColor != null && layer.Value.BackgroundColor.Count >= 3)
                        {
                            camera.GetComponent <Camera>().ClearColor = new ColorRgba((byte)layer.Value.BackgroundColor[0], (byte)layer.Value.BackgroundColor[1], (byte)layer.Value.BackgroundColor[2]);
                        }
                    }
                    else
                    {
                        type = LayerType.Other;
                    }

                    tileMap.ReadLayerConfiguration(type, levelPath, layer.Key, layer.Value);
                }

                // Read animated tiles
                string animTilesPath = PathOp.Combine(levelPath, "Animated.tiles");
                if (FileOp.Exists(animTilesPath))
                {
                    tileMap.ReadAnimatedTiles(animTilesPath);
                }

                CameraController controller = camera.GetComponent <CameraController>();
                controller.ViewRect = new Rect(tileMap.Size * tileMap.Tileset.TileSize);

                // Read events
                //eventMap = new EventMap(this, tileMap.Size);

                //string eventsPath = PathOp.Combine(levelPath, "Events.layer");
                //if (FileOp.Exists(animTilesPath)) {
                //    eventMap.ReadEvents(eventsPath, config.Version.LayerFormat, difficulty);
                //}

                levelTexts = config.TextEvents ?? new Dictionary <int, string>();

                GameObject tilemapHandler = new GameObject("TilemapHandler");
                tilemapHandler.Parent = rootObject;
                tilemapHandler.AddComponent(tileMap);

                // Load default music
                //musicPath = PathOp.Combine(DualityApp.DataDirectory, "Music", config.Description.DefaultMusic);
                //music = DualityApp.Sound.PlaySound(new OpenMptStream(musicPath));
                //music.BeginFadeIn(0.5f);
            }
        }
Пример #2
0
        public bool ChangeLevel(string levelName, MultiplayerLevelType levelType)
        {
            string path = Path.Combine(DualityApp.DataDirectory, "Episodes", levelName + ".level");

            if (!File.Exists(path))
            {
                return(false);
            }

            IFileSystem levelPackage = new CompressedContent(path);

            lock (sync) {
                currentLevel     = levelName;
                currentLevelType = levelType;

                // Load new level
                using (Stream s = levelPackage.OpenFile(".res", FileAccessMode.Read)) {
                    // ToDo: Cache parser
                    JsonParser json = new JsonParser();
                    LevelHandler.LevelConfigJson config = json.Parse <LevelHandler.LevelConfigJson>(s);

                    if (config.Version.LayerFormat > LevelHandler.LayerFormatVersion || config.Version.EventSet > LevelHandler.EventSetVersion)
                    {
                        throw new NotSupportedException("Version not supported");
                    }

                    currentLevelFriendlyName = BitmapFont.StripFormatting(config.Description.Name);

                    Log.Write(LogType.Info, "Loading level \"" + currentLevelFriendlyName + "\" (" + currentLevelType + ")...");

                    Point2 tileMapSize;
                    using (Stream s2 = levelPackage.OpenFile("Sprite.layer", FileAccessMode.Read))
                        using (BinaryReader r = new BinaryReader(s2)) {
                            tileMapSize.X = r.ReadInt32();
                            tileMapSize.Y = r.ReadInt32();
                        }

                    levelBounds = new Rect(tileMapSize * /*tileMap.Tileset.TileSize*/ 32);

                    collisions = new DynamicTreeBroadPhase <ICollisionable>();

                    // Read events
                    eventMap = new ServerEventMap(tileMapSize);

                    if (levelPackage.FileExists("Events.layer"))
                    {
                        using (Stream s2 = levelPackage.OpenFile("Events.layer", FileAccessMode.Read)) {
                            eventMap.ReadEvents(s2, config.Version.LayerFormat);
                        }
                    }
                }

                // Send request to change level to all players
                foreach (KeyValuePair <NetConnection, Player> pair in players)
                {
                    pair.Value.State = PlayerState.NotReady;
                }

                playerConnections.Clear();

                foreach (KeyValuePair <NetConnection, Player> pair in players)
                {
                    Send(new LoadLevel {
                        LevelName           = currentLevel,
                        LevelType           = currentLevelType,
                        AssignedPlayerIndex = pair.Value.Index
                    }, 64, pair.Key, NetDeliveryMethod.ReliableUnordered, PacketChannels.Main);
                }
            }

            return(true);
        }