Fix char2d; add camera reset in editor; default entity physics is static

This commit is contained in:
John Alanbrook 2023-11-02 22:25:00 +00:00
parent 71d5ae7619
commit a854764636
5 changed files with 108 additions and 64 deletions

View file

@ -171,7 +171,7 @@ tools/libcdb.a:
mv $(CDB)/libcdb.a tools mv $(CDB)/libcdb.a tools
DOCOS = Sound gameobject Game Window physics Profile Time Player Mouse IO Log ColorMap sprite SpriteAnim DOCOS = Sound gameobject Game Window physics Profile Time Player Mouse IO Log ColorMap sprite SpriteAnim Render
DOCHTML := $(addsuffix .api.html, $(DOCOS)) DOCHTML := $(addsuffix .api.html, $(DOCOS))
DOCMD := $(addsuffix .api.md, $(DOCOS)) DOCMD := $(addsuffix .api.md, $(DOCOS))

View file

@ -36,11 +36,13 @@ var component = {
nc.sync(); nc.sync();
assign_impl(nc,this.impl); assign_impl(nc,this.impl);
Object.hide(nc, ...this.hides); Object.hide(nc, ...this.hides);
nc.post();
return nc; return nc;
}, },
kill() { Log.info("Kill not created for this component yet"); }, kill() { Log.info("Kill not created for this component yet"); },
sync() {}, sync() {},
post(){},
gui() { }, gui() { },
gizmo() { }, gizmo() { },
@ -147,7 +149,7 @@ var SpriteAnim = {
return anim; return anim;
}, },
strip(path, frames) { strip(path, frames, time=0.05) {
var anim = {}; var anim = {};
anim.frames = []; anim.frames = [];
anim.path = path; anim.path = path;
@ -155,11 +157,15 @@ var SpriteAnim = {
for (var f = 0; f < frames; f++) { for (var f = 0; f < frames; f++) {
var frame = {}; var frame = {};
frame.rect = {s0:xslice*f, s1: xslice*(f+1), t0:0, t1:1}; frame.rect = {s0:xslice*f, s1: xslice*(f+1), t0:0, t1:1};
frame.time = 0.05; frame.time = time;
anim.frames.push(frame); anim.frames.push(frame);
} }
anim.dim = cmd(64,path); anim.dim = cmd(64,path);
anim.dim.x /= frames; anim.dim.x /= frames;
anim.toJSON = function()
{
return anim.path;
}
return anim; return anim;
}, },
@ -198,26 +204,34 @@ var SpriteAnim = {
return anims; return anims;
}, },
validate(anim)
{
if (!Object.isObject(anim)) return false;
if (typeof anim.path !== 'string') return false;
if (typeof anim.dim !== 'object') return false;
return true;
},
find(path) {
if (!IO.exists(path + ".asset")) return;
var asset = JSON.parse(IO.slurp(path + ".asset"));
},
}; };
SpriteAnim.doc = 'Functions to create Primum animations from varying sources.'; SpriteAnim.doc = 'Functions to create Primum animations from varying sources.';
SpriteAnim.gif.doc = 'Convert a gif.'; SpriteAnim.gif.doc = 'Convert a gif.';
SpriteAnim.strip.doc = 'Given a path and number of frames, converts a horizontal strip animation, where each cell is the same width.' SpriteAnim.strip.doc = 'Given a path and number of frames, converts a horizontal strip animation, where each cell is the same width.'
SpriteAnim.aseprite.doc = 'Given an aseprite json metadata, returns an object of animations defined in the aseprite file.'; SpriteAnim.aseprite.doc = 'Given an aseprite json metadata, returns an object of animations defined in the aseprite file.';
SpriteAnim.find.doc = 'Given a path, find the relevant animation for the file.';
/* Container to play sprites and anim2ds */ /* Container to play sprites and anim2ds */
component.char2d = Object.copy(component, {
get enabled() { return cmd(114,this.id); },
set enabled(x) { cmd(20,this.id,x); },
set color(x) { cmd(96,this.id,x); },
get pos() { return cmd(111, this.id); },
set pos(x) { cmd(37,this.id,x); },
set layer(x) { cmd(60, this.id, x); },
get layer() { return this.gameobject.draw_layer; },
component.char2d = Object.create(component.sprite);
Object.assign(component.char2d, {
boundingbox() { boundingbox() {
var dim = this.curplaying.dim.slice(); var dim = this.acur.dim.slice();
dim = dim.scale(this.gameobject.scale); dim = dim.scale(this.gameobject.scale);
var realpos = this.pos.slice(); var realpos = this.pos.slice();
realpos.x = realpos.x * dim.x + (dim.x/2); realpos.x = realpos.x * dim.x + (dim.x/2);
@ -225,58 +239,56 @@ component.char2d = Object.copy(component, {
return cwh2bb(realpos,dim); return cwh2bb(realpos,dim);
}, },
anims:{},
sync() { sync() {
if (this.path) if (this.path)
cmd(12,this.id,this.path,this.rect); cmd(12,this.id,this.path,this.rect);
}, },
make(go) {
Log.say('creating animation');
var char = Object.create(this);
char.gameobject = go;
Object.assign(char, make_sprite(go.body));
char.frame = 0;
char.timer = timer.make(char.advance.bind(char), 1);
char.timer.loop = true;
Object.hide(char, 'timer');
return char;
},
frame: 0, frame: 0,
play_anim(anim) {
this.acur = anim;
this.frame = 0;
this.timer.time = this.acur.frames[this.frame].time;
this.timer.start();
this.setsprite();
},
play(name) { play(name) {
if (!(name in this)) { if (!(name in this)) {
Log.info("Can't find an animation named " + name); Log.info("Can't find an animation named " + name);
return; return;
} }
if (this.curplaying === this[name]) { if (this.acur === this[name]) {
this.timer.start(); this.timer.start();
return; return;
} }
this.curplaying = this[name]; this.acur = this[name];
this.frame = 0; this.frame = 0;
this.timer.time = this.curplaying.frames[this.frame].time; this.timer.time = this.acur.frames[this.frame].time;
this.timer.start(); this.timer.start();
this.setsprite(); this.setsprite();
}, },
setsprite() { setsprite() {
cmd(12, this.id, this.curplaying.path, this.curplaying.frames[this.frame].rect); cmd(12, this.id, this.acur.path, this.acur.frames[this.frame].rect);
}, },
advance() { advance() {
this.frame = (this.frame + 1) % this.curplaying.frames.length; this.frame = (this.frame + 1) % this.acur.frames.length;
this.setsprite(); this.setsprite();
if (this.frame === 0 && !this.curplaying.loop) if (this.frame === 0 && !this.acur.loop)
this.timer.pause(); this.timer.pause();
}, },
devance() { devance() {
this.frame = (this.frame - 1); this.frame = (this.frame - 1);
if (this.frame === -1) this.frame = this.curplaying.frames-1; if (this.frame === -1) this.frame = this.acur.frames-1;
this.setsprite(); this.setsprite();
}, },
@ -298,19 +310,46 @@ component.char2d = Object.copy(component, {
this.timer.kill(); this.timer.kill();
cmd(9, this.id); cmd(9, this.id);
}, },
add_anim(anim,name) {
if (name in this) return;
this[name] = function() {
this.play_anim(anim);
}
},
post() {
this.timer = timer.make(this.advance.bind(this), 1);
this.timer.loop = true;
Object.hide(this,'timer');
for (var k in this.anims) {
var path = this.anims[k];
this.anims[k] = run_env(path + ".asset", path);
this.add_anim(this.anims[k], k);
}
Object.hide(this, 'acur');
},
}); });
component.char2d.impl = { component.char2d.doc = {
get enabled() { return cmd(114,this.id); }, doc: "An animation player for sprites.",
set enabled(x) { cmd(20,this.id,x); }, frame: "The current frame of animation.",
set color(x) { cmd(96,this.id,x); }, anims: "A list of all animations in this player.",
acur: "The currently playing animation object.",
get pos() { return cmd(111, this.id); }, advance: "Advance the animation by one frame.",
set pos(x) { cmd(37,this.id,x); }, devance: "Go back one frame in the animation.",
set layer(x) { cmd(60, this.id, x); }, setframe: "Set a specific frame of animation.",
get layer() { return this.gameobject.draw_layer; }, stop: "Stops the animation and returns to the first frame.",
pause: "Pauses the animation sequence in place.",
play: "Given an animation string, play it. Equivalent to anim.[name].play().",
play_anim: "Play a given animation object.",
add_anim: "Add an animation object with the given name."
}; };
// sprite.timer = timer.make(sprite.advance.bind(sprite),1);
// sprite.timer.loop = true;
/* Returns points specifying this geometry, with ccw */ /* Returns points specifying this geometry, with ccw */
var Geometry = { var Geometry = {
box(w, h) { box(w, h) {

View file

@ -176,12 +176,6 @@ var editor = {
return; return;
} }
if (num === 0) {
this.camera.pos = [0,0];
this.camera.zoom = 1;
return;
}
if (num in this.camera_recalls) if (num in this.camera_recalls)
Object.assign(this.camera, this.camera_recalls[num]); Object.assign(this.camera, this.camera_recalls[num]);
}, },
@ -1304,7 +1298,10 @@ editor.inputs['C-v'] = function() {
editor.inputs['C-v'].doc = "Pull objects from killring to world."; editor.inputs['C-v'].doc = "Pull objects from killring to world.";
editor.inputs.char = function(c) { editor.inputs.char = function(c) {
if (c === '0') {
this.camera.pos = [0,0];
this.camera.zoom = 1;
}
}; };
var brushmode = {}; var brushmode = {};

View file

@ -19,6 +19,12 @@ function run(file)
return cmd(117, file); return cmd(117, file);
} }
function run_env(file, env)
{
var script = IO.slurp(file);
return function(){return eval(script);}.call(env);
}
load("scripts/diff.js"); load("scripts/diff.js");
Log.level = 1; Log.level = 1;
@ -240,7 +246,6 @@ var timer = {
} }
var t = Object.create(this); var t = Object.create(this);
// assign_impl(t, this.impl);
t.callback = fn; t.callback = fn;
var guardfn = function() { var guardfn = function() {
@ -253,6 +258,7 @@ var timer = {
}; };
t.id = make_timer(guardfn, secs, app); t.id = make_timer(guardfn, secs, app);
t.loop = loop; t.loop = loop;
t.pause();
return t; return t;
}, },
@ -262,7 +268,6 @@ var timer = {
var t = this.make(fn, secs, obj, 0, app); var t = this.make(fn, secs, obj, 0, app);
t.start(); t.start();
}, },
// impl: {
get remain() { return cmd(32, this.id); }, get remain() { return cmd(32, this.id); },
get on() { return cmd(33, this.id); }, get on() { return cmd(33, this.id); },
get loop() { return cmd(34, this.id); }, get loop() { return cmd(34, this.id); },
@ -275,19 +280,8 @@ var timer = {
set time(x) { cmd(28, this.id, x); }, set time(x) { cmd(28, this.id, x); },
get time() { return cmd(29, this.id); }, get time() { return cmd(29, this.id); },
get pct() { return this.remain / this.time; }, get pct() { return this.remain / this.time; },
// },
// remain: 0,
// on: false,
// loop: false,
// start(){},
// stop(){},
// kill(){},
// pause(){},
// time: 0,
// pct: 0,
}; };
timer.toJSON = function() { return undefined; };
timer.doc = { timer.doc = {
doc: "Quickly make timers to fire off events once or multiple times.", doc: "Quickly make timers to fire off events once or multiple times.",
oneshot: "Executes a given function after the given number of seconds.", oneshot: "Executes a given function after the given number of seconds.",
@ -384,6 +378,12 @@ var Render = {
}, },
}; };
Render.doc = {
doc: "Functions for rendering modes.",
normal: "Final render with all lighting.",
wireframe: "Show only wireframes of models."
};
load("scripts/physics.js"); load("scripts/physics.js");
load("scripts/input.js"); load("scripts/input.js");
load("scripts/sound.js"); load("scripts/sound.js");
@ -873,3 +873,7 @@ Window.name = "Primum Machinam (V0.1)";
Window.width = 1280; Window.width = 1280;
Window.height = 720; Window.height = 720;
var Asset = {};
Asset.doc = {
doc: "Functions to manage the loading and unloading of assets, like sounds and images."
};

View file

@ -272,7 +272,7 @@ var gameobject = {
register_collide(1, x.collide, x, obj.body, x.shape); register_collide(1, x.collide, x, obj.body, x.shape);
}); });
}, },
phys:1, phys:Physics.static,
flipx:false, flipx:false,
flipy:false, flipy:false,
scale:1, scale:1,
@ -554,6 +554,10 @@ var gameobject = {
return this.objects[newname]; return this.objects[newname];
}, },
add_component(comp) {
if (typeof comp['comp'] !== 'string') return;
},
register_hit(fn, obj) { register_hit(fn, obj) {
if (!obj) if (!obj)
obj = this; obj = this;