Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int travelForFrames = 0;
- int focusForFrames = 0;
- Random ran = GlobalVariables.ran;
- private Quaternion NewDirection()
- {
- // Get the target object based on the name
- SceneInterface.ActiveSceneInterface.ObjectManager.Find("PlayerRepEntity", true, out targetSceneObject);
- // Get the details of the child matrix
- sceneObject.World.Decompose(out parentObjectScale, out parentObjectRotationQ, out parentObjectTranslation);
- // Get the details of the target
- targetSceneObject.World.Decompose(out targetScale, out targetRotationQ, out targetTranslation);
- if (travelForFrames <= 0)
- {
- // Calculate the difference (direction) between the child to the target
- // Subtract the child from the target (we want the child to face the target)
- distance = targetTranslation - parentObjectTranslation;
- // If the zombie is further than 10 meters
- if (distance.Length() > 15 && distance.Length() < 50)
- {
- if (focusForFrames <= 0)
- {
- // 20% of the time we will
- if (ran.Next(10) > 8)
- {
- // Stuff around with the new direction for the zombie to face
- targetTranslation = targetTranslation + GetRandomVector3(0, 30, true, true, false, true);
- distance = targetTranslation - parentObjectTranslation;
- // Set the counter with a some what random amount of time before the new direciton is recalculated
- travelForFrames = ran.Next(30, 260);
- focusForFrames = ran.Next(10, 160);
- }
- }
- else
- {
- focusForFrames--;
- }
- }
- }
- else
- {
- travelForFrames--;
- }
- //return Math.Atan2(distance.Y, distance.X);
- newLook = distance;
- // Normalize the distance to turn it to a direction
- newLook.Normalize();
- // Matrix of the direction we want the enemy to face
- rotate = Matrix.CreateWorld(Vector3.Zero, newLook, Vector3.Up);
- // Lerp towards the direction we want the enemy to face, from it's current facing direction
- Quaternion newRotation = Quaternion.Lerp(parentObjectRotationQ, Quaternion.CreateFromRotationMatrix(rotate), 0.125f);
- // Update the scene objects rotation to face this new direction
- sceneObject.World = Matrix.CreateScale(parentObjectScale) * Matrix.CreateFromQuaternion(newRotation) * Matrix.CreateTranslation(parentObjectTranslation);
- return newRotation;
- }
- private Vector3 GetRandomVector3(float Minimum, float Maximum, bool NegativeRangeRequired, bool x, bool y, bool z)
- {
- Vector3 returnVector = new Vector3();
- if (x)
- {
- returnVector.X = (float)ran.Next((int)Minimum, (int)Maximum);
- }
- if (y)
- {
- returnVector.Y = (float)ran.Next((int)Minimum, (int)Maximum);
- }
- if (z)
- {
- returnVector.Z = (float)ran.Next((int)Minimum, (int)Maximum);
- }
- if (NegativeRangeRequired)
- {
- if (x)
- {
- returnVector.X = returnVector.X * ReturnPositiveOrNegative();
- }
- if (y)
- {
- returnVector.Y = returnVector.Y * ReturnPositiveOrNegative();
- }
- if (z)
- {
- returnVector.Z = returnVector.Z * ReturnPositiveOrNegative();
- }
- }
- return returnVector;
- }
- private float ReturnPositiveOrNegative()
- {
- float numberToReturn = 0;
- while (numberToReturn == 0.0f)
- {
- numberToReturn = (float)ran.Next(-1, 2);
- }
- return numberToReturn;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement