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 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 NPC(TmxObject obj, Tileset tileset, string mapName) : base(obj, tileset, mapName) { Sheet = new SpriteSheet(TextureId, tileset); DialogId = obj.Properties.ContainsKey("dialog") ? int.Parse(obj.Properties["dialog"]) : -1; if (TextureId == Sheet.GidUp) Direction = FacingDirection.Up; else if (TextureId == Sheet.GidLeft) Direction = FacingDirection.Left; else if (TextureId == Sheet.GidRight) Direction = FacingDirection.Right; else Direction = FacingDirection.Down; DefaultDirection = Direction; }
public MapObject(TmxObject obj, Tileset tileset, string mapName = "") { // Name Name = obj.Name != null ? obj.Name : "NoName"; // Identifier used for ObjectInfo instance Identifier = String.Format("{0}_{1}_{2}", obj.Id, mapName, Name).Replace(" ", string.Empty); // ObjectInfo instance, holding information about the object in runtime SavedInformation = new ObjectInfo(Identifier); if (obj.Properties.ContainsKey("active")) { SavedInformation.IsActive = obj.Properties["active"] == "true" ? true : false; } // Gets the script id ScriptId = obj.Properties.ContainsKey("script") ? int.Parse(obj.Properties["script"]) : -1; // Gets the name color if (obj.Properties.ContainsKey("color")) { string[] rgba = obj.Properties["color"].Split(','); NameColor = rgba.Count() == 4 ? new Color(float.Parse(rgba[0]), float.Parse(rgba[1]), float.Parse(rgba[2]), float.Parse(rgba[3])) : Color.White; } else { NameColor = Color.White; } // Gets texture information Texture = tileset.Texture; TextureId = obj.Tile != null ? obj.Tile.Gid - tileset.FirstGid : -1; // Workaround for the Y axis when it's a tile object (instead of rectangle) int y = obj.ObjectType == TmxObjectType.Tile ? (int)obj.Y - 1 : (int)obj.Y; // Sets position MapPosition = new Vector2((int)obj.X / Global.TileSize, y / Global.TileSize); CurrentPosition = MapPosition * Global.TileSize; DestinationPosition = CurrentPosition; IsMoving = false; // Width and height for objects bigger than a tile Size = new Vector2((int)obj.Width / Global.TileSize, (int)obj.Height / Global.TileSize); }
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; } }
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()); } } }
// Creates Farseer Fixture based on tmxObj specifications // Can optionally be offset from position given by tmxObj public FarseerFixtureComponent(TmxObject tmxObj, World world, Vector2 offset = default(Vector2)) { this.world = world; CreateBody(tmxObj, offset); CreateFixture(tmxObj); }
public Player(TmxObject obj, Tileset tileset) : base(obj, tileset, "") { Name = "Stryfe"; Attributes = new AttributeSheet(); }