2024-02-29 13:54:33 -06:00
var actor = { } ;
var a _db = { } ;
actor . spawn = function ( script , config ) {
if ( typeof script !== 'string' ) return undefined ;
if ( ! a _db [ script ] ) a _db [ script ] = io . slurp ( script ) ;
var padawan = Object . create ( actor ) ;
2024-03-04 11:15:55 -06:00
eval _env ( a _db [ script ] , padawan , script ) ;
2024-02-29 13:54:33 -06:00
if ( typeof config === 'object' )
Object . merge ( padawan , config ) ;
padawan . padawans = [ ] ;
padawan . timers = [ ] ;
padawan . master = this ;
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 ( ) ) ;
Player . do _uncontrol ( this ) ;
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. ` ;
actor . delay = function ( fn , seconds ) {
2024-03-04 15:18:11 -06:00
var t = timer . delay ( fn . bind ( this ) , seconds ) ;
2024-02-29 13:54:33 -06:00
this . timers . push ( t ) ;
2024-03-04 15:18:11 -06:00
return t ;
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 } ;