/// <summary> /// Load a map from a file /// </summary> /// <param name="filename">The filename to read from</param> /// <param name="player">The starting position on the loaded map</param> public void ReadXML(XmlReader xmlr, Backend.Coords targetCoords = null, bool resetPlayer = false) { List<Player> players = new List<Player>(); // Move all players to the new map if (_actors.Count > 0) { for (int i = 0; i < _actors.Count; ++i) { if (_actors[i].tile != null) _actors[i].tile.enabled = false; if (_actors[i] is Player) { players.Add(_actors[i] as Player); players[players.Count - 1].tile = null; break; } } } ClearActors(); _tiles.Clear(); _updateTiles.Clear(); xmlr.MoveToContent();//xml _width = int.Parse(xmlr.GetAttribute("width")); _height = int.Parse(xmlr.GetAttribute("height")); if (xmlr.GetAttribute("name") != null) _name = xmlr.GetAttribute("name"); if (xmlr.GetAttribute("level") != null) _level = int.Parse(xmlr.GetAttribute("level")); if (xmlr.GetAttribute("dungeon") != null) _dungeonname = xmlr.GetAttribute("dungeon"); if (xmlr.GetAttribute("floor") != null) _floorFile = xmlr.GetAttribute("floor"); if (xmlr.GetAttribute("wall") != null) _wallFile = xmlr.GetAttribute("wall"); if (xmlr.GetAttribute("id") != null) _id = Int32.Parse(xmlr.GetAttribute("id")); if (xmlr.GetAttribute("light") != null) _light = int.Parse(xmlr.GetAttribute("light")); if (xmlr.GetAttribute("music") != null) _music = xmlr.GetAttribute("music"); xmlr.ReadStartElement("GameMap");//GameMap for (int row = 0; row < _height; ++row) { _tiles.Add(new List<FloorTile>()); for (int col = 0; col < _width; ++col) { _tiles[row].Add(new FloorTile(this, new Backend.Coords(col, row))); } } while ((xmlr.NodeType != XmlNodeType.EndElement) && (xmlr.NodeType != XmlNodeType.None)) { // Add Tiles and overlay-Tiles switch (xmlr.Name) { case "Tile": FloorTile tile = _tiles[Int32.Parse(xmlr.GetAttribute("CoordY"))][Int32.Parse(xmlr.GetAttribute("CoordX"))]; if (xmlr.GetAttribute("visited") != null) { tile.visible = Boolean.Parse(xmlr.GetAttribute("visited")); } if (!xmlr.IsEmptyElement) { xmlr.Read(); while ((xmlr.NodeType != XmlNodeType.EndElement)) { switch (xmlr.Name) { case "WallTile": WallTile wall = new WallTile(this); if (xmlr.GetAttribute("Enabled") != null) wall.enabled = Boolean.Parse(xmlr.GetAttribute("Enabled")); if (xmlr.GetAttribute("Health") != null) wall.health = Int32.Parse(xmlr.GetAttribute("Health")); if (xmlr.GetAttribute("Type") != null) wall.type = (WallType)Enum.Parse(typeof(WallType), xmlr.GetAttribute("Type").ToString()); if (xmlr.GetAttribute("Illusion") != null) wall.illusion = Boolean.Parse(xmlr.GetAttribute("Illusion")); if (xmlr.GetAttribute("Illusionvisible") != null) wall.illusionVisible = Boolean.Parse(xmlr.GetAttribute("Illusionvisible")); tile.Add(wall); break; case "ProjectileTile": { uint id = 0; if (xmlr.GetAttribute("id", _id.ToString()) != null) id = UInt32.Parse(xmlr.GetAttribute("id")); Direction dir = Direction.Up; if (xmlr.GetAttribute("direction", _id.ToString()) != null) dir = (Direction)Enum.Parse(typeof(Direction), xmlr.GetAttribute("direction").ToString()); ProjectileTile projectile = new ProjectileTile(tile, dir, id); } break; case "ItemTile": Item item = new Item(); xmlr.Read(); item.Load(xmlr); ItemTile itemTile = new ItemTile(tile, item); item.tile = itemTile; tile.Add(itemTile); xmlr.Read(); // End Item break; case "TargetTile": tile.Add(new TargetTile(tile)); break; case "TrapTile": TrapTile trap = new TrapTile(this, Int32.Parse(xmlr.GetAttribute("damage"))); if (xmlr.GetAttribute("penetrate") != null) trap.penetrate = Int32.Parse(xmlr.GetAttribute("penetrate")); if (xmlr.GetAttribute("evade") != null) trap.evade = Int32.Parse(xmlr.GetAttribute("evade")); if (xmlr.GetAttribute("changing") != null) { trap.type = trap.type | TrapType.Changing; } if (xmlr.GetAttribute("hidden") != null) { trap.type = trap.type | TrapType.Hidden; } if (xmlr.GetAttribute("onlyonce") != null) { trap.type = trap.type | TrapType.OnlyOnce; } if (xmlr.GetAttribute("broken") != null) { trap.status = TrapState.Destroyed; } if (xmlr.GetAttribute("disabled") != null) { trap.status = TrapState.Disabled; } if (xmlr.GetAttribute("invisible") != null) { trap.status = TrapState.NoDisplay; } tile.Add(trap); _updateTiles.Add(tile.coords); break; case "ReservedTile": ReservedTile reserved = new ReservedTile(tile); if (xmlr.GetAttribute("Enabled") != null) { reserved.enabled = Boolean.Parse(xmlr.GetAttribute("Enabled")); } if (xmlr.GetAttribute("CanEnter") != null) { reserved.canEnter = Boolean.Parse(xmlr.GetAttribute("CanEnter")); } if (xmlr.GetAttribute("Filename") != null) { reserved.filename = xmlr.GetAttribute("Filename"); } if (xmlr.GetAttribute("Index") != null) { reserved.index = Int32.Parse(xmlr.GetAttribute("Index")); } tile.Add(reserved); break; case "DoorTile": DoorTile door = new DoorTile(tile); if (xmlr.GetAttribute("open") != null) { door.open = Boolean.Parse(xmlr.GetAttribute("open")); } if (xmlr.GetAttribute("key") != null) { door.key = int.Parse(xmlr.GetAttribute("key")); } tile.Add(door); break; case "TeleportTile": TeleportTile transporter = new TeleportTile(tile, xmlr.GetAttribute("nextRoom"), new Backend.Coords(Int32.Parse(xmlr.GetAttribute("nextX")), Int32.Parse(xmlr.GetAttribute("nextY")))); if (xmlr.GetAttribute("hidden") != null) { transporter.hidden = Boolean.Parse(xmlr.GetAttribute("hidden")); } if (xmlr.GetAttribute("enabled") != null) { transporter.enabled = Boolean.Parse(xmlr.GetAttribute("enabled")); } if (xmlr.GetAttribute("teleport") != null) { transporter.teleport = Boolean.Parse(xmlr.GetAttribute("teleport")); } if (xmlr.GetAttribute("down") != null) { transporter.down = Boolean.Parse(xmlr.GetAttribute("down")); } tile.Add(transporter); break; case "TriggerTile": Backend.Coords target = new Backend.Coords(-1, -1); if (xmlr.GetAttribute("affectX") != null) target.x = Int32.Parse(xmlr.GetAttribute("affectX")); if (xmlr.GetAttribute("affectY") != null) target.y = Int32.Parse(xmlr.GetAttribute("affectY")); TriggerTile trigger = new TriggerTile(tile, target); if (xmlr.GetAttribute("explanation") != null) { trigger.explanationEnabled = xmlr.GetAttribute("explanation"); }; if (xmlr.GetAttribute("explanationdisabled") != null) { trigger.explanationDisabled = xmlr.GetAttribute("explanationdisabled"); }; if (xmlr.GetAttribute("enabled") != null) { trigger.enabled = Boolean.Parse(xmlr.GetAttribute("enabled")); }; if (xmlr.GetAttribute("repeat") != null) { trigger.repeat = Int32.Parse(xmlr.GetAttribute("repeat")); }; if (xmlr.GetAttribute("alwaysShowDisabled") != null) { trigger.alwaysShowDisabled = Boolean.Parse(xmlr.GetAttribute("alwaysShowDisabled")); }; if (xmlr.GetAttribute("alwaysShowEnabled") != null) { trigger.alwaysShowEnabled = Boolean.Parse(xmlr.GetAttribute("alwaysShowEnabled")); }; if (xmlr.GetAttribute("tileimage") != null) { trigger.tileimage = xmlr.GetAttribute("tileimage"); }; if (xmlr.GetAttribute("reactToEnemies") != null) { trigger.reactToEnemies = Boolean.Parse(xmlr.GetAttribute("reactToEnemies")); }; if (xmlr.GetAttribute("reactToObjects") != null) { trigger.reactToObjects = Boolean.Parse(xmlr.GetAttribute("reactToObjects")); }; if (xmlr.GetAttribute("reactToItem") != null) { trigger.reactToItem = Int32.Parse(xmlr.GetAttribute("reactToItem")); } tile.Add(trigger); break; case "CheckpointTile": int bonusLifes = 0; bool visited = false; if (xmlr.GetAttribute("bonuslife") != null) bonusLifes = Convert.ToInt32(xmlr.GetAttribute("bonuslife")); if (xmlr.GetAttribute("visited") != null) visited = Convert.ToBoolean(xmlr.GetAttribute("visited")); tile.Add(new CheckpointTile(tile, visited, bonusLifes)); break; case "ActorTile": Actor actor; xmlr.Read(); switch (xmlr.Name) { case "Enemy": actor = new Enemy(); actor.Load(xmlr); break; case "Player": actor = new Player(); actor.Load(xmlr); break; default: actor = new NPC(); actor.Load(xmlr); break; } while (actor.id > _actors.Count - 1) { _actors.Add(new NPC()); _actors[_actors.Count - 1].id = _actors.Count - 1; } if (!(actor is Player)) { ActorTile tile2 = new ActorTile(tile, actor); tile2.enabled = (actor.health > 0); actor.tile = tile2; tile.Add(tile2); _actors[actor.id] = actor; _updateTiles.Add(tile.coords); } else { if (players.Count > 0) { bool found = false; for (int i = 0; i < players.Count; ++i) { if (players[i].GUID == actor.GUID) { if (!resetPlayer) actor.copyFrom(players[i]); players.RemoveAt(i); found = true; break; } } } if (targetCoords == null) { targetCoords = tile.coords; } if (!resetPlayer) { ActorTile actortile = new ActorTile(this[targetCoords], actor); actor.tile = actortile; actortile.enabled = (actor.health > 0); actortile.parent = this[targetCoords]; this[targetCoords].Add(actortile); _actors[actor.id] = actor; _updateTiles.Add(targetCoords); Uncover(actors[actor.id].tile.coords, actors[actor.id].viewRange); } else { ActorTile actortile = new ActorTile(tile, actor); actor.tile = actortile; actortile.enabled = (actor.health > 0); actortile.parent = tile; tile.Add(actortile); _actors[actor.id] = actor; _updateTiles.Add(tile.coords); Uncover(actors[actor.id].tile.coords, actors[actor.id].viewRange); } } break; } xmlr.Read(); } xmlr.ReadEndElement(); } else { xmlr.Read(); } break; } } if (targetCoords == null) { targetCoords = new Coords(1, 1); } while (players.Count > 0) { ActorTile actortile = new ActorTile(this[targetCoords], players[0]); actortile.enabled = (players[0].health > 0); actortile.parent = this[targetCoords]; this[targetCoords].Add(actortile); players[0].tile = actortile; _updateTiles.Add(targetCoords); bool found = false; foreach (Actor a in _actors) { if ((a.GUID == players[0].GUID) && (players[0].GUID != "")) { found = true; a.copyFrom(players[0]); a.tile = actortile; break; } } if (!found) { foreach (Actor a in _actors) { if ((a is Player) && (a.GUID == "")) { found = true; a.copyFrom(players[0]); a.GUID = players[0].GUID; a.tile = actortile; break; } } } if (!found) { players[0].id = _actors.Count; _actors.Add(players[0]); } players.RemoveAt(0); } foreach (Actor a in _actors) { if (a.tile == null) { ActorTile actortile = new ActorTile(this[targetCoords], a); actortile.enabled = (a.health > 0); actortile.parent = this[targetCoords]; this[targetCoords].Add(actortile); a.tile = actortile; a.online = false; _updateTiles.Add(targetCoords); } } }
/// <summary> /// Method to read actor properties from a XML-file. /// </summary> /// <param name="reader">The used XMLReader for reading the actor.</param> public void Load(XmlReader reader) { // System.Diagnostics.Debug.WriteLine(reader.Name); _newItems = 0; _inventory.Clear(); if (reader.GetAttribute("name") != null) _name = reader.GetAttribute("name"); if (reader.GetAttribute("maxhp") != null) _maxhealth = Convert.ToInt32(reader.GetAttribute("maxhp")); if (reader.GetAttribute("id") != null) _id = Convert.ToInt32(reader.GetAttribute("id")); if (reader.GetAttribute("maxMana") != null) _maxMana = Convert.ToInt32(reader.GetAttribute("maxMana")); if (reader.GetAttribute("file") != null) _animationFile = Convert.ToString(reader.GetAttribute("file")); if (reader.GetAttribute("viewRange") != null) _viewRange = Convert.ToInt32(reader.GetAttribute("viewRange")); if (reader.GetAttribute("online") != null) _online = Convert.ToBoolean(reader.GetAttribute("online")); if (reader.GetAttribute("hp") != null) _health = Convert.ToInt32(reader.GetAttribute("hp")); else _health = _maxhealth; if (reader.GetAttribute("level") != null) _level = Convert.ToInt32(reader.GetAttribute("level")); if (reader.GetAttribute("mana") != null) _mana = Convert.ToInt32(reader.GetAttribute("mana")); else _mana = maxMana; if (reader.GetAttribute("evade") != null) _evade = Convert.ToInt32(reader.GetAttribute("evade")); if (reader.GetAttribute("block") != null) _block = Convert.ToInt32(reader.GetAttribute("block")); if (reader.GetAttribute("penetrate") != null) _penetrate = Convert.ToInt32(reader.GetAttribute("penetrate")); if (reader.GetAttribute("healthReg") != null) _healthReg = Convert.ToInt32(reader.GetAttribute("healthReg")); if (reader.GetAttribute("skills") != null) _skills = Convert.ToInt32(reader.GetAttribute("skills")); if (reader.GetAttribute("abilityPoints") != null) _abilityPoints = Convert.ToInt32(reader.GetAttribute("abilityPoints")); if (reader.GetAttribute("armor") != null) _armor = Convert.ToInt32(reader.GetAttribute("armor")); if (reader.GetAttribute("stealHealth") != null) _stealHealth = Convert.ToInt32(reader.GetAttribute("stealHealth")); if (reader.GetAttribute("stealMana") != null) _stealMana = Convert.ToInt32(reader.GetAttribute("stealMana")); if (reader.GetAttribute("fireDamage") != null) _fireDamage = Convert.ToInt32(reader.GetAttribute("fireDamage")); if (reader.GetAttribute("iceDamage") != null) _iceDamage = Convert.ToInt32(reader.GetAttribute("iceDamage")); if (reader.GetAttribute("fireDefense") != null) _fireDefense = Convert.ToInt32(reader.GetAttribute("fireDefense")); if (reader.GetAttribute("iceDefense") != null) _iceDefense = Convert.ToInt32(reader.GetAttribute("iceDefense")); if (reader.GetAttribute("expNeeded") != null) _expNeeded = Convert.ToInt32(reader.GetAttribute("expNeeded")); if (reader.GetAttribute("exp") != null) _exp = Convert.ToInt32(reader.GetAttribute("exp")); if (reader.GetAttribute("resist") != null) _resist = Convert.ToInt32(reader.GetAttribute("resist")); if (reader.GetAttribute("damage") != null) _damage = Convert.ToInt32(reader.GetAttribute("damage")); if (reader.GetAttribute("level") != null) _level = Convert.ToInt32(reader.GetAttribute("level")); if (reader.GetAttribute("gold") != null) _gold = Convert.ToInt32(reader.GetAttribute("gold")); if (reader.GetAttribute("manaReg") != null) _manaReg = Convert.ToInt32(reader.GetAttribute("manaReg")); if (reader.GetAttribute("destroyWeapon") != null) _destroyWeapon = Convert.ToInt32(reader.GetAttribute("destroyWeapon")); if (reader.GetAttribute("destroyArmor") != null) _destroyArmor = Convert.ToInt32(reader.GetAttribute("destroyArmor")); if (reader.GetAttribute("direction") != null) _direction = (Direction)Enum.Parse(typeof(Direction), reader.GetAttribute("direction")); if (_direction == Direction.None) _direction = Direction.Up; if (!(this is Player)) { if (reader.GetAttribute("crazy") != null) _crazy = Convert.ToBoolean(reader.GetAttribute("crazy")); if (reader.GetAttribute("ranged") != null) _ranged = Convert.ToBoolean(reader.GetAttribute("ranged")); if (reader.GetAttribute("aggro") != null) _aggro = Convert.ToBoolean(reader.GetAttribute("aggro")); if (reader.GetAttribute("friendly") != null) _friendly = Convert.ToBoolean(reader.GetAttribute("friendly")); } else { if (reader.GetAttribute("lives") != null) _lives = Convert.ToInt32(reader.GetAttribute("lives")); if (reader.GetAttribute("GUID") != null) _GUID = reader.GetAttribute("GUID"); if (reader.GetAttribute("lastCheckpoint") != null) { _lastCheckpoint = Convert.ToInt32(reader.GetAttribute("lastCheckpoint")); _checkPointCoords = new Coords(Convert.ToInt32(reader.GetAttribute("cpX")), Convert.ToInt32(reader.GetAttribute("cpY"))); } } if (reader.GetAttribute("stunned") != null) _stunned = Convert.ToInt32(reader.GetAttribute("stunned")); if (reader.GetAttribute("charmed") != null) _charmed = Convert.ToInt32(reader.GetAttribute("charmed")); if (reader.GetAttribute("scared") != null) _scared = Convert.ToInt32(reader.GetAttribute("scared")); bool hasQuests = false; switch (_actorType) { case ActorType.NPC: NPC n = this as NPC; if (n != null) { if (reader.GetAttribute("love") != null) n.love = Convert.ToInt32(reader.GetAttribute("love")); if (reader.GetAttribute("hasShop") != null) n.hasShop = Convert.ToBoolean(reader.GetAttribute("hasShop")); if (reader.GetAttribute("hasDialogue") != null) n.hasDialog = Convert.ToBoolean(reader.GetAttribute("hasDialogue")); if (reader.GetAttribute("hasQuests") != null) hasQuests = true; n.ClearQuests(); } break; case ActorType.Player: (this as Player).ClearQuests(); if (reader.GetAttribute("hasQuests") != null) hasQuests = true; break; } reader.Read(); if (reader.IsEmptyElement) { reader.Read(); } else { reader.Read(); while (reader.NodeType != XmlNodeType.EndElement) { Item item = new Item(); item.Load(reader); item.owner = this; _inventory.Add(item); if (item.isNew) _newItems += 1; reader.Read(); } reader.ReadEndElement(); } // System.Diagnostics.Debug.WriteLine(reader.Name); if (reader.Name == "Abilities") { // System.Diagnostics.Debug.WriteLine("Abilities"); if (reader.IsEmptyElement) { reader.Read(); } else { reader.Read(); while (reader.NodeType != XmlNodeType.EndElement) { Ability ability = new Ability(); ability.Load(reader); _abilities.Add(ability); reader.Read(); } reader.ReadEndElement(); } if (hasQuests) { if (reader.IsEmptyElement) { reader.Read(); } else { reader.Read(); while (reader.NodeType != XmlNodeType.EndElement) { /* Quest.QuestType q1 = (Quest.QuestType)Enum.Parse(typeof(Quest.QuestType), reader.GetAttribute("type")); string text = Convert.ToString(reader.GetAttribute("text")); int xp = Convert.ToInt32(reader.GetAttribute("xp")); List<Item> itemList = new List<Item>(); int goal = Convert.ToInt32(reader.GetAttribute("goal")); reader.Read(); if (reader.IsEmptyElement) { reader.Read(); } else { reader.Read(); while (reader.NodeType != XmlNodeType.EndElement) { Item item = new Item(); item.Load(reader); item.owner = null; item.tile = null; itemList.Add(item); reader.Read(); } reader.ReadEndElement(); } Quest q = new Quest(q1, text, xp, itemList, goal); (this as Player).AddQuest(q); reader.Read();*/ } reader.ReadEndElement(); } } // Read Quickbar / Common attacks (ranked by frequency) if (reader.IsEmptyElement) { reader.Read(); } else { // System.Diagnostics.Debug.WriteLine("Quickbar"); int id = 0; reader.Read(); while (reader.NodeType != XmlNodeType.EndElement) { // System.Diagnostics.Debug.WriteLine(reader.Name); if (reader.HasAttributes) { _quicklist[id] = Convert.ToInt32(reader.GetAttribute(0)); // System.Diagnostics.Debug.WriteLine(reader.GetAttribute(0)); ++id; } reader.Read(); } reader.ReadEndElement(); } reader.ReadEndElement(); // System.Diagnostics.Debug.WriteLine(reader.Name); } else return; }