Exemple #1
0
        /// <summary>
        /// Points camera in direction of its <see cref="attachedEntity"/>
        /// </summary>
        /// <returns>Returns false is camera has no attached <seealso cref="BaseEntity"/></returns>
        public bool LookAtAttached()
        {
            // Look at this entity's parent entity, if it has one
            if (this.parentEntity != null)
            {
                MsgSetRotation setRotMsg = ObjectPool.Aquire <MsgSetRotation>();
                Matrix         rot       = Matrix.Identity;
                rot.Forward = this.parentEntity.ParentEntity.position - this.parentEntity.position;

                Vector3 referenceVector = Vector3.UnitY;

                // On the slim chance that the camera is pointer perfectly parallel with the Y Axis, we cannot
                // use cross product with a parallel axis, so we change the reference vector to the forward axis (Z).
                if (rot.Forward.Y == referenceVector.Y || rot.Forward.Y == -referenceVector.Y)
                {
                    referenceVector = Vector3.UnitZ;
                }

                rot.Right = Vector3.Cross(rot.Forward, referenceVector);
                rot.Up    = Vector3.Cross(rot.Right, rot.Forward);

                setRotMsg.Rotation     = rot;
                setRotMsg.UniqueTarget = this.uniqueID;
                this.game.SendMessage(setRotMsg);

                return(true);
            }

            return(false);
        }
Exemple #2
0
        /// <summary>
        /// Points camera in direction of any position.
        /// </summary>
        /// <param name="x">X coordinate</param>
        /// <param name="y">Y coordinate</param>
        /// <param name="z">Z coordinate</param>
        public void LookAt(float x, float y, float z)
        {
            Vector3 newForward = new Vector3(x - this.position.X, y - this.position.Y, z - this.position.Z);

            newForward.Normalize();

            Matrix rot = Matrix.Identity;

            rot.Forward = newForward;

            Vector3 referenceVector = Vector3.UnitY;

            // On the slim chance that the camera is pointer perfectly parallel with the Y Axis, we cannot
            // use cross product with a parallel axis, so we change the reference vector to the forward axis (Z).
            if (rot.Forward.Y == referenceVector.Y || rot.Forward.Y == -referenceVector.Y)
            {
                referenceVector = Vector3.UnitZ;
            }

            rot.Right = Vector3.Cross(rot.Forward, referenceVector);
            rot.Up    = Vector3.Cross(rot.Right, rot.Forward);

            MsgSetRotation setRotMsg = ObjectPool.Aquire <MsgSetRotation>();

            setRotMsg.Rotation     = rot;
            setRotMsg.UniqueTarget = this.uniqueID;
            this.game.SendMessage(setRotMsg);
        }
