2024-02-29 13:54:33 -06:00
var actor = { } ;
2024-04-01 17:58:29 -05:00
actor . spawn = function ( script , config , callback ) {
2024-02-29 13:54:33 -06:00
if ( typeof script !== 'string' ) return undefined ;
2024-03-18 14:27:52 -05:00
console . info ( ` spawning actor with script ${ script } ` ) ;
2024-02-29 13:54:33 -06:00
var padawan = Object . create ( actor ) ;
2024-03-21 11:33:36 -05:00
use ( script , padawan ) ;
2024-02-29 13:54:33 -06:00
if ( typeof config === 'object' )
Object . merge ( padawan , config ) ;
padawan . padawans = [ ] ;
padawan . timers = [ ] ;
padawan . master = this ;
2024-04-03 00:44:08 -05:00
Object . hide ( padawan , "master" , "timers" , "padawans" ) ;
2024-03-04 15:18:11 -06:00
check _registers ( padawan ) ;
2024-02-29 13:54:33 -06:00
this . padawans . push ( padawan ) ;
return padawan ;
} ;
actor . spawn . doc = ` Create a new actor, using this actor as the master, initializing it with 'script' and with data (as a JSON or Nota file) from 'config'. ` ;
2024-03-04 11:15:55 -06:00
actor . rm _pawn = function ( pawn )
{
this . padawans . remove ( pawn ) ;
}
2024-02-29 13:54:33 -06:00
actor . timers = [ ] ;
actor . kill = function ( ) {
if ( this . _ _dead _ _ ) return ;
2024-03-04 15:18:11 -06:00
this . timers . forEach ( t => t ( ) ) ;
2024-04-03 08:37:29 -05:00
input . do _uncontrol ( this ) ;
2024-03-04 15:18:11 -06:00
Event . rm _obj ( this ) ;
2024-03-04 11:15:55 -06:00
if ( this . master ) this . master . rm _pawn ( this ) ;
2024-02-29 13:54:33 -06:00
this . padawans . forEach ( p => p . kill ( ) ) ;
this . padawans = [ ] ;
this . _ _dead _ _ = true ;
if ( typeof this . die === 'function' ) this . die ( ) ;
if ( typeof this . stop === 'function' ) this . stop ( ) ;
} ;
actor . kill . doc = ` Remove this actor and all its padawans from existence. ` ;
2024-03-19 17:00:49 -05:00
actor . interval = function ( fn , seconds ) {
var cur ;
var stop = function ( ) {
cur ( ) ;
}
var f = function ( ) {
fn . call ( this ) ;
cur = this . delay ( f , seconds ) ;
}
cur = this . delay ( f , seconds ) ;
return stop ;
}
2024-02-29 13:54:33 -06:00
actor . delay = function ( fn , seconds ) {
2024-04-02 07:41:46 -05:00
var timers = this . timers ;
2024-03-19 17:00:49 -05:00
var stop = function ( ) {
2024-04-02 07:41:46 -05:00
timers . remove ( stop ) ;
2024-03-19 17:00:49 -05:00
rm ( ) ;
}
function execute ( ) {
2024-04-02 07:41:46 -05:00
fn ( ) ;
2024-03-19 17:00:49 -05:00
stop ( ) ;
}
stop . remain = seconds ;
stop . seconds = seconds ;
2024-03-19 23:01:31 -05:00
2024-03-19 17:00:49 -05:00
function update ( dt ) {
stop . remain -= dt ;
if ( stop . remain <= 0 )
execute ( ) ;
}
var rm = Register . appupdate . register ( update ) ;
2024-04-02 07:41:46 -05:00
timers . push ( stop ) ;
2024-03-19 17:00:49 -05:00
return stop ;
2024-02-29 13:54:33 -06:00
} ;
actor . delay . doc = ` Call 'fn' after 'seconds' with 'this' set to the actor. ` ;
actor . padawans = [ ] ;
global . app = Object . create ( actor ) ;
2024-03-15 11:21:36 -05:00
app . die = function ( ) { os . quit ( ) ; }
2024-02-29 13:54:33 -06:00
return { actor , app } ;