public IEnumerable <Status> PerformCurrentAction() { Creature.MoveAction action = Path.First(); float t = 0; int currentIndex = 0; if (!GetCurrentAction(ref action, ref t, ref currentIndex)) { yield break; } int nextID = currentIndex + 1; bool hasNextAction = false; Vector3 half = Vector3.One * 0.5f; Vector3 nextPosition = Vector3.Zero; Vector3 currPosition = action.Voxel.Position + half; currPosition += RandomPositionOffsets[currentIndex]; if (nextID < Path.Count) { hasNextAction = true; nextPosition = Path[nextID].Voxel.Position; nextPosition += RandomPositionOffsets[nextID] + half; } Matrix transform = Agent.Physics.LocalTransform; Vector3 diff = (nextPosition - currPosition); switch (action.MoveType) { case Creature.MoveType.Walk: Creature.OverrideCharacterMode = false; Creature.CurrentCharacterMode = Creature.CharacterMode.Walking; if (hasNextAction) { transform.Translation = diff * t + currPosition; Agent.Physics.Velocity = diff; } else { transform.Translation = currPosition; } break; case Creature.MoveType.Swim: Creature.OverrideCharacterMode = false; Creature.CurrentCharacterMode = Creature.CharacterMode.Swimming; if (hasNextAction) { transform.Translation = diff * t + currPosition; Agent.Physics.Velocity = diff; } else { transform.Translation = currPosition; } break; case Creature.MoveType.Jump: Creature.OverrideCharacterMode = false; Creature.CurrentCharacterMode = Creature.Physics.Velocity.Y > 0 ? Creature.CharacterMode.Jumping : Creature.CharacterMode.Falling; if (hasNextAction) { float z = Easing.Ballistic(t, 1.0f, 1.0f); Vector3 start = currPosition; Vector3 end = nextPosition; Vector3 dx = (end - start) * t + start; dx.Y = start.Y * (1 - t) + end.Y * (t) + z; transform.Translation = dx; Agent.Physics.Velocity = new Vector3(diff.X, (dx.Y - Agent.Physics.Position.Y), diff.Z); } else { transform.Translation = currPosition; } break; case Creature.MoveType.Fall: Creature.OverrideCharacterMode = false; Creature.CurrentCharacterMode = Creature.CharacterMode.Falling; if (hasNextAction) { transform.Translation = diff * t + currPosition; Agent.Physics.Velocity = diff; } else { transform.Translation = currPosition; } break; case Creature.MoveType.Climb: Creature.OverrideCharacterMode = false; Creature.CurrentCharacterMode = Creature.CharacterMode.Walking; Creature.OverrideCharacterMode = true; if (hasNextAction) { transform.Translation = diff * t + currPosition; Agent.Physics.Velocity = diff; } else { transform.Translation = currPosition; } break; case Creature.MoveType.Fly: Creature.OverrideCharacterMode = false; Creature.CurrentCharacterMode = Creature.CharacterMode.Flying; Creature.OverrideCharacterMode = true; if (hasNextAction) { transform.Translation = diff * t + currPosition; Agent.Physics.Velocity = diff; } else { transform.Translation = currPosition; } break; case Creature.MoveType.DestroyObject: if (action.InteractObject.IsDead) { Creature.OverrideCharacterMode = false; Creature.CurrentCharacterMode = Creature.CharacterMode.Walking; if (hasNextAction) { transform.Translation = diff * t + currPosition; Agent.Physics.Velocity = diff; } else { transform.Translation = currPosition; } break; } else { float current = (TrajectoryTimer.CurrentTimeSeconds); Matrix transformMatrix = Agent.Physics.LocalTransform; MeleeAct act = new MeleeAct(Creature.AI, action.InteractObject.GetRootComponent().GetChildrenOfType <Body>(true).FirstOrDefault()); act.Initialize(); foreach (Act.Status status in act.Run()) { Agent.Physics.LocalTransform = transformMatrix; TrajectoryTimer.StartTimeSeconds = (float)DwarfTime.LastTime.TotalGameTime.TotalSeconds - current; yield return(status); } } break; } Agent.Physics.LocalTransform = transform; yield break; }
public void PerformCurrentAction() { Creature.MoveAction action = Path.First(); float t = 0; int currentIndex = 0; if (!GetCurrentAction(ref action, ref t, ref currentIndex)) { return; } int nextID = currentIndex + 1; bool hasNextAction = false; Vector3 half = Vector3.One * 0.5f; Vector3 nextPosition = Vector3.Zero; Vector3 currPosition = action.Voxel.Position + half; currPosition += RandomPositionOffsets[currentIndex]; if (nextID < Path.Count) { hasNextAction = true; nextPosition = Path[nextID].Voxel.Position; nextPosition += RandomPositionOffsets[nextID] + half; } Matrix transform = Agent.Physics.LocalTransform; Vector3 diff = (nextPosition - currPosition); switch (action.MoveType) { case Creature.MoveType.Walk: Creature.OverrideCharacterMode = false; if (hasNextAction) { transform.Translation = diff * t + currPosition; Agent.Physics.Velocity = diff; } else { transform.Translation = currPosition; } break; case Creature.MoveType.Swim: Creature.OverrideCharacterMode = false; if (hasNextAction) { transform.Translation = diff * t + currPosition; Agent.Physics.Velocity = diff; } else { transform.Translation = currPosition; } break; case Creature.MoveType.Jump: Creature.OverrideCharacterMode = false; if (hasNextAction) { float z = Easing.Ballistic(t, 1.0f, 1.0f); Vector3 start = currPosition; Vector3 end = nextPosition; Vector3 dx = (end - start) * t + start; dx.Y = start.Y * (1 - t) + end.Y * (t) + z; transform.Translation = dx; Agent.Physics.Velocity = new Vector3(diff.X, (dx.Y - Agent.Physics.Position.Y), diff.Z); } else { transform.Translation = currPosition; } break; case Creature.MoveType.Fall: Creature.OverrideCharacterMode = false; if (hasNextAction) { transform.Translation = diff * t + currPosition; Agent.Physics.Velocity = diff; } else { transform.Translation = currPosition; } break; case Creature.MoveType.Climb: Creature.OverrideCharacterMode = true; Creature.CurrentCharacterMode = Creature.CharacterMode.Walking; if (hasNextAction) { transform.Translation = diff * t + currPosition; Agent.Physics.Velocity = diff; } else { transform.Translation = currPosition; } break; } Agent.Physics.LocalTransform = transform; }