add gif for animations; unique anim types

This commit is contained in:
2023-09-24 16:26:44 +00:00
parent 174a9ed586
commit 0256f4cd15
18 changed files with 1006 additions and 20991 deletions

View File

@@ -72,11 +72,14 @@ Object.isAccessor = function(obj, prop)
Object.mergekey = function(o1,o2,k)
{
if (typeof(o2[k]) === 'object') {
if (Object.isAccessor(o2,k))
Object.defineProperty(o1, k, Object.getOwnPropertyDescriptor(o2,k));
else if (typeof o2[k] === 'object') {
if (Array.isArray(o2[k]))
o1[k] = o2[k].slice();
else
else {
Object.merge(o1[k], o2[k]);
}
} else
o1[k] = o2[k];
}

View File

@@ -87,18 +87,47 @@ sprite.inputs.kp2 = function() { this.pos = [-0.5,-1]; };
sprite.inputs.kp1 = function() { this.pos = [-1,-1]; };
Object.seal(sprite);
var gif2anim = function(gif)
{
var anim = {};
anim.frames = [];
anim.path = gif;
var frames = cmd(139,gif);
Log.warn(`gif has ${frames} frames`);
var yslice = 1/frames;
for (var f = 0; f < frames; f++) {
var frame = {};
frame.rect = {
s0: 0,
s1: 1,
t0: yslice*f,
t1: yslice*(f+1)
};
frame.time = 0.05;
anim.frames.push(frame);
}
anim.loop = true;
return anim;
}
var strip2anim = function(strip)
{
var anim = {};
anim.frames = [];
anim.path = strip;
var frames = 8;
var xslice = 1/frames;
for (var f = 0; f < frames; f++) {
var frame = {};
frame.rect = {s0:xslice*f, s1: slice*(f+1), t0:0, t1:1};
frame.time = 0.05;
anim.frames.push(frame);
}
return anim;
}
/* Container to play sprites and anim2ds */
component.char2d = Object.copy(sprite, {
frame2rect(frames, frame) {
var rect = {s0:0,s1:1,t0:0,t1:1};
var frameslice = 1/frames;
rect.s0 = frameslice*frame;
rect.s1 = frameslice*(frame+1);
return rect;
},
get enabled() { return cmd(114,this.id); },
set enabled(x) { cmd(20,this.id,x); },
set color(x) { cmd(96,this.id,x); },
@@ -106,74 +135,63 @@ component.char2d = Object.copy(sprite, {
set pos(x) { cmd(37,this.id,x); },
set layer(x) { cmd(60, this.id, x); },
get layer() { return this.gameobject.draw_layer; },
boundingbox() {
var dim = cmd(64,this.path);
dim = dim.scale(this.gameobject.scale);
dim.x *= 1/6;
var realpos = [0,0];
// var realpos = this.pos.slice();
// realpos.x = realpos.x * dim.x + (dim.x/2);
// realpos.y = realpos.y * dim.y + (dim.y/2);
return cwh2bb(realpos,dim);
},
boundingbox() {
var dim = cmd(64,this.path);
dim = dim.scale(this.gameobject.scale);
var realpos = [0,0];
// var realpos = this.pos.slice();
sync() {
if (this.path)
cmd(12,this.id,this.path,this.rect);
},
// realpos.x = realpos.x * dim.x + (dim.x/2);
// realpos.y = realpos.y * dim.y + (dim.y/2);
return cwh2bb(realpos,dim);
},
kill() { cmd(9,this.id); },
sync() {
if (this.path)
cmd(12,this.id,this.path,this.rect);
},
kill() { cmd(9,this.id); },
ur: {
},
make(go) {
var char = Object.create(this);
char.curplaying = char.anims.array()[0];
char.obscure('curplaying');
char.id = make_sprite(go, char.curplaying.path, this.pos);
char.obscure('id');
Object.assign(char, make_sprite(go));
char.frame = 0;
char.timer = timer.make(char.advance.bind(char), 1/char.curplaying.fps);
char.timer = timer.make(char.advance.bind(char), 1);
char.timer.loop = true;
char.obscure('timer');
// char.obscure('rect');
char.rect = {};
char.setsprite();
return char;
},
frame: 0,
play(name) {
if (!(name in this.anims)) {
if (!(name in this)) {
Log.info("Can't find an animation named " + name);
return;
}
if (this.curplaying === this.anims[name]) {
if (this.curplaying === this[name]) {
this.timer.start();
return;
}
this.curplaying = this.anims[name];
this.timer.time = 1/this.curplaying.fps;
this.timer.start();
this.curplaying = this[name];
this.frame = 0;
this.timer.time = this.curplaying.frames[this.frame].time;
this.timer.start();
this.setsprite();
},
setsprite() {
this.path = this.curplaying.path;
this.rect = this.frame2rect(this.curplaying.frames, this.frame);
cmd(12, this.id, this.path, this.rect);
cmd(12, this.id, this.curplaying.path, this.curplaying.frames[this.frame].rect);
},
advance() {
this.frame = (this.frame + 1) % this.curplaying.frames;
this.frame = (this.frame + 1) % this.curplaying.frames.length;
this.setsprite();
if (this.frame === 0 && !this.curplaying.loop)

View File

@@ -186,6 +186,7 @@ return;
},
key_move(dir) {
if (!editor.grabselect) return;
if (Keys.ctrl())
this.selectlist.forEach(this.snapper(dir.scale(1.01), editor_config.grid_size));
else
@@ -561,7 +562,7 @@ return;
GUI.image("icons/icons8-lock-16.png", world2screen(obj.pos));
});
Debug.draw_grid(1, editor_config.grid_size/editor.camera.zoom, Color.Editor.grid.alpha(0.3));
Debug.draw_grid(1, editor_config.grid_size, Color.Editor.grid.alpha(0.3));
var startgrid = screen2world([-20,Window.height]).map(function(x) { return Math.snap(x, editor_config.grid_size); });
var endgrid = screen2world([Window.width, 0]);
@@ -1160,6 +1161,14 @@ editor.inputs.mouse.move = function(pos, dpos)
});
}
editor.inputs.mouse.scroll = function(scroll)
{
scroll.y *= -1;
editor.grabselect?.forEach(function(x) {
x.pos = x.pos.add(scroll.scale(editor.camera.zoom));
});
}
editor.inputs.mouse['C-scroll'] = function(scroll)
{
editor.camera.pos = editor.camera.pos.sub(scroll.scale(editor.camera.zoom * 3).scale([1,-1]));

View File

@@ -221,7 +221,7 @@ var timer = {
return;
}
var t = clone(this);
var t = Object.create(this);
t.callback = fn;
var guardfn = function() {
if (typeof t.callback === 'function')
@@ -803,7 +803,6 @@ function save_game_configs() {
load("scripts/physics.js");
Game.view_camera = function(cam)
{
Game.camera = cam;