prosperon/scripts/actor.js

88 lines
2.1 KiB
JavaScript
Raw Normal View History

var actor = {};
2024-04-01 17:58:29 -05:00
actor.spawn = function(script, config, callback){
if (typeof script !== 'string') return undefined;
var padawan = Object.create(actor);
use(script, padawan);
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-07-24 14:17:32 -05:00
padawan.toString = function() { return script; }
check_registers(padawan);
this.padawans.push(padawan);
return padawan;
};
2024-07-18 12:39:58 -05:00
actor.tween = function(from,to,time,fn) {
var stop = tween(from,to,time,fn);
this.timers.push(stop);
return stop;
}
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'.`;
actor.rm_pawn = function(pawn)
{
this.padawans.remove(pawn);
}
actor.timers = [];
actor.kill = function(){
if (this.__dead__) return;
this.timers.forEach(t => t());
2024-04-03 08:37:29 -05:00
input.do_uncontrol(this);
Event.rm_obj(this);
if (this.master) this.master.rm_pawn(this);
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();
2024-07-18 12:39:58 -05:00
if (typeof this.garbage === 'function') this.garbage();
};
actor.kill.doc = `Remove this actor and all its padawans from existence.`;
actor.delay = function(fn, seconds) {
var timers = this.timers;
2024-03-19 17:00:49 -05:00
var stop = function() {
timers.remove(stop);
2024-03-19 17:00:49 -05:00
rm();
}
function execute() {
2024-07-18 12:39:58 -05:00
if (fn) fn();
if (stop.then) stop.then();
2024-03-19 17:00:49 -05:00
stop();
}
stop.remain = seconds;
stop.seconds = seconds;
2024-04-14 14:53:41 -05:00
stop.pct = function() { return 1-(stop.remain / stop.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);
timers.push(stop);
2024-03-19 17:00:49 -05:00
return stop;
};
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(); }
return {actor, app};