| Scripts/missile_beep.js | // This is the world script for Missile Beep OXP
"use strict";
this.name        = "Missile Beep";
this.description = "Missile Beep World Script";
this.version     = "1.3";
this.author      = "hoqllnq";
this.copyright   = "2017 hoqllnq";
this.licence     = "CC-BY-NC-SA 3.0";
this._timer = null;			// To periodically check for incoming missiles
this._fcb = null;			// To time the beeps
this._sound_source = null;	// To play the beeps
this._beep_time = 0;		// How long beep (or silence) has been playing
this._beep_interval = 0;	// How long beep (or silence) should last
this._beep_on = false;		// Currently beeping?
this._debug = false;		// Logging
// Initialize the timer and the sound source
this.startUp = function()
{
	if (!this._timer)
	{
		this._timer = new Timer(this, this._timer_hook.bind(this),1,1);
	}
	
	if (!this._sound_source)
	{
		this._sound_source = new SoundSource();
		this._sound_source.loop = false;
		this._sound_source.sound = "[missile_beep]";
	}
	
	if (this._debug)
		log(this.name,"Started");
}
// A missile was fired at us, it could be the nearest one,
// don't wait for timer to fire, call timer hook now to check
this.shipAttackedWithMissile = function(missile,whom)
{
	if (this._debug)
		log(this.name,"Missile fired");
	
	this._timer_hook();
}
// Periodic check for presence and distance of incoming missiles
this._timer_hook = function()
{
	var things;
	var dist;
 
	if (player && player.ship)
	{	// Find all missiles in scanner range
		things = system.entitiesWithScanClass("CLASS_MISSILE",player.ship,25600);
		
		// See if any of them are targeted at us
		for (var i = 0;i < things.length;i++)
		{
			if (things[i].target == player.ship)
			{	// Yes.
				// This is the nearest one, since the list is ordered by distance
				dist = things[i].position.distanceTo(player.ship.position);
				if (this._debug)
					log(this.name,"Missile at " + dist);
				
				// Beep duration based on distance
				this._beep_interval = 0.05 + dist / 51200.0;
				
				// If we weren't beeping yet, start now
				if(!isValidFrameCallback(this._fcb))
				{
					this._fcb = addFrameCallback(this._fcb_hook.bind(this));
					this._timer.interval = 0.5;
				}
				
				return;
			}
		}
	}
	
	// No incoming missiles
	// If we were beeping, stop now.
	if (isValidFrameCallback(this._fcb))
	{
		if (this._debug)
			log(this.name,"No more incoming missiles");
		
		removeFrameCallback(this._fcb);
		this._timer.interval = 1.0;
	}
	
	if (this._sound_source.isPlaying)
		this._sound_source.stop();
	
	this._beep_time = 0;
	this._beep_on = false;
}
// Frame callback to turn beep on/off
// This does the timing for the beeping speed
this._fcb_hook = function(delta)
{
	this._beep_time += delta;
	
	// Time for next beep/silence?
	if (this._beep_time < this._beep_interval)
		return; // No.
	
	// Reset time
	this._beep_time -= this._beep_interval;
	
	// Toggle beep
	if (this._beep_on)
	{
		if (this._sound_source.isPlaying)
			this._sound_source.stop();
		
		this._beep_on = false;
	}
	else
	{
		if (this._debug)
			log(this.name,"Beep!");
		
		if (!this._sound_source.isPlaying)
			this._sound_source.play();
		
		this._beep_on = true;
	}
}
 |