/// <summary> /// Returns the set of all objects in the map. /// </summary> /// <returns>A new set of all objects in the map.</returns> public IEnumerable <MapObject> GetAllObjects() { foreach (var layer in Layers) { MapObjectLayer objLayer = layer as MapObjectLayer; if (objLayer == null) { continue; } foreach (var obj in objLayer.Objects) { yield return(obj); } } }
/// <summary> /// Finds a collection of objects in the map using a delegate. /// </summary> /// <remarks> /// This method performs basically the same process as FindObject, but instead /// of returning the first object for which the delegate returns true, it returns /// a collection of all objects for which the delegate returns true. /// </remarks> /// <param name="finder">The delegate used to search for the object.</param> /// <returns>A collection of all MapObjects for which the delegate returned true.</returns> public IEnumerable <MapObject> FindObjects(MapObjectFinder finder) { foreach (var layer in Layers) { MapObjectLayer objLayer = layer as MapObjectLayer; if (objLayer == null) { continue; } foreach (var obj in objLayer.Objects) { if (finder(objLayer, obj)) { yield return(obj); } } } }
/// <summary> /// Finds an object in the map using a delegate. /// </summary> /// <remarks> /// This method is used when an object is desired, but there is no specific /// layer to find the object on. The delegate allows the caller to create /// any logic they want for finding the object. A simple example for finding /// the first object named "goal" in any layer would be this: /// /// var goal = map.FindObject((layer, obj) => return obj.Name.Equals("goal")); /// /// You could also use the layer name or any other logic to find an object. /// The first object for which the delegate returns true is the object returned /// to the caller. If the delegate never returns true, the method returns null. /// </remarks> /// <param name="finder">The delegate used to search for the object.</param> /// <returns>The MapObject if the delegate returned true, null otherwise.</returns> public MapObject FindObject(MapObjectFinder finder) { foreach (var layer in Layers) { MapObjectLayer objLayer = layer as MapObjectLayer; if (objLayer == null) { continue; } foreach (var obj in objLayer.Objects) { if (finder(objLayer, obj)) { return(obj); } } } return(null); }
//private Layer collisionLayer; internal Map(ContentReader reader) { // read in the basic map information Version = new Version(reader.ReadString()); Orientation = (Orientation)reader.ReadByte(); Width = reader.ReadInt32(); Height = reader.ReadInt32(); TileWidth = reader.ReadInt32(); TileHeight = reader.ReadInt32(); Properties = new PropertyCollection(); Properties.Read(reader); // create a list for our tiles List <Tile> tiles = new List <Tile>(); Tiles = new Collection <Tile>(tiles); // read in each tile set int numTileSets = reader.ReadInt32(); for (int i = 0; i < numTileSets; i++) { // get the id and texture int firstId = reader.ReadInt32(); string tilesetName = reader.ReadString(); bool collisionSet = reader.ReadBoolean(); Texture2D texture = reader.ReadExternalReference <Texture2D>(); Texture2D whiteTexture = reader.ReadExternalReference <Texture2D>(); // Read in color data for collision purposes // You'll probably want to limit this to just the tilesets that are used for collision // I'm checking for the name of my tileset that contains wall tiles // Color data takes up a fair bit of RAM Color[] collisionData = null; bool[] collisionBitData = null; if (collisionSet) { collisionData = new Color[texture.Width * texture.Height]; collisionBitData = new bool[texture.Width * texture.Height]; texture.GetData <Color>(collisionData); for (int col = 0; col < collisionData.Length; col++) { if (collisionData[col].A > 0) { collisionBitData[col] = true; } } collisionData = null; } // read in each individual tile int numTiles = reader.ReadInt32(); for (int j = 0; j < numTiles; j++) { int id = firstId + j; Rectangle source = reader.ReadObject <Rectangle>(); PropertyCollection props = new PropertyCollection(); props.Read(reader); Tile t = new Tile(texture, whiteTexture, source, props, collisionBitData); while (id >= tiles.Count) { tiles.Add(null); } tiles.Insert(id, t); } } // read in all the layers List <Layer> layers = new List <Layer>(); Layers = new ReadOnlyCollection <Layer>(layers); int numLayers = reader.ReadInt32(); for (int i = 0; i < numLayers; i++) { Layer layer = null; // read generic layer data string type = reader.ReadString(); string name = reader.ReadString(); int width = reader.ReadInt32(); int height = reader.ReadInt32(); bool visible = reader.ReadBoolean(); float opacity = reader.ReadSingle(); PropertyCollection props = new PropertyCollection(); props.Read(reader); // using the type, figure out which object to create if (type == "layer") { int[] data = reader.ReadObject <int[]>(); layer = new TileLayer(name, width, height, visible, opacity, props, this, data); } else if (type == "objectgroup") { List <MapObject> objects = new List <MapObject>(); // read in all of our objects int numObjects = reader.ReadInt32(); for (int j = 0; j < numObjects; j++) { string objName = reader.ReadString(); string objType = reader.ReadString(); Rectangle objLoc = reader.ReadObject <Rectangle>(); List <Point> objPoints = reader.ReadObject <List <Point> >(); PropertyCollection objProps = new PropertyCollection(); objProps.Read(reader); objects.Add(new MapObject(objName, objType, objLoc, objPoints, objProps)); } layer = new MapObjectLayer(name, width, height, visible, opacity, props, objects); } else { throw new Exception("Invalid type: " + type); } layers.Add(layer); namedLayers.Add(name, layer); } }
internal Map(ContentReader reader) { // read in the basic map information Version = new Version(reader.ReadString()); Orientation = (Orientation)reader.ReadByte(); WidthInTiles = reader.ReadInt32(); HeightInTiles = reader.ReadInt32(); TileWidth = reader.ReadInt32(); TileHeight = reader.ReadInt32(); Properties = new PropertyCollection(reader); bool makeTilesUnique = reader.ReadBoolean(); // create a list for our tiles List <Tile> tiles = new List <Tile>(); Tiles = new ReadOnlyCollection <Tile>(tiles); // read in each tile set int numTileSets = reader.ReadInt32(); for (int i = 0; i < numTileSets; i++) { // get the id and texture int firstId = reader.ReadInt32(); Texture2D texture = reader.ReadExternalReference <Texture2D>(); // read in each individual tile int numTiles = reader.ReadInt32(); for (int j = 0; j < numTiles; j++) { int id = firstId + j; Rectangle source = reader.ReadObject <Rectangle>(); PropertyCollection props = new PropertyCollection(reader); Tile t = new Tile(texture, source, props); while (id >= tiles.Count) { tiles.Add(null); } tiles.Insert(id, t); } } // read in all the layers List <Layer> layers = new List <Layer>(); Layers = new ReadOnlyCollection <Layer>(layers); int numLayers = reader.ReadInt32(); for (int i = 0; i < numLayers; i++) { Layer layer = null; // read generic layer data string type = reader.ReadString(); string name = reader.ReadString(); int width = reader.ReadInt32(); int height = reader.ReadInt32(); bool visible = reader.ReadBoolean(); float opacity = reader.ReadSingle(); PropertyCollection props = new PropertyCollection(reader); // calculate the default layer depth of the layer float layerDepth = 1f - (LayerDepthSpacing * i); // using the type, figure out which object to create if (type == "layer") { uint[] data = reader.ReadObject <uint[]>(); layer = new TileLayer(name, width, height, layerDepth, visible, opacity, props, this, data, makeTilesUnique); } else if (type == "objectgroup") { List <MapObject> objects = new List <MapObject>(); // read in all of our objects int numObjects = reader.ReadInt32(); for (int j = 0; j < numObjects; j++) { string objName = reader.ReadString(); string objType = reader.ReadString(); Rectangle objLoc = reader.ReadObject <Rectangle>(); PropertyCollection objProps = new PropertyCollection(reader); objects.Add(new MapObject(objName, objType, objLoc, objProps)); } layer = new MapObjectLayer(name, width, height, layerDepth, visible, opacity, props, objects); // read in the layer's color (layer as MapObjectLayer).Color = reader.ReadColor(); } else { throw new Exception("Invalid type: " + type); } layers.Add(layer); namedLayers.Add(name, layer); } }
//private Layer collisionLayer; // Construct a TiledLib.Map from a TmxReader.map public Map(ContentManager content, string mapName) { string mapPath = AppDomain.CurrentDomain.BaseDirectory + "Content/" + mapName + ".tmx"; TmxMap tmx = new TmxMap(mapPath); Version = new Version(tmx.Version); switch(tmx.Orientation) { case TmxMap.OrientationType.Isometric: Orientation = Orientation.Isometric; break; case TmxMap.OrientationType.Orthogonal: Orientation = Orientation.Orthogonal; break; case TmxMap.OrientationType.Staggered: throw new Exception( "TiledLib doesn't support maps with Staggered " + "orientation."); } Width = tmx.Width; Height = tmx.Height; TileWidth = tmx.TileWidth; TileHeight = tmx.TileHeight; Properties = new PropertyCollection(); foreach(KeyValuePair<string, string> kvp in tmx.Properties) { Properties.Add(kvp); } // Create a list for our tiles List<Tile> tiles = new List<Tile>(); Tiles = new Collection<Tile>(tiles); // Read in each TileSet foreach (TmxTileset ts in tmx.Tilesets) { string tilesetName = ts.Name; Texture2D texture = content.Load<Texture2D>(ts.Image.Source); bool collisionSet = ts.Properties.ContainsKey("CollisionSet") && ts.Properties["CollisionSet"] == "True" ? true : false; /* TODO: Add this intelligence to TiledSharp. * If the texture is bigger than a individual tile, infer all the * additional tiles that must be created. */ if (texture.Width > ts.TileWidth || texture.Height > ts.TileHeight) { // Deconstruct tileset to individual tiles int id = 0; for (int y = 0; y < texture.Height / ts.TileHeight; y++) { for (int x = 0; x < texture.Width / ts.TileWidth; x++) { Rectangle source = new Rectangle( x * ts.TileWidth, y * ts.TileHeight, ts.TileWidth, ts.TileHeight ); Color[] collisionData = null; bool[] collisionBitData = null; PropertyCollection props = new PropertyCollection(); TmxTilesetTile tsTile = ts.Tiles.Find(i => i.Id == id); if(tsTile != null) { foreach (KeyValuePair<string, string> kvp in tsTile.Properties) { props.Add(kvp); // Inherit tilesets collision bool collidable = collisionSet; if (kvp.Key == "CollisionSet") { // Allow override per tile if (kvp.Value == "True") { collidable = true; } else { collidable = false; } } if (collidable) { int numOfBytes = ts.TileWidth * ts.TileHeight; collisionData = new Color[numOfBytes]; collisionBitData = new bool[numOfBytes]; texture.GetData<Color>( 0, source, collisionData, 0, numOfBytes ); for (int col = 0; col < numOfBytes; col++) { if (collisionData[col].A > 0) { collisionBitData[col] = true; } } collisionData = null; } } } Tile t = new Tile( texture, source, props, collisionBitData ); while (id >= tiles.Count) { tiles.Add(null); } tiles.Insert(id + ts.FirstGid, t); id++; } } } } // Process Map Items (layers, objectgroups, etc.) List<Layer> layers = new List<Layer>(); Layers = new Collection<Layer>(layers); foreach(TmxLayer l in tmx.Layers) { Layer layer = null; PropertyCollection props = new PropertyCollection(); foreach(KeyValuePair<string, string> kvp in l.Properties) { props.Add(kvp); } layer = new TileLayer( l.Name, tmx.Width, // As of TiledQT always same as layer. tmx.Height, // As of TiledQT always same as layer. l.Visible, (float) l.Opacity, props, this, l.Tiles ); layers.Add(layer); namedLayers.Add(l.Name, layer); } foreach(TmxObjectGroup og in tmx.ObjectGroups) { Layer layer = null; PropertyCollection props = new PropertyCollection(); foreach(KeyValuePair<string, string> kvp in og.Properties) { props.Add(kvp); } List<MapObject> objects = new List<MapObject>(); // read in all of our objects foreach(TmxObjectGroup.TmxObject i in og.Objects) { Rectangle objLoc = new Rectangle( i.X, i.Y, i.Width, i.Height ); List<Point> objPoints = new List<Point>(); if (i.Points != null) { foreach (Tuple<int, int> tuple in i.Points) { objPoints.Add(new Point(tuple.Item1, tuple.Item2)); } } PropertyCollection objProps = new PropertyCollection(); foreach(KeyValuePair<string, string> kvp in i.Properties) { objProps.Add(kvp); } objects.Add( new MapObject( i.Name, i.Type, objLoc, objPoints, objProps) ); } layer = new MapObjectLayer( og.Name, tmx.Width, tmx.Height, og.Visible, (float) og.Opacity, props, objects ); layers.Add(layer); namedLayers.Add(og.Name, layer); } }
internal Map(ContentReader reader) { // read in the basic map information Version = new Version(reader.ReadString()); Orientation = (Orientation)reader.ReadByte(); Width = reader.ReadInt32(); Height = reader.ReadInt32(); TileWidth = reader.ReadInt32(); TileHeight = reader.ReadInt32(); Properties = new PropertyCollection(); Properties.Read(reader); // create a list for our tiles List<Tile> tiles = new List<Tile>(); Tiles = new Collection<Tile>(tiles); // read in each tile set int numTileSets = reader.ReadInt32(); for (int i = 0; i < numTileSets; i++) { // get the id and texture int firstId = reader.ReadInt32(); string tilesetName = reader.ReadString(); bool collisionSet = reader.ReadBoolean(); Texture2D texture = reader.ReadExternalReference<Texture2D>(); // read in each individual tile int numTiles = reader.ReadInt32(); for (int j = 0; j < numTiles; j++) { int id = firstId + j; // Read the source rectangle from the file. Rectangle source = reader.ReadObject<Rectangle>(); PropertyCollection props = new PropertyCollection(); props.Read(reader); // Read in color data for collision purposes // You'll probably want to limit this to just the tilesets that are used for collision // I'm checking for the name of my tileset that contains wall tiles // Color data takes up a fair bit of RAM Color[] collisionData = null; bool[] collisionBitData = null; if (collisionSet) { int numOfBytes = TileWidth * TileHeight; collisionData = new Color[numOfBytes]; collisionBitData = new bool[numOfBytes]; texture.GetData<Color>( 0, source, collisionData, 0, numOfBytes ); for (int col = 0; col < numOfBytes; col++) { if (collisionData[col].A > 0) { collisionBitData[col] = true; } } collisionData = null; } Tile t = new Tile(texture, source, props, collisionBitData); while (id >= tiles.Count) { tiles.Add(null); } tiles.Insert(id, t); } } // read in all the layers List<Layer> layers = new List<Layer>(); Layers = new Collection<Layer>(layers); int numLayers = reader.ReadInt32(); for (int i = 0; i < numLayers; i++) { Layer layer = null; // read generic layer data string type = reader.ReadString(); string name = reader.ReadString(); int width = reader.ReadInt32(); int height = reader.ReadInt32(); bool visible = reader.ReadBoolean(); float opacity = reader.ReadSingle(); PropertyCollection props = new PropertyCollection(); props.Read(reader); // using the type, figure out which object to create if (type == "layer") { int[] data = reader.ReadObject<int[]>(); layer = new TileLayer(name, width, height, visible, opacity, props, this, data); } else if (type == "objectgroup") { List<MapObject> objects = new List<MapObject>(); // read in all of our objects int numObjects = reader.ReadInt32(); for (int j = 0; j < numObjects; j++) { string objName = reader.ReadString(); string objType = reader.ReadString(); Rectangle objLoc = reader.ReadObject<Rectangle>(); List<Point> objPoints = reader.ReadObject<List<Point>>(); PropertyCollection objProps = new PropertyCollection(); objProps.Read(reader); objects.Add(new MapObject(objName, objType, objLoc, objPoints, objProps)); } layer = new MapObjectLayer(name, width, height, visible, opacity, props, objects); } else { throw new Exception("Invalid type: " + type); } layers.Add(layer); namedLayers.Add(name, layer); } }
//private Layer collisionLayer; // Construct a TiledLib.Map from a TmxReader.map public Map(ContentManager content, string mapName) { string mapPath = AppDomain.CurrentDomain.BaseDirectory + "Content/" + mapName + ".tmx"; TmxMap tmx = new TmxMap(mapPath); Version = new Version(tmx.Version); switch (tmx.Orientation) { case TmxMap.OrientationType.Isometric: Orientation = Orientation.Isometric; break; case TmxMap.OrientationType.Orthogonal: Orientation = Orientation.Orthogonal; break; case TmxMap.OrientationType.Staggered: throw new Exception( "TiledLib doesn't support maps with Staggered " + "orientation."); } Width = tmx.Width; Height = tmx.Height; TileWidth = tmx.TileWidth; TileHeight = tmx.TileHeight; Properties = new PropertyCollection(); foreach (KeyValuePair <string, string> kvp in tmx.Properties) { Properties.Add(kvp); } // Create a list for our tiles List <Tile> tiles = new List <Tile>(); Tiles = new Collection <Tile>(tiles); // Read in each TileSet foreach (TmxTileset ts in tmx.Tilesets) { string tilesetName = ts.Name; Texture2D texture = content.Load <Texture2D>(ts.Image.Source); bool collisionSet = ts.Properties.ContainsKey("CollisionSet") && ts.Properties["CollisionSet"] == "True" ? true : false; /* TODO: Add this intelligence to TiledSharp. * If the texture is bigger than a individual tile, infer all the * additional tiles that must be created. */ if (texture.Width > ts.TileWidth || texture.Height > ts.TileHeight) { // Deconstruct tileset to individual tiles int id = 0; for (int y = 0; y < texture.Height / ts.TileHeight; y++) { for (int x = 0; x < texture.Width / ts.TileWidth; x++) { Rectangle source = new Rectangle( x * ts.TileWidth, y * ts.TileHeight, ts.TileWidth, ts.TileHeight ); Color[] collisionData = null; bool[] collisionBitData = null; PropertyCollection props = new PropertyCollection(); TmxTilesetTile tsTile = ts.Tiles.Find(i => i.Id == id); if (tsTile != null) { foreach (KeyValuePair <string, string> kvp in tsTile.Properties) { props.Add(kvp); // Inherit tilesets collision bool collidable = collisionSet; if (kvp.Key == "CollisionSet") { // Allow override per tile if (kvp.Value == "True") { collidable = true; } else { collidable = false; } } if (collidable) { int numOfBytes = ts.TileWidth * ts.TileHeight; collisionData = new Color[numOfBytes]; collisionBitData = new bool[numOfBytes]; texture.GetData <Color>( 0, source, collisionData, 0, numOfBytes ); for (int col = 0; col < numOfBytes; col++) { if (collisionData[col].A > 0) { collisionBitData[col] = true; } } collisionData = null; } } } Tile t = new Tile( texture, source, props, collisionBitData ); while (id >= tiles.Count) { tiles.Add(null); } tiles.Insert(id + ts.FirstGid, t); id++; } } } } // Process Map Items (layers, objectgroups, etc.) List <Layer> layers = new List <Layer>(); Layers = new Collection <Layer>(layers); foreach (TmxLayer l in tmx.Layers) { Layer layer = null; PropertyCollection props = new PropertyCollection(); foreach (KeyValuePair <string, string> kvp in l.Properties) { props.Add(kvp); } layer = new TileLayer( l.Name, tmx.Width, // As of TiledQT always same as layer. tmx.Height, // As of TiledQT always same as layer. l.Visible, (float)l.Opacity, props, this, l.Tiles ); layers.Add(layer); namedLayers.Add(l.Name, layer); } foreach (TmxObjectGroup og in tmx.ObjectGroups) { Layer layer = null; PropertyCollection props = new PropertyCollection(); foreach (KeyValuePair <string, string> kvp in og.Properties) { props.Add(kvp); } List <MapObject> objects = new List <MapObject>(); // read in all of our objects foreach (TmxObjectGroup.TmxObject i in og.Objects) { Rectangle objLoc = new Rectangle( i.X, i.Y, i.Width, i.Height ); List <Point> objPoints = new List <Point>(); if (i.Points != null) { foreach (Tuple <int, int> tuple in i.Points) { objPoints.Add(new Point(tuple.Item1, tuple.Item2)); } } PropertyCollection objProps = new PropertyCollection(); foreach (KeyValuePair <string, string> kvp in i.Properties) { objProps.Add(kvp); } objects.Add( new MapObject( i.Name, i.Type, objLoc, objPoints, objProps) ); } layer = new MapObjectLayer( og.Name, tmx.Width, tmx.Height, og.Visible, (float)og.Opacity, props, objects ); layers.Add(layer); namedLayers.Add(og.Name, layer); } }
internal Map(ContentReader reader) { // read in the basic map information Version = new Version(reader.ReadString()); Orientation = (Orientation)reader.ReadByte(); WidthInTiles = reader.ReadInt32(); HeightInTiles = reader.ReadInt32(); TileWidth = reader.ReadInt32(); TileHeight = reader.ReadInt32(); Properties = new PropertyCollection(reader); bool makeTilesUnique = reader.ReadBoolean(); // create a list for our tiles List<Tile> tiles = new List<Tile>(); Tiles = new ReadOnlyCollection<Tile>(tiles); // read in each tile set int numTileSets = reader.ReadInt32(); for (int i = 0; i < numTileSets; i++) { // get the id and texture int firstId = reader.ReadInt32(); Texture2D texture = reader.ReadExternalReference<Texture2D>(); // read in each individual tile int numTiles = reader.ReadInt32(); for (int j = 0; j < numTiles; j++) { int id = firstId + j; Rectangle source = reader.ReadObject<Rectangle>(); PropertyCollection props = new PropertyCollection(reader); Tile t = new Tile(texture, source, props); while (id >= tiles.Count) { tiles.Add(null); } tiles.Insert(id, t); } } // read in all the layers List<Layer> layers = new List<Layer>(); Layers = new ReadOnlyCollection<Layer>(layers); int numLayers = reader.ReadInt32(); for (int i = 0; i < numLayers; i++) { Layer layer = null; // read generic layer data string type = reader.ReadString(); string name = reader.ReadString(); int width = reader.ReadInt32(); int height = reader.ReadInt32(); bool visible = reader.ReadBoolean(); float opacity = reader.ReadSingle(); PropertyCollection props = new PropertyCollection(reader); // calculate the default layer depth of the layer float layerDepth = 1f - (LayerDepthSpacing * i); // using the type, figure out which object to create if (type == "layer") { uint[] data = reader.ReadObject<uint[]>(); layer = new TileLayer(name, width, height, layerDepth, visible, opacity, props, this, data, makeTilesUnique); } else if (type == "objectgroup") { List<MapObject> objects = new List<MapObject>(); // read in all of our objects int numObjects = reader.ReadInt32(); for (int j = 0; j < numObjects; j++) { string objName = reader.ReadString(); string objType = reader.ReadString(); Rectangle objLoc = reader.ReadObject<Rectangle>(); PropertyCollection objProps = new PropertyCollection(reader); objects.Add(new MapObject(objName, objType, objLoc, objProps)); } layer = new MapObjectLayer(name, width, height, layerDepth, visible, opacity, props, objects); // read in the layer's color (layer as MapObjectLayer).Color = reader.ReadColor(); } else { throw new Exception("Invalid type: " + type); } layers.Add(layer); namedLayers.Add(name, layer); } }
public void LoadContent(ContentManager content, MapObjectLayer spawnLayer) { VoxelSprite asteroid = new VoxelSprite(16,16,16); LoadVoxels.LoadSprite(Path.Combine(content.RootDirectory, "enemies", "asteroids.vxs"), ref asteroid); spriteSheets.Add("Asteroid", asteroid); VoxelSprite omega = new VoxelSprite(15,15,15); LoadVoxels.LoadSprite(Path.Combine(content.RootDirectory, "enemies", "omega.vxs"), ref omega); spriteSheets.Add("Omega", omega); VoxelSprite turret = new VoxelSprite(15, 15, 15); LoadVoxels.LoadSprite(Path.Combine(content.RootDirectory, "enemies", "turret.vxs"), ref turret); spriteSheets.Add("Turret", turret); VoxelSprite squid = new VoxelSprite(15, 15, 15); LoadVoxels.LoadSprite(Path.Combine(content.RootDirectory, "enemies", "squid.vxs"), ref squid); spriteSheets.Add("Squid", squid); foreach (MapObject o in spawnLayer.Objects) Spawns.Add(o); }