Example #1
0
 public MapObject(Body body, BodyInfo bodyInfo, string textureRef)
 {
     this.TextureReference = textureRef;
     this.MapBody = body;
     this.MapBodyInfo = bodyInfo;
     this.textures.Add(new Tuple<Texture2D, Vector2>(ContentController.Instance.GetContent<Texture2D>(textureRef), Vector2.Zero));
 }
Example #2
0
        public static Tuple<Body, BodyInfo> DeserializeBody(World world, int mapHeight, XElement element)
        {
            BodyInfo bodyInfo = null;
            Body body = null;

            bodyInfo = new BodyInfo(mapHeight);
            bodyInfo.XmlDeserialize(element);

            switch (bodyInfo.BodyCategory)
            {
                case BodyCategory.Capsule:
                    body = BodyFactory.CreateCapsule(
                                                        world,
                                                        bodyInfo.Height.ToSimUnits(),
                                                        bodyInfo.TopRadius.ToSimUnits(),
                                                        bodyInfo.TopEdge,
                                                        bodyInfo.BottomRadius.ToSimUnits(),
                                                        bodyInfo.BottomEdge,
                                                        bodyInfo.Density,
                                                        bodyInfo.Position.ToSimUnits());
                    break;

                case BodyCategory.ChainShape:
                    body = BodyFactory.CreateChainShape(
                                                        world,
                                                        bodyInfo.Vertices.ElementAt(0).ToSimUnits(),
                                                        bodyInfo.Position.ToSimUnits());
                    break;

                case BodyCategory.Circle:
                    body = BodyFactory.CreateCircle(
                                                        world,
                                                        bodyInfo.Radius.ToSimUnits(),
                                                        bodyInfo.Density,
                                                        bodyInfo.Position.ToSimUnits());
                    break;

                case BodyCategory.CompoundPolygon:
                    List<FarseerPhysics.Common.Vertices> convertedVerts = new List<FarseerPhysics.Common.Vertices>();
                    foreach (var vert in bodyInfo.Vertices)
                    {
                        convertedVerts.Add(vert.ToSimUnits());
                    }

                    body = BodyFactory.CreateCompoundPolygon(
                                                        world,
                                                        convertedVerts,
                                                        bodyInfo.Density.ToSimUnits(),
                                                        bodyInfo.Position.ToSimUnits());
                    break;

                case BodyCategory.Edge:
                    body = BodyFactory.CreateEdge(
                                                        world,
                                                        bodyInfo.Start.ToSimUnits(),
                                                        bodyInfo.End.ToSimUnits());
                    body.Position = bodyInfo.Position;
                    break;

                case BodyCategory.Ellipse:
                    body = BodyFactory.CreateEllipse(
                                                        world,
                                                        bodyInfo.XRadius.ToSimUnits(),
                                                        bodyInfo.YRadius.ToSimUnits(),
                                                        bodyInfo.Edges,
                                                        bodyInfo.Density,
                                                        bodyInfo.Position.ToSimUnits());
                    break;

                case BodyCategory.Gear:
                    body = BodyFactory.CreateGear(
                                                        world,
                                                        bodyInfo.Radius.ToSimUnits(),
                                                        bodyInfo.NumberOfTeeth,
                                                        bodyInfo.TipPercentage.ToSimUnits(),
                                                        bodyInfo.ToothHeight.ToSimUnits(),
                                                        bodyInfo.Density);

                    body.Position = bodyInfo.Position.ToSimUnits();
                    break;

                case BodyCategory.LineArc:
                    body = BodyFactory.CreateLineArc(
                                                        world,
                                                        bodyInfo.Radians,
                                                        bodyInfo.Sides,
                                                        bodyInfo.Radius.ToSimUnits(),
                                                        bodyInfo.Position.ToSimUnits(),
                                                        bodyInfo.Angle,
                                                        bodyInfo.Closed);
                    break;

                case BodyCategory.LoopShape:
                    body = BodyFactory.CreateLoopShape(
                                                        world,
                                                        bodyInfo.Vertices.ElementAt(0).ToSimUnits(),
                                                        bodyInfo.Position.ToSimUnits());
                    break;

                case BodyCategory.Polygon:
                    body = BodyFactory.CreatePolygon(
                                                        world,
                                                        bodyInfo.Vertices.ElementAt(0).ToSimUnits(),
                                                        bodyInfo.Density,
                                                        bodyInfo.Position.ToSimUnits());
                    break;

                case BodyCategory.Rectangle:
                    body = BodyFactory.CreateRectangle(
                                                        world,
                                                        bodyInfo.Width.ToSimUnits(),
                                                        bodyInfo.Height.ToSimUnits(),
                                                        bodyInfo.Density,
                                                        bodyInfo.Position.ToSimUnits());
                    break;

                case BodyCategory.RoundedRectangle:
                    body = BodyFactory.CreateRoundedRectangle(
                                                        world,
                                                        bodyInfo.Width.ToSimUnits(),
                                                        bodyInfo.Height.ToSimUnits(),
                                                        bodyInfo.XRadius.ToSimUnits(),
                                                        bodyInfo.YRadius.ToSimUnits(),
                                                        bodyInfo.Segments,
                                                        bodyInfo.Density,
                                                        bodyInfo.Position.ToSimUnits());
                    break;

                case BodyCategory.SolidArc:
                    body = BodyFactory.CreateSolidArc(
                                                        world,
                                                        bodyInfo.Density,
                                                        bodyInfo.Radians,
                                                        bodyInfo.Sides,
                                                        bodyInfo.Radius.ToSimUnits(),
                                                        bodyInfo.Position.ToSimUnits(),
                                                        bodyInfo.Angle);

                    break;
            }

            body.BodyType = bodyInfo.BodyType;
            body.FixedRotation = bodyInfo.FixedRotation;
            body.Rotation = bodyInfo.Rotation;

            return new Tuple<Body, BodyInfo>(body, bodyInfo);
        }
