prosperon/scripts/engine.js

469 lines
11 KiB
JavaScript
Raw Normal View History

2023-12-11 16:59:59 -06:00
"use math";
2024-02-25 17:31:48 -06:00
globalThis.global = globalThis;
2023-05-24 20:45:50 -05:00
function use(file)
{
if (use.files[file]) return use.files[file];
var c = io.slurp(file);
var script = `(function() { ${c} })();`;
use.files[file] = cmd(123,script,global,file);
return use.files[file];
}
use.files = {};
function include(file,that)
{
if (!that) return;
var c = io.slurp(file);
eval_env(c, that, file);
}
function eval_env(script, env, file)
2024-02-23 16:05:30 -06:00
{
2024-02-25 17:31:48 -06:00
env ??= {};
file ??= "SCRIPT";
script = `(function() { ${script} }).call(this);`;
return cmd(123,script,env,file);
2024-02-23 16:05:30 -06:00
}
global.check_registers = function(obj)
{
if (typeof obj.update === 'function')
obj.timers.push(Register.update.register(obj.update.bind(obj)));
if (typeof obj.physupdate === 'function')
obj.timers.push(Register.physupdate.register(obj.physupdate.bind(obj)));
if (typeof obj.collide === 'function')
register_collide(0, obj.collide.bind(obj), obj.body);
if (typeof obj.separate === 'function')
register_collide(3,obj.separate.bind(obj), obj.body);
if (typeof obj.draw === 'function')
obj.timers.push(Register.draw.register(obj.draw.bind(obj), obj));
if (typeof obj.debug === 'function')
obj.timers.push(Register.debug.register(obj.debug.bind(obj)));
if (typeof obj.gui === 'function')
obj.timers.push(Register.gui.register(obj.gui.bind(obj)));
for (var k in obj) {
if (!k.startswith("on_")) continue;
var signal = k.fromfirst("on_");
Event.observe(signal, obj, obj[k]);
};
}
2024-02-25 17:31:48 -06:00
eval_env.dov = `Counterpart to /load_env/, but with a string.`;
2024-02-23 16:05:30 -06:00
function feval_env(file, env)
{
eval_env(io.slurp(file), env, file);
}
2024-02-25 17:31:48 -06:00
function load_env(file,env)
2023-05-29 10:47:30 -05:00
{
2024-02-25 17:31:48 -06:00
env ??= global;
// var script = io.slurp(file);
2024-02-25 17:31:48 -06:00
var script = io.slurp(file);
eval_env(script, env, file);
2024-02-25 17:31:48 -06:00
// cmd(16, file, env);
// var script = io.slurp(file);
// cmd(123, script, env, file);
2023-05-29 10:47:30 -05:00
}
2024-02-25 17:31:48 -06:00
load_env.doc = `Load a given file with 'env' as **this**. Does not add to the global namespace.`;
2023-05-29 10:47:30 -05:00
var load = use;
2024-02-25 17:31:48 -06:00
Object.assign(global, use("scripts/base.js"));
global.obscure('global');
global.mixin("scripts/render.js");
global.Game = {
engine_start(fn) {
2024-03-11 22:23:02 -05:00
console.warn("engine starting.");
cmd(257, fn);
},
object_count() {
return cmd(214);
},
all_objects(fn) {
/* Wind down from Primum */
},
/* Returns a list of objects by name */
find(name) {
},
/* Return a list of objects derived from a specific prototype */
find_proto(proto) {
},
/* List of all objects spawned that have a specific tag */
find_tag(tag){
},
quit() {
sys_cmd(0);
},
pause() { sys_cmd(3); },
stop() { Game.pause(); },
step() { sys_cmd(4);},
editor_mode(m) { sys_cmd(10, m); },
playing() { return sys_cmd(5); },
paused() { return sys_cmd(6); },
stepping() { return cmd(79); },
play() { sys_cmd(1); },
wait_fns: [],
wait_exec(fn) {
if (!phys_stepping())
fn();
else
this.wait_fns.push(fn);
},
exec() {
this.wait_fns.forEach(function(x) { x(); });
this.wait_fns = [];
},
set width(w) { cmd(125, w); },
set height(h) { cmd(126, h); },
get width() { return cmd(48); },
get height() { return cmd(49); },
dimensions() { return [this.width,this.height]; },
};
Game.gc = function() { cmd(259); }
Game.gc.doc = "Force the garbage collector to run.";
Game.doc = {};
Game.doc.object = "Returns the entity belonging to a given id.";
Game.doc.quit = "Immediately quit the game.";
Game.doc.pause = "Pause game simulation.";
Game.doc.stop = "Stop game simulation. This does the same thing as 'pause', and if the game is a debug build, starts its editor.";
Game.doc.play = "Resume or start game simulation.";
Game.doc.editor_mode = "Set to true for the game to only update on input; otherwise the game updates every frame.";
Game.doc.dt = "Current frame dt.";
Game.doc.view_camera = "Set the camera for the current view.";
Game.doc.camera = "Current camera.";
2024-03-11 22:23:02 -05:00
global.prosperon = {};
2024-02-23 16:05:30 -06:00
prosperon.version = cmd(255);
prosperon.revision = cmd(256);
prosperon.semver = {};
prosperon.semver.valid = function(v, range)
{
v = v.split('.');
range = range.split('.');
if (v.length !== 3) return undefined;
if (range.length !== 3) return undefined;
if (range[0][0] === '^') {
range[0] = range[0].slice(1);
if (parseInt(v[0]) >= parseInt(range[0])) return true;
return false;
}
if (range[0] === '~') {
range[0] = range[0].slice(1);
for (var i = 0; i < 2; i++)
if (parseInt(v[i]) < parseInt(range[i])) return false;
return true;
}
return prosperon.semver.cmp(v.join('.'), range.join('.')) === 0;
}
prosperon.semver.cmp = function(v1, v2)
{
var ver1 = v1.split('.');
var ver2 = v2.split('.');
for (var i = 0; i < 3; i++) {
var n1 = parseInt(ver1[i]);
var n2 = parseInt(ver2[i]);
if (n1 > n2)
return 1;
else if (n1 < n2)
return -1;
}
return 0;
}
prosperon.semver.doc = "Functions for semantic versioning numbers. Semantic versioning is given as a triple digit number, as MAJOR.MINOR.PATCH.";
prosperon.semver.cmp.doc = "Compare two semantic version numbers, given like X.X.X.";
prosperon.semver.valid.doc = `Test if semantic version v is valid, given a range.
Range is given by a semantic versioning number, prefixed with nothing, a ~, or a ^.
~ means that MAJOR and MINOR must match exactly, but any PATCH greater or equal is valid.
^ means that MAJOR must match exactly, but any MINOR and PATCH greater or equal is valid.`;
prosperon.iconified = function(icon) {};
prosperon.focus = function(focus) {};
prosperon.resize = function(dimensions) {};
prosperon.suspended = function(sus) {};
prosperon.mouseenter = function(){};
prosperon.mouseleave = function(){};
prosperon.touchpress = function(touches){};
prosperon.touchrelease = function(touches){};
prosperon.touchmove = function(touches){};
prosperon.clipboardpaste = function(str){};
2024-03-11 22:23:02 -05:00
global.mixin("scripts/input.js");
global.mixin("scripts/std.js");
console.level = 1;
global.mixin("scripts/diff.js");
global.mixin("scripts/color.js");
global.mixin("scripts/gui.js");
var timer = {
update(dt) {
this.remain -= dt;
2023-12-26 15:39:46 -06:00
if (this.remain <= 0) {
this.fn();
this.kill();
}
},
kill() {
2024-01-03 14:26:42 -06:00
this.end();
2023-12-29 19:08:53 -06:00
this.fn = undefined;
},
2023-11-29 17:31:41 -06:00
delay(fn, secs) {
var t = Object.create(this);
t.time = secs;
t.remain = secs;
2023-12-26 15:39:46 -06:00
t.fn = fn;
2024-01-03 14:26:42 -06:00
t.end = Register.update.register(timer.update.bind(t));
2023-11-29 17:31:41 -06:00
return function() { t.kill(); };
},
};
global.mixin("scripts/tween.js");
global.mixin("scripts/physics.js");
global.mixin("scripts/ai.js");
global.mixin("scripts/geometry.js");
2023-04-25 14:59:26 -05:00
var Register = {
kbm_input(mode, btn, state, ...args) {
if (state === 'released') {
2023-12-18 06:45:27 -06:00
btn = btn.split('-').last();
}
switch(mode) {
case "emacs":
player[0].raw_input(btn, state, ...args);
break;
case "mouse":
player[0].mouse_input(btn, state, ...args);
break;
case "char":
player[0].char_input(btn);
break;
};
},
2023-04-25 16:59:12 -05:00
gamepad_playermap: [],
gamepad_input(pad, btn, state, ...args) {
var player = this.gamepad_playermap[pad];
if (!player) return;
var statestr = Input.state2str(state);
2023-04-25 16:59:12 -05:00
var rawfn = `gamepad_${btn}_${statestr}`;
player.input(rawfn, ...args);
2024-02-25 17:31:48 -06:00
input.action.actions.forEach(x => {
2023-04-25 16:59:12 -05:00
if (x.inputs.includes(btn))
player.input(`action_${x.name}_${statestr}`, ...args);
});
},
2024-01-03 14:26:42 -06:00
unregister_obj(obj) { Player.uncontrol(obj); },
2023-05-24 20:45:50 -05:00
2023-05-27 10:13:20 -05:00
endofloop(fn) {
if (!this.inloop)
fn();
else {
this.loopcbs.push(fn);
}
},
2023-04-25 16:59:12 -05:00
clear() {
Register.registries.forEach(function(n) {
n.entries = [];
});
},
registries: [],
2023-06-05 17:19:43 -05:00
2024-03-11 22:23:02 -05:00
add_cb(name) {
var n = {};
2024-01-03 14:26:42 -06:00
var fns = [];
n.register = function(fn, obj) {
2024-01-03 14:26:42 -06:00
if (typeof fn !== 'function') return;
if (typeof obj === 'object')
fn = fn.bind(obj);
fns.push(fn);
return function() { fns.remove(fn); };
}
2024-03-11 22:23:02 -05:00
prosperon[name] = function(...args) { fns.forEach(x => x(...args)); }
2024-01-03 14:26:42 -06:00
n.clear = function() { fns = []; }
Register[name] = n;
Register.registries.push(n);
return n;
},
};
2024-03-11 22:23:02 -05:00
Register.add_cb("update").doc = "Called once per frame.";
Register.add_cb("appupdate");
Register.add_cb("physupdate");
Register.add_cb("gui");
Register.add_cb("debug");
Register.add_cb("gamepad_input");
Register.add_cb("draw");
2024-03-01 11:45:06 -06:00
Register.gamepad_playermap[0] = Player.players[0];
2023-11-22 03:51:43 -06:00
var Event = {
events: {},
observe(name, obj, fn) {
this.events[name] ??= [];
this.events[name].push([obj, fn]);
},
unobserve(name, obj) {
this.events[name] = this.events[name].filter(x => x[0] !== obj);
},
2023-12-20 17:20:29 -06:00
rm_obj(obj) {
Object.keys(this.events).forEach(name => Event.unobserve(name,obj));
},
2023-11-22 03:51:43 -06:00
notify(name) {
if (!this.events[name]) return;
this.events[name].forEach(function(x) {
x[1].call(x[0]);
});
},
};
// Window
2023-06-06 15:49:55 -05:00
var Window = {
fullscreen(f) { cmd(145, f); },
dimensions() { return cmd(265); },
get width() { return this.dimensions().x; },
get height() { return this.dimensions().y; },
2024-03-09 18:22:06 -06:00
set width(x) { cmd(266, x); },
set height(x) { cmd(267,x); },
mode: {
2024-03-09 18:22:06 -06:00
stretch: 0, // stretch to fill window
keep: 1, // keep exact dimensions
width: 2, // keep width
height: 3, // keep height
expand: 4, // expand width or height
full: 5 // expand out beyond window
},
aspect(x) { cmd(264, x); },
2024-02-25 17:31:48 -06:00
title(str) { cmd(134, str); },
2023-10-04 08:18:09 -05:00
boundingbox() {
return {
t: Window.height,
b: 0,
r: Window.width,
l: 0
};
},
2023-06-06 15:49:55 -05:00
};
Game.width = 1920;
Game.height = 1080;
2024-02-25 17:31:48 -06:00
Window.screen2world = function(screenpos) {
if (Game.camera)
return Game.camera.view2world(screenpos);
2024-02-25 17:31:48 -06:00
return screenpos;
}
Window.world2screen = function(worldpos) {
return Game.camera.world2view(worldpos);
}
2024-02-25 17:31:48 -06:00
Window.icon = function(path) { cmd(90, path); };
Window.icon.doc = "Set the icon of the window using the PNG image at path.";
global.mixin("scripts/debug.js");
global.mixin("scripts/spline.js");
global.mixin("scripts/components.js");
Window.doc = {};
Window.doc.width = "Width of the game window.";
Window.doc.height = "Height of the game window.";
Window.doc.dimensions = "Window width and height packaged in an array [width,height]";
Window.doc.title = "Name in the title bar of the window.";
Window.doc.boundingbox = "Boundingbox of the window, with top and right being its height and width.";
Register.update.register(Game.exec, Game);
global.mixin("scripts/actor.js");
global.mixin("scripts/entity.js");
2023-05-27 07:01:17 -05:00
function world_start() {
globalThis.world = Object.create(gameobject);
world.objects = {};
world.check_dirty = function() {};
world.namestr = function(){};
world._ed = {
2023-11-29 17:31:41 -06:00
selectable:false,
dirty:false,
};
world.toString = function() { return "world"; };
world.master = gameobject;
world.ur = "world";
world.kill = function() { this.clear(); };
world.phys = 2;
2024-02-23 16:05:30 -06:00
gameobject.level = world;
2024-02-23 16:05:30 -06:00
gameobject.body = make_gameobject();
cmd(113,gameobject.body, gameobject);
Object.hide(gameobject, 'timescale');
var cam = world.spawn("scripts/camera2d.jso");
Game.view_camera(cam);
}
2023-09-08 12:35:06 -05:00
global.mixin("scripts/physics.js");
Game.view_camera = function(cam)
{
Game.camera = cam;
cmd(61, Game.camera.body);
}
2024-02-25 17:31:48 -06:00
Window.title(`Prosperon v${prosperon.version}`);