public TilesetDocument Load(FilePath filePath) { var tilesetReader = _core.Reader.GetTilesetReader(filePath); var tileset = tilesetReader.Load(filePath); var tilesetDocument = new TilesetDocument(tileset); return tilesetDocument; }
public Tileset Load(FilePath path) { var tileset = new Tileset(); tileset.FilePath = path; var doc = XDocument.Load(path.Absolute); var reader = doc.Element("Tileset"); if (reader == null) throw new Exception("The specified tileset definition file does not contain a Tileset tag."); var sheetPath = FilePath.FromRelative(reader.Attribute("tilesheet").Value, path.BasePath); tileset.ChangeSheetPath(sheetPath.Absolute); int size; if (!int.TryParse(reader.Attribute("tilesize").Value, out size)) throw new Exception("The tileset definition does not contain a valid tilesize attribute."); tileset.TileSize = size; var propParent = reader.Element("TileProperties"); if (propParent != null) { foreach (XElement propNode in propParent.Elements("Properties")) { var prop = new TileProperties(propNode); tileset.AddProperties(prop); } } foreach (XElement tileNode in reader.Elements("Tile")) { int id = int.Parse(tileNode.Attribute("id").Value); string name = tileNode.Attribute("name").Value; var spriteNode = tileNode.Element("Sprite"); if (spriteNode == null) throw new GameXmlException(tileNode, "All Tile tags must contain a Sprite tag."); var sprite = LoadSprite(spriteNode); var tileSprite = new TileSprite(tileset, sprite); Tile tile = new Tile(id, tileSprite); string propName = "Default"; XAttribute propAttr = tileNode.Attribute("properties"); if (propAttr != null) propName = propAttr.Value; tile.Properties = tileset.GetProperties(propName); tile.Sprite.Play(); tileset.Add(tile); } return tileset; }
public static FilePath FromRelative(string relative, string basepath) { FilePath fp = new FilePath { basepath = Path.GetFullPath(basepath), relative = relative, absolute = Path.Combine(basepath, relative) }; return fp; }
public TilesetDocument CreateNew(FilePath filePath) { var tileset = new Tileset() { FilePath = filePath, TileSize = 16 }; var document = new TilesetDocument(tileset); AddDefaultTileProperties(document); return document; }
private ScreenInfo LoadScreenXml(XElement node, FilePath stagePath, Tileset tileset) { string id = node.RequireAttribute("id").Value; var screen = new ScreenInfo(id, tileset); screen.Layers.Add(LoadScreenLayer(node, stagePath.Absolute, id, tileset, 0, 0, false)); foreach (var overlay in node.Elements("Overlay")) { var name = overlay.RequireAttribute("name").Value; var x = overlay.TryAttribute<int>("x"); var y = overlay.TryAttribute<int>("y"); bool foreground = overlay.TryAttribute<bool>("foreground"); bool parallax = overlay.TryAttribute<bool>("parallax"); var layer = LoadScreenLayer(overlay, stagePath.Absolute, name, tileset, x, y, foreground); layer.Parallax = parallax; screen.Layers.Add(layer); } foreach (XElement teleport in node.Elements("Teleport")) { TeleportInfo info; int from_x = teleport.TryAttribute<int>("from_x"); int from_y = teleport.TryAttribute<int>("from_y"); int to_x = teleport.TryAttribute<int>("to_x"); int to_y = teleport.TryAttribute<int>("to_y"); info.From = new Point(from_x, from_y); info.To = new Point(to_x, to_y); info.TargetScreen = teleport.Attribute("to_screen").Value; screen.Teleports.Add(info); } var blocks = new List<BlockPatternInfo>(); foreach (XElement blockNode in node.Elements("Blocks")) { BlockPatternInfo pattern = _blockReader.FromXml(blockNode); screen.BlockPatterns.Add(pattern); } screen.Commands = HandlerXmlReader.LoadCommands(node, stagePath.BasePath); return screen; }
private Texture2D GetTextureFromPath(FilePath path) { StreamReader sr = new StreamReader(path.Absolute); return Texture2D.FromStream(_graphicsDevice, sr.BaseStream); }
public IResourceImage LoadResource(FilePath texturePath, string paletteName = null) { if (!_loadedResources.ContainsKey(texturePath)) { var texture = GetTextureFromPath(texturePath); var resource = AddTexture(texture, paletteName); _loadedResources[texturePath] = resource; } return _loadedResources[texturePath]; }
public ITilesetReader GetTilesetReader(FilePath path) { var reader = new TilesetXmlReader(); reader.Init(_dataSource); return reader; }
public IStageReader GetStageReader(FilePath path) { var reader = new StageXmlReader(this, new EntityPlacementXmlReader(), new HandlerCommandXmlReader()); reader.Init(_dataSource); return reader; }
public Map(FilePath path) : this() { LoadMapXml(path); }
/// <summary> /// Changes the tileset by specifying an absolute path to the new tileset XML file. /// </summary> /// <param name="path">If it's not absolute, I'll make it so.</param> public void ChangeTileset(string path) { tilePath = FilePath.FromAbsolute(path, StagePath.Absolute); Tileset = new Tileset(tilePath.Absolute); foreach (Screen s in Screens.Values) s.Tileset = Tileset; }
public StageDocument CreateNew(FilePath filePath) { throw new NotImplementedException(); }
public StageInfo(FilePath filePath) { StagePath = filePath; Screens = new Dictionary<string, ScreenInfo>(); Joins = new List<Join>(); continuePoints = new Dictionary<string, Point>(); }
public TilesetDocument Load(FilePath filePath) { var tileset = _reader.Load(filePath); var tilesetDocument = new TilesetDocument(tileset); return tilesetDocument; }
public StageInfo LoadStageXml(FilePath path) { _info = new StageInfo(); _info.StagePath = path; var mapXml = XElement.Load(Path.Combine(_info.StagePath.Absolute, "map.xml")); _info.Name = Path.GetFileNameWithoutExtension(_info.StagePath.Absolute); string tilePathRel = mapXml.Attribute("tiles").Value; var tilePath = FilePath.FromRelative(tilePathRel, _info.StagePath.BasePath); var tileset = new TilesetXmlReader().Load(tilePath); _info.ChangeTileset(tileset); _info.PlayerStartX = 3; _info.PlayerStartY = 3; LoadMusicXml(mapXml); LoadScreens(mapXml); XElement start = mapXml.Element("Start"); if (start != null) { _info.StartScreen = start.RequireAttribute("screen").Value; _info.PlayerStartX = start.GetAttribute<int>("x"); _info.PlayerStartY = start.GetAttribute<int>("y"); } foreach (XElement contPoint in mapXml.Elements("Continue")) { string screen = contPoint.GetAttribute<string>("screen"); int x = contPoint.GetAttribute<int>("x"); int y = contPoint.GetAttribute<int>("y"); _info.AddContinuePoint(screen, new Point(x, y)); } foreach (XElement join in mapXml.Elements("Join")) { string t = join.Attribute("type").Value; JoinType type; if (t.ToLower() == "horizontal") type = JoinType.Horizontal; else if (t.ToLower() == "vertical") type = JoinType.Vertical; else throw new GameXmlException(join, "map.xml file contains invalid join type."); string s1 = join.RequireAttribute("s1").Value; string s2 = join.RequireAttribute("s2").Value; int offset1 = join.GetAttribute<int>("offset1"); int offset2 = join.GetAttribute<int>("offset2"); int size = join.GetAttribute<int>("size"); JoinDirection direction; XAttribute dirAttr = join.Attribute("direction"); if (dirAttr == null || dirAttr.Value.ToUpper() == "BOTH") direction = JoinDirection.Both; else if (dirAttr.Value.ToUpper() == "FORWARD") direction = JoinDirection.ForwardOnly; else if (dirAttr.Value.ToUpper() == "BACKWARD") direction = JoinDirection.BackwardOnly; else throw new GameXmlException(dirAttr, "map.xml file contains invalid join direction."); string bosstile = null; XAttribute bossAttr = join.Attribute("bossdoor"); bool bossdoor = (bossAttr != null); if (bossdoor) bosstile = bossAttr.Value; Join j = new Join(); j.direction = direction; j.screenOne = s1; j.screenTwo = s2; j.offsetOne = offset1; j.offsetTwo = offset2; j.type = type; j.Size = size; j.bossDoor = bossdoor; j.bossEntityName = bosstile; _info.Joins.Add(j); } return _info; }
public static FilePath FromRelative(string relative, string basepath) { if (string.IsNullOrWhiteSpace(relative)) { throw new ArgumentException("Path cannot be null or empty.", "relative"); } if (string.IsNullOrWhiteSpace(basepath)) { throw new ArgumentException("Path cannot be null or empty.", "basepath"); } FilePath fp = new FilePath { basepath = Path.GetFullPath(basepath), relative = relative, absolute = Path.GetFullPath(Path.Combine(basepath, relative)) }; return fp; }
public void LoadMapXml(FilePath path) { StagePath = path; var mapXml = XElement.Load(Path.Combine(StagePath.Absolute, "map.xml")); Name = Path.GetFileNameWithoutExtension(StagePath.Absolute); string tilePathRel = mapXml.Attribute("tiles").Value; tilePath = FilePath.FromRelative(tilePathRel, StagePath.Absolute); Tileset = new Tileset(tilePath.Absolute); PlayerStartX = 3; PlayerStartY = 3; LoadMusicXml(mapXml); LoadScreenXml(mapXml); XElement start = mapXml.Element("Start"); if (start != null) { int px, py; var screenAttr = start.Attribute("screen"); if (screenAttr == null) throw new Exception("Start tag must have a screen attribute!"); StartScreen = screenAttr.Value; if (!start.Attribute("x").Value.TryParse(out px)) throw new Exception("Start tag x is not a valid integer!"); PlayerStartX = px; if (!start.Attribute("y").Value.TryParse(out py)) throw new Exception("Start tag y is not a valid integer!"); PlayerStartY = py; } foreach (XElement contPoint in mapXml.Elements("Continue")) { string screen = contPoint.Attribute("screen").Value; int x; contPoint.Attribute("x").Value.TryParse(out x); int y; contPoint.Attribute("y").Value.TryParse(out y); continuePoints.Add(screen, new Point(x, y)); } foreach (XElement join in mapXml.Elements("Join")) { string t = join.Attribute("type").Value; JoinType type; if (t.ToLower() == "horizontal") type = JoinType.Horizontal; else if (t.ToLower() == "vertical") type = JoinType.Vertical; else throw new Exception("map.xml file contains invalid join type."); string s1 = join.Attribute("s1").Value; string s2 = join.Attribute("s2").Value; int offset1; join.Attribute("offset1").Value.TryParse(out offset1); int offset2; join.Attribute("offset2").Value.TryParse(out offset2); int size; join.Attribute("size").Value.TryParse(out size); JoinDirection direction; XAttribute dirAttr = join.Attribute("direction"); if (dirAttr == null || dirAttr.Value.ToUpper() == "BOTH") direction = JoinDirection.Both; else if (dirAttr.Value.ToUpper() == "FORWARD") direction = JoinDirection.ForwardOnly; else if (dirAttr.Value.ToUpper() == "BACKWARD") direction = JoinDirection.BackwardOnly; else throw new Exception("map.xml file contains invalid join direction."); string bosstile = null; XAttribute bossAttr = join.Attribute("bossdoor"); bool bossdoor = (bossAttr != null); if (bossdoor) bosstile = bossAttr.Value; Join j = new Join(); j.direction = direction; j.screenOne = s1; j.screenTwo = s2; j.offsetOne = offset1; j.offsetTwo = offset2; j.type = type; j.Size = size; j.bossDoor = bossdoor; j.bossEntityName = bosstile; Joins.Add(j); } }