예제 #1
0
        protected override object GetValueEx(string name)
        {
            if (name != "Body")
            {
                return(base.GetValueEx(name));
            }

            if (BodyObject != null)
            {
                if (string.IsNullOrEmpty(BodyObjectTypeName))
                {
                    BodyObjectTypeName = BodyObject.GetType().AssemblyQualifiedName;
                    return(BodyObject.ToXMLString());
                }
                else
                {
                    // to support SetBodyObject(XObject obj, Type serializeAccordingToType)
                    // e.g. SetBodyObject(T_Demography, T_Doc_Meta)

                    Type t = Type.GetType(BodyObjectTypeName, false, false);
                    return(BodyObject.ToXMLString(t));
                }
            }

            return(Body);
        }
예제 #2
0
        public CustomFieldItemAddRequest(object value, Card card, string fieldId) : base("cards/{cardId}/customField/{fieldId}/item", Method.PUT)
        {
            AddParameter("cardId", card.CardId, ParameterType.UrlSegment);
            AddParameter("fieldId", fieldId, ParameterType.UrlSegment);
            switch (value)
            {
            case int _:
            case long _:
            case decimal _:
                BodyObject.Add("value", new { number = Convert.ToString(value) });
                break;

            case bool _:
                BodyObject.Add("value", new { @checked = Convert.ToString(value) });
                break;

            case string _:
                BodyObject.Add("value", new { text = Convert.ToString(value) });
                break;

            case IEnumerable _:
                BodyObject.Add("value", new { list = Convert.ToString(value) });
                break;

            case DateTime _:
                BodyObject.Add("value", new { date = Convert.ToString(value) });
                break;

            default:
                BodyObject.Add("value", new { text = Convert.ToString(value) });
                break;
            }
        }
예제 #3
0
    /// <summary>
    /// Calculates a gravimetric result optimised for when the probe is moving
    /// </summary>
    /// <param name="body">Body to calculate gravity with</param>
    /// <returns>Returns a GravimetricResult object containing the data used/resulting by/from the gravity calculation</returns>
    public GravimetricResult GetGravity(BodyObject body)
    {
        Vector3 direction = body.GetPosition() - GetComponent <GeneralObject>().GetPosition();                          //work out direction between both objects
        float   distance  = direction.magnitude;                                                                        //get distance from direction
        float   magnitude = G * ((body.GetMass() * GetComponent <GeneralObject>().GetMass()) / Mathf.Pow(distance, 2)); //calculates the magnitude of the gravitational force
        Vector3 force     = direction.normalized * (magnitude * body.GravimetricStrength);                              //convert calculation into workable force

        return(new GravimetricResult(force, distance));
    }
    private static int BodyIndex(BodyObject b, List <BodyObject> l)
    {
        int i = -1;

        foreach (BodyObject c in l)
        {
            i++;
            if (c == b)
            {
                return(i);
            }
        }
        Debug.LogError("Object not found.");
        return(-1);
    }
    private int FindBodyIndex(BodyObject b, List <BodyObject> l)
    {
        int i = -1;

        foreach (BodyObject c in l)
        {
            i++;
            if (c == b)
            {
                return(i);
            }
        }
        Debug.LogError("BodyObject not found in list.");
        return(-1);
    }
