Scripts/barrel_roll.js |
"use strict";
this.name = "BarrelRoll";
this.author = "Dybal";
this.copyright = "2020 Dybal";
this.license = "CC-BY-NC-SA 4.0";
this.description = "Barrel Roll auto-pilot";
this.version = "1.1";
this.$debug = false;
this.$logging = true;
this.$FCB_tracking_ID = null;
this.$rbEnabled = false;
this.$rbActive = false;
//
// Event Handlers
//
//------------------------------------------------------------------------------------------//
this.startUpComplete = function _BarrelRoll_startUpComplete() {
this.ship = player.ship;
if (this.ship.equipmentStatus("EQ_BARREL_ROLL") === "EQUIPMENT_OK")
this.$rbEnabled = true;
else
this.$rbEnabled = false;
}
//------------------------------------------------------------------------------------------//
this.playerBoughtNewShip = this.playerReplacedShip = function _BarrelRoll_playerGotNewShip(ship) {
this.ship = ship;
if (this.ship.equipmentStatus("EQ_BARREL_ROLL") === "EQUIPMENT_OK")
this.$rbEnabled = true;
else
this.$rbEnabled = false;
}
//------------------------------------------------------------------------------------------//
this.equipmentAdded = function _BarrelRoll_equipmentAdded(equipmentKey) {
if (equipmentKey === "EQ_BARREL_ROLL") {
if (this.$debug) log(this.name, "Barrel Roll added")
this.$rbEnabled = true;
this.$rbActive = false;
}
}
//------------------------------------------------------------------------------------------//
this.equipmentRemoved = function _BarrelRoll_equipmentRemoved(equipmentKey) {
if (equipmentKey === "EQ_BARREL_ROLL") {
if (this.$debug) log(this.name, "Barrel Roll removed")
this.$rbEnabled = false;
if (this.$rbActive) {
if (this.$FCB_tracking_ID && isValidFrameCallback(this.$FCB_tracking_ID))
removeFrameCallback(this.$FCB_tracking_ID);
this.$rbActive = false;
}
}
}
//------------------------------------------------------------------------------------------//
this.shipWillLaunchFromStation = this.shipWillExitWitchspace = function _BarrelRoll_StartFlight() {
this.$rbActive = false;
}
//------------------------------------------------------------------------------------------//
this.activated = function _BarrelRoll_activated() {
var ws = worldScripts.BarrelRoll;
if (ws.$rbEnabled) {
if (ws.$rbActive) {
// de-activate
if (ws.$FCB_tracking_ID && isValidFrameCallback(ws.$FCB_tracking_ID))
removeFrameCallback(ws.$FCB_tracking_ID);
ws.$rbActive = false;
if (ws.$logging) log(ws.name, "De-activating")
} else {
if (!isValidFrameCallback(ws.$FCB_tracking_ID))
ws.$FCB_tracking_ID = addFrameCallback(ws.$FCB.bind(ws));
ws.$rbActive = true;
if (ws.$logging) log(ws.name, "Activating")
}
}
}
//
// Internal Functions
//
//------------------------------------------------------------------------------------------//
this.$FCB = function _BarrelRoll_FCB(delta) {
var ship = this.ship;
var pitch, roll, heading, right, orientation;
if (!this.$rbEnabled || !this.$rbActive || delta == 0) return;
pitch = ship.maxPitch * delta;
roll = ship.maxRoll * delta;
heading = ship.heading;
right = ship.vectorRight;
// roll the ship anti-clock-wise
orientation = ship.orientation.rotate(heading, -roll);
// pitch the ship
ship.orientation = orientation.rotate(orientation.vectorRight(), -pitch);
}
|