Exemple #3
0
        /// <summary>
        /// Send a message directly to an Entity through this method.
        /// This function MUST be called only from QSGame, otherwise it will never mark the message properly for release
        /// and it will leak memory.
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <returns>Returns true if message was handled</returns>
        public virtual bool ExecuteMessage(IMessage message)
        {
            switch (message.Type)
            {
            case MessageType.GetName:
            {
                MsgGetName msgGetName = message as MsgGetName;
                message.TypeCheck(msgGetName);

                msgGetName.Name = this.name;
            }
            break;

            case MessageType.GetParentID:
            {
                MsgGetParentID msgGetID = message as MsgGetParentID;
                message.TypeCheck(msgGetID);

                if (this.parentEntity != null)
                {
                    msgGetID.ParentEntityID = this.parentEntity.UniqueID;
                }
            }
            break;

            case MessageType.SetPosition:
            {
                MsgSetPosition setPositionMsg = message as MsgSetPosition;
                message.TypeCheck(setPositionMsg);

                this.Position = setPositionMsg.Position;
            }
            break;

            case MessageType.ModifyPosition:
            {
                MsgModifyPosition modPosMsg = message as MsgModifyPosition;
                message.TypeCheck(modPosMsg);

                this.Position += modPosMsg.Position;
            }
            break;

            case MessageType.GetPosition:
            {
                MsgGetPosition getPosMsg = message as MsgGetPosition;
                message.TypeCheck(getPosMsg);

                getPosMsg.Position = this.position;
            }
            break;

            case MessageType.SetRotation:
            {
                MsgSetRotation setRotMsg = message as MsgSetRotation;
                message.TypeCheck(setRotMsg);

                this.Rotation = setRotMsg.Rotation;
            }
            break;

            case MessageType.ModifyRotation:
            {
                MsgModifyRotation modRotMsg = message as MsgModifyRotation;
                message.TypeCheck(modRotMsg);

                this.Rotation *= modRotMsg.Rotation;
            }
            break;

            case MessageType.GetRotation:
            {
                MsgGetRotation getRotMsg = message as MsgGetRotation;
                message.TypeCheck(getRotMsg);

                getRotMsg.Rotation = this.rotation;
            }
            break;

            case MessageType.GetVectorForward:
            {
                MsgGetVectorForward getVectorMsg = message as MsgGetVectorForward;
                message.TypeCheck(getVectorMsg);

                getVectorMsg.Forward = this.rotation.Forward;
            }
            break;

            case MessageType.GetVectorUp:
            {
                MsgGetVectorUp getVectorMsg = message as MsgGetVectorUp;
                message.TypeCheck(getVectorMsg);

                getVectorMsg.Up = this.rotation.Up;
            }
            break;

            case MessageType.GetVectorRight:
            {
                MsgGetVectorRight getVectorMsg = message as MsgGetVectorRight;
                message.TypeCheck(getVectorMsg);

                getVectorMsg.Right = this.rotation.Right;
            }
            break;

            case MessageType.LookAtPosition:
            {
                MsgLookAtPosition setPositionMsg = message as MsgLookAtPosition;
                message.TypeCheck(setPositionMsg);

                this.LookAt(setPositionMsg.Position);
            }
            break;

            case MessageType.Pitch:
            {
                MsgModifyPitch camPitchMsg = message as MsgModifyPitch;
                message.TypeCheck(camPitchMsg);

                Pitch(camPitchMsg.PitchAmount);
            }
            break;

            case MessageType.Yaw:
            {
                MsgModifyYaw camYawMsg = message as MsgModifyYaw;
                message.TypeCheck(camYawMsg);

                Yaw(camYawMsg.YawAmount);
            }
            break;

            case MessageType.YawWorldUp:
            {
                MsgModifyYawWorldUp camYawMsg = message as MsgModifyYawWorldUp;
                message.TypeCheck(camYawMsg);

                YawAroundWorldUp(camYawMsg.YawAmount);
            }
            break;

            case MessageType.SetParent:
            {
                MsgSetParent msgSetParent = message as MsgSetParent;
                message.TypeCheck(msgSetParent);

                SetParent(msgSetParent.ParentEntity);
            }
            break;

            case MessageType.RemoveChild:
            {
                MsgRemoveChild msgRemChild = message as MsgRemoveChild;
                message.TypeCheck(msgRemChild);

                RemoveChild(msgRemChild.Child);
            }
            break;

            case MessageType.ParentRemoved:
            {
                MsgParentRemoved msgParentRem = message as MsgParentRemoved;
                message.TypeCheck(msgParentRem);

                if (parentEntity == msgParentRem.Parent)
                {
                    SetParent(null);
                }
            }
            break;

            case MessageType.ParentAdded:
            {
                MsgParentAdded msgParentAdded = message as MsgParentAdded;
                message.TypeCheck(msgParentAdded);

                SetParent(msgParentAdded.Parent);
            }
            break;

            case MessageType.ChildRemoved:
            {
                MsgChildRemoved msgChildRem = message as MsgChildRemoved;
                message.TypeCheck(msgChildRem);

                if (msgChildRem.Child != null)
                {
                    this.children.Remove(msgChildRem.Data.UniqueID);
                }
            }
            break;

            case MessageType.ChildAdded:
            {
                MsgChildAdded msgChildAdd = message as MsgChildAdded;
                message.TypeCheck(msgChildAdd);

                if (msgChildAdd.Child != null && msgChildAdd.Child != this)
                {
                    this.children.Add(msgChildAdd.Data.UniqueID, msgChildAdd.Child);
                }
            }
            break;

            default:
                return(SendMessageThroughComponents(message));
            }

            // For messages handled directly by the Entity but aren't Request Protocol,
            // we still pass them to the Entity's components in case those components care.
            if (message.Protocol != MessageProtocol.Request)
            {
                return(SendMessageThroughComponents(message));
            }

            return(true);
        }