예제 #6
0
    /// <summary>
    /// Shecks if the object is in range of the passed through planetary body
    /// </summary>
    /// <param name="obj">Body's BodyObject for getting planetary data from</param>
    /// <returns>Returns a boolean that indicates if a body is within range</returns>
    public bool checkInRange(BodyObject obj)
    {
        //throw if BodyObjects refers to a destroyed object
        if (!obj)
        {
            return(false);
        }

        //calculate relative distance between object and body
        Vector3 direction = obj.GetPosition() - GetComponent <ProbeObject>().GetPosition();
        float   distance  = direction.magnitude;

        //check if object and body are within range
        if (distance < obj.GetSize() + InteractivityRange)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
예제 #7
0
        public PhysicsSprite(PhysicsSimulator physicsSim, Canvas parentCanvas, UIElement element, List <Point> points, bool showDebug, float defaultFriction, UIElement boundaryElement)
        {
#if SILVERLIGHT
            RootCanvas = (Canvas)System.Windows.Markup.XamlReader.Load(xaml);
#else
            using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(new System.IO.StringReader(xaml)))
                RootCanvas = (Canvas)System.Windows.Markup.XamlReader.Load(xmlReader);
#endif
            this.Content      = RootCanvas;
            this.InsideCanvas = (Canvas)RootCanvas.FindName("InsideCanvas");

            Simulator            = physicsSim;
            RotationTransform    = RootCanvas.FindName("rotateSprite") as RotateTransform;
            TranslationTransform = RootCanvas.FindName("translateTransform") as TranslateTransform;

            IsNestedUsercontrol = BoundaryHelper.IsInsideNestedUsercontrol(element);
            OriginalNestedLeft  = 0; OriginalNestedTop = 0;
            OriginalElementName = element.GetValue(Canvas.NameProperty).ToString();
            OriginalCanvasName  = parentCanvas.GetValue(Canvas.NameProperty).ToString();

            if (IsNestedUsercontrol)
            {
                // this is a user control with nested physics elements.
                // store it's offset for later positioning
                Canvas      cnvParent           = (element as FrameworkElement).Parent as Canvas;
                UserControl ucParentUserControl = (cnvParent.Parent as UserControl);
                OriginalUserControlName = ucParentUserControl.Name;
                OriginalNestedLeft      = Convert.ToInt32(ucParentUserControl.GetValue(Canvas.LeftProperty));
                OriginalNestedTop       = Convert.ToInt32(ucParentUserControl.GetValue(Canvas.TopProperty));

                // we'll process the storyboards so that they point to element references
                foreach (DictionaryEntry item in ucParentUserControl.Resources)
                {
                    if (item.Value is Storyboard)
                    {
                        Storyboard sb = item.Value as Storyboard;
                        foreach (Timeline tl in sb.Children)
                        {
                            //if (tl.GetValue(Storyboard.TargetNameProperty) == element.GetValue(Canvas.NameProperty))
                            string    target        = tl.GetValue(Storyboard.TargetNameProperty).ToString();
                            UIElement targetElement = ucParentUserControl.FindName(target) as UIElement;
                            if (targetElement != null)
                            {
                                Storyboard.SetTarget(sb, targetElement);
                            }
                        }
                    }
                }
            }

            // do we want to use a different element to determine boundary outline?
            UIElement elementBoundary = element;
            if (boundaryElement != null)
            {
                elementBoundary = boundaryElement;
            }


            if (points == null)
            {
                points = BoundaryHelper.GetPointsForElement(elementBoundary, element, false);
            }
            else
            {
                BoundaryHelper.GetPointsForElement(elementBoundary, element, true);
            }

            if (points == null || points.Count == 0)
            {
                return;
            }

            OutlinePoints = new List <Point>();
            OutlinePoints.AddRange(points);


            // let's clean up the number of points, for a little performance boost..
            int   distanceThreshold = 5;
            Point ptLast            = points[points.Count - 1];
            for (int i = points.Count - 2; i >= 0; i--)
            {
                if (points.Count < 10)
                {
                    break;
                }

                if (PhysicsController.DistanceBetweenTwoPoints(ptLast, points[i]) < distanceThreshold)
                {
                    points.Remove(points[i]);
                }
                else
                {
                    ptLast = points[i];
                }
            }

            // get the overall dimensions
            double width, height, minX, minY;
            PhysicsController.GetPointDimensions(points, out width, out height, out minX, out minY);


            // make the canvas size match the path it contains
            this.Width  = width;
            this.Height = height;
            RootCanvas.SetValue(Canvas.WidthProperty, width);
            RootCanvas.SetValue(Canvas.HeightProperty, height);

            // nested user control elements are removed from their user control
            // and copied into the main game canvas
            if (BoundaryHelper.IsInsideNestedUsercontrol(element))
            {
                UserControl ucParent = BoundaryHelper.GetParentUserControl(element);

                Canvas cnvParent = (element as FrameworkElement).Parent as Canvas;
                cnvParent.Children.Remove(element);
            }

            // draw the polygon
            if (uiElement != null)
            {
                RootCanvas.Children.Remove(uiElement);
                uiElement = null;
            }

            // create the physics body
            Vertices vertices = new Vertices();

            double centerX = (this.Width / 2);
            double centerY = (this.Height / 2);


            foreach (Point point in points)
            {
                // note vertices are based on 0,0 at center of body
                double x, y;

                // first we need to remove the offset position of the drawn shape on the main canvas
                x = Convert.ToDouble(point.X) - Convert.ToDouble(element.GetValue(Canvas.LeftProperty));                //minX;
                y = Convert.ToDouble(point.Y) - Convert.ToDouble(element.GetValue(Canvas.TopProperty));                 //minY;

                // we need to make points relative to origin (center) of object
                x = x - centerX;
                y = y - centerY;

                vertices.Add(new Vector2((float)x, (float)y));
            }

            // if there are too few points, then subdivide the edges
            if (vertices.Count <= 4)
            {
                vertices.SubDivideEdges(6);
            }

            _offsetCentroid = vertices.GetCentroid();


            // initialize the polygon sprite
            BodyObject     = BodyFactory.Instance.CreateBody(physicsSim, 0.5f, vertices.GetMomentOfInertia());
            GeometryObject = GeomFactory.Instance.CreatePolygonGeom(physicsSim, BodyObject, vertices, 0);

            GeometryObject.FrictionCoefficient = defaultFriction;
            GeometryObject.OnCollision        += new FarseerGames.FarseerPhysics.Collisions.CollisionEventHandler(HandleCollision);

            BodyObject.Enabled = true;
            BodyObject.ClearForce();
            BodyObject.ClearTorque();

            // adjust position to take into consideration origin at middle
            Vector2 position = new Vector2((float)minX, (float)minY);
            position.X = (position.X + ((float)getWidth / 2));
            position.Y = (position.Y + ((float)getHeight / 2));

            // adjust for centroid
            bool bAdjustCentroid = true;
            if ((element is Image) && (element as Image).Clip != null)
            {
                bAdjustCentroid = false;
            }

            if (bAdjustCentroid)
            {
                position.X = position.X + _offsetCentroid.X;
                position.Y = position.Y + _offsetCentroid.Y;
            }

            BodyObject.Position = position;

            Update();

            uiElement = element;
#if SILVERLIGHT
            parentCanvas.Children.Remove(uiElement);
#else
            if (parentCanvas == System.Windows.Media.VisualTreeHelper.GetParent(element))
            {
                parentCanvas.Children.Remove(uiElement);
            }
            else
            {
                Canvas cnv = (Canvas)System.Windows.Media.VisualTreeHelper.GetParent(element);
                if (cnv == null)
                {
                    parentCanvas.Children.Remove(uiElement);
                }
                else
                {
                    cnv.Children.Remove(element);
                }
            }
#endif

            uiElement.SetValue(Canvas.LeftProperty, -(width / 2));
            uiElement.SetValue(Canvas.TopProperty, -(height / 2));

            TranslateTransform trans = new TranslateTransform();
            trans.X = width / 2;
            trans.Y = height / 2;
            uiElement.RenderTransform = trans;

            InsideCanvas.Children.Add(uiElement);

            uiElement.SetValue(Canvas.LeftProperty, (double)(-((width / 2) + _offsetCentroid.X)));
            uiElement.SetValue(Canvas.TopProperty, (double)(-((height / 2) + _offsetCentroid.Y)));

            if (IsNestedUsercontrol)
            {
                BodyObject.Position = new Vector2((float)(BodyObject.Position.X + OriginalNestedLeft), (float)(BodyObject.Position.Y + OriginalNestedTop));
                Update();
            }

            if (showDebug)
            {
                _pathDebug         = PhysicsController.CreatePathFromVertices(vertices, getWidth, getHeight);
                _pathDebug.Opacity = 0.7;
                RootCanvas.Children.Add(_pathDebug);
            }


            SetDebug(showDebug);
        }