public override bool OnUpdate(float inDeltaSeconds) { base.OnUpdate(inDeltaSeconds); if (Actor.Blackboard.Target != null) { Vector2 target = Actor.Blackboard.Target.Spatial.WorldPosition; float targetHeading = (float)Actor.Spatial.WorldPosition.AngleTo(target) * SMath.RAD2DEG; float headingDelta = SMath.GetDeltaAngle(Actor.Spatial.WorldHeading, targetHeading); float headingToChange = Math.Sign(headingDelta) * Math.Min(Math.Abs(headingDelta), inDeltaSeconds * Actor.TurnSpeed); Turning = Math.Abs(headingToChange) > 10.0f; if (Turning) { // Turn Actor.Spatial.SetFacing(Actor.Spatial.WorldHeading + headingToChange); } else { // Attack Actor.Weapon.FireRequested = true; } DebugRenderer.Instance.Line(Actor.Spatial.WorldPosition, target, turning ? Color.Blue : Color.Red); } return(true); }
public override bool OnUpdate(float inDeltaSeconds) { base.OnUpdate(inDeltaSeconds); float headingDelta = SMath.GetDeltaAngle(Actor.Spatial.WorldHeading, targetHeading); float headingToChange = Math.Sign(headingDelta) * Math.Min(Math.Abs(headingDelta), inDeltaSeconds * Actor.TurnSpeed); Turning = Math.Abs(headingToChange) > 0.5f; if (Turning) { // Turn Actor.Spatial.SetFacing(Actor.Spatial.WorldHeading + headingToChange); } else { // Move Vector2 positionDelta = target - Actor.Spatial.WorldPosition; Vector2 positionToChange = positionDelta.Normalize() * Math.Min(positionDelta.Length(), Actor.MoveSpeed * inDeltaSeconds); if (positionToChange.Length() > 0.5f) { Actor.Spatial.SetPosition(Actor.Spatial.WorldPosition + positionToChange); } else { Actor.DebugObject.Log("Reached destination!"); return(false); // Done } } DebugRenderer.Instance.Rect(target, 3.0f, turning ? Color.Orange : Color.Green, true); DebugRenderer.Instance.Line(Actor.Spatial.WorldPosition, target, turning ? Color.Orange : Color.Green); return(true); }