/// <summary> /// Update player transform to follow the path /// </summary> /// <param name="gameTime">The current game time</param> protected override void Update(TimeSpan gameTime) { // Gets the current frame specified by the screenplay manager. int frame = (int)this.screenplay.CurrentFrameTime; if (frame < this.objectPath.Start) { // If the frame is earlier than the path start, return... this.Owner.IsVisible = false; return; } if (frame >= this.objectPath.End - 1) { // If the frame is greather than the path end, return... this.Owner.IsVisible = false; return; } if (!this.Owner.IsVisible) { this.Owner.IsVisible = true; } int startFrame = (frame - this.objectPath.Start) % this.objectPath.Duration; int endFrame = (startFrame + 1) % this.objectPath.Duration; float lerp = (float)(this.screenplay.CurrentFrameTime - frame); ObjectFrame startPoint = this.objectPath.Frames[startFrame]; ObjectFrame endPoint = this.objectPath.Frames[endFrame]; // Update entity trasnform using the current path frame this.transform.Position = Vector3.Lerp(startPoint.Position, endPoint.Position, lerp); this.transform.Orientation = Quaternion.Lerp(startPoint.Rotation, endPoint.Rotation, lerp); }
/// <summary> /// Instantiate a object path specified by a file /// </summary> /// <param name="objectPath">The object path filepath</param> /// <param name="offsetRotation">Orientation offset</param> public ObjectPath(string objectPath, Quaternion offsetRotation) { using (var stream = WaveServices.Storage.OpenContentFile(objectPath)) { var streamReader = new StreamReader(stream); string line = streamReader.ReadLine(); var lineParts = line.Split(' '); this.Start = int.Parse(lineParts[0]); this.End = int.Parse(lineParts[1]); this.Steps = int.Parse(lineParts[2]); this.Duration = this.End - this.Start; this.Frames = new ObjectFrame[this.Duration]; Vector3 axis; float angle; for (int i = 0; i < this.Duration; i++) { line = streamReader.ReadLine(); lineParts = line.Split(' '); Vector3 position = new Vector3( float.Parse(lineParts[0], System.Globalization.CultureInfo.InvariantCulture), float.Parse(lineParts[1], System.Globalization.CultureInfo.InvariantCulture), float.Parse(lineParts[2], System.Globalization.CultureInfo.InvariantCulture)); Quaternion rotation = new Quaternion( float.Parse(lineParts[3], System.Globalization.CultureInfo.InvariantCulture), float.Parse(lineParts[4], System.Globalization.CultureInfo.InvariantCulture), float.Parse(lineParts[5], System.Globalization.CultureInfo.InvariantCulture), float.Parse(lineParts[6], System.Globalization.CultureInfo.InvariantCulture)); Quaternion.ToAngleAxis(ref rotation, out axis, out angle); float temp = axis.Z; axis.Z = -axis.Y; axis.Y = -temp; Quaternion fixedRotation; Quaternion.CreateFromAxisAngle(ref axis, angle, out fixedRotation); fixedRotation = Quaternion.CreateFromAxisAngle(Vector3.UnitY, MathHelper.Pi) * fixedRotation * offsetRotation; ObjectFrame point = new ObjectFrame() { Position = position, Rotation = fixedRotation }; this.Frames[i] = point; } } }