| Scripts/reverseycontrol.js | "use strict";
this.name        = "reverseycontrol";
this.author      = "Norby";
this.copyright   = "2014 Norbert Nagy";
this.licence     = "CC BY-NC-SA 3.0";
this.description = "Reverse up-down control in the aft view.";
//customizable properties
this.$ReverseControlOn = true; //can turn off without uninstall
//internal properties, should not touch
this.$ReverseControlFCB = null; //FrameCallBack
this.$ReverseControlPrevOri = null; //previous orientation
//world script events
this.startUp = function() {
}
this.shipLaunchedFromStation = this.shipExitedWitchspace = function( station ) {
	this.viewDirectionChanged(player.ship.viewDirection);
}
this.shipWillDockWithStation = function(station) {
	this.$Stop();
}
this.viewDirectionChanged = function( view ) {
	switch ( view ) {
		case "VIEW_AFT":
		case "VIEW_FORWARD":
		case "VIEW_PORT":
		case "VIEW_STARBOARD":
			var ps = player.ship;
			if( ps && ps.isValid ) this.$ReverseControlPrevOri = ps.orientation;
			if( this.$ReverseControlOn && !isValidFrameCallback( this.$ReverseControlFCB ) )
				this.$ReverseControlFCB = addFrameCallback( this.$ReverseControl_FCB.bind(this) );
			break;
		default:
			this.$Stop();
	}
}
//ReverseControl methods
this.$ReverseControl_FCB = function( delta ) { //FrameCallBack
	var ps = player.ship;
	var r = worldScripts["reversecontrol"];
	if( ps && ps.isValid && !ps.docked && this.$ReverseControlOn && ps.AI != "dockingAI.plist" ) {
		var prevori = this.$ReverseControlPrevOri;
		if( prevori ) switch ( ps.viewDirection ) {
			case "VIEW_FORWARD":
				//reverse up-down
				//var a = ps.heading.angleTo( prevori.vectorUp() ) - Math.PI / 2; //buggy with sniperlock
				var a = ps.pitch * delta;
				var c = prevori.vectorRight();
				ps.orientation = ps.orientation.rotate( c, 2 * a );
				break;
			case "VIEW_AFT":
				//reverse roll
				//var a = ps.orientation.vectorRight().angleTo( prevori.vectorUp() ) - Math.PI / 2; //buggy with sniperlock
				var a = ps.roll * delta;
				var c = prevori.vectorForward();
				ps.orientation = ps.orientation.rotate( c, 2 * a );
				break;
			case "VIEW_PORT":
				//pitch -> -roll
				var a = ps.pitch * delta;
				var c = prevori.vectorForward();
				ps.orientation = ps.orientation.rotate( c, 2 * a );
				//roll -> -pitch
				var a = ps.roll * delta;
				var c = prevori.vectorRight();
				ps.orientation = ps.orientation.rotate( c, 2 * a );
				break;
			case "VIEW_STARBOARD":
				//pitch -> roll
				var a = ps.pitch * delta;
				var c = prevori.vectorForward();
				ps.orientation = ps.orientation.rotate( c, -2 * a );
				//roll -> pitch
				var a = ps.roll * delta;
				var c = prevori.vectorRight();
				ps.orientation = ps.orientation.rotate( c, -2 * a );
				break;
			default:
				this.$Stop();
		}
		this.$ReverseControlPrevOri = ps.orientation;
	} else {
		this.$ReverseControlPrevOri = null;
	}
}
this.$Stop = function() {
	this.$ReverseControlPrevOri = null;
	if( isValidFrameCallback( this.$ReverseControlFCB ) )
		removeFrameCallback( this.$ReverseControlFCB );
}
 |