private void VerticalMerge(Map map0, Map map1) { int originalHeight = map0.Height * map0.Tileheight; map0.Height += map1.Height; foreach (var layer in map0.Layers) { var l2 = map1.Layers.Last(x => x.Name == layer.Name); layer.Height += l2.Height; var orig = layer.Data.RawData; var concated = l2.Data.RawData; layer.Data.RawData = orig.Remove(orig.Length - 1) + "," + concated; } for (int i = 0; i < map0.ObjectGroups.Count; i++) { foreach (var tmxMapObject in map0.ObjectGroups[i].TmxMapObjects) { tmxMapObject.Id = objectId++; } foreach (var tmxMapObject in map1.ObjectGroups[i].TmxMapObjects) { tmxMapObject.Id = objectId++; tmxMapObject.Y += originalHeight; } map0.ObjectGroups[i].TmxMapObjects.AddRange(map1.ObjectGroups[i].TmxMapObjects); } }
public Vector2I TileBorder { get; protected set; } // how much the size of the tile is increased because of // correct texture filtering public TilesetTable(Map tmxMap, StreamReader tilesetFile) { if (tmxMap != null) { Tilesets.AddRange(tmxMap.Tilesets); TileSize = new Vector2I(tmxMap.Tilewidth, tmxMap.Tileheight); TileMargins = Vector2I.One; } TileBorder = new Vector2I(TileSize.X, TileSize.Y); // this much border is needed for small resolutions var dataTable = new DataTable(); string readLine = tilesetFile.ReadLine(); Debug.Assert(readLine != null, "readLine != null"); foreach (string header in readLine.Split(';')) { dataTable.Columns.Add(header); } while (!tilesetFile.EndOfStream) { string line = tilesetFile.ReadLine(); Debug.Assert(line != null, "line != null"); string[] row = line.Split(';'); if (dataTable.Columns.Count != row.Length) break; DataRow newRow = dataTable.NewRow(); foreach (DataColumn column in dataTable.Columns) { newRow[column.ColumnName] = row[column.Ordinal]; } dataTable.Rows.Add(newRow); } tilesetFile.Close(); IEnumerable<DataRow> enumerable = dataTable.Rows.Cast<DataRow>(); var dataRows = enumerable.ToArray(); try { m_namesValuesDictionary = dataRows.Where(x => x["IsDefault"].ToString() == "1") .ToDictionary(x => x["NameOfTile"].ToString(), x => int.Parse(x["PositionInTileset"].ToString())); } catch (ArgumentException) { Log.Instance.Error("Duplicate NameOfTiles in tileset table. Try set IsDefault to 0."); } m_valuesNamesDictionary = dataRows.ToDictionary(x => int.Parse(x["PositionInTileset"].ToString()), x => x["NameOfTile"].ToString()); }
public ToyWorld(Map tmxDeserializedMap, StreamReader tileTable) { if (tileTable == null) throw new ArgumentNullException("tileTable"); if (tmxDeserializedMap == null) throw new ArgumentNullException("tmxDeserializedMap"); Contract.EndContractBlock(); Size = new Vector2I(tmxDeserializedMap.Width, tmxDeserializedMap.Height); AutoupdateRegister = new AutoupdateRegister(100000); TilesetTable = new TilesetTable(tmxDeserializedMap, tileTable); InitAtlas(tmxDeserializedMap); InitPhysics(); TileDetectorRegister = new TileDetectorRegister(Atlas, TilesetTable); RegisterSignals(); }
private void HorizontalMerge(Map map0, Map map1) { int originalWidth = map0.Width * map0.Tilewidth; map0.Width += map1.Width; foreach (var layer in map0.Layers) { var l2 = map1.Layers.Last(x => x.Name == layer.Name); layer.Width += l2.Width; var lines0 = layer.Data.RawData.Split('\n'); var lines1 = l2.Data.RawData.Split('\n'); var sb = new StringBuilder(); for (var i = 0; i < lines0.Length - 2; i++) { sb.Append(lines0[i]).Append(lines1[i]).Append("\n"); } sb.Append(lines0[lines0.Length - 2]).Append(",").Append(lines1[lines0.Length - 2]).Append("\n"); layer.Data.RawData = sb.ToString(); } for (int i = 0; i < map0.ObjectGroups.Count; i++) { foreach (var tmxMapObject in map0.ObjectGroups[i].TmxMapObjects) { tmxMapObject.Id = objectId++; } foreach (var tmxMapObject in map1.ObjectGroups[i].TmxMapObjects) { tmxMapObject.Id = objectId++; tmxMapObject.X += originalWidth; } map0.ObjectGroups[i].TmxMapObjects.AddRange(map1.ObjectGroups[i].TmxMapObjects); } }
private static void SetTileRelations(IAtlas atlas, Map map) { ObjectGroup foregroundObjects = map.ObjectGroups.FirstOrDefault(x => x.Name == "ForegroundObject"); Debug.Assert(foregroundObjects != null, "foregroundObjects != null"); List<TmxObject> tmxMapObjects = foregroundObjects.TmxMapObjects; IEnumerable<TmxObject> switcherToSwitchablePolylines = tmxMapObjects.Where(x => x.Type == "SwitcherToSwitchable"); foreach (TmxObject switcherToSwitchablePolyline in switcherToSwitchablePolylines) { Polyline polyline = switcherToSwitchablePolyline.Polyline; if (polyline == null) { throw new ArgumentException("Foreground object SwitcherToSwitchable is wrong type. Should be Polyline."); } List<Vector2> polylinePoints = PolylineTransform(map, switcherToSwitchablePolyline).ToList(); Vector2 source = polylinePoints.First(); Vector2 target = polylinePoints.Last(); IEnumerable<GameActorPosition> sourceGameActors = atlas.ActorsAt(source); GameActorPosition switcherPosition = sourceGameActors.FirstOrDefault(x => x.Actor is ISwitcherGameActor); if (switcherPosition == null) { Log.Instance.Error("SwitcherToSwitchable polyline expects Switcher type at [" + source.X + ";" + source.Y + "]."); return; } ISwitcherGameActor switcherGameActor = switcherPosition.Actor as ISwitcherGameActor; IEnumerable<GameActorPosition> targetGameActors = atlas.ActorsAt(target); GameActorPosition switchablePosition = targetGameActors.FirstOrDefault(x => x.Actor is ISwitchableGameActor); if (switchablePosition == null) { Log.Instance.Error("SwitcherToSwitchable polyline expects Switchable type at [" + target.X + ";" + target.Y + "]."); return; } ISwitchableGameActor switchable = switchablePosition.Actor as ISwitchableGameActor; if (switcherGameActor != null) switcherGameActor.Switchable = switchable; } }
/// <summary> /// Loads map from specified path, creates tiles and objects and put them into new Atlas object. /// </summary> /// <param name="map">Deserialized .tmx file</param> /// <param name="tilesetTable">Table of numbers of game object and theirs classes</param> /// <param name="initializer"></param> /// <returns>Atlas with initial state of ToyWorld</returns> public static IAtlas LoadMap(Map map, TilesetTable tilesetTable, Action<GameActor> initializer) { IAtlas atlas = new Atlas.Layers.Atlas(); map.Tilesets = map.Tilesets.OrderBy(x => x.Firstgid).ToList(); foreach (LayerType layerType in Enum.GetValues(typeof(LayerType)).Cast<LayerType>()) { string layerName = Enum.GetName(typeof(LayerType), layerType); Debug.Assert(layerName != null); int type = (int)layerType; bool simpleType = type > 0 && (type & (type - 1)) == 0; if (!simpleType) continue; if (layerName.Contains("Object")) { ObjectGroup objectLayer = map.ObjectGroups.FirstOrDefault(x => x.Name == layerName); if (objectLayer == null) // TMX does not contain such layer { Log.Instance.Warn("Layer " + layerName + " not found in given .tmx file!"); continue; } IObjectLayer filledObjectLayer = FillObjectLayer( atlas, objectLayer, layerType, initializer, map.Tilesets, map.Tilewidth, map.Tileheight, map.Height ); atlas.ObjectLayers.Add( filledObjectLayer); } else { Layer tileLayer = map.Layers.FirstOrDefault(x => x.Name == layerName); if (tileLayer == null) // TMX does not contain such layer { Log.Instance.Warn("Layer " + layerName + " not found in given .tmx file!"); continue; } Random rnd = new Random(); // Shared across all layers ITileLayer filledTileLayer = FillTileLayer( tileLayer, layerType, atlas.StaticTilesContainer, tilesetTable, initializer, rnd); atlas.TileLayers.Add( filledTileLayer); } } FillNamedAreas(atlas, map); SetTileRelations(atlas, map); return atlas; }
private static void FillNamedAreas(IAtlas atlas, Map map) { ObjectGroup foregroundObjects = map.ObjectGroups.First(x => x.Name == "ForegroundObject"); List<TmxObject> tmxMapObjects = foregroundObjects.TmxMapObjects; List<TmxObject> areaLabels = tmxMapObjects.Where(x => x.Type.Trim() == "AreaLabel").ToList(); IEnumerable<Vector2I> positions = areaLabels.Select(x => new Vector2I((int)Math.Floor(x.X), (int)Math.Floor(x.Y))); IEnumerable<string> names = areaLabels.Select(x => x.Name); List<Tuple<Vector2I, string>> namesPositions = positions.Zip(names, (x, y) => new Tuple<Vector2I, string>(x, y)).ToList(); atlas.AreasCarrier = new AreasCarrier((ITileLayer)atlas.GetLayer(LayerType.Background), (ITileLayer)atlas.GetLayer(LayerType.Area), namesPositions); }
private static IEnumerable<Vector2> PolylineTransform(Map map, TmxObject polyline) { float tilewidth = map.Tilewidth; float tileheight = map.Tileheight; float polX = polyline.X; float polY = polyline.Y; foreach (Vector2 point in polyline.Polyline.GetPoints()) { yield return new Vector2(polX + point.X / tilewidth, polY - point.Y / tileheight); } }
public TestingToyWorld(Map tmxDeserializedMap, StreamReader tileTable) : base(tmxDeserializedMap, tileTable) { }
private void InitAtlas(Map tmxDeserializedMap) { Action<GameActor> initializer = delegate(GameActor actor) { IAutoupdateableGameActor updateable = actor as IAutoupdateableGameActor; if (updateable != null && updateable.NextUpdateAfter > 0) AutoupdateRegister.Register(updateable, updateable.NextUpdateAfter); }; Atlas = MapLoader.LoadMap(tmxDeserializedMap, TilesetTable, initializer); Atlas.DayLength = TWConfig.Instance.DayLengh; Atlas.YearLength = TWConfig.Instance.YearLength; IAtmosphere atmosphere = new SimpleAtmosphere(Atlas); Atlas.Atmosphere = atmosphere; }