editor fixes

This commit is contained in:
2023-04-28 17:49:18 +00:00
parent 02707a9ada
commit 8ca1ab4384
16 changed files with 190 additions and 97 deletions

View File

@@ -435,6 +435,8 @@ Math.lerp = function (s, f, dt) {
return s + (Math.clamp(dt, 0, 1) * (f - s));
};
Math.random_range = function(min,max) { return Math.random() * (max-min) + min; };
Math.snap = function(val, grid) {
if (!grid || grid === 1) return Math.round(val);

View File

@@ -26,7 +26,9 @@ var component = {
var sprite = clone(component, {
name: "sprite",
path: "",
_path: "",
get path() { return this._path; },
set path(x) { this._path = x; this.load_img(x); },
_pos: [0, 0],
get layer() {
if (!this.gameobject)
@@ -101,7 +103,6 @@ var sprite = clone(component, {
if (!this.hasOwn('id')) return;
cmd(60, this.id, this.layer);
cmd(37, this.id, this.pos);
cmd(12, this.id, this.path, this.rect);
},
load_img(img) {
@@ -135,11 +136,10 @@ var char2d = clone(sprite, {
make(go) {
var char = clone(this);
char.curplaying = char.anims.array()[0];
Object.defineProperty(char, 'id', {value:make_sprite(go,this.path,this.pos)});
Object.defineProperty(char, 'id', {value:make_sprite(go,char.curplaying.path,this.pos)});
char.frame = 0;
char.timer = timer.make(char.advance, 1/char.curplaying.fps, char);
char.timer = timer.make(char.advance.bind(char), 1/char.curplaying.fps);
char.timer.loop = true;
char.rect = char.frame2rect(char.curplaying.frames, char.frame);
char.setsprite();
return char;
},
@@ -152,7 +152,10 @@ var char2d = clone(sprite, {
return;
}
if (this.curplaying === this.anims[name]) return;
if (this.curplaying === this.anims[name]) {
this.timer.start();
return;
}
this.curplaying = this.anims[name];
this.timer.time = 1/this.curplaying.fps;
@@ -170,6 +173,9 @@ var char2d = clone(sprite, {
advance() {
this.frame = (this.frame + 1) % this.curplaying.frames;
this.setsprite();
if (this.frame === 0 && !this.curplaying.loop)
this.timer.pause();
},
devance() {
@@ -177,15 +183,19 @@ var char2d = clone(sprite, {
if (this.frame === -1) this.frame = this.curplaying.frames-1;
this.setsprite();
},
setframe(frame) {
this.frame = frame;
this.setsprite();
},
pause() {
this.timer.pause();
},
stop() {
this.frame = 0;
this.setframe(0);
this.timer.stop();
this.setsprite();
},
kill() {

View File

@@ -48,16 +48,6 @@ var Debug = {
register_debug(fn,obj);
},
print_callstack() {
for (var i = -3;; i--) {
var t = Duktape.act(i);
if (!t) break;
var file = t.function ? t.function.fileName : "";
var line = t.lineNumber;
Log.info(file + ":" + line);
}
},
line(points, color, type) {
if (!type)
type = 0;

View File

@@ -82,11 +82,14 @@ function unmerge(target, source) {
/* Deeply merge two objects, not clobbering objects on target with objects on source */
function deep_merge(target, source)
{
Log.warn("Doing a deep merge ...");
for (var key in source) {
if (typeof source[key] === 'object' && !Array.isArray(source[key]))
if (typeof source[key] === 'object' && !Array.isArray(source[key])) {
deep_merge(target[key], source[key]);
else
}
else {
target[key] = source[key];
}
}
};

View File

@@ -3,6 +3,12 @@
selectable
*/
var required_files = ["proto.json"];
required_files.forEach(x => {
if (!IO.exists(x)) slurpwrite("", x);
});
var editor_level = Level.create();
var editor_camera = editor_level.spawn(camera2d);
editor_camera.save = false;
@@ -221,7 +227,7 @@ var editor = {
if (this.sel_comp) return;
if (!this.selectlist.empty) {
this.selectlist.forEach(function(x) { x.draw_layer++; });
this.selectlist.forEach(x => x.draw_layer++);
return;
}
@@ -246,16 +252,21 @@ var editor = {
save_proto() {
if (this.selectlist.length !== 1) return;
var protos = JSON.parse(slurp("proto.json"));
Log.warn(`Saving prototype ${this.selectlist[0].toString()}`);
var protos = JSON.parse(IO.slurp("proto.json"));
var tobj = this.selectlist[0].prop_obj();
var pobj = this.selectlist[0].__proto__.prop_obj();
Log.warn("Going to deep merge.");
deep_merge(pobj, tobj);
Log.warn("Finished deep merge.");
pobj.from = this.selectlist[0].__proto__.from;
protos[this.selectlist[0].__proto__.name] = pobj;
Log.warn(JSON.stringify(protos));
slurpwrite(JSON.stringify(protos, null, 2), "proto.json");
/* Save object changes to parent */
@@ -265,11 +276,12 @@ var editor = {
unmerge(this.selectlist[0], tobj);
/* Now sync all objects */
Game.objects.forEach(function(x) { x.sync(); });
Game.objects.forEach(x => x.sync());
},
save_prototypes() {
slurpwrite(JSON.stringify(gameobjects,null,2), "proto.json");
save_gameobjects_as_prototypes();
},
/* Save the selected object as a new prototype, extending the chain */
@@ -278,7 +290,6 @@ var editor = {
Log.info("Already an object with name '" + name + "'. Choose another one.");
return;
}
var newp = this.selectlist[0].__proto__.clone(name);
for (var key in newp)
@@ -535,7 +546,7 @@ var editor = {
},
input_delete_pressed() {
this.selectlist.forEach(function(x) { x.kill(); });
this.selectlist.forEach(x => x.kill());
this.unselect();
},

View File

@@ -1,3 +1,5 @@
"use strict";
var Log = {
set level(x) { cmd(92,x); },
get level() { return cmd(93); },
@@ -44,6 +46,7 @@ var Log = {
},
stack(skip = 0) {
Log.warn("Printing stack");
var stack = (new Error()).stack;
var n = stack.next('\n',0)+1;
for (var i = 0; i < skip; i++)
@@ -53,6 +56,7 @@ var Log = {
},
};
var files = {};
function load(file) {
var modtime = cmd(0, file);
@@ -162,7 +166,7 @@ var Yugine = {
};
var timer = {
make(fn, secs, obj, loop) {
make(fn, secs,obj,loop) {
if (secs === 0) {
fn.call(obj);
return;
@@ -170,23 +174,17 @@ var timer = {
var t = clone(this);
t.id = make_timer(fn, secs, obj);
if (loop)
t.loop = loop;
else
t.loop = 0;
Log.info("Made timer " + t.id);
return t;
},
oneshot(fn, secs, obj) {
var t = clone(this);
var killfn = function() {
fn.call(this);
oneshot(fn, secs,obj) {
var t = this.make(() => {
fn.call();
t.kill();
};
t.id = make_timer(killfn,secs,obj);
},secs);
t.loop = 0;
t.start();
},
get remain() { return cmd(32, this.id); },
@@ -508,6 +506,8 @@ var Register = {
Player.players.forEach(x => x.uncontrol(obj));
},
};
Register.unregister_obj(null);
register(0, Register.update, Register);
register(1, Register.physupdate, Register);
register(2, Register.gui, Register);
@@ -515,6 +515,7 @@ register(3, Register.nk_gui, Register);
register(6, Register.debug, Register);
register(7, Register.kbm_input, Register);
register(8, Register.gamepad_input, Register);
register(9, Log.stack, this);
Register.gamepad_playermap[0] = Player.players[0];
@@ -576,7 +577,13 @@ var Signal = {
var IO = {
exists(file) { return cmd(65, file);},
slurp(file) { return cmd(38,file); },
slurp(file) {
if (!this.exists(file)) {
Log.warn(`File ${file} does not exist; can't slurp.`);
return "";
}
return cmd(38,file);
},
slurpwrite(str, file) { return cmd(39, str, file); },
extensions(ext) { return cmd(66, "." + ext); },
};
@@ -1442,6 +1449,7 @@ var gameobject = {
},
kill() {
Log.warn(`Killing ${this.toString()}`);
cmd(2, this.body);
delete Game.objects[this.body];
@@ -1613,7 +1621,7 @@ function add_sync_prop(obj, prop, syncfn) {
function load_configs(file) {
var configs = JSON.parse(IO.slurp(file));
for (var key in configs) {
Object.assign(this[key], configs[key]);
Object.assign(globalThis[key], configs[key]);
}
Collision.sync();
@@ -1653,7 +1661,7 @@ function save_game_configs() {
Game.objects.forEach(function(x) { x.sync(); });
};
Collision = {
var Collision = {
types: {},
num: 10,
set_collide(a, b, x) {
@@ -1718,3 +1726,38 @@ var camera2d = gameobject.clone("camera2d", {
win_make(Game.title, Game.resolution[0], Game.resolution[1]);
win_icon("icon.png");
/* Default objects */
gameobject.clone("polygon2d", {
polygon2d: polygon2d.clone(),
});
gameobject.clone("edge2d", {
edge2d: bucket.clone(),
});
gameobject.clone("sprite", {
sprite: sprite.clone(),
});
load("config.js");
var prototypes = JSON.parse(slurp("proto.json"));
for (var key in prototypes) {
if (key in gameobjects)
dainty_assign(gameobjects[key], prototypes[key]);
else {
/* Create this gameobject fresh */
Log.info("Making new prototype: " + key + " from " + prototypes[key].from);
var newproto = gameobjects[prototypes[key].from].clone(key);
gameobjects[key] = newproto;
for (var pkey in newproto)
if (typeof newproto[pkey] === 'object' && newproto[pkey] && 'clone' in newproto[pkey])
newproto[pkey] = newproto[pkey].clone();
dainty_assign(gameobjects[key], prototypes[key]);
}
}
function save_gameobjects_as_prototypes() { slurpwrite(JSON.stringify(gameobjects,null,2), "proto.json"); };