示例#1
0
        /// <summary>
        /// Loads a new level asynchronously
        /// </summary>
        /// <param name="level">The name of the level to load</param>
        public void LoadLevel(string level)
        {
            Loading = true;
            DungeonCrawlerGame game = (DungeonCrawlerGame)this.game;

            ThreadStart threadStarter = delegate
            {
                if (!LoadedTilemaps.ContainsKey(level))
                {
                    CurrentMap = game.Content.Load<Tilemap>("Tilemaps/" + level);
                    CurrentMap.LoadContent(game.Content);

                    // Load the background music
                    //if (CurrentMap.MusicTitle != null && CurrentMap.MusicTitle != "")
                    //{
                    //    CurrentSong = game.Content.Load<Song>("Music/" + CurrentMap.MusicTitle);
                    //}
                    //else
                    //{
                    //    CurrentSong = null;
                    //}
                    currentRoomID = game.RoomFactory.CreateRoom(level, CurrentMap.Width, CurrentMap.Height, CurrentMap.TileWidth, CurrentMap.TileHeight, CurrentMap.WallWidth, level);
                    Room room = game.RoomComponent[currentRoomID];

                    for (int i = 0; i < CurrentMap.GameObjectGroupCount; i++)
                    {
                        for (int j = 0; j < CurrentMap.GameObjectGroups[i].GameObjectData.Count(); j++)
                        {
                            GameObjectData goData = CurrentMap.GameObjectGroups[i].GameObjectData[j];
                            Vector2 position = new Vector2(goData.Position.Center.X, goData.Position.Center.Y);

                            uint entityID = uint.MaxValue;

                        switch (goData.Category)
                        {
                            case "PlayerSpawn":
                                room.playerSpawns.Add(goData.properties["SpawnName"], new Vector2(goData.Position.X, goData.Position.Y));
                                break;
                            case "Enemy":
                                switch (goData.Type)
                                {
                                    case "MovingTarget":
                                        entityID = game.EnemyFactory.CreateEnemy(EnemyType.MovingTarget, new Position { Center = new Vector2(goData.Position.X, goData.Position.Y), RoomID = currentRoomID, Radius = 32 });
                                        break;
                                    case "StationaryTarget":
                                        entityID = game.EnemyFactory.CreateEnemy(EnemyType.MovingTarget, new Position { Center = new Vector2(goData.Position.X, goData.Position.Y), RoomID = currentRoomID, Radius = 32 });
                                        break;
                                    case "Alien":
                                        entityID = game.EnemyFactory.CreateEnemy(EnemyType.Alien, new Position { Center = new Vector2(goData.Position.X, goData.Position.Y), RoomID = currentRoomID, Radius = 16 });
                                        break;
                                    case "Spider":
                                        entityID = game.EnemyFactory.CreateEnemy(EnemyType.Spider, new Position { Center = new Vector2(goData.Position.X, goData.Position.Y), RoomID = currentRoomID, Radius = 16 });
                                        break;
                                    default:
                                        break;
                                }
                                break;
                            case "NPC":
                                entityID = game.NPCFactory.CreateNPC((NPCName) Enum.Parse(typeof(NPCName), goData.Type), new Position
                                            { Center = new Vector2(goData.Position.X, goData.Position.Y), RoomID = currentRoomID, Radius = 32});
                                break;
                            case "Trigger":
                                switch (goData.Type)
                                {
                                    case "Door":
                                        entityID = game.DoorFactory.CreateDoor(currentRoomID, goData.properties["DestinationRoom"], goData.properties["DestinationSpawnName"], goData.Position);
                                        break;
                                    case "Wall":
                                        entityID = game.WallFactory.CreateWall(currentRoomID, goData.Position);
                                        break;
                                    default:
                                        break;
                                }
                                break;
                        }
                            if (goData.properties.Keys.Contains("id"))
                            {
                                room.idMap.Add(goData.properties["id"], entityID);
                                room.targetTypeMap.Add(goData.properties["id"], goData.Type);
                            }

                        }
                    }

                    game.RoomComponent[currentRoomID] = room;

                    //// Load the game objects
                    //for (int i = 0; i < CurrentMap.GameObjectGroupCount; i++)
                    //{
                    //    for (int j = 0; j < CurrentMap.GameObjectGroups[i].GameObjectData.Count(); j++)
                    //    {
                    //        GameObjectData goData = CurrentMap.GameObjectGroups[i].GameObjectData[j];
                    //        Vector2 position = new Vector2(goData.Position.Center.X, goData.Position.Center.Y);
                    //        GameObject go;

                    //        switch (goData.Category)
                    //        {
                    //            case "PlayerStart":
                    //                ScrollingShooterGame.Game.Player.Position = position;
                    //                ScrollingShooterGame.Game.Player.LayerDepth = CurrentMap.GameObjectGroups[i].LayerDepth;
                    //                scrollDistance = -2 * position.Y + 300;
                    //                break;

                    //            case "LevelEnd":
                    //                break;

                    //            case "Powerup":
                    //                go = ScrollingShooterGame.GameObjectManager.CreatePowerup((PowerupType)Enum.Parse(typeof(PowerupType), goData.Type), position);
                    //                CurrentMap.GameObjectGroups[i].GameObjectData[j].ID = go.ID;
                    //                go.LayerDepth = CurrentMap.GameObjectGroups[i].LayerDepth;
                    //                go.ScrollingSpeed = CurrentMap.GameObjectGroups[i].ScrollingSpeed;
                    //                break;

                    //            case "Enemy":
                    //                go = ScrollingShooterGame.GameObjectManager.CreateEnemy((EnemyType)Enum.Parse(typeof(EnemyType), goData.Type), position);
                    //                CurrentMap.GameObjectGroups[i].GameObjectData[j].ID = go.ID;
                    //                go.LayerDepth = CurrentMap.GameObjectGroups[i].LayerDepth;
                    //                go.ScrollingSpeed = CurrentMap.GameObjectGroups[i].ScrollingSpeed;
                    //                break;
                    //            case "Boss":
                    //                go = ScrollingShooterGame.GameObjectManager.CreateBoss((BossType)Enum.Parse(typeof(BossType), goData.Type), position);
                    //                CurrentMap.GameObjectGroups[i].GameObjectData[j].ID = go.ID;
                    //                go.LayerDepth = CurrentMap.GameObjectGroups[i].LayerDepth;
                    //                go.ScrollingSpeed = CurrentMap.GameObjectGroups[i].ScrollingSpeed;
                    //                break;
                    //        }
                    //    }
                    //}

                    LoadedTilemaps.Add(level, CurrentMap);
                }
                else
                {
                    Room newRoom = game.RoomComponent.FindRoom(level);
                    if(newRoom.Tilemap == level)
                    {
                        CurrentMap = LoadedTilemaps[level];
                        currentRoomID = newRoom.EntityID;
                    }
                }

                // Mark level as loaded
                Loading = false;
            };

            Thread loadingThread = new Thread(threadStarter);
            loadingThread.Start();
        }
示例#2
0
        /// <summary>
        /// Loads a new level asynchronously
        /// </summary>
        /// <param name="level">The name of the level to load</param>
        public void LoadLevel(string level)
        {
            Loading = true;

            ThreadStart threadStarter = delegate
            {
                CurrentMap = game.Content.Load<Tilemap>("Tilemaps/" + level);
                CurrentMap.LoadContent(game.Content);

                // Load the background music
                //////////if (CurrentMap.MusicTitle != null && CurrentMap.MusicTitle != "")
                //////////{
                //////////    CurrentSong = game.Content.Load<Song>("Music/" + CurrentMap.MusicTitle);
                //////////}
                //////////else
                //////////{
                //////////    CurrentSong = null;
                //////////}

                for (int i = 0; i < CurrentMap.GameObjectGroupCount; i++)
                {
                    for (int j = 0; j < CurrentMap.GameObjectGroups[i].GameObjectData.Count(); j++)
                    {
                        GameObjectData goData = CurrentMap.GameObjectGroups[i].GameObjectData[j];
                        Vector2 position = new Vector2(goData.Position.Center.X, goData.Position.Center.Y);

                        switch (goData.Category)
                        {
                            case "PlayerSpawn":
                                break;

                            case "Enemy":
                                //goData.Type
                                break;
                            case "Trigger":
                                //goData.Type
                                break;

                        }
                    }
                }

                //// Load the game objects
                //for (int i = 0; i < CurrentMap.GameObjectGroupCount; i++)
                //{
                //    for (int j = 0; j < CurrentMap.GameObjectGroups[i].GameObjectData.Count(); j++)
                //    {
                //        GameObjectData goData = CurrentMap.GameObjectGroups[i].GameObjectData[j];
                //        Vector2 position = new Vector2(goData.Position.Center.X, goData.Position.Center.Y);
                //        GameObject go;

                //        switch (goData.Category)
                //        {
                //            case "PlayerStart":
                //                ScrollingShooterGame.Game.Player.Position = position;
                //                ScrollingShooterGame.Game.Player.LayerDepth = CurrentMap.GameObjectGroups[i].LayerDepth;
                //                scrollDistance = -2 * position.Y + 300;
                //                break;

                //            case "LevelEnd":
                //                break;

                //            case "Powerup":
                //                go = ScrollingShooterGame.GameObjectManager.CreatePowerup((PowerupType)Enum.Parse(typeof(PowerupType), goData.Type), position);
                //                CurrentMap.GameObjectGroups[i].GameObjectData[j].ID = go.ID;
                //                go.LayerDepth = CurrentMap.GameObjectGroups[i].LayerDepth;
                //                go.ScrollingSpeed = CurrentMap.GameObjectGroups[i].ScrollingSpeed;
                //                break;

                //            case "Enemy":
                //                go = ScrollingShooterGame.GameObjectManager.CreateEnemy((EnemyType)Enum.Parse(typeof(EnemyType), goData.Type), position);
                //                CurrentMap.GameObjectGroups[i].GameObjectData[j].ID = go.ID;
                //                go.LayerDepth = CurrentMap.GameObjectGroups[i].LayerDepth;
                //                go.ScrollingSpeed = CurrentMap.GameObjectGroups[i].ScrollingSpeed;
                //                break;
                //            case "Boss":
                //                go = ScrollingShooterGame.GameObjectManager.CreateBoss((BossType)Enum.Parse(typeof(BossType), goData.Type), position);
                //                CurrentMap.GameObjectGroups[i].GameObjectData[j].ID = go.ID;
                //                go.LayerDepth = CurrentMap.GameObjectGroups[i].LayerDepth;
                //                go.ScrollingSpeed = CurrentMap.GameObjectGroups[i].ScrollingSpeed;
                //                break;
                //        }
                //    }
                //}

                // Mark level as loaded
                Loading = false;
            };

            Thread loadingThread = new Thread(threadStarter);
            loadingThread.Start();
        }