/// <summary> /// Allows the game to run logic. /// </summary> protected override void Update(GameTime gameTime) { HandleInput(); UpdateCamera(gameTime); Matrix ShipMatrix = Matrix.CreateScale(7f); clipPlayer.Update(gameTime, ShipMatrix); base.Update(gameTime); }
public override void Update(GameTime gameTime) { base.Update(gameTime); if (!Enabled) return; float elapsed = (float) gameTime.ElapsedGameTime.TotalSeconds*0.33f; switch (Direction) { case Direction.Left: ClipPlayer.IsFlipped = true; break; case Direction.Right: ClipPlayer.IsFlipped = false; break; } if (ClipPlayer.Texture != null && ClipPlayer.CurrentSequence != null && ClipPlayer.CurrentSequence.Frames.Count > 0) { #region Pathing Nodes if (AttachedPathingNode != null) { // We are attached to a path check for nodes in the path if (AttachedPathingNode.Nodes.Count > 0) { SpriteEffects = AttachedPathingNode.Nodes[CurrentPathNodeIndex].SpriteEffect; // Update our draw order to be the same as the current node we are on if (AttachedPathingNode.UseNodesDrawOrder) DrawOrder = AttachedPathingNode.Nodes[CurrentPathNodeIndex].DrawOrder; // See if we should alter our animation sequence if (AttachedPathingNode.Nodes[CurrentPathNodeIndex].AnimationSequence != null && CurrentPathLerpPosition == 0) { // Update our Clip Player ClipPlayer.Play(AttachedPathingNode.Nodes[CurrentPathNodeIndex].AnimationSequence.Name, AttachedPathingNode.Nodes[CurrentPathNodeIndex].IsLooped); } if (CurrentPathNodeIndex <= (AttachedPathingNode.Nodes.Count - 1)) { if (AttachedPathingNode.IsTraveling) { // Lerp to our next node CurrentPathLerpPosition += (AttachedPathingNode.Nodes[CurrentPathNodeIndex].TravelSpeed*elapsed); } if (CurrentPathLerpPosition >= 1) { if (IsLerpingBackToFirstNode) { CurrentPathNodeIndex--; CurrentPathLerpPosition = 0; } else { if (CurrentPathNodeIndex < (AttachedPathingNode.Nodes.Count - 1)) { CurrentPathNodeIndex++; CurrentPathLerpPosition = 0; } } } // Check to see we are on the last node in the chain if (CurrentPathNodeIndex == (AttachedPathingNode.Nodes.Count - 1) && !IsLerpingBackToFirstNode) { // we want to respawn back to the first so reset everything switch (AttachedPathingNode.LedgeTavelAlgo) { case LedgeTravelAlgo.RespawnAtFirstNode: if (AttachedPathingNode.Nodes[CurrentPathNodeIndex].RespawnDelay > TimeSpan.Zero) { if (AttachedPathingNode.Nodes[CurrentPathNodeIndex].NodeElapsedTime > AttachedPathingNode.Nodes[CurrentPathNodeIndex].RespawnDelay) { // purge the last node elapsed time AttachedPathingNode.Nodes[CurrentPathNodeIndex].NodeElapsedTime = TimeSpan.Zero; // Reset to the begining pathing node CurrentPathNodeIndex = 0; // Reset our Lerp CurrentPathLerpPosition = 0; } else { AttachedPathingNode.Nodes[CurrentPathNodeIndex].NodeElapsedTime += gameTime.ElapsedGameTime; } } else { // Reset to the begining pathing node CurrentPathNodeIndex = 0; // Reset our Lerp CurrentPathLerpPosition = 0; } break; case LedgeTravelAlgo.LerpToFirstNode: IsLerpingBackToFirstNode = true; CurrentPathLerpPosition = 0; break; case LedgeTravelAlgo.StopAtLastNode: AttachedPathingNode.IsTraveling = false; CurrentPathLerpPosition = 0; break; } } else if (CurrentPathNodeIndex == 0 && IsLerpingBackToFirstNode) { IsLerpingBackToFirstNode = false; CurrentPathLerpPosition = 0; } else { // Continue to move along the path if (IsLerpingBackToFirstNode) { // Lerping Rotation Calculation ** value1 + (value2 - value1) * amount ** float n1 = AttachedPathingNode.Nodes[CurrentPathNodeIndex - 1].Rotation; float n2 = AttachedPathingNode.Nodes[CurrentPathNodeIndex].Rotation; Rotation = n1 + (n2 - n1) * CurrentPathLerpPosition; // Moving Back towards the last node Position = Vector2.Lerp(AttachedPathingNode.Nodes[CurrentPathNodeIndex].Position, AttachedPathingNode.Nodes[CurrentPathNodeIndex - 1].Position, CurrentPathLerpPosition); // Scaling Scale = Vector2.Lerp(AttachedPathingNode.Nodes[CurrentPathNodeIndex].Scale, AttachedPathingNode.Nodes[CurrentPathNodeIndex - 1].Scale, CurrentPathLerpPosition); } else { // Lerping Rotation Calculation ** value1 + (value2 - value1) * amount ** float n1 = AttachedPathingNode.Nodes[CurrentPathNodeIndex].Rotation; float n2 = AttachedPathingNode.Nodes[CurrentPathNodeIndex + 1].Rotation; Rotation = n1 + (n2 - n1) * CurrentPathLerpPosition; // Moving Position = Vector2.Lerp(AttachedPathingNode.Nodes[CurrentPathNodeIndex].Position, AttachedPathingNode.Nodes[CurrentPathNodeIndex + 1].Position, CurrentPathLerpPosition); // Scaling Scale = Vector2.Lerp(AttachedPathingNode.Nodes[CurrentPathNodeIndex].Scale, AttachedPathingNode.Nodes[CurrentPathNodeIndex + 1].Scale, CurrentPathLerpPosition); } } } } } #endregion // Incase it walks past the max frame count on the sequences if (ClipPlayer.CurrentFrameIndex >= ClipPlayer.CurrentSequence.Frames.Count) { if (ClipPlayer.IsLooped) ClipPlayer.CurrentFrameIndex = 0; else ClipPlayer.CurrentFrameIndex = ClipPlayer.CurrentSequence.Frames.Count - 1; } float tw = (ClipPlayer.CurrentSequence.Frames[ClipPlayer.CurrentFrameIndex].SourceRectangle.Width + BoundingBoxScale.X)*Scale.X; float th = (ClipPlayer.CurrentSequence.Frames[ClipPlayer.CurrentFrameIndex].SourceRectangle.Height + BoundingBoxScale.Y)*Scale.Y; if (IsCentered) { // Center Aligned means the position is actually in the center of the actor and needs to remove that float widthHalf = tw/2; float heightHalf = th/2; float posX1 = Position.X - widthHalf; float posY1 = Position.Y - heightHalf; float posX2 = Position.X + widthHalf; float posY2 = Position.Y + heightHalf; // Update our Bounding Box Vector3[] points = new Vector3[2]; points[0] = new Vector3(posX1 + CameraOffsetX, posY1 + CameraOffsetY, -5); points[1] = new Vector3(posX2 + CameraOffsetX, posY2 + CameraOffsetY, 5); BoundingBox = BoundingBox.CreateFromPoints(points); } else { // Update our Bounding Box Vector3[] points = new Vector3[2]; points[0] = new Vector3(Position.X + CameraOffsetX, Position.Y + CameraOffsetY, -5); points[1] = new Vector3(Position.X + CameraOffsetX + tw, Position.Y + CameraOffsetY + th, 5); // Create our Bounding Box BoundingBox = BoundingBox.CreateFromPoints(points); } if (BoundingBoxRenderer != null) { BoundingBoxRenderer.ShowBoundingBox = ShowBoundingBox; BoundingBoxRenderer.BoundingBox = BoundingBox; BoundingBoxRenderer.Update(gameTime); } // Update our Clip Player ClipPlayer.IsCentered = IsCentered; ClipPlayer.Position = Position; ClipPlayer.Velocity = Velocity; ClipPlayer.Scale = Scale; ClipPlayer.Rotation = Rotation; ClipPlayer.SpriteEffects = SpriteEffects; ClipPlayer.CameraOffsetX = CameraOffsetX; ClipPlayer.CameraOffsetY = CameraOffsetY; ClipPlayer.CameraZoomOffset = CameraZoomOffset; ClipPlayer.BoundingBox = BoundingBox; ClipPlayer.ShowBoundingBox = ShowBoundingBox; ClipPlayer.Update(gameTime); } }