From 2195f9f5dbbb8921cf8c61dbff50ee78881fd4fc Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 27 Dec 2023 23:28:10 +0000 Subject: [PATCH] Resources for animations --- scripts/components.js | 37 +++++++++++++++++++++---------------- scripts/editor.js | 1 + scripts/entity.js | 3 +++ scripts/std.js | 11 +++++++++++ 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/scripts/components.js b/scripts/components.js index 6c2d5bb..76f5a1b 100644 --- a/scripts/components.js +++ b/scripts/components.js @@ -79,6 +79,8 @@ component.sprite = Object.copy(component, { _enghook: make_sprite, }); +Object.hide(component.sprite, 'rect'); + component.sprite.impl = { set path(x) { //cmd(12,this.id,prototypes.resani(this.gameobject.__proto__.toString(), x),this.rect); @@ -107,6 +109,7 @@ component.sprite.impl = { get drawmode() { return cmd(220,this.id); }, set drawmode(x) { cmd(219,this.id,x); }, emissive(x) { cmd(170, this.id, x); }, + sync() { this.path = this.path; }, pickm() { return this; }, move(d) { this.pos = this.pos.add(d); }, @@ -118,9 +121,14 @@ component.sprite.impl = { }, kill() { cmd(9,this.id); }, - dimensions() { return cmd(64,this.path); }, - width() { return cmd(64,this.path).x; }, - height() { return cmd(64,this.path).y; }, + dimensions() { + var dim = Resources.texture.dimensions(this.path); + dim.x *= (this.rect.s1-this.rect.s0); + dim.y *= (this.rect.t1-this.rect.t0); + return dim; + }, + width() { return this.dimensions().x; }, + height() { return this.dimensions().y; }, }; Object.freeze(sprite); @@ -160,7 +168,7 @@ var SpriteAnim = { var anim = {}; anim.frames = []; anim.path = path; - var frames = cmd(139,path); + var frames = Resources.gif.frames(path); var yslice = 1/frames; for (var f = 0; f < frames; f++) { var frame = {}; @@ -174,9 +182,10 @@ var SpriteAnim = { anim.frames.push(frame); } anim.loop = true; - var dim = cmd(64,path); + var dim = Resources.texture.dimensions(path); dim.y /= frames; anim.dim = dim; + anim.toJSON = function() { return {}; }; return anim; }, @@ -191,7 +200,7 @@ var SpriteAnim = { frame.time = time; anim.frames.push(frame); } - anim.dim = cmd(64,path); + anim.dim = Resources.texture.dimensions(path); anim.dim.x /= frames; return anim; }, @@ -267,12 +276,7 @@ component.char2dimpl = { }, anims:{}, - - sync() { - if (this.path) - cmd(12,this.id,this.path,this.rect); - }, - + acur:{}, frame: 0, play_anim(anim) { @@ -301,7 +305,8 @@ component.char2dimpl = { }, setsprite() { - cmd(12, this.id, this.acur.path, this.acur.frames[this.frame].rect); + this.rect = this.acur.frames[this.frame].rect; + this.path = this.path; }, advance() { @@ -340,9 +345,7 @@ component.char2dimpl = { }, }; -Object.assign(component.char2d, component.char2dimpl); - -component.char2d.doc = { +component.char2dimpl.doc = { doc: "An animation player for sprites.", frame: "The current frame of animation.", anims: "A list of all animations in this player.", @@ -357,6 +360,8 @@ component.char2d.doc = { add_anim: "Add an animation object with the given name." }; +Object.hide(component.char2dimpl, "doc"); + /* Returns points specifying this geometry, with ccw */ var Geometry = { box(w, h) { diff --git a/scripts/editor.js b/scripts/editor.js index 94a00a1..cda19bc 100644 --- a/scripts/editor.js +++ b/scripts/editor.js @@ -870,6 +870,7 @@ editor.inputs['M-t'] = function() { editor.edit_level.objects.forEach(function(x editor.inputs['M-t'].doc = "Unlock all objects in current level."; editor.inputs['C-n'] = function() { + return; }; editor.inputs['C-n'].doc = "Create an empty object."; diff --git a/scripts/entity.js b/scripts/entity.js index 996c102..d53c03d 100644 --- a/scripts/entity.js +++ b/scripts/entity.js @@ -480,10 +480,13 @@ var gameobject = { transform() { var t = {}; t.pos = this.pos; + if (t.pos.every(x=>x===0)) delete t.pos; t.angle = Math.places(this.angle,4); + if (t.angle === 0) delete t.angle; t.scale = this.scale; t.scale = t.scale.map((x,i) => x/this.__proto__.scale[i]); t.scale = t.scale.map(x => Math.places(x,3)); + if (t.scale.every(x=>x===1)) delete t.scale; return t; }, diff --git a/scripts/std.js b/scripts/std.js index 9ae50ec..08da0cc 100644 --- a/scripts/std.js +++ b/scripts/std.js @@ -23,6 +23,17 @@ Resources.is_sound = function(path) { return Resources.sounds.any(x => x === ext); } +Resources.is_animation = function(path) +{ + if (path.ext() === 'gif' && Resources.gif.frames(path) > 1) return true; +} + +Resources.texture = {}; +Resources.texture.dimensions = function(path) { return cmd(64,path); } + +Resources.gif = {}; +Resources.gif.frames = function(path) { return cmd(139,path); } + var Log = { set level(x) { cmd(92,x); }, get level() { return cmd(93); },