Example #3
0
        public override void XmlDeserialize(XElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            XAttribute nameAtt = element.Attribute("name");
            if (nameAtt != null)
            {
                this.Name = nameAtt.Value;
            }

            this.MovementSpeed = ExtensionMethods.XmlDeserializeVector2(element.Element("MovementSpeed"));

            // Create GameSprites out of the Deserialze functions in GameSprite
            foreach (XElement gameSpriteEle in element.Element("Sprites").Elements("GameSprite"))
            {
                GameSprite gameSprite = new GameSprite();
                gameSprite.XmlDeserialize(gameSpriteEle);
                this.sprites.Add(gameSprite.SpriteName, gameSprite);
            }

            // ----------------------
            // Find the Body element
            XElement bodyElement = element.Element("BodyInfo");
            if (bodyElement != null)
            {
                var bodyData = XmlBodyFactory.DeserializeBody(this.World, this.Map.Height, bodyElement.Elements().ElementAt(0));
                this.Body = bodyData.Item1;
                this.bodyInfo = bodyData.Item2;
            }

            this.PathManager = new PathManager(this);
            this.PathManager.Screen = this.Screen;
            XElement pathManagerEle = element.Element("PathManager");
            if (pathManagerEle != null)
            {
                this.PathManager.XmlDeserialize(pathManagerEle);
            }

            // ----------------------------------
            // Assign the new values to the Actor
            this.Health = int.Parse(element.Attribute("health").Value, CultureInfo.CurrentCulture);
            this.Rotation = float.Parse(element.Attribute("rotation").Value, CultureInfo.CurrentCulture);
            this.IsEnabled = bool.Parse(element.Attribute("isEnabled").Value);

            this.VisibleState = (Visibility)Enum.Parse(typeof(Visibility), element.Attribute("visibleState").Value);
        }