コード例 #1
0
        /// <summary>
        /// Grabs an entity. Returns true if the grab is successful. False otherwise.
        /// </summary>
        /// <param name="grabber">The component grabbing this component</param>
        /// <returns>True if successful grab</returns>
        public bool Grab(GrabComponent grabber)
        {
            if (IsGrabbed)
            {
                // Can't be grabbed twice. Though grab stealing could be a cool feature...
                return false;
            }

            timeSinceGrab = 0;
            this.grabber = grabber;
            Vector3 currentPos = bepuPhysicsComponent.Box.Position;
            this.bepuPhysicsComponent.Box.Position = new Vector3(currentPos.X , grabber.GetPosition().Y, currentPos.Z);
            this.grabberPositionOffset = this.bepuPhysicsComponent.Box.Position - grabber.GetPosition();
            this.grabberPrevDirection = ((MoveComponent)grabber.Parent.GetComponent("MoveComponent")).DirectionX;
            this.bepuPhysicsComponent.Box.IsAffectedByGravity = false; // disable gravity, else it falls fast when dropped.

            // Disable the move component if the entity has a moveable
            MoveComponent moveComponent = (MoveComponent) Parent.GetComponent("MoveComponent");
            if (moveComponent != null)
            {
                moveComponent.Enabled = false;
            }

            // Do any event handlers
            EventHandler handler = OnGrab;
            if (handler != null)
            {
                handler(this, null);
            }

            return true;
        }