示例#1
0
        public override void Update(GameTime gameTime)
        {
            time += (float)gameTime.ElapsedGameTime.TotalSeconds + (float)rand.NextDouble() * 0.01f;
            if (energy < 100)
            {
                energyRecoverTime -= (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000;               //energy recover
                if (energyRecoverTime <= 0)
                {
                    energyRecoverTime = 5;
                    energy           += 1;
                }
            }
            else
            {
                energy            = 100;
                energyRecoverTime = 5;
            }


            animTime += (float)gameTime.ElapsedGameTime.TotalSeconds;

            float animationFactor  = 0f;
            float animationFactor2 = (float)(0.5f + Math.Sin(time + animationOffset)) * 5;

            dirX = (float)Math.Sin(angle);
            dirZ = (float)Math.Cos(angle);

            Boolean shouldAnimate = true;

            if (!ifColisionTerrain)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.LeftShift))
                {
                    if (Keyboard.GetState().IsKeyDown(Keys.E) && isHunting)
                    {
                        isAtacking = true;
                        animTime   = 0;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.W))
                    {
                        if (!isAtacking)
                        {
                            animationSystem.Play(animations["Idle"]);
                        }
                        speedFactor     = 2.5f;
                        animationFactor = 20f + animationFactor2;
                        position       += new Vector3(dirX / speedFactor, 0, dirZ / speedFactor);
                    }
                    else
                    {
                        speedFactor *= 1 + (float)gameTime.ElapsedGameTime.TotalSeconds * 6;
                        if (speedFactor < 50)
                        {
                            position += new Vector3(dirX / speedFactor, 0, dirZ / speedFactor);
                        }
                        shouldAnimate = false;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.A))
                    {
                        angle += 0.05f;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.D))
                    {
                        angle -= 0.05f;
                    }
                }
                else
                {
                    if (Keyboard.GetState().IsKeyDown(Keys.E) && isHunting)
                    {
                        isAtacking = true;
                        animTime   = 0;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.W))
                    {
                        if (!isAtacking)
                        {
                            animationSystem.Play(animations["Idle"]);
                        }
                        speedFactor     = 4.5f;
                        animationFactor = 10f + animationFactor2;
                        position       += new Vector3(dirX / speedFactor, 0, dirZ / speedFactor);
                    }
                    else
                    {
                        shouldAnimate = false;
                        speedFactor  *= 1f + (float)gameTime.ElapsedGameTime.TotalSeconds * 6;
                        if (speedFactor < 50)
                        {
                            position += new Vector3(dirX / speedFactor, 0, dirZ / speedFactor);
                        }
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.A))
                    {
                        angle += 0.05f;
                    }
                    if (Keyboard.GetState().IsKeyDown(Keys.D))
                    {
                        angle -= 0.05f;
                    }
                }
            }



            if (animationSystem != null && !shouldAnimate && !isAtacking)
            {
                animationSystem.animation.frameSpeed = 0.02f * (1f + animationSystem.animation.CurrentFrame * 2f / animationSystem.animation.NumberOfFrames);
                if (animationSystem.animation.CurrentFrame == 0)
                {
                    animationSystem.Stop();
                }
                animationSystem.Update(gameTime);
            }

            if (animationSystem != null && shouldAnimate && !isAtacking)
            {
                animationSystem.animation.frameSpeed = 0.2f / animationFactor;
                animationSystem.Update(gameTime);
            }
            if (animationSystem != null && isAtacking)
            {
                animationSystem.Play(animations["Atak"]);
                animationSystem.Update(gameTime);
            }
            if (animTime >= animations["Atak"].NumberOfFrames * animations["Atak"].frameSpeed)
            {
                isAtacking = false;
            }

            world = Matrix.CreateRotationX(MathHelper.ToRadians(-90)) * Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(position);// * Matrix.CreateFromAxisAngle(Vector3.UnitY, MathHelper.ToRadians(-90)); ;



            collider = new BoundingBox(new Vector3(world.Translation.X - colliderSize / 2, world.Translation.Y - colliderSize / 2, world.Translation.Z - colliderSize / 2),
                                       new Vector3(world.Translation.X + colliderSize / 2, world.Translation.Y + colliderSize / 2, world.Translation.Z + colliderSize / 2));

            foreach (ModelMesh mesh in model.Meshes)
            {
                mesh.BoundingSphere = BoundingSphere.CreateFromBoundingBox(collider);
            }


            base.Update(gameTime);
        }
示例#2
0
        public override void Update(GameTime gameTime)
        {
            time     += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000;
            animTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
            //float animationFactor = 0f;
            //float animationFactor2 = (float)Math.Sin(time + animationOffset) * 5;

            if (time >= turnFrequency && !ifInTrouble)
            {
                angle += rand.Next(10, 50);
                time   = 0;
            }

            dirX = (float)Math.Sin(angle);
            dirZ = (float)Math.Cos(angle);

            if (!ifInTrouble)
            {
                if (!ifColisionTerrain && animTime >= animationFrequency)
                {
                    position += new Vector3(dirX / speedFactor, 0, dirZ / speedFactor);
                    animationSystem.Play(animations["Move"]);
                }
                else
                {
                    //if (animTime <= animationFrequency + 1.33f)
                    //{
                    //    if (!ifInTrouble && animations.ContainsKey("Idle"))
                    //    {
                    //        animationSystem.Play(animations["Idle"]);
                    //    }

                    //}
                    //else
                    //{
                    animationSystem.Stop();
                    animTime           = 0;
                    animationFrequency = rand.Next(1, 3);
                    //}
                }
            }
            else
            {
                position += new Vector3(dirX / speedFactor, 0, dirZ / speedFactor);
                animationSystem.Play(animations["Move"]);
            }


            CheckSecurity();



            world = Matrix.CreateRotationX(MathHelper.ToRadians(-90)) * Matrix.CreateRotationY(angle) * Matrix.CreateTranslation(position);// * Matrix.CreateFromAxisAngle(Vector3.UnitY, MathHelper.ToRadians(-90)); ;

            collider = new BoundingBox(new Vector3(world.Translation.X - colliderSize / 2, world.Translation.Y - colliderSize / 2, world.Translation.Z - colliderSize / 2),
                                       new Vector3(world.Translation.X + colliderSize / 2, world.Translation.Y + colliderSize / 2, world.Translation.Z + colliderSize / 2));
            foreach (ModelMesh mesh in model.Meshes)
            {
                mesh.BoundingSphere = BoundingSphere.CreateFromBoundingBox(collider);
            }
            sphere = BoundingSphere.CreateFromBoundingBox(collider);

            animationSystem.Update(gameTime);
            base.Update(gameTime);
        }