Ejemplo n.º 1
0
        /// <summary>
        /// This function processes the animation content of the model, and stores the data in the Matrix[] List
        /// </summary>
        /// <param name="animations"></param>
        /// <param name="bones"></param>
        /// <param name="outKeyFrames"></param>
        /// <returns></returns>
        private Dictionary <string, InstancedAnimationClip> ProcessAnimations(
            AnimationContentDictionary animations,
            IList <BoneContent> bones,
            List <Matrix[]> outKeyFrames)
        {
            // Create a map of bones - just like in the SkinnedModel Smaple
            Dictionary <string, int> boneMap = new Dictionary <string, int>();

            for (int i = 0; i < bones.Count; i++)
            {
                string boneName = bones[i].Name;

                if (!string.IsNullOrEmpty(boneName))
                {
                    boneMap.Add(boneName, i);
                }
            }

            // This is a dictionary of all the animations in the model
            Dictionary <string, InstancedAnimationClip> animationDictionary = new Dictionary <string, InstancedAnimationClip>();

            // Loop through each animation, and add that stuff to our animation texture, as well as to our dictionary we are going to output
            foreach (KeyValuePair <string, AnimationContent> data in animations)
            {
                string           animationName = data.Key;
                AnimationContent animation     = data.Value;

                InstancedAnimationClip clip = ProcessAnimation(animation, outKeyFrames, boneMap);
                animationDictionary.Add(animationName, clip);
            }

            return(animationDictionary);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Update the animation and behavior of this dwarf
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="playerPosition"></param>
        public void Update(GameTime gameTime, ref Vector3 playerPosition)
        {
            //If we don't have an animation, we should probably pick a new one out
            if (this.currentAnimation == null)
            {
                this.currentAnimation = this.GetNextAnimation(ref playerPosition);
                this.animationFrame   = this.currentAnimation.StartRow;
            }

            //Otherwise, increment the animation frame on our current animation
            if (this.currentAnimation != null)
            {
                this.animationFrame += (float)(gameTime.ElapsedGameTime.TotalSeconds * this.currentAnimation.FrameRate);

                if ((int)this.animationFrame >= this.currentAnimation.EndRow)
                {
                    //If we still need to repeat some, repeat
                    if (this.repeatAnimation > 0)
                    {
                        this.repeatAnimation--;
                        this.animationFrame = this.currentAnimation.StartRow;
                    }
                    else
                    {
                        this.animationFrame = this.currentAnimation.EndRow;
                        //Time to switch animation
                        this.currentAnimation = null;
                    }
                }
            }

            //Rotate the dwarf to face the player
            float zDelta   = playerPosition.Z - this.translation.Z;
            float xDelta   = playerPosition.X - this.translation.X;
            float rotation = (float)(Math.Atan2(xDelta, zDelta));

            //Create the rotation matrix based on the rotation we calculated
            Matrix rotationMatrix;

            Matrix.CreateRotationY(rotation, out rotationMatrix);

            //Calculate our final transform matrix for the dwarf
            Matrix.Multiply(ref rotationMatrix, ref this.translationScaleMatrix, out this.transform);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Update the animation and behavior of this dwarf
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="playerPosition"></param>
        public void Update(GameTime gameTime, ref Vector3 playerPosition)
        {
            //If we don't have an animation, we should probably pick a new one out
            if (this.currentAnimation == null)
            {
                this.currentAnimation = this.GetNextAnimation(ref playerPosition);
                this.animationFrame = this.currentAnimation.StartRow;
            }

            //Otherwise, increment the animation frame on our current animation
            if (this.currentAnimation != null)
            {
                this.animationFrame += (float)(gameTime.ElapsedGameTime.TotalSeconds * this.currentAnimation.FrameRate);

                if ((int)this.animationFrame >= this.currentAnimation.EndRow)
                {
                    //If we still need to repeat some, repeat
                    if (this.repeatAnimation > 0)
                    {
                        this.repeatAnimation--;
                        this.animationFrame = this.currentAnimation.StartRow;
                    }
                    else
                    {
                        this.animationFrame = this.currentAnimation.EndRow;
                        //Time to switch animation
                        this.currentAnimation = null;
                    }
                }
            }

            //Rotate the dwarf to face the player
            float zDelta = playerPosition.Z - this.translation.Z;
            float xDelta = playerPosition.X - this.translation.X;
            float rotation = (float)(Math.Atan2(xDelta, zDelta));

            //Create the rotation matrix based on the rotation we calculated
            Matrix rotationMatrix;
            Matrix.CreateRotationY(rotation, out rotationMatrix);

            //Calculate our final transform matrix for the dwarf
            Matrix.Multiply(ref rotationMatrix, ref this.translationScaleMatrix, out this.transform);
        }