Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PadController" /> class.
        /// </summary>
        /// <param name="padColor">Color of the pad.</param>
        public PadBody(Color padColor)
            : base()
        {
            this.entity = new Entity()
                          .AddComponent(new Transform2D()
            {
                Origin = Vector2.Center, Scale = new Vector2(2, 1)
            })
                          .AddComponent(new Sprite("Content/pad.wpk")
            {
                TintColor = padColor
            })
                          .AddComponent(new RectangleCollider())
                          .AddComponent(new PadBehavior())
                          .AddComponent(new RigidBody2D()
            {
                CollisionCategories = Physic2DCategory.Cat2, CollidesWith = Physic2DCategory.Cat1, FixedRotation = true
            })
                          .AddComponent(new JointMap2D())
                          .AddComponent(new SpriteRenderer(DefaultLayers.Alpha));

            this.SpringJoint = new FixedMouseJoint2D();
            this.entity.FindComponent <JointMap2D>().AddJoint("spring", this.SpringJoint);

            // Controls the pad collisions
            this.entity.FindComponent <RigidBody2D>().OnPhysic2DCollision += this.OnOnPhysic2DCollision;
        }
Beispiel #2
0
        /// <summary>
        /// Update Method
        /// </summary>
        /// <param name="gameTime">Current Game Time</param>
        protected override void Update(TimeSpan gameTime)
        {
            this.input = WaveServices.Input;

            if (this.input.TouchPanelState.IsConnected)
            {
                this.touchState = this.input.TouchPanelState;

                // Checks Mouse Left Button Click and anyone entity linked
                if (this.touchState.Count > 0 && this.mouseJoint == null)
                {
                    // Udpates Mouse Position
                    this.touchPosition = this.touchState[0].Position;

                    foreach (Entity entity in this.Scene.EntityManager.EntityGraph)
                    {
                        Collider2D collider = entity.FindComponent <Collider2D>(false);
                        if (collider != null)
                        {
                            // Collider Test
                            if (collider.Contain(touchPosition))
                            {
                                RigidBody2D rigidBody = entity.FindComponent <RigidBody2D>();
                                if (rigidBody != null)
                                {
                                    // Forbiden Mouse Joint of Kinematic Bodies
                                    if (rigidBody.PhysicBodyType != PhysicBodyType.Kinematic)
                                    {
                                        this.connectedEntity = entity;

                                        // Create Mouse Joint
                                        this.mouseJoint = new FixedMouseJoint2D(this.touchPosition);
                                        JointMap2D jointMap = this.connectedEntity.FindComponent <JointMap2D>();
                                        jointMap.AddJoint("mouseJoint", this.mouseJoint);

                                        // We can break after collider test when true, but we'll miss overlapped entities if Physic entity is
                                        // under a non Physic entity. We are breaking here just for sample.
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                // Checks Mouse Left Button Release
                if (this.touchState.Count == 0 && this.mouseJoint != null)
                {
                    if (!this.connectedEntity.IsDisposed)
                    {
                        // Remove Fixed Joint
                        JointMap2D jointMap2D = this.connectedEntity.FindComponent <JointMap2D>();
                        jointMap2D.RemoveJoint("mouseJoint");
                    }

                    this.mouseJoint = null;
                }

                // If joint exists then update joint anchor position
                if (this.mouseJoint != null)
                {
                    this.touchPosition          = this.touchState[0].Position;
                    this.mouseJoint.WorldAnchor = this.touchPosition;
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Update Method
        /// </summary>
        /// <param name="gameTime">Current Game Time</param>
        protected override void Update(TimeSpan gameTime)
        {
            this.input = WaveServices.Input;

            if (this.input.TouchPanelState.IsConnected)
            {
                this.touchState = this.input.TouchPanelState;

                // Checks Mouse Left Button Click and anyone entity linked
                if (this.touchState.Count > 0 && this.mouseJoint == null)
                {
                    // Udpates Mouse Position
                    this.touchPosition = this.touchState[0].Position;
                    this.vsm.ToVirtualPosition(ref this.touchPosition);

                    foreach (Entity entity in this.Owner.Scene.EntityManager.FindAllByTag("Draggable"))
                    {
                        Collider2D collider = entity.FindComponent<Collider2D>(false);
                        if (collider != null)
                        {
                            // Collider Test
                            if (collider.Contain(touchPosition))
                            {
                                RigidBody2D rigidBody = entity.FindComponent<RigidBody2D>();
                                if (rigidBody != null)
                                {
                                    // Forbiden Mouse Joint of Kinematic Bodies
                                    if (rigidBody.PhysicBodyType == PhysicBodyType.Dynamic)
                                    {
                                        this.connectedEntity = entity;

                                        // Create Mouse Joint
                                        this.mouseJoint = new FixedMouseJoint2D(this.touchPosition);
                                        this.connectedEntity.FindComponent<JointMap2D>().AddJoint("mouseJoint", this.mouseJoint);

                                        // We can break after collider test when true, but we'll miss overlapped entities if Physic entity is
                                        // under a non Physic entity. We are breaking here just for sample.
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                // Checks Mouse Left Button Release
                if (this.touchState.Count == 0 && this.mouseJoint != null)
                {
                    if (!this.connectedEntity.IsDisposed)
                    {
                        // Remove Fixed Joint
                        this.connectedEntity.FindComponent<JointMap2D>().RemoveJoint("mouseJoint");
                    }

                    this.mouseJoint = null;
                }

                // If joint exists then update joint anchor position
                if (this.mouseJoint != null)
                {
                    this.touchPosition = this.touchState[0].Position;
                    this.vsm.ToVirtualPosition(ref this.touchPosition);
                    this.mouseJoint.WorldAnchor = this.touchPosition;
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Update Method
        /// </summary>
        /// <param name="gameTime">Current Game Time</param>
        protected override void Update(TimeSpan gameTime)
        {
            this.input = WaveServices.Input;

            if (this.input.MouseState.IsConnected)
            {
                this.mouseState = this.input.MouseState;

                // Checks Mouse Left Button Click and anyone entity linked
                if (this.mouseState.LeftButton == ButtonState.Pressed && this.mouseJoint == null)
                {
                    // Udpates Mouse Position
                    this.mousePosition.X = this.mouseState.X;
                    this.mousePosition.Y = this.mouseState.Y;

                    foreach (Entity entity in this.Scene.EntityManager.EntityGraph)
                    {
                        Collider2D collider = entity.FindComponentOfType<Collider2D>();
                        if (collider != null)
                        {
                            // Collider Test
                            if (collider.Contain(mousePosition))
                            {
                                RigidBody2D rigidBody = entity.FindComponentOfType<RigidBody2D>();
                                if (rigidBody != null)
                                {
                                    // Forbiden Mouse Joint of Kinematic Bodies
                                    if (!rigidBody.IsKinematic)
                                    {
                                        this.connectedEntity = entity;

                                        // Create Mouse Joint
                                        this.mouseJoint = new FixedMouseJoint2D(this.mousePosition);
                                        this.connectedEntity.AddComponent(this.mouseJoint);

                                        // We can break after collider test when true, but we'll miss overlapped entities if Physic entity is
                                        // under a non Physic entity. We are breaking here just for sample.
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                // Checks Mouse Left Button Release
                if (this.mouseState.LeftButton == ButtonState.Release && this.mouseJoint != null)
                {
                    // Remove Fixed Joint
                    this.connectedEntity.RemoveComponent<FixedMouseJoint2D>();
                    this.mouseJoint = null;
                }

                // If joint exists then update joint anchor position
                if (this.mouseJoint != null)
                {
                    this.mousePosition.X = this.mouseState.X;
                    this.mousePosition.Y = this.mouseState.Y;
                    this.mouseJoint.WorldAnchor = this.mousePosition;
                }
            }
        }