public void Parse(XmlNode node) { ///1.property if (node.Attributes.GetNamedItem("name") != null) { name = node.Attributes.GetNamedItem("name").Value; } if (node.Attributes.GetNamedItem("visible") != null) { visible = System.Convert.ToInt32(node.Attributes.GetNamedItem("visible").Value) != 0 ? true : false; } if (node.Attributes.GetNamedItem("opacity") != null) { opacity = System.Convert.ToSingle(node.Attributes.GetNamedItem("opacity").Value); } ///2.object XmlNodeList node_list = node.SelectNodes("object"); foreach (XmlNode obj in node_list) { TmxObject item = new TmxObject(); item.Parse(obj); DicObject.Add(item.id, item); } }
public void CreateGatherable(TmxObject thing, Vector2 pos) { if (thing.Type.Equals("tree")) { Tree anotherTree = new Tree(Tree.TreeType.kNormalTree); anotherTree.LoadContent("Art/tree", Content); anotherTree._Position = _TilemapManager.findTile(pos).tileCenter; anotherTree.Name = thing.Name; _GatherableListActive.Add(anotherTree); _GatherableListActive.Add(anotherTree); } else if (thing.Type.Equals("rock")) { Rock anotherRock = new Rock(Rock.RockType.kNormalRock); anotherRock.LoadContent("Art/" + thing.Type, Content); anotherRock._Position = _TilemapManager.findTile(pos).tileCenter; anotherRock.Name = thing.Name; _GatherableListActive.Add(anotherRock); _GatherableListActive.Add(anotherRock); } else if (thing.Type.Equals("FishingHole")) { FishingHole anotherFish = new FishingHole(FishingHole.FishingType.kNetType); anotherFish.LoadContent("Art/" + thing.Type, Content); anotherFish._Position = _TilemapManager.findTile(pos).tileCenter; anotherFish.Name = thing.Name; _GatherableListActive.Add(anotherFish); _GatherableListActive.Add(anotherFish); } }
private void CreateBody(TmxObject tmxObj, Vector2 offset) { Vector2 position = ConvertUnits.ToSimUnits(new Vector2((float)tmxObj.X, (float)tmxObj.Y)); position += ConvertUnits.ToSimUnits(offset); switch (tmxObj.ObjectType) { case TmxObjectType.Basic: case TmxObjectType.Tile: case TmxObjectType.Ellipse: { //convert position from top right to center Vector2 size = ConvertUnits.ToSimUnits(new Vector2((float)tmxObj.Width, (float)tmxObj.Height)); position += new Vector2(size.X / 2, size.Y / 2); ULoffset = new Vector2(-1 * size.X / 2, -1 * size.Y / 2); break; } default: { ULoffset = Vector2.Zero; break; } } body = new Body(world, position); foreach (var property in DefaultBodyProperties) { string value; tmxObj.Properties.TryGetValue(property, out value); // allow null elements to act as a flag to set default values SetProperty(property, value, body); } }
public Teleport(TmxObject obj, Tileset tileset) : base(obj, tileset) { string[] property = obj.Properties["teleport"].Split(','); Map = property[0]; TeleportPosition = new Vector2(int.Parse(property[1]), int.Parse(property[2])); switch (property[3]) { case "up": Direction = FacingDirection.Up; break; case "down": Direction = FacingDirection.Down; break; case "left": Direction = FacingDirection.Left; break; case "right": Direction = FacingDirection.Right; break; } }
public void VisitObject(TmxObject obj) { if (obj is TmxObjectTile) { (obj as TmxObjectTile).DrawOrderIndex = drawOrderIndex++; } }
public Vector2 GetSpawnPoint(int spawnPointNumber) { TmxObject spawnPoint = _map.ObjectGroups["SpawnPoints"].Objects[spawnPointNumber]; Vector2 spawnPointLocation = new Vector2((float)spawnPoint.X, (float)spawnPoint.Y); return(spawnPointLocation); }
public BadGuyTiled(TmxObject e, Scene scene) : base(e, scene) { BadGuy g = new BadGuy(new Vector2((int)e.X, (int)e.Y), scene); g.Sprite.Effects = e.Properties.ContainsKey("Flipped") ? SpriteEffects.FlipHorizontally : SpriteEffects.None; }
public static void CrearSolidos(TmxObject obj, Dictionary <string, string> properties, Entity defaultCollidable) { if (properties["Collidable"] == "true") { defaultCollidable.AddComponent(new BoxCollider(obj.X, obj.Y, obj.Width, obj.Height)); } }
public Trigger(TmxObject obj, LevelSettings settings) { repeat = obj.properties.GetInt("Repeat", 0) != 0; string targetDialogue; if (obj.properties.GetTag("Dialogue", out targetDialogue)) { var dia = settings.dialogueMap[targetDialogue]; Run += () => { if (!LevelManager.instance.settings.HasDialogueFlag(dia)) { LevelManager.instance.settings.StoreDialogueFlag(dia); DialogueManager.DialogueComplete = GameWorld.GoBackToLevel; DialogueManager.ActivateDialogue(dia); } }; } area = obj.position; Trigger.ActionType at = Trigger.ActionType.Any; try { at = (Trigger.ActionType)Enum.Parse(typeof(Trigger.ActionType), obj.type, true); } catch (ArgumentException ae) { Debug.LogError(ae.Message); } type = at; }
public LightPole(TmxObject e, Scene scene) : base(e, scene) { _scene = scene; l = new Light(new Vector2((int)e.X, (int)e.Y), scene); l.Intensity = .7f; l.Position -= new Vector2(l.Sprite.TextureWidth / 2, l.Sprite.TextureHeight / 1.3f); scene.GameObjects.AddGameObject("Light", l); l.Body.GravityScale = 0; l.Body.IsStatic = false; l.Velocity = new Vector2(-300, 0); intensityPerSecondCurrent = 0.0453f; _lightpoleGameObject = new GameObject("LightPole", scene, new Vector2(-700, (float)e.Y)); _lightpoleGameObject.Body.GravityScale = 0f; _lightpoleGameObject.Velocity = new Vector2(-300, 0); var i = _lightpoleGameObject.AddComponent("Sprite", new Sprite(_lightpoleGameObject, "LightPole")); Sprite sprite = (Sprite)_lightpoleGameObject.GetComponent(i); sprite.Width = (int)(sprite.Width * 1.8f); sprite.Height = (int)(sprite.Height * 1.8f); _lightpoleGameObject.Position -= new Vector2(sprite.Width / 2, sprite.Height / 2); scene.GameObjects.AddGameObject("LightPole", _lightpoleGameObject); }
public IntroCredits(TmxObject e, Scene scene) : base(e, scene) { _scene = scene; state = 0; this.Depth = 1.0f; }
private void CreateFixture(TmxObject tmxObj) { float density = 1f; string densityStr; tmxObj.Properties.TryGetValue("Density", out densityStr); if (densityStr != null) { density = float.Parse(densityStr); } // A Farseer Body's Position variable defines the CENTER of the Body, so we add half the width and height to get it to the desired location. switch (tmxObj.ObjectType) { case TmxObjectType.Basic: case TmxObjectType.Tile: { Vector2 size = ConvertUnits.ToSimUnits(new Vector2((float)tmxObj.Width, (float)tmxObj.Height)); fixture = FixtureFactory.AttachRectangle(size.X, size.Y, density, Vector2.Zero, body); break; } case TmxObjectType.Ellipse: { Vector2 size = ConvertUnits.ToSimUnits(new Vector2((float)tmxObj.Width, (float)tmxObj.Height)); if (size.X == size.Y) { fixture = FixtureFactory.AttachCircle(size.X / 2, density, body); } else { fixture = FixtureFactory.AttachEllipse(size.X / 2, size.Y / 2, Settings.MaxPolygonVertices, density, body); } break; } case TmxObjectType.Polygon: { Vertices vertices = new Vertices(); foreach (var v in tmxObj.Points) { vertices.Add(ConvertUnits.ToSimUnits(new Vector2((float)v.X, (float)v.Y))); } List<Vertices> decomposedVertices = Triangulate.ConvexPartition(vertices, TriangulationAlgorithm.Bayazit); fixtures = FixtureFactory.AttachCompoundPolygon(decomposedVertices, density, body); break; } case TmxObjectType.Polyline: { Vertices vertices = new Vertices(); foreach (var v in tmxObj.Points) { vertices.Add(ConvertUnits.ToSimUnits(new Vector2((float)v.X, (float)v.Y))); } fixture = FixtureFactory.AttachChainShape(vertices, body); break; } default: { throw new InvalidOperationException("TmxObjectType not recognized: " + tmxObj.ObjectType.ToString()); } } }
private void AddRectangleToCollisionPolygons(TmxObject tmxObject, int gid, Collision collision) { var rectangle = new Rectangle((int)(tmxObject.X), (int)(tmxObject.Y), (int)tmxObject.Width, (int)tmxObject.Height); Tuple <Polygon, Collision> terrainCollision = new Tuple <Polygon, Collision>(new Polygon(rectangle), collision); CollisionPolygons.Add(gid, terrainCollision); }
public ItemObject(TmxObject obj, Tileset tileset) : base(obj, tileset) { if (ScriptId == -1) { Item = Global.GetItem(int.Parse(obj.Properties["item"])); Quantity = obj.Properties.ContainsKey("quantity") ? int.Parse(obj.Properties["quantity"]) : 1; } }
public void LoadObj(TmxObject obj) { this.id = obj.Id; this.name = obj.Name; position = new RectangleF((float)obj.X, (float)obj.Y, (float)obj.Width, (float)obj.Height); GetProperties(obj); }
public Flag(TmxObject tmxObject, TmxMap tmxMap) : base(SwinGame.LoadBitmap(@"Resources\flag.png")) { Position = new Vector2 { X = (float)tmxObject.X, Y = (float)tmxObject.Y }; }
public Key(TmxObject tmxObject, TmxMap tmxMap) : base(SwinGame.LoadBitmap(@"Resources\key.png")) { Position = new Vector2 { X = (float)tmxObject.X, Y = (float)tmxObject.Y + 4 }; }
/// <summary> /// From absolute position in pixels to side of tile = 1 /// </summary> /// <param name="tmxObject"></param> /// <param name="tileWidth"></param> /// <param name="tileHeight"></param> /// <param name="worldHeight"></param> private static void NormalizeObjectPosition(TmxObject tmxObject, int tileWidth, int tileHeight, int worldHeight) { tmxObject.X /= tileWidth; tmxObject.Y /= tileHeight; tmxObject.Y = worldHeight - tmxObject.Y; tmxObject.Width /= tileWidth; tmxObject.Height /= tileHeight; tmxObject.Rotation = -MathHelper.ToRadians(tmxObject.Rotation); }
public LightPrefab(TmxObject e, Scene scene) : base(e, scene) { l = new Light(new Vector2((int)e.X, (int)e.Y), scene); l.Intensity = .7f; l.Position -= new Vector2(l.Sprite.TextureWidth / 2, l.Sprite.TextureHeight / 2); scene.GameObjects.AddGameObject("Light", l); intensityPerSecondCurrent = 0.043f; }
public void OnObjectLoaded(List <TiledMapComponent> allMapComponents, TmxObject loadedObject) { if (loadedObject.Type.Equals("playerSpawn", StringComparison.CurrentCultureIgnoreCase) == false) { return; } SpawnLocations.Add(new Vector2((float)loadedObject.X, (float)loadedObject.Y)); }
public TiledPrefab(TmxObject e, Scene scene) { _e = e; _scene = scene; DoTransform = true; Depth = 0.5f; Color = Color.White; scene.DrawSystem.AddTiledPrefab(this); }
public ObjectSceneEntity(TmxObject obj, int tileSize) { Width = obj.Width; Height = obj.Height; Type = obj.Type; Name = obj.Name; TileSize = tileSize; this.obj = obj; }
/// <summary> /// Transforms from rotation around left lower corner to rotation around center /// </summary> /// <param name="tmxObject"></param> private static void TransformObjectPosition(TmxObject tmxObject) { Vector2 rotationCenter = new Vector2(tmxObject.X, tmxObject.Y); Vector2 size = new Vector2(tmxObject.Width / 2, tmxObject.Height / 2); Vector2 newPosition = RotateAroundCenter(rotationCenter + size, rotationCenter, tmxObject.Rotation); tmxObject.X = newPosition.X; tmxObject.Y = newPosition.Y; }
public ClimbablePrefab(TmxObject e, Scene scene) : base(e, scene) { _me = new GameObject("Climable", scene, new Vector2((int)e.X, (int)e.Y)); _me.AddComponent("Climbable", new RectangleCollider((int)e.Width / 2, (int)e.Height / 2, (int)e.Width, (int)e.Height, 1f)); _me.Static = true; _me.Body.Friction = 1.0f; _me.Tags.Add("Climbable"); }
public void OnObjectLoaded(List <TiledMapComponent> onMap, TmxObject loadedObject) { if (loadedObject.Type.Equals("player", StringComparison.CurrentCultureIgnoreCase)) { Actor playerActor = stage.CreateActor(1); playerActor.AddComponent <Transform2>().WorldPosition = onMap[0].TiledToWorld(new Vector2((float)loadedObject.X, (float)loadedObject.Y)); playerActor.AddComponent <TiledTestPlayer>(); playerActor.AddComponent <DrawTextureRegionComponent>().region = textureRegion; } }
public void SetupTiles() { if (Core.Scene == null || enabled) { return; } //enabled = true; TmxMap map = Core.Scene.Content.LoadTiledMap("Assets/map.tmx"); Entity entity = Core.Scene.CreateEntity("tiled-map"); TiledMapRenderer tmr = entity.AddComponent(new TiledMapRenderer(map, "Collision", true)); TmxLayer CustomCollisionLayer = (TmxLayer)map.GetLayer("CustomCollision"); foreach (TmxLayerTile tile in CustomCollisionLayer.Tiles) { if (tile != null && tile.TilesetTile != null) { TmxList <TmxObjectGroup> objgl = tile.TilesetTile.ObjectGroups; if (objgl != null && objgl.Count > 0) { TmxObjectGroup objg = objgl[0]; if (objg.Objects != null && objg.Objects.Count > 0) { TmxObject obj = objg.Objects[0]; TmxObjectType type = obj.ObjectType; if (type == TmxObjectType.Ellipse) { //Draw Ellipse as collision Core.Scene.CreateEntity(obj.Name, new Vector2(tile.Position.X * tile.Tileset.TileWidth + obj.Width / 2, tile.Position.Y * tile.Tileset.TileHeight + obj.Height / 2)) .AddComponent(new FSCollisionEllipse(obj.Width / 2, obj.Height / 2)) .AddComponent(new CircleCollider((obj.Width + obj.Height) / 4)); // have to get an average of sides, hence / 4 } else if (type == TmxObjectType.Polygon) { Vector2[] points = obj.Points; Core.Scene.CreateEntity(obj.Name, new Vector2(tile.Tileset.TileWidth * tile.Position.X + obj.X, tile.Tileset.TileHeight * tile.Position.Y + obj.Y)) .AddComponent(new FSCollisionPolygon(points)) .AddComponent(new PolygonCollider(points)); } //basic is rectangle else if (type == TmxObjectType.Basic) { Core.Scene.CreateEntity(obj.Name, new Vector2(tile.Position.X * tile.Tileset.TileWidth + obj.Width / 2, tile.Position.Y * tile.Tileset.TileHeight + obj.Height / 2)) .AddComponent(new FSCollisionBox(obj.Width, obj.Height)) .AddComponent(new BoxCollider(obj.Width, obj.Height)); } } } } } tmr.SetLayersToRender(new string[] { }); }
public void CreateTiledPrefab(string name, TmxObject tmxobject) { try { LoadedTiledPrefabs.Add((TiledPrefab)Activator.CreateInstance(TiledPrefabs[name], tmxobject, this)); } catch (System.Reflection.TargetInvocationException e) { if (e.InnerException != null) { throw e.InnerException; } } }
public LevelLoaderPrefab(TmxObject e, Scene scene) : base(e, scene) { level = e.Properties["Level"]; _scene = scene; GameObject a = new GameObject("LevelLoader", scene, new Vector2((int)e.X + (int)e.Width / 2, (int)e.Y + (int)e.Height / 2)); a.AddComponent("Collider", new RectangleCollider(0, 0, (int)e.Width, (int)e.Height, 0f)); a.Body.IsSensor = true; a.Body.IsStatic = true; a.Body.OnCollision += Body_OnCollision; }
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)); } }
private static void ReadObjects(XmlNode node, TmxMap result) { if (node.HasChildNodes) { foreach (XmlNode child in node.ChildNodes) { if (child.Name == "object") { Rect rect = new Rect( (float)(child.ReadDouble("x")) / 100, (float)(child.ReadDouble("y")) / 100, (float)(child.ReadDouble("width", 100)) / 100, (float)(child.ReadDouble("height", 100)) / 100); rect.y = result.Height - rect.height - rect.y; child.ReadTag("type", "any"); var properties = new Dictionary <string, string>(); bool ellipse = false; foreach (XmlNode grandchild in child.ChildNodes) { if (grandchild.Name == "properties") { ReadProperties(grandchild, properties); } else if (grandchild.Name == "ellipse") { ellipse = true; } } TmxObject.ObjectType oType; int id = child.ReadInt("gid", -1); if (ellipse == true) { oType = TmxObject.ObjectType.Ellipse; } if (id != -1) { oType = TmxObject.ObjectType.Tile; } else { oType = TmxObject.ObjectType.Rect; } string type = child.ReadTag("type", "any"); TmxObject obj = new TmxObject(rect, oType, type, properties); result.Objects.Add(obj); } } } }
private static void DrawObjectCollider(Graphics g, TmxMap tmxMap, TmxObject tmxObject, Color color) { using (Brush brush = TmxHelper.CreateObjectColliderBrush(color)) using (Pen pen = new Pen(color)) { GraphicsState state = g.Save(); PointF xfPosition = TmxMath.ObjectPointFToMapSpace(tmxMap, tmxObject.Position); g.TranslateTransform(xfPosition.X, xfPosition.Y); g.RotateTransform(tmxObject.Rotation); if (tmxObject.GetType() == typeof(TmxObjectPolygon)) { DrawPolygon(g, pen, brush, tmxMap, tmxObject as TmxObjectPolygon); } else if (tmxObject.GetType() == typeof(TmxObjectRectangle)) { if (tmxMap.Orientation == TmxMap.MapOrientation.Isometric) { TmxObjectPolygon tmxIsometricRectangle = TmxObjectPolygon.FromRectangle(tmxMap, tmxObject as TmxObjectRectangle); DrawPolygon(g, pen, brush, tmxMap, tmxIsometricRectangle); } else { // Rectangles are polygons DrawPolygon(g, pen, brush, tmxMap, tmxObject as TmxObjectPolygon); } } else if (tmxObject.GetType() == typeof(TmxObjectEllipse)) { DrawEllipse(g, pen, brush, tmxMap, tmxObject as TmxObjectEllipse); } else if (tmxObject.GetType() == typeof(TmxObjectPolyline)) { DrawPolyline(g, pen, tmxMap, tmxObject as TmxObjectPolyline); } else if (tmxObject.GetType() == typeof(TmxObjectTile)) { GraphicsState tileState = g.Save(); TmxObjectTile tmxObjectTile = tmxObject as TmxObjectTile; // Isometric tiles are off by a half-width if (tmxMap.Orientation == TmxMap.MapOrientation.Isometric) { g.TranslateTransform(-tmxMap.TileWidth * 0.5f, 0); } // Apply scale SizeF scale = tmxObjectTile.GetTileObjectScale(); g.ScaleTransform(scale.Width, scale.Height); // Apply horizontal flip if (tmxObjectTile.FlippedHorizontal) { g.TranslateTransform(tmxObjectTile.Tile.TileSize.Width, 0); g.ScaleTransform(-1, 1); } // Apply vertical flip if (tmxObjectTile.FlippedVertical) { g.TranslateTransform(0, -tmxObjectTile.Tile.TileSize.Height); g.ScaleTransform(1, -1); } // (Note: Now we can draw the tile and collisions as normal as the transforms have been set up.) // Draw the tile Rectangle destination = new Rectangle(0, -tmxObjectTile.Tile.TileSize.Height, tmxObjectTile.Tile.TileSize.Width, tmxObjectTile.Tile.TileSize.Height); Rectangle source = new Rectangle(tmxObjectTile.Tile.LocationOnSource, tmxObjectTile.Tile.TileSize); g.DrawImage(tmxObjectTile.Tile.TmxImage.ImageBitmap, destination, source, GraphicsUnit.Pixel); // Put a black border around the tile so it sticks out a bit as an object g.DrawRectangle(Pens.Black, destination); // Draw the collisions // Temporarily set orienation to Orthogonal for tile colliders TmxMap.MapOrientation restoreOrientation = tmxMap.Orientation; tmxMap.Orientation = TmxMap.MapOrientation.Orthogonal; { // Make up for the fact that the bottom-left corner is the origin g.TranslateTransform(0, -tmxObjectTile.Tile.TileSize.Height); foreach (var obj in tmxObjectTile.Tile.ObjectGroup.Objects) { DrawObjectCollider(g, tmxMap, obj, Color.Gray); } } tmxMap.Orientation = restoreOrientation; g.Restore(tileState); } else { g.Restore(state); Logger.WriteWarning("Unhandled object: {0}", tmxObject.GetNonEmptyName()); } // Restore our state g.Restore(state); } }