public void GotoObject(GameObject obj, bool cruise = true, float maxThrottle = 1, float gotoRange = 40) { _targetObject = obj; CurrentBehaviour = AutopilotBehaviours.Goto; CanCruise = cruise; _maxThrottle = maxThrottle; this.gotoRange = gotoRange; }
public void GotoVec(Vector3 vec, bool cruise, float maxThrottle = 1, float gotoRange = 40) { _targetObject = null; _targetPosition = vec; _targetRadius = 5; _maxThrottle = maxThrottle; CurrentBehaviour = AutopilotBehaviours.Goto; CanCruise = cruise; this.gotoRange = gotoRange; }
public void StartDock(GameObject target) { _targetObject = target; var docking = target.GetComponent <CDockComponent>(); if (docking != null) { if (docking.Action.Kind == DockKinds.Tradelane) { var hpend = docking.GetDockHardpoints(Parent.PhysicsComponent.Body.Position).Last(); tlDockHP = hpend.Name; } } CurrentBehaviour = AutopilotBehaviours.Dock; CanCruise = true; gotoRange = 40; }
public override void FixedUpdate(TimeSpan time) { var control = Parent.GetComponent <ShipControlComponent>(); if (control == null) { return; } control.Pitch = control.Yaw = 0; if (CurrentBehaviour == AutopilotBehaviours.None) { ResetDockState(); return; } Vector3 targetPoint = Vector3.Zero; float radius = -1; float maxSpeed = 1f; if (CurrentBehaviour == AutopilotBehaviours.Goto) { targetPoint = TargetObject.PhysicsComponent == null?TargetObject.GetTransform().Transform(Vector3.Zero) : TargetObject.PhysicsComponent.Body.Position; ResetDockState(); } else { var docking = TargetObject.GetComponent <DockComponent>(); if (docking == null) { CurrentBehaviour = AutopilotBehaviours.None; ResetDockState(); Parent.World.BroadcastMessage(Parent, GameMessageKind.ManeuverFinished); return; } var hp = docking.GetDockHardpoints(Parent.PhysicsComponent.Body.Position).Skip(lastTargetHp).First(); radius = 5; targetPoint = (hp.Transform * TargetObject.GetTransform()).Transform(Vector3.Zero); if (lastTargetHp > 0) { maxSpeed = 0.3f; } if (lastTargetHp == 2) { radius = docking.TriggerRadius; } if (!hasTriggeredAnimation && docking.TryTriggerAnimation(Parent)) { hasTriggeredAnimation = true; } if (docking.RequestDock(Parent)) { ResetDockState(); DockComplete(docking.Action); } var d2 = (targetPoint - Parent.PhysicsComponent.Body.Position).Length; if (d2 < 80) { maxSpeed = 0.3f; } } //Bring ship to within 40 metres of target var targetRadius = TargetObject.PhysicsComponent.Body.Collider.Radius; var myRadius = Parent.PhysicsComponent.Body.Collider.Radius; var distance = (targetPoint - Parent.PhysicsComponent.Body.Position).Length; var distrad = radius < 0 ? (targetRadius + myRadius + 40) : radius + myRadius; bool distanceSatisfied = distrad >= distance; if (distanceSatisfied) { control.EnginePower = 0; } else { control.EnginePower = maxSpeed; } //Orientation var dt = time.TotalSeconds; var vec = Parent.InverseTransformPoint(targetPoint); //normalize it vec.Normalize(); // bool directionSatisfied = (Math.Abs(vec.X) < 0.0015f && Math.Abs(vec.Y) < 0.0015f); if (!directionSatisfied) { control.Yaw = MathHelper.Clamp((float)YawControl.Update(0, vec.X, dt), -1, 1); control.Pitch = MathHelper.Clamp((float)PitchControl.Update(0, -vec.Y, dt), -1, 1); } else { control.Yaw = 0; control.Pitch = 0; } if (distanceSatisfied && directionSatisfied && CurrentBehaviour == AutopilotBehaviours.Goto) { Parent.World.BroadcastMessage(Parent, GameMessageKind.ManeuverFinished); CurrentBehaviour = AutopilotBehaviours.None; } if (distanceSatisfied && directionSatisfied && CurrentBehaviour == AutopilotBehaviours.Dock) { if (lastTargetHp < 2) { lastTargetHp++; } else { control.EnginePower = maxSpeed; } } }
public void Cancel() { _targetObject = null; CurrentBehaviour = AutopilotBehaviours.None; }
public override void FixedUpdate(double time) { var control = Parent.GetComponent <ShipPhysicsComponent>(); var input = Parent.GetComponent <ShipInputComponent>(); if (input != null) { input.AutopilotThrottle = 0; } if (control == null) { return; } control.Pitch = control.Yaw = 0; if (CurrentBehaviour == AutopilotBehaviours.None) { ResetDockState(); return; } Vector3 targetPoint = Vector3.Zero; float radius = -1; float maxSpeed = 1f; if (CurrentBehaviour == AutopilotBehaviours.Goto) { targetPoint = GetTargetPoint(); ResetDockState(); } else { var docking = _targetObject.GetComponent <CDockComponent>(); if (docking == null) { CurrentBehaviour = AutopilotBehaviours.None; ResetDockState(); Parent.World.BroadcastMessage(Parent, GameMessageKind.ManeuverFinished); return; } var hp = docking.GetDockHardpoints(Parent.PhysicsComponent.Body.Position).Skip(lastTargetHp).First(); radius = 5; targetPoint = Vector3.Transform(Vector3.Zero, hp.Transform * _targetObject.WorldTransform); if (lastTargetHp > 0) { maxSpeed = 0.3f; } if (lastTargetHp == 2) { radius = docking.TriggerRadius; } if (docking.Action.Kind == DockKinds.Tradelane) { if (docking.TryDockTL(Parent, tlDockHP)) { CurrentBehaviour = AutopilotBehaviours.None; ResetDockState(); //Parent.World.BroadcastMessage(Parent, GameMessageKind.ManeuverFinished); return; } } var d2 = (targetPoint - Parent.PhysicsComponent.Body.Position).Length(); if (d2 < 80) { maxSpeed = 0.3f; } } float targetPower = 0; //Bring ship to within GotoRange metres of target var targetRadius = GetTargetRadius(); var myRadius = Parent.PhysicsComponent.Body.Collider.Radius; var distance = (targetPoint - Parent.PhysicsComponent.Body.Position).Length(); if ((distance - gotoRange) > 2000 && CanCruise) { control.BeginCruise(); } else if ((distance - gotoRange) < 200) { control.EndCruise(); } var distrad = radius < 0 ? (targetRadius + myRadius + gotoRange) : radius + myRadius; bool distanceSatisfied = distrad >= distance; if (distanceSatisfied) { targetPower = 0; } else { targetPower = maxSpeed; } //Orientation var dt = time; var vec = Parent.InverseTransformPoint(targetPoint); //normalize it vec.Normalize(); // bool directionSatisfied = (Math.Abs(vec.X) < 0.0015f && Math.Abs(vec.Y) < 0.0015f); if (!directionSatisfied) { control.Yaw = MathHelper.Clamp((float)YawControl.Update(0, vec.X, dt), -1, 1); control.Pitch = MathHelper.Clamp((float)PitchControl.Update(0, -vec.Y, dt), -1, 1); } else { control.Yaw = 0; control.Pitch = 0; } if (distanceSatisfied && directionSatisfied && CurrentBehaviour == AutopilotBehaviours.Goto) { Parent.World.BroadcastMessage(Parent, GameMessageKind.ManeuverFinished); CurrentBehaviour = AutopilotBehaviours.None; } if (distanceSatisfied && directionSatisfied && CurrentBehaviour == AutopilotBehaviours.Dock) { if (lastTargetHp < 2) { lastTargetHp++; } else { targetPower = maxSpeed; } } if (targetPower > _maxThrottle) { targetPower = _maxThrottle; } if (input != null) { input.AutopilotThrottle = targetPower; } control.EnginePower = targetPower; }