public TileMapCollisionObject(TileMapObject tileMapObj, Texture2D debugTexture) { Position = new Vector2(tileMapObj.X, tileMapObj.Y); if (tileMapObj.ObjectType == TileMapObjectType.Rectangle) { TileMapRectangleObject rectObj = (TileMapRectangleObject)tileMapObj; Position += rectObj.Size / 2f; } _debugTexture = debugTexture; }
public override TileMap Process(TileMapData input, ContentProcessorContext context) { TileMap tileMap = new TileMap(input.TileWidth, input.TileWidth, input.Width, input.Height); foreach (var tsRef in input.TileSets) { string tsPath = $"{TILE_SET_PATH}/{Path.GetFileNameWithoutExtension(tsRef.Source)}"; tileMap.AddTileSetReference(tsRef.FirstGid, tsPath); } foreach (var layer in input.Layers) { TileMatrix tileMatrix = TileMatrix.ParseCsv(layer.Data); tileMap.AddLayer(new TileMapTileLayer(tileMap, layer.Id, layer.Name, tileMatrix)); } foreach (var objGroup in input.ObjectGroups) { TileMapObjectLayer objLayer = new TileMapObjectLayer(tileMap, objGroup.Id, objGroup.Name); tileMap.AddLayer(objLayer); foreach (var objData in objGroup.Objects) { if (objData.Point != null) { var pointObj = new TileMapPointObject(objData.Name, objData.Type, objData.Id, (float)objData.X, (float)objData.Y); ProcessObjectProperties(objData, pointObj); objLayer.AddObject(pointObj); } else if (objData.Polygon != null) { IEnumerable <Vector2> vertices = objData.Polygon.GetVertices(); var polygonObj = new TileMapPolygonObject(vertices, objData.Name, objData.Type, objData.Id, (float)objData.X, (float)objData.Y); objLayer.AddObject(polygonObj); } else { TileMapRectangleObject tileMapRectangleObject = new TileMapRectangleObject(objData.Width, objData.Height, objData.Name, objData.Type, objData.Id, (float)objData.X, (float)objData.Y); objLayer.AddObject(tileMapRectangleObject); } } } return(tileMap); }
private IEnumerable <TileMapObjectLayer> ReadObjectLayers(ContentReader input, TileMap tileMap) { List <TileMapObjectLayer> layers = new List <TileMapObjectLayer>(); int objLayerCount = input.ReadInt32(); for (int i = 0; i < objLayerCount; i++) { int layerId = input.ReadInt32(); string layerName = input.ReadString(); TileMapObjectLayer tileMapObjectLayer = new TileMapObjectLayer(tileMap, layerId, layerName); int pointObjectCount = input.ReadInt32(); for (int j = 0; j < pointObjectCount; j++) { var sharedData = ReadSharedObjectData(input); TileMapPointObject pointObj = new TileMapPointObject(sharedData.Name, sharedData.Type, sharedData.Id, sharedData.X, sharedData.Y); ReadProperyMap(input, pointObj.CustomProperties); tileMapObjectLayer.AddObject(pointObj); } int polygonObjectCount = input.ReadInt32(); for (int j = 0; j < polygonObjectCount; j++) { var sharedData = ReadSharedObjectData(input); PropertyMap propMap = new PropertyMap(); ReadProperyMap(input, propMap); List <Vector2> vertices = new List <Vector2>(); int vertexCount = input.ReadInt32(); for (int v = 0; v < vertexCount; v++) { Vector2 vertex = input.ReadVector2(); vertices.Add(vertex); } TileMapPolygonObject tileMapPolygon = new TileMapPolygonObject(vertices, sharedData.Name, sharedData.Type, sharedData.Id, sharedData.X, sharedData.Y); foreach (var kvp in propMap) { tileMapPolygon.CustomProperties.Add(kvp); } tileMapObjectLayer.AddObject(tileMapPolygon); } int rectObjectCount = input.ReadInt32(); for (int j = 0; j < rectObjectCount; j++) { var sharedData = ReadSharedObjectData(input); PropertyMap propMap = new PropertyMap(); ReadProperyMap(input, propMap); float width = input.ReadSingle(); float height = input.ReadSingle(); TileMapRectangleObject rectangleObject = new TileMapRectangleObject(width, height, sharedData.Name, sharedData.Type, sharedData.Id, sharedData.X, sharedData.Y); tileMapObjectLayer.AddObject(rectangleObject); } layers.Add(tileMapObjectLayer); } return(layers); }