Skip to content

Commit

Permalink
moved old rotors to fixedUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
snjo committed Jul 31, 2014
1 parent 30e94b2 commit 9380d6c
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 92 deletions.
68 changes: 39 additions & 29 deletions Firespitter/engine/FSrotorTrim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class FSrotorTrim : PartModule
private Transform partTransform;
private Vector3 thrustTransformDefaultPosition = Vector3.zero;

private bool initialized = false;

[KSPAction("Toggle Steering")]
public void toggleSteeringAction(KSPActionParam param)
{
Expand Down Expand Up @@ -127,52 +129,60 @@ public override void OnStart(PartModule.StartState state)
if (partTransform != null)
{
thrustTransformDefaultPosition = partTransform.localPosition;
initialized = true;
}
else
{
Debug.Log("FSrotorTrim: Could not find partTransform '" + targetPartObject + "', disabling module");
}
}

public override void OnFixedUpdate()
public void FixedUpdate()
{
if (!HighLogic.LoadedSceneIsFlight || !vessel.isActiveVessel) return;

FlightCtrlState ctrl = vessel.ctrlState;

Vector3 steeringInput = new Vector3(0, 0, 0);

if (altInputModeEnabled)
if (initialized)
{
steeringInput.x = ctrl.yaw;
}
else
{
steeringInput.x = ctrl.roll;
}
if (!HighLogic.LoadedSceneIsFlight || !vessel.isActiveVessel) return;

steeringInput.z = -ctrl.pitch;
FlightCtrlState ctrl = vessel.ctrlState;

bool inputReceived = (steeringInput != new Vector3(0, 0, 0));
Vector3 steeringInput = new Vector3(0, 0, 0);

if (steeringEnabled) // && inputReceived)
{
if (useTransformTranslation)
if (altInputModeEnabled)
{
translateThrustTransform(steeringInput);
steeringInput.x = ctrl.yaw;
}
else
{
steerPart(steerAmount, new Vector3(steeringInput.x, steeringInput.y, steeringInput.z));
steeringInput.x = ctrl.roll;
}
}
else steerPart(0, steeringInput);

if (!useTransformTranslation)
{
if (Input.GetKey(hoverKey)) //Auto hover
steeringInput.z = -ctrl.pitch;

bool inputReceived = (steeringInput != new Vector3(0, 0, 0));

if (steeringEnabled) // && inputReceived)
{
autoHover();
if (useTransformTranslation)
{
translateThrustTransform(steeringInput);
}
else
{
steerPart(steerAmount, new Vector3(steeringInput.x, steeringInput.y, steeringInput.z));
}
}
else
else steerPart(0, steeringInput);

if (!useTransformTranslation)
{
setPartRotation();
if (Input.GetKey(hoverKey)) //Auto hover
{
autoHover();
}
else
{
setPartRotation();
}
}
}
}
Expand Down
134 changes: 71 additions & 63 deletions Firespitter/engine/Stock based modules/FStailRotorThrust.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class FStailRotorThrust : PartModule
{
ModuleEngines engine = new ModuleEngines();
Transform partTransform;
Transform RotorParent;
Transform rotorParentTransform;

private int timeCount = 0;
private float idleThrust = 0.001f;
Expand Down Expand Up @@ -38,16 +38,18 @@ public class FStailRotorThrust : PartModule
[KSPField(guiActive = true, guiName = "Inverted Left/Right", isPersistant = true)]
public bool invertInput = false;

private bool initialized = false;

//[KSPField(guiActive = true, guiName = "Trim with Alt Key", isPersistant = true)]
//public bool trimWithAlt = true;

[KSPEvent(name = "toggleAltInputMode", active = true, guiActive = true, guiName = "QE or AD to rotate")]
[KSPEvent(name = "toggleAltInputMode", active = true, guiActive = true, guiActiveEditor = true, guiName = "QE or AD to rotate")]
public void toggleAltInputMode()
{
altInputModeEnabled = !altInputModeEnabled;
}

[KSPEvent(name = "toggleInvertInput", active = true, guiActive = true, guiName = "Invert Left/Right")]
[KSPEvent(name = "toggleInvertInput", active = true, guiActive = true, guiActiveEditor = true, guiName = "Invert Left/Right")]
public void toggleInvertInput()
{
invertInput = !invertInput;
Expand Down Expand Up @@ -110,78 +112,84 @@ public void setThrust(int modifier)
}

public override void OnStart(PartModule.StartState state)
{
base.OnStart(state);
{
engine = part.Modules.OfType<ModuleEngines>().FirstOrDefault();
partTransform = part.FindModelTransform(thrustTransform);
RotorParent = part.FindModelTransform(rotorparent);
rotorParentTransform = part.FindModelTransform(rotorparent);
if (partTransform != null)
{
defaultRotation = partTransform.localRotation;
}
//maxThrust = engine.maxThrust;
}

public override void OnUpdate()
{
if (!HighLogic.LoadedSceneIsFlight || !vessel.isActiveVessel) return;
FlightCtrlState ctrl = vessel.ctrlState;
Vector3 steeringInput = new Vector3(0, 0, 0);




if (altInputModeEnabled)
{
steeringInput.y = ctrl.roll;
initialized = true;
}
else
{
steeringInput.y = ctrl.yaw;
Debug.Log("FSrotorTrim: Could not find partTransform '" + thrustTransform + "', disabling module");
}
}

//bool inputReceived = steeringInput.y != 0f;
//Debug.Log("Force: " + steeringInput.y);

if (invertInput) steeringInput *= -1; // if the part is upside down, you can toggle inverse controls for it.

engine.throttleLocked = true;

if (steeringInput.y > 0)
{
partTransform.localRotation = defaultRotation.Inverse();
}
else
{
partTransform.localRotation = defaultRotation;
}

if (steeringInput.y == 0)
{
engine.maxThrust = idleThrust;
}
else
{
engine.maxThrust = maxThrust * Mathf.Abs(steeringInput.y);
}

// blade rotation

bool engineActive = engine && engine.getIgnitionState && !engine.getFlameoutState;

if (engineActive && timeCount < 1000)
{
timeCount += spinUpTime;
}
else if (!engineActive && timeCount > 0)
public void FixedUpdate()
{
if (initialized)
{
timeCount -= spinUpTime;
if (!HighLogic.LoadedSceneIsFlight || !vessel.isActiveVessel) return;
FlightCtrlState ctrl = vessel.ctrlState;
Vector3 steeringInput = new Vector3(0, 0, 0);

if (altInputModeEnabled)
{
steeringInput.y = ctrl.roll;
}
else
{
steeringInput.y = ctrl.yaw;
}

//bool inputReceived = steeringInput.y != 0f;
//Debug.Log("Force: " + steeringInput.y);

if (invertInput) steeringInput *= -1; // if the part is upside down, you can toggle inverse controls for it.

engine.throttleLocked = true;

if (steeringInput.y > 0)
{
partTransform.localRotation = defaultRotation.Inverse();
}
else
{
partTransform.localRotation = defaultRotation;
}

if (steeringInput.y == 0)
{
engine.maxThrust = idleThrust;
}
else
{
engine.maxThrust = maxThrust * Mathf.Abs(steeringInput.y);
}

// blade rotation

bool engineActive = engine && engine.getIgnitionState && !engine.getFlameoutState;

if (engineActive && timeCount < 1000)
{
timeCount += spinUpTime;
}
else if (!engineActive && timeCount > 0)
{
timeCount -= spinUpTime;
}

if (timeCount < 0) timeCount = 0; // in case people give the spinUpTime in an unexpected way

float currentSpeed = ((rotationSpeed * 6) * TimeWarp.deltaTime * ((float)timeCount / 1000));
if (rotorParentTransform != null)
{
rotorParentTransform.transform.Rotate(Vector3.forward * currentSpeed);
}
}

if (timeCount < 0) timeCount = 0; // in case people give the spinUpTime in an unexpected way

float currentSpeed = ((rotationSpeed * 6) * TimeWarp.deltaTime * ((float)timeCount / 1000));
RotorParent.transform.Rotate(Vector3.forward * currentSpeed);
}
}
}
Binary file modified For release/Firespitter/Plugins/Firespitter.dll
Binary file not shown.

0 comments on commit 9380d6c

Please sign in to comment.