public bool Collides(Player player) { if (player.center.Z == z1) if (this.rectangle.Contains(player.rectangle)) if (player.direction == upDirection) { player.location.Z = z2; return true; } if (player.center.Z == z2) if (this.rectangle.Contains(player.rectangle)) if (player.direction == downDirection) { player.location.Z = z1; return true; } return false; }
List<Weapon> weapons; // a list to hold all of the weapons for the level #endregion Fields #region Constructors // constructor public Level(string mapFile) { // initializes things this.mapFile = mapFile; basicEnemies = new List<Enemy>(); walls = new List<Wall>(); weapons = new List<Weapon>(); coins = new List<Coin>(); doors = new List<Door>(); keys = new List<Key>(); disguises = new List<Disguise>(); floorSwitchers = new List<FloorSwitcher>(); font = ScreenManager.SharedManager.Content.Load<SpriteFont>("Arial"); graphics = ScreenManager.SharedManager.gDevice; spriteBatch = ScreenManager.SharedManager.sBatch; levelMap = new Map(mapFile, new int[11] { 1, 1, 1, 1, 1, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue, 0, 0 }); roomGraph = new RoomGraph(); foreach (var groupname in levelMap.ObjectGroups.Keys) // goes through each layer in the map file { int currentFloor = int.Parse(groupname[5].ToString()) - 1; // gets the floor number of the map layer. if (groupname.Substring(6, 8) == "Entities") // the layer contains GameObjects { foreach (var entity in levelMap.ObjectGroups[groupname]) // goes through each entity and creates a GameObject if possible. { if (entity.Type == "Enemy") // adds an enemy to the basicEnemies list. { string sDir = entity.Properties.Find(x => x.Item1 == "startDirection").Item2; int dir = (sDir != null) ? int.Parse(sDir) : 3; basicEnemies.Add(EntityGenerator.GenerateEnemy(new Vector3(entity.X, entity.Y, currentFloor), entity.Properties, dir)); } else if (entity.Type == "Player") // creates the player. { player = EntityGenerator.GeneratePlayer(new Vector3(entity.X, entity.Y, currentFloor), 5, 400); } else if (entity.Type == "Wall") // adds a wall to the walls list. { walls.Add(EntityGenerator.GenerateWall(new Vector3(entity.X, entity.Y, currentFloor), entity.Width, entity.Height)); } else if (entity.Type == "LockedDoor") // adds a door to the doors list. { doors.Add(EntityGenerator.GenerateDoor(new Vector3(entity.X, entity.Y, currentFloor), doors.Count)); } else if (entity.Type == "Key") // adds a key to the keys list. { keys.Add(EntityGenerator.GenerateKey(new Vector3(entity.X, entity.Y, currentFloor), keys.Count)); } else if (entity.Type == "Weapon") // adds a weapon to the weapons list. { weapons.Add(EntityGenerator.GenerateWeapon(new Vector3(entity.X, entity.Y, currentFloor), int.Parse(entity.Properties.Find(x => x.Item1 == "durability").Item2), entity.Properties.Find(x => x.Item1 == "type").Item2)); } else if (entity.Type == "Coin") // adds a coin to the coins list. { coins.Add(EntityGenerator.GenerateCoin(new Vector3(entity.X, entity.Y, currentFloor), int.Parse(entity.Properties.Find(x => x.Item1 == "value").Item2))); } else if (entity.Type == "Disguise") //adds a disguise to the disguises list. { disguises.Add(EntityGenerator.GenerateDisguise(new Vector3(entity.X, entity.Y, currentFloor), entity.Properties.Find(x => x.Item1 == "disguiseType").Item2)); } else if (entity.Type == "FloorSwitcher") // adds a floor switcher to the floorSwitchers list. { floorSwitchers.Add(new FloorSwitcher(new Rectangle(entity.X, entity.Y, entity.Width, entity.Height), currentFloor, currentFloor + 1, bool.Parse(entity.Properties.Find(x => x.Item1 == "Horizontal").Item2))); } } } if (groupname.Contains("Room")) // layer contains rooms { foreach (var entity in levelMap.ObjectGroups[groupname]) // goes through each room object in the layer. { // creates a new RoomGraphNode and sets up it's connections. RoomGraphNode node = new RoomGraphNode(new Rectangle(entity.X, entity.Y, entity.Width, entity.Height), currentFloor, entity.Name); List<string> connections = new List<string>(); foreach (var t in entity.Properties) { if (t.Item1 != null) connections.Add(t.Item1); if (t.Item2 != null) node.validDisguises.Add(t.Item2); } roomGraph.AddNode(node, connections); } } // how much money the player needs to complete the level is 70% of the total money in the level. player.moneyNeeded = (basicEnemies.Count + coins.Count) * 70; } foreach (Door lockedDoor in doors) // if there aren't enough keys, the extra locked doors are unlocked. { if (keys[lockedDoor.keyAssociation] == null) lockedDoor.locked = false; } // sets up what part of the level to show on screen. windowSpace = new Rectangle((int)(player.location.X + (player.rectangle.Width / 2)) - (graphics.Viewport.Width / 2), (int)(player.location.Y + (player.rectangle.Height / 2)) - (graphics.Viewport.Height / 2), graphics.Viewport.Width, graphics.Viewport.Height); }
/// <summary> /// Generates a Player object /// </summary> /// <param name="position">The position of the player</param> /// <param name="lives">the number of lives the player has</param> /// <param name="goal">The final goal of the player</param> /// <returns>A player object</returns> public static Player GeneratePlayer(Vector3 position, int lives, int goal) { Player player = new Player( GameVariables.playerSpeed, GameVariables.playerAnimations, GameVariables.playerAnimationNames, position, new Rectangle( (int)position.X, (int)position.Y, GameVariables.tileWidth, GameVariables.tileHeight), lives, goal); player.CurrentAnimation = "StandUp"; return player; }