prosperon/scripts/actor.js

164 lines
3.9 KiB
JavaScript
Raw Normal View History

var actor = {};
2024-08-05 15:26:18 -05:00
var actor_urs = {};
2024-08-06 16:05:24 -05:00
var script_times = {};
var actor_spawns = {};
2024-08-05 15:26:18 -05:00
2024-09-26 11:36:09 -05:00
globalThis.class_use = function (script, config, base, callback) {
2024-08-06 16:05:24 -05:00
var file = Resources.find_script(script);
if (!file) {
var ret = Object.create(base);
if (callback) callback(ret);
return ret;
}
if (!actor_urs[file]) {
2024-08-05 15:26:18 -05:00
var newur = Object.create(base);
2024-08-06 16:05:24 -05:00
actor_urs[file] = newur;
script_times[file] = io.mod(file);
actor_spawns[file] = [];
2024-08-05 15:26:18 -05:00
}
2024-08-06 16:05:24 -05:00
var padawan = Object.create(actor_urs[file]);
actor_spawns[file].push(padawan);
padawan._file = file;
2024-08-27 13:57:38 -05:00
padawan._root = file.dir();
2024-08-05 15:26:18 -05:00
if (callback) callback(padawan);
2024-08-05 15:26:18 -05:00
var script = Resources.replstrs(file);
2024-09-13 17:56:02 -05:00
script = `(function() { var self = this; var $ = this.__proto__; ${script}; })`;
2024-09-26 11:36:09 -05:00
var fn = os.eval(file, script);
2024-08-05 15:26:18 -05:00
fn.call(padawan);
2024-09-26 11:36:09 -05:00
if (typeof config === "object") Object.merge(padawan, config);
2024-09-06 18:51:09 -05:00
2024-08-05 15:26:18 -05:00
return padawan;
2024-09-26 11:36:09 -05:00
};
2024-09-25 12:46:59 -05:00
2024-09-26 11:36:09 -05:00
globalThis.rmactor = function (e) {
2024-09-25 12:46:59 -05:00
if (!actor_spawns[e._file]) return;
actor_spawns[e._file].remove(e);
2024-09-26 11:36:09 -05:00
};
2024-09-25 12:46:59 -05:00
2024-09-26 11:36:09 -05:00
actor.__stats = function () {
2024-09-25 12:46:59 -05:00
var total = 0;
var stats = {};
2024-09-30 19:00:09 -05:00
game.all_objects(obj => {
if (!actor_spawns[obj._file]) return;
stats[obj._file] ??= 0;
stats[obj._file]++;
total++;
});
/* for (var i in actor_spawns) {
2024-09-25 12:46:59 -05:00
stats[i] = actor_spawns[i].length;
total += stats[i];
2024-09-30 19:00:09 -05:00
}*/
// stats.total = total;
2024-09-25 12:46:59 -05:00
return stats;
2024-09-26 11:36:09 -05:00
};
2024-08-05 15:26:18 -05:00
2024-09-26 11:36:09 -05:00
actor.hotreload = function () {
2024-08-06 16:05:24 -05:00
profile.cache("hotreload", "check");
for (var i in script_times) {
if (io.mod(i) > script_times[i]) {
say(`HOT RELAODING ${i}`);
script_times[i] = io.mod(i);
var script = Resources.replstrs(i);
script = `(function() {
var self = this;
var $ = this.__proto__;
${script};
})`;
2024-09-26 11:36:09 -05:00
var fn = os.eval(i, script);
2024-08-06 16:05:24 -05:00
for (var obj of actor_spawns[i]) {
var a = obj;
2024-09-26 11:36:09 -05:00
for (var t of a.timers) t();
a.timers = [];
var save = json.decode(json.encode(a));
2024-08-06 16:05:24 -05:00
fn.call(a);
2024-09-26 11:36:09 -05:00
Object.merge(a, save);
check_registers(a);
2024-08-06 16:05:24 -05:00
}
}
}
profile.endcache();
2024-09-26 11:36:09 -05:00
};
2024-08-06 16:05:24 -05:00
2024-09-26 11:36:09 -05:00
actor.spawn = function (script, config) {
if (typeof script !== "string") return undefined;
2024-08-05 15:26:18 -05:00
var padawan = class_use(script, config, actor);
2024-09-26 11:36:09 -05:00
padawan.padawans = [];
padawan.timers = [];
padawan.master = this;
2024-09-26 11:36:09 -05:00
Object.hide(padawan, "master", "padawans");
padawan.toString = function () {
return script;
};
check_registers(padawan);
this.padawans.push(padawan);
2024-08-06 16:05:24 -05:00
if (padawan.awake) padawan.awake();
return padawan;
};
2024-09-26 11:36:09 -05:00
actor.tween = function (from, to, time, fn) {
var stop = tween(from, to, time, fn);
2024-07-18 12:39:58 -05:00
this.timers.push(stop);
return stop;
2024-09-26 11:36:09 -05:00
};
2024-07-18 12:39:58 -05:00
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-09-26 11:36:09 -05:00
actor.rm_pawn = function (pawn) {
this.padawans.remove(pawn);
2024-09-26 11:36:09 -05:00
};
actor.timers = [];
2024-09-26 11:36:09 -05:00
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;
2024-08-06 16:05:24 -05:00
actor_spawns[this._file].remove(this);
2024-09-26 11:36:09 -05:00
if (typeof this.die === "function") this.die();
if (typeof this.stop === "function") this.stop();
if (typeof this.garbage === "function") this.garbage();
if (typeof this.then === "function") this.then();
};
actor.kill.doc = `Remove this actor and all its padawans from existence.`;
2024-09-30 10:14:56 -05:00
actor.delay = function (fn, seconds) { prosperon.add_timer(this, fn, seconds); }
actor.delay.doc = `Call 'fn' after 'seconds' with 'this' set to the actor.`;
2024-09-26 11:36:09 -05:00
actor.interval = function (fn, seconds) {
2024-08-06 14:23:21 -05:00
var self = this;
var stop;
2024-09-26 11:36:09 -05:00
var usefn = function () {
2024-08-06 14:23:21 -05:00
fn();
stop = self.delay(usefn, seconds);
2024-09-26 11:36:09 -05:00
};
2024-08-06 14:23:21 -05:00
stop = self.delay(usefn, seconds);
return stop;
2024-09-26 11:36:09 -05:00
};
2024-08-06 14:23:21 -05:00
actor.padawans = [];
global.app = Object.create(actor);
2024-09-26 11:36:09 -05:00
app.die = function () {
os.quit();
};
2024-09-26 11:36:09 -05:00
return { actor, app };