public static List<Platform> LoadPlatformFromLayer(CCTileMap TileMap, CCTileMapObjectGroup PlatformHolder, Container gameContainer) { List<Platform> LoadedPlatforms = new List<Platform> (); foreach (Dictionary<string,string> LayerObject in PlatformHolder.Objects) { if (LayerObject.ContainsKey ("type") == true) { if (LayerObject ["type"] == "platform") { int LoadedSpeed = 200; if (LayerObject.ContainsKey ("speed")) LoadedSpeed = Convert.ToInt32 (LayerObject ["speed"]); List<CCPoint> LoadedWaipoints = new List<CCPoint> (); LoadedWaipoints.Add (new CCPoint ((float)Convert.ToInt32 (LayerObject ["x"]) * TileMap.ScaleX, (float)Convert.ToInt32 (LayerObject ["y"]) * TileMap.ScaleY)); if (LayerObject.ContainsKey ("waypoints") == true) { foreach (string WayPointPair in LayerObject ["waypoints"].Split(new char[]{';'},StringSplitOptions.RemoveEmptyEntries)) { CCPoint TempLoadedPoint = new CCPoint (); string[] Waypoint = WayPointPair.Split (new char[]{ ',' }, StringSplitOptions.RemoveEmptyEntries); if (Waypoint.Length > 1) { TempLoadedPoint.X = LoadedWaipoints [LoadedWaipoints.Count - 1].X + (float)Convert.ToInt32 (Waypoint [0]) * TileMap.ScaleX * TileMap.TileTexelSize.Width; TempLoadedPoint.Y = LoadedWaipoints [LoadedWaipoints.Count - 1].Y + (float)Convert.ToInt32 (Waypoint [1]) * TileMap.ScaleY * TileMap.TileTexelSize.Height; } else { throw new ArgumentException ("Incorrect Waypoints"); } LoadedWaipoints.Add (TempLoadedPoint); } } LoadedPlatforms.Add (new Platform (LoadedWaipoints, LoadedSpeed, gameContainer)); } } } return LoadedPlatforms; }
void ParsePropertyElement() { if (currentParentElement == CCTileMapProperty.None) { CCLog.Log("CCTileMapInfo: ParsePropertyElement: Parent element is unsupported. Cannot add property named '{0}' with value '{1}'", currentAttributeDict[PropertyElementName], currentAttributeDict[PropertyElementValue]); } else if (currentParentElement == CCTileMapProperty.Map) { // The parent element is the map string value = currentAttributeDict[PropertyElementValue]; string key = currentAttributeDict[PropertyElementName]; MapProperties.Add(key, value); } else if (currentParentElement == CCTileMapProperty.Layer) { int layersCount = Layers != null ? Layers.Count : 0; CCTileLayerInfo layer = layersCount > 0 ? Layers[layersCount - 1] : null; string value = currentAttributeDict[PropertyElementValue]; string key = currentAttributeDict[PropertyElementName]; // Add the property to the layer layer.Properties.Add(key, value); } else if (currentParentElement == CCTileMapProperty.ObjectGroup) { int objGroupsCount = ObjectGroups != null ? ObjectGroups.Count : 0; CCTileMapObjectGroup objectGroup = objGroupsCount > 0 ? ObjectGroups[objGroupsCount - 1] : null; string value = currentAttributeDict[PropertyElementValue]; string key = currentAttributeDict[PropertyElementName]; objectGroup.Properties.Add(key, value); } else if (currentParentElement == CCTileMapProperty.Object) { // The parent element is the last object int objGroupsCount = ObjectGroups != null ? ObjectGroups.Count : 0; CCTileMapObjectGroup objectGroup = objGroupsCount > 0 ? ObjectGroups[objGroupsCount - 1] : null; List <Dictionary <string, string> > objects = objectGroup.Objects; int objCount = objects != null ? objects.Count : 0; Dictionary <string, string> dict = objCount > 0 ? objects[objCount - 1] : null; string propertyName = currentAttributeDict[PropertyElementName]; string propertyValue = currentAttributeDict[PropertyElementValue]; dict.Add(propertyName, propertyValue); } else if (currentParentElement == CCTileMapProperty.Tile) { Dictionary <string, string> dict = TileProperties[ParentGID]; string propertyName = currentAttributeDict[PropertyElementName]; string propertyValue = currentAttributeDict[PropertyElementValue]; dict.Add(propertyName, propertyValue); } }
void ParseEllipseElement() { if (ObjectGroups == null || ObjectGroups.Count == 0) { return; } CCTileMapObjectGroup objectGroup = ObjectGroups[ObjectGroups.Count - 1]; if (objectGroup == null || objectGroup.Objects.Count == 0) { return; } var dict = objectGroup.Objects[objectGroup.Objects.Count - 1]; dict[ObjectElementShape] = ObjectElementShapeEllipse; }
void ParseObjectGroupElement() { var objectGroup = new CCTileMapObjectGroup(); objectGroup.GroupName = currentAttributeDict[ObjectGrpElementName]; CCPoint positionOffset = CCPoint.Zero; if (currentAttributeDict.ContainsKey(ObjectGrpElementXOffset)) { positionOffset.X = CCUtils.CCParseFloat(currentAttributeDict[ObjectGrpElementXOffset]) * TileTexelSize.Width; } if (currentAttributeDict.ContainsKey(ObjectGrpElementYOffset)) { positionOffset.Y = CCUtils.CCParseFloat(currentAttributeDict[ObjectGrpElementYOffset]) * TileTexelSize.Height; } objectGroup.PositionOffset = positionOffset; ObjectGroups.Add(objectGroup); currentParentElement = CCTileMapProperty.ObjectGroup; }
public static List<JumpPad> LoadJumpPadFromLayer(CCTileMap TileMap, CCTileMapObjectGroup PlatformHolder, Container gameContainer) { List<JumpPad> LoadedJumpPads = new List<JumpPad> (); foreach (Dictionary<string,string> LayerObject in PlatformHolder.Objects) { if (LayerObject.ContainsKey ("type") == true) { if (LayerObject ["type"] == "jumppad") { b2Vec2 LoadedBoostVector = new b2Vec2 (); if (LayerObject.ContainsKey ("boostvec")) { string[] BoostVecData = LayerObject ["boostvec"].Split (new char[]{ ',' }, StringSplitOptions.RemoveEmptyEntries); if (BoostVecData.Length > 1) { LoadedBoostVector = new b2Vec2 ((float)Convert.ToInt32 (BoostVecData [0]), (float)Convert.ToInt32 (BoostVecData [1])); } } CCPoint LoadedPosition; LoadedPosition = new CCPoint ((float)Convert.ToInt16 (LayerObject ["x"]) * TileMap.ScaleX - JumpPad.JumpPadSize.Width / 2, (float)Convert.ToInt32 (LayerObject ["y"]) * TileMap.ScaleY - JumpPad.JumpPadSize.Height); LoadedJumpPads.Add (new JumpPad (LoadedBoostVector, LoadedPosition, gameContainer.physicsHandler.gameWorld)); } } } return LoadedJumpPads; }
void ParseObjectElement() { if (ObjectGroups == null || ObjectGroups.Count == 0) { return; } CCTileMapObjectGroup objectGroup = ObjectGroups[ObjectGroups.Count - 1]; // The value for "type" was blank or not a valid class name // Create an instance of TMXObjectInfo to store the object and its properties var dict = new Dictionary <string, string>(); var array = new[] { ObjectElementName, ObjectElementType, ObjectElementWidth, ObjectElementHeight, ObjectElementGid }; foreach (string key in array) { if (currentAttributeDict.ContainsKey(key)) { dict.Add(key, currentAttributeDict[key]); } } float x = float.Parse(currentAttributeDict[ObjectElementXPosition]) + objectGroup.PositionOffset.X; float y = float.Parse(currentAttributeDict[ObjectElementYPosition]) + objectGroup.PositionOffset.Y; // Correct y position. Tiled uses inverted y-coordinate system where top is y=0 y = (MapDimensions.Row * TileTexelSize.Height) - y - (currentAttributeDict.ContainsKey(ObjectElementHeight) ? float.Parse(currentAttributeDict[ObjectElementHeight]) : 0); dict.Add(ObjectElementXPosition, ToFloatString(x)); dict.Add(ObjectElementYPosition, ToFloatString(y)); objectGroup.Objects.Add(dict); currentParentElement = CCTileMapProperty.Object; }
void ParseMultilineShape(string shapeName) { // Find parent object's dict and add points to it. If at any time we don't find the objects we are expecting // based on the state of the parser, just return without doing anything instead of crashing. if (ObjectGroups == null || ObjectGroups.Count == 0) { return; } CCTileMapObjectGroup objectGroup = ObjectGroups[ObjectGroups.Count - 1]; if (objectGroup == null || objectGroup.Objects.Count == 0) { return; } var dict = objectGroup.Objects[objectGroup.Objects.Count - 1]; if (!currentAttributeDict.ContainsKey(ObjectElementPoints)) { return; } string value = currentAttributeDict[ObjectElementPoints]; if (String.IsNullOrWhiteSpace(value)) { return; } if (!dict.ContainsKey(ObjectElementXPosition) || !dict.ContainsKey(ObjectElementYPosition)) { return; } float objectXOffset = float.Parse(dict[ObjectElementXPosition]); float objectYOffset = float.Parse(dict[ObjectElementYPosition]); string[] pointPairs = value.Split(' '); var points = new CCPoint[pointPairs.Length]; var sb = new StringBuilder(); for (int i = 0; i < pointPairs.Length; i++) { string pointPair = pointPairs[i]; string[] pointCoords = pointPair.Split(','); if (pointCoords.Length != 2) { return; } // Adjust the offsets relative to the parent object. When adjusting the coordinates, // correct y position. Tiled uses inverted y-coordinate system where top is y=0. // We have to invert the y coordinate to make it move in the correct direction relative to the parent. points[i].X = float.Parse(pointCoords[0]) + objectXOffset; points[i].Y = float.Parse(pointCoords[1]) * -1 + objectYOffset; sb.Append(ToFloatString(points[i].X)); sb.Append(","); sb.Append(ToFloatString(points[i].Y)); sb.Append(" "); } // Strip the trailing space string pointsString = sb.Length > 0 ? sb.ToString(0, sb.Length - 1) : null; dict.Add(ObjectElementPoints, pointsString); dict[ObjectElementShape] = shapeName; }
void ParseObjectGroupElement() { var objectGroup = new CCTileMapObjectGroup(); objectGroup.GroupName = currentAttributeDict[ObjectGrpElementName]; CCPoint positionOffset = CCPoint.Zero; if (currentAttributeDict.ContainsKey(ObjectGrpElementXOffset)) positionOffset.X = CCUtils.CCParseFloat(currentAttributeDict[ObjectGrpElementXOffset]) * TileTexelSize.Width; if (currentAttributeDict.ContainsKey(ObjectGrpElementYOffset)) positionOffset.Y = CCUtils.CCParseFloat(currentAttributeDict[ObjectGrpElementYOffset]) * TileTexelSize.Height; objectGroup.PositionOffset = positionOffset; ObjectGroups.Add(objectGroup); currentParentElement = CCTileMapProperty.ObjectGroup; }
protected override void AddedToScene() { base.AddedToScene (); Background = new CCSprite ("background"); Background.Scale = screenSize.Width / Background.ContentSize.Width; Map = new CCTileMap (new CCTileMapInfo ("tilemaps/bitte.tmx")); //bei einer Invocation Exception einfach ne neue Map machen //vielleicht auch mal schauen, ob die XML_Datei korrekt ist(Attribute, fehlende < etc.) //wenn die Maplayer nicht vollständig geladen werden, muss man die Compression umstellen //mapproperties if (Map.MapPropertyNamed ("Creator") != null) mapCreator = Map.MapPropertyNamed ("Creator"); if (Map.MapPropertyNamed ("Version") != null) mapVersion = Map.MapPropertyNamed ("Version"); if (Map.MapPropertyNamed ("Name") != null) mapName = Map.MapPropertyNamed ("Name"); Map.Scale = 3f; //Scale auch beim Spawnpoint ändern Map.Antialiased = false; //Map.LayerNamed ("mainlayer") pointLayer = Map.ObjectGroupNamed ("points"); Dictionary<string,string> spawnPoint = pointLayer.ObjectNamed ("SpawnPoint"); gameContainer.mainCharacter.Position = new CCPoint ((float)Convert.ToInt32 (spawnPoint ["x"]) * Map.ScaleX, (float)Convert.ToInt32 (spawnPoint ["y"]) * Map.ScaleY); gameContainer.physicsHandler.Initialize (Map.MapDimensions.Size.Width * Map.TileTexelSize.Width * Map.ScaleX, gameContainer.mainCharacter, Map.LayerNamed ("mainlayer"), Map, gameContainer); gameContainer.mainCharacter.bindToPhysicsHandler (gameContainer.physicsHandler); //Platform hinzufügen gameContainer.platformContainer.AddRange (TMXLayerDataLoader.LoadPlatformFromLayer (Map, Map.ObjectGroupNamed ("aditionallayer"), gameContainer)); gameContainer.jumpPadContainer.AddRange (TMXLayerDataLoader.LoadJumpPadFromLayer (Map, Map.ObjectGroupNamed ("aditionallayer"), gameContainer)); this.AddChild (Background, -1); this.AddChild (Map, 0); this.AddChild (gameContainer.mainCharacter.Sprite, 2); this.AddChild (gameContainer.physicsHandler.debugDrawer.DrawNode, 1); foreach (Platform knownPlatform in gameContainer.platformContainer) { this.AddChild (knownPlatform); } foreach (JumpPad knownJumpPad in gameContainer.jumpPadContainer) { this.AddChild (knownJumpPad); } //particle init PlayerMovingParticle = new GroundParticle (); this.AddChild (PlayerMovingParticle); PlayerMovingParticle.Position = gameContainer.mainCharacter.Position; }