2023-04-22 16:44:26 -05:00
|
|
|
var component = {
|
2023-09-20 17:58:18 -05:00
|
|
|
components: [],
|
2023-04-22 16:44:26 -05:00
|
|
|
toString() {
|
|
|
|
if ('gameobject' in this)
|
|
|
|
return this.name + " on " + this.gameobject;
|
|
|
|
else
|
|
|
|
return this.name;
|
|
|
|
},
|
|
|
|
name: "component",
|
|
|
|
component: true,
|
2023-05-27 07:01:17 -05:00
|
|
|
enabled: true,
|
2023-04-22 16:44:26 -05:00
|
|
|
enable() { this.enabled = true; },
|
|
|
|
disable() { this.enabled = false; },
|
2023-10-10 17:37:58 -05:00
|
|
|
|
2023-12-24 11:50:01 -06:00
|
|
|
isComponent(c) {
|
|
|
|
if (typeof c !== 'object') return false;
|
|
|
|
if (typeof c.toString !== 'function') return false;
|
|
|
|
if (typeof c.make !== 'function') return false;
|
|
|
|
return (typeof component[c.toString()] === 'object');
|
|
|
|
},
|
|
|
|
|
2023-10-10 17:37:58 -05:00
|
|
|
make(go) {
|
|
|
|
var nc = Object.create(this);
|
2023-12-19 15:34:36 -06:00
|
|
|
nc.gameobject = go;
|
2024-03-20 16:07:23 -05:00
|
|
|
Object.mixin(nc, this._enghook(go));
|
2023-10-10 17:37:58 -05:00
|
|
|
assign_impl(nc,this.impl);
|
2024-03-14 09:33:15 -05:00
|
|
|
Object.hide(nc, 'gameobject', 'id');
|
2023-11-02 17:25:00 -05:00
|
|
|
nc.post();
|
2023-10-10 17:37:58 -05:00
|
|
|
return nc;
|
|
|
|
},
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2024-02-25 17:31:48 -06:00
|
|
|
kill() { console.info("Kill not created for this component yet"); },
|
2024-03-13 03:51:44 -05:00
|
|
|
sync(){},
|
2023-11-02 17:25:00 -05:00
|
|
|
post(){},
|
2024-03-13 03:51:44 -05:00
|
|
|
gui(){},
|
|
|
|
gizmo(){},
|
2023-04-22 16:44:26 -05:00
|
|
|
|
|
|
|
finish_center() {},
|
2023-12-19 15:34:36 -06:00
|
|
|
extend(spec) { return Object.copy(this, spec); },
|
2023-04-22 16:44:26 -05:00
|
|
|
};
|
|
|
|
|
2024-02-29 13:54:33 -06:00
|
|
|
var make_point_obj = function(o, p)
|
2024-02-25 17:31:48 -06:00
|
|
|
{
|
|
|
|
return {
|
|
|
|
pos: p,
|
|
|
|
move(d) {
|
|
|
|
d = o.gameobject.dir_world2this(d);
|
|
|
|
p.x += d.x;
|
|
|
|
p.y += d.y;
|
|
|
|
},
|
|
|
|
sync: o.sync.bind(o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 13:54:33 -06:00
|
|
|
var assign_impl = function(obj, impl)
|
2024-02-25 17:31:48 -06:00
|
|
|
{
|
|
|
|
var tmp = {};
|
|
|
|
for (var key of Object.keys(impl))
|
|
|
|
if (typeof obj[key] !== 'undefined' && typeof obj[key] !== 'function')
|
|
|
|
tmp[key] = obj[key];
|
|
|
|
|
|
|
|
Object.mixin(obj, impl);
|
|
|
|
|
|
|
|
for (var key in tmp)
|
|
|
|
obj[key] = tmp[key];
|
|
|
|
}
|
|
|
|
|
2024-03-13 03:51:44 -05:00
|
|
|
function json_from_whitelist(whitelist)
|
|
|
|
{
|
|
|
|
return function() {
|
|
|
|
var o = {};
|
|
|
|
for (var p of whitelist)
|
|
|
|
o[p] = this[p];
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
}
|
2024-01-01 14:48:58 -06:00
|
|
|
|
2024-03-19 14:39:19 -05:00
|
|
|
Object.mixin(os.sprite(true), {
|
2024-03-13 03:51:44 -05:00
|
|
|
toJSON:json_from_whitelist([
|
|
|
|
"path",
|
|
|
|
"pos",
|
|
|
|
"scale",
|
|
|
|
"angle",
|
|
|
|
"color",
|
|
|
|
"emissive",
|
|
|
|
"parallax",
|
|
|
|
"frame"
|
|
|
|
]),
|
|
|
|
anim:{},
|
|
|
|
playing: 0,
|
|
|
|
play(str) {
|
2024-03-26 07:53:36 -05:00
|
|
|
console.trace();
|
|
|
|
this.del_anim?.();
|
2024-03-13 03:51:44 -05:00
|
|
|
var sp = this;
|
2024-03-27 15:00:59 -05:00
|
|
|
var stop;
|
2024-03-21 21:07:24 -05:00
|
|
|
this.del_anim = function() {
|
|
|
|
sp = undefined;
|
|
|
|
advance = undefined;
|
2024-03-22 09:02:10 -05:00
|
|
|
this.del_anim = undefined;
|
|
|
|
stop();
|
2024-03-21 21:07:24 -05:00
|
|
|
}
|
2024-03-13 03:51:44 -05:00
|
|
|
str ??= 0;
|
|
|
|
var playing = this.anim[str];
|
2024-03-21 21:07:24 -05:00
|
|
|
if (!playing) return;
|
2024-03-13 03:51:44 -05:00
|
|
|
var f = 0;
|
2024-03-26 07:53:36 -05:00
|
|
|
sp.path = playing.path;
|
2024-03-13 03:51:44 -05:00
|
|
|
|
|
|
|
function advance() {
|
2024-03-21 21:07:24 -05:00
|
|
|
if (!sp) this.del_anim();
|
|
|
|
if (!sp.gameobject) return;
|
2024-03-26 07:53:36 -05:00
|
|
|
//sp.path = playing.path;
|
2024-03-13 03:51:44 -05:00
|
|
|
sp.frame = playing.frames[f].rect;
|
|
|
|
f = (f+1)%playing.frames.length;
|
2024-03-27 15:00:59 -05:00
|
|
|
if (f === 0) {
|
|
|
|
sp.anim_done?.();
|
|
|
|
if (!sp.loop) { sp.stop(); return; }
|
|
|
|
}
|
2024-03-22 09:02:10 -05:00
|
|
|
stop = sp.gameobject.delay(advance, playing.frames[f].time);
|
2023-12-29 19:08:53 -06:00
|
|
|
}
|
2024-03-26 07:53:36 -05:00
|
|
|
this.tex(game.texture(playing.path));
|
2024-03-13 03:51:44 -05:00
|
|
|
advance();
|
|
|
|
},
|
2024-03-22 09:02:10 -05:00
|
|
|
stop() {
|
|
|
|
this.del_anim?.();
|
|
|
|
},
|
2024-03-13 03:51:44 -05:00
|
|
|
set path(p) {
|
|
|
|
p = Resources.find_image(p);
|
2024-03-26 07:53:36 -05:00
|
|
|
if (!p) {
|
|
|
|
console.warn(`Could not find image ${p}.`);
|
|
|
|
return;
|
|
|
|
}
|
2024-03-13 03:51:44 -05:00
|
|
|
if (p === this.path) return;
|
2024-03-21 21:07:24 -05:00
|
|
|
this._p = p;
|
2024-03-26 07:53:36 -05:00
|
|
|
this.del_anim?.();
|
2024-03-22 09:02:10 -05:00
|
|
|
this.texture = game.texture(p);
|
|
|
|
this.tex(this.texture);
|
2024-03-21 21:07:24 -05:00
|
|
|
|
2024-03-13 03:51:44 -05:00
|
|
|
var anim = SpriteAnim.make(p);
|
|
|
|
if (!anim) return;
|
|
|
|
this.anim = anim;
|
|
|
|
this.play();
|
2024-03-26 07:53:36 -05:00
|
|
|
|
|
|
|
this.pos = this.dimensions().scale(this.anchor);
|
2023-09-26 17:07:51 -05:00
|
|
|
},
|
|
|
|
get path() {
|
2024-03-21 21:07:24 -05:00
|
|
|
return this._p;
|
2023-12-29 19:08:53 -06:00
|
|
|
},
|
2024-03-15 10:51:04 -05:00
|
|
|
kill() {
|
2024-03-21 21:07:24 -05:00
|
|
|
this.del_anim?.();
|
2024-03-15 10:51:04 -05:00
|
|
|
this.anim = undefined;
|
2024-03-21 21:07:24 -05:00
|
|
|
this.gameobject = undefined;
|
2024-03-15 10:51:04 -05:00
|
|
|
},
|
2023-10-10 17:37:58 -05:00
|
|
|
toString() { return "sprite"; },
|
2024-03-13 03:51:44 -05:00
|
|
|
move(d) { this.pos = this.pos.add(d); },
|
2024-03-05 22:23:38 -06:00
|
|
|
grow(x) {
|
|
|
|
this.scale = this.scale.scale(x);
|
|
|
|
this.pos = this.pos.scale(x);
|
|
|
|
},
|
2024-03-26 07:53:36 -05:00
|
|
|
anchor:[0,0],
|
2024-01-01 14:48:58 -06:00
|
|
|
sync() { },
|
2024-03-13 03:51:44 -05:00
|
|
|
pick() { return this; },
|
2023-09-20 17:58:18 -05:00
|
|
|
boundingbox() {
|
|
|
|
var dim = this.dimensions();
|
2023-12-20 17:20:29 -06:00
|
|
|
dim = dim.scale(this.gameobject.gscale());
|
|
|
|
var realpos = dim.scale(0.5).add(this.pos);
|
2024-02-29 13:54:33 -06:00
|
|
|
return bbox.fromcwh(realpos,dim);
|
2023-09-20 17:58:18 -05:00
|
|
|
},
|
2024-03-13 03:51:44 -05:00
|
|
|
|
2023-12-27 17:28:10 -06:00
|
|
|
dimensions() {
|
2024-03-22 09:02:10 -05:00
|
|
|
var dim = [this.texture.width, this.texture.height];
|
2024-03-13 03:51:44 -05:00
|
|
|
dim.x *= this.frame.w;
|
|
|
|
dim.y *= this.frame.h;
|
2023-12-27 17:28:10 -06:00
|
|
|
return dim;
|
|
|
|
},
|
|
|
|
width() { return this.dimensions().x; },
|
|
|
|
height() { return this.dimensions().y; },
|
2024-03-13 03:51:44 -05:00
|
|
|
});
|
|
|
|
|
2024-03-19 14:39:19 -05:00
|
|
|
os.sprite(true).make = function(go)
|
2024-03-13 03:51:44 -05:00
|
|
|
{
|
2024-03-19 14:39:19 -05:00
|
|
|
var sp = os.sprite();
|
2024-03-20 16:07:23 -05:00
|
|
|
sp.go = go;
|
2024-03-13 03:51:44 -05:00
|
|
|
sp.gameobject = go;
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
|
2024-03-19 14:39:19 -05:00
|
|
|
component.sprite = os.sprite(true);
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-10-04 17:57:37 -05:00
|
|
|
var sprite = component.sprite;
|
2023-09-20 13:33:11 -05:00
|
|
|
|
2023-10-23 08:08:11 -05:00
|
|
|
sprite.doc = {
|
|
|
|
path: "Path to the texture.",
|
|
|
|
color: "Color to mix with the sprite.",
|
|
|
|
pos: "The offset position of the sprite, relative to its entity."
|
|
|
|
};
|
|
|
|
|
2024-03-26 07:53:36 -05:00
|
|
|
sprite.setanchor = function(anch) {
|
2024-03-22 09:02:10 -05:00
|
|
|
var off = [0,0];
|
|
|
|
switch(anch) {
|
|
|
|
case "ll": break;
|
|
|
|
case "lm": off = [-0.5,0]; break;
|
|
|
|
case "lr": off = [-1,0]; break;
|
|
|
|
case "ml": off = [0,-0.5]; break;
|
|
|
|
case "mm": off = [-0.5,-0.5]; break;
|
|
|
|
case "mr": off = [-1,-0.5]; break;
|
|
|
|
case "ul": off = [0,-1]; break;
|
|
|
|
case "um": off = [-0.5,-1]; break;
|
|
|
|
case "ur": off = [-1,-1]; break;
|
|
|
|
}
|
2024-03-26 07:53:36 -05:00
|
|
|
this.anchor = off;
|
2024-03-22 09:02:10 -05:00
|
|
|
this.pos = this.dimensions().scale(off);
|
|
|
|
}
|
|
|
|
|
2023-09-13 16:49:22 -05:00
|
|
|
sprite.inputs = {};
|
2024-03-26 07:53:36 -05:00
|
|
|
sprite.inputs.kp9 = function() { this.setanchor("ll"); }
|
|
|
|
sprite.inputs.kp8 = function() { this.setanchor("lm"); }
|
|
|
|
sprite.inputs.kp7 = function() { this.setanchor("lr"); }
|
|
|
|
sprite.inputs.kp6 = function() { this.setanchor("ml"); }
|
|
|
|
sprite.inputs.kp5 = function() { this.setanchor("mm"); }
|
|
|
|
sprite.inputs.kp4 = function() { this.setanchor("mr"); }
|
|
|
|
sprite.inputs.kp3 = function() { this.setanchor("ur"); }
|
|
|
|
sprite.inputs.kp2 = function() { this.setanchor("um"); }
|
|
|
|
sprite.inputs.kp1 = function() { this.setanchor("ul"); }
|
2024-03-22 09:02:10 -05:00
|
|
|
|
2023-09-23 12:35:02 -05:00
|
|
|
Object.seal(sprite);
|
2023-09-13 16:49:22 -05:00
|
|
|
|
2023-12-29 19:08:53 -06:00
|
|
|
/* sprite anim returns a data structure for the given file path
|
|
|
|
frames: array of frames
|
|
|
|
rect: frame rectangle
|
|
|
|
time: miliseconds to hold the frame for
|
|
|
|
loop: true if it should be looped
|
|
|
|
*/
|
2024-03-26 07:53:36 -05:00
|
|
|
var animcache = {};
|
2023-11-01 15:33:22 -05:00
|
|
|
var SpriteAnim = {
|
2023-12-28 14:34:00 -06:00
|
|
|
make(path) {
|
2024-03-26 07:53:36 -05:00
|
|
|
if (animcache[path]) return animcache[path];
|
|
|
|
var anim;
|
|
|
|
if (io.exists(path.set_ext(".json")))
|
|
|
|
anim = SpriteAnim.aseprite(path.set_ext(".json"));
|
|
|
|
else if (path.ext() === 'gif')
|
|
|
|
anim = SpriteAnim.gif(path);
|
2024-01-01 14:48:58 -06:00
|
|
|
else if (path.ext() === 'ase')
|
2024-03-26 07:53:36 -05:00
|
|
|
anim = SpriteAnim.aseprite(path);
|
2023-12-29 19:08:53 -06:00
|
|
|
else
|
|
|
|
return undefined;
|
2024-03-26 07:53:36 -05:00
|
|
|
|
|
|
|
animcache[path] = anim;
|
|
|
|
console.spam(`Created animation like this:\n${json.encode(animcache[path])}`);
|
|
|
|
return animcache[path];
|
2023-12-28 14:34:00 -06:00
|
|
|
},
|
2023-11-01 15:33:22 -05:00
|
|
|
gif(path) {
|
2024-03-21 21:07:24 -05:00
|
|
|
console.info(`making an anim from ${path}`);
|
2023-11-01 15:33:22 -05:00
|
|
|
var anim = {};
|
|
|
|
anim.frames = [];
|
|
|
|
anim.path = path;
|
2024-03-21 21:07:24 -05:00
|
|
|
var tex = game.texture(path);
|
|
|
|
var frames = tex.frames;
|
|
|
|
console.info(`frames are ${frames}`);
|
|
|
|
if (frames === 1) return undefined;
|
2023-11-01 15:33:22 -05:00
|
|
|
var yslice = 1/frames;
|
|
|
|
for (var f = 0; f < frames; f++) {
|
|
|
|
var frame = {};
|
|
|
|
frame.rect = {
|
2024-03-13 03:51:44 -05:00
|
|
|
x: 0,
|
|
|
|
w: 1,
|
|
|
|
y: yslice*f,
|
|
|
|
h: yslice
|
2023-11-01 15:33:22 -05:00
|
|
|
};
|
|
|
|
frame.time = 0.05;
|
|
|
|
anim.frames.push(frame);
|
|
|
|
}
|
2024-03-21 21:07:24 -05:00
|
|
|
var times = tex.delays;
|
2023-12-29 19:08:53 -06:00
|
|
|
for (var i = 0; i < frames; i++)
|
|
|
|
anim.frames[i].time = times[i]/1000;
|
2023-11-01 15:33:22 -05:00
|
|
|
anim.loop = true;
|
2024-03-21 21:07:24 -05:00
|
|
|
var dim = [tex.width,tex.height];
|
|
|
|
console.info(`dimensions are ${dim}`);
|
2023-11-01 15:33:22 -05:00
|
|
|
dim.y /= frames;
|
|
|
|
anim.dim = dim;
|
2024-01-01 14:48:58 -06:00
|
|
|
return {0:anim};
|
2023-11-01 15:33:22 -05:00
|
|
|
},
|
2023-09-25 08:21:02 -05:00
|
|
|
|
2023-11-02 17:25:00 -05:00
|
|
|
strip(path, frames, time=0.05) {
|
2023-11-01 15:33:22 -05:00
|
|
|
var anim = {};
|
|
|
|
anim.frames = [];
|
|
|
|
anim.path = path;
|
|
|
|
var xslice = 1/frames;
|
|
|
|
for (var f = 0; f < frames; f++) {
|
|
|
|
var frame = {};
|
|
|
|
frame.rect = {s0:xslice*f, s1: xslice*(f+1), t0:0, t1:1};
|
2023-11-02 17:25:00 -05:00
|
|
|
frame.time = time;
|
2023-11-01 15:33:22 -05:00
|
|
|
anim.frames.push(frame);
|
|
|
|
}
|
2023-12-27 17:28:10 -06:00
|
|
|
anim.dim = Resources.texture.dimensions(path);
|
2023-11-01 15:33:22 -05:00
|
|
|
anim.dim.x /= frames;
|
|
|
|
return anim;
|
|
|
|
},
|
2023-09-25 08:21:02 -05:00
|
|
|
|
2023-11-01 15:33:22 -05:00
|
|
|
aseprite(path) {
|
2024-03-26 07:53:36 -05:00
|
|
|
function aseframeset2anim(frameset, meta) {
|
|
|
|
var anim = {};
|
|
|
|
anim.frames = [];
|
|
|
|
anim.path = path.dir() + "/" + meta.image;
|
|
|
|
var dim = meta.size;
|
|
|
|
|
|
|
|
var ase_make_frame = function(ase_frame) {
|
|
|
|
var f = ase_frame.frame;
|
|
|
|
var frame = {};
|
|
|
|
frame.rect = {
|
|
|
|
x: f.x/dim.w,
|
|
|
|
w: f.w/dim.w,
|
|
|
|
y: f.y/dim.h,
|
|
|
|
h: f.h/dim.h
|
|
|
|
};
|
|
|
|
frame.time = ase_frame.duration / 1000;
|
|
|
|
anim.frames.push(frame);
|
2023-11-01 15:33:22 -05:00
|
|
|
};
|
2023-09-25 08:21:02 -05:00
|
|
|
|
2024-03-26 07:53:36 -05:00
|
|
|
frameset.forEach(ase_make_frame);
|
|
|
|
anim.dim = frameset[0].sourceSize;
|
|
|
|
anim.loop = true;
|
|
|
|
return anim;
|
2023-09-24 11:26:44 -05:00
|
|
|
};
|
|
|
|
|
2024-03-26 07:53:36 -05:00
|
|
|
var data = json.decode(io.slurp(path));
|
2023-11-01 15:33:22 -05:00
|
|
|
var anims = {};
|
2024-03-26 07:53:36 -05:00
|
|
|
var frames = Array.isArray(data.frames) ? data.frames : Object.values(data.frames);
|
2024-01-01 14:48:58 -06:00
|
|
|
var f = 0;
|
2024-03-26 07:53:36 -05:00
|
|
|
for (var tag of data.meta.frameTags) {
|
|
|
|
anims[tag.name] = aseframeset2anim(frames.slice(tag.from, tag.to+1), data.meta);
|
2024-01-01 14:48:58 -06:00
|
|
|
anims[f] = anims[tag.name];
|
|
|
|
f++;
|
|
|
|
}
|
2023-11-01 15:33:22 -05:00
|
|
|
|
|
|
|
return anims;
|
|
|
|
},
|
2023-11-02 17:25:00 -05:00
|
|
|
|
|
|
|
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) {
|
2024-02-25 17:31:48 -06:00
|
|
|
if (!io.exists(path + ".asset")) return;
|
|
|
|
var asset = JSON.parse(io.slurp(path + ".asset"));
|
2023-11-02 17:25:00 -05:00
|
|
|
|
|
|
|
},
|
2023-11-01 15:33:22 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
SpriteAnim.doc = 'Functions to create Primum animations from varying sources.';
|
|
|
|
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.aseprite.doc = 'Given an aseprite json metadata, returns an object of animations defined in the aseprite file.';
|
2023-11-02 17:25:00 -05:00
|
|
|
SpriteAnim.find.doc = 'Given a path, find the relevant animation for the file.';
|
2023-09-24 11:26:44 -05:00
|
|
|
|
2023-04-22 16:44:26 -05:00
|
|
|
/* For all colliders, "shape" is a pointer to a phys2d_shape, "id" is a pointer to the shape data */
|
2023-09-20 17:58:18 -05:00
|
|
|
var collider2d = Object.copy(component, {
|
2023-04-22 16:44:26 -05:00
|
|
|
name: "collider 2d",
|
2023-05-25 21:55:55 -05:00
|
|
|
sensor: false,
|
2023-04-22 16:44:26 -05:00
|
|
|
|
|
|
|
kill() {}, /* No killing is necessary - it is done through the gameobject's kill */
|
|
|
|
|
2023-10-11 17:22:41 -05:00
|
|
|
impl: {
|
2024-03-19 17:00:49 -05:00
|
|
|
set sensor(x) { pshape.set_sensor(this.shape,x); },
|
|
|
|
get sensor() { return pshape.get_sensor(this.shape); },
|
|
|
|
set enabled(x) { pshape.set_enabled(this.shape,x); },
|
|
|
|
get enabled() { return pshape.get_enabled(this.shape); }
|
2023-10-11 17:22:41 -05:00
|
|
|
},
|
2023-04-22 16:44:26 -05:00
|
|
|
});
|
|
|
|
|
2023-10-31 08:31:56 -05:00
|
|
|
Object.hide(collider2d.impl, 'enabled');
|
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
collider2d.inputs = {};
|
|
|
|
collider2d.inputs['M-s'] = function() { this.sensor = !this.sensor; }
|
|
|
|
collider2d.inputs['M-s'].doc = "Toggle if this collider is a sensor.";
|
|
|
|
|
|
|
|
collider2d.inputs['M-t'] = function() { this.enabled = !this.enabled; }
|
|
|
|
collider2d.inputs['M-t'].doc = "Toggle if this collider is enabled.";
|
|
|
|
|
2023-09-20 17:58:18 -05:00
|
|
|
component.polygon2d = Object.copy(collider2d, {
|
2024-03-13 03:51:44 -05:00
|
|
|
toJSON:json_from_whitelist([
|
|
|
|
'points',
|
|
|
|
'sensor'
|
|
|
|
]),
|
2023-12-24 11:50:01 -06:00
|
|
|
toString() { return "polygon2d"; },
|
2023-10-10 17:37:58 -05:00
|
|
|
flipx: false,
|
|
|
|
flipy: false,
|
2023-09-23 12:35:02 -05:00
|
|
|
|
|
|
|
boundingbox() {
|
2024-02-27 10:09:15 -06:00
|
|
|
return bbox.frompoints(this.spoints());
|
2023-09-23 12:35:02 -05:00
|
|
|
},
|
2023-10-10 17:37:58 -05:00
|
|
|
|
|
|
|
hides: ['id', 'shape', 'gameobject'],
|
2024-03-20 09:04:28 -05:00
|
|
|
_enghook: os.make_poly2d,
|
2023-10-05 17:30:17 -05:00
|
|
|
points:[],
|
2023-11-16 09:27:04 -06:00
|
|
|
setpoints(points) {
|
|
|
|
this.points = points;
|
|
|
|
this.sync();
|
|
|
|
},
|
2023-10-05 17:30:17 -05:00
|
|
|
|
2023-05-27 07:01:17 -05:00
|
|
|
/* EDITOR */
|
2023-12-19 15:34:36 -06:00
|
|
|
spoints() {
|
2023-04-22 16:44:26 -05:00
|
|
|
var spoints = this.points.slice();
|
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
if (this.flipx) {
|
2023-04-22 16:44:26 -05:00
|
|
|
spoints.forEach(function(x) {
|
|
|
|
var newpoint = x.slice();
|
|
|
|
newpoint.x = -newpoint.x;
|
|
|
|
spoints.push(newpoint);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
if (this.flipy) {
|
2023-04-22 16:44:26 -05:00
|
|
|
spoints.forEach(function(x) {
|
|
|
|
var newpoint = x.slice();
|
|
|
|
newpoint.y = -newpoint.y;
|
|
|
|
spoints.push(newpoint);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return spoints;
|
|
|
|
},
|
|
|
|
|
|
|
|
gizmo() {
|
2024-02-25 17:31:48 -06:00
|
|
|
this.spoints().forEach(x => render.point(this.gameobject.this2screen(x), 3, Color.green));
|
2024-03-19 23:01:31 -05:00
|
|
|
this.points.forEach((x,i)=>render.coordinate(this.gameobject.this2screen(x), i));
|
2023-04-22 16:44:26 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
pick(pos) {
|
2023-10-05 17:30:17 -05:00
|
|
|
if (!Object.hasOwn(this,'points'))
|
|
|
|
this.points = deep_copy(this.__proto__.points);
|
|
|
|
|
2023-12-18 06:45:27 -06:00
|
|
|
var i = Gizmos.pick_gameobject_points(pos, this.gameobject, this.points);
|
|
|
|
var p = this.points[i];
|
2023-11-16 09:27:04 -06:00
|
|
|
if (p)
|
|
|
|
return make_point_obj(this, p);
|
2023-09-27 12:36:32 -05:00
|
|
|
|
|
|
|
return undefined;
|
2023-04-22 16:44:26 -05:00
|
|
|
},
|
2023-10-05 17:30:17 -05:00
|
|
|
});
|
|
|
|
|
2024-03-05 22:23:38 -06:00
|
|
|
function pointscaler(x) {
|
|
|
|
if (typeof x === 'number') return;
|
|
|
|
this.points = this.points.map(p => p.mult(x));
|
|
|
|
}
|
|
|
|
|
2023-10-31 08:31:56 -05:00
|
|
|
component.polygon2d.impl = Object.mix(collider2d.impl, {
|
2024-03-20 09:04:28 -05:00
|
|
|
sync() { poly2d.setverts(this.id,this.spoints()); },
|
2024-03-19 14:39:19 -05:00
|
|
|
query() { return physics.shape_query(this.shape); },
|
2024-03-05 22:23:38 -06:00
|
|
|
grow: pointscaler,
|
2023-10-31 08:31:56 -05:00
|
|
|
});
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-09-20 17:58:18 -05:00
|
|
|
var polygon2d = component.polygon2d;
|
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
polygon2d.inputs = {};
|
2023-12-27 07:04:18 -06:00
|
|
|
//polygon2d.inputs.post = function() { this.sync(); };
|
2023-08-25 01:30:39 -05:00
|
|
|
polygon2d.inputs.f10 = function() {
|
2024-02-25 17:31:48 -06:00
|
|
|
this.points = Math.sortpointsccw(this.points);
|
2023-08-25 01:30:39 -05:00
|
|
|
};
|
|
|
|
polygon2d.inputs.f10.doc = "Sort all points to be CCW order.";
|
|
|
|
|
|
|
|
polygon2d.inputs['C-lm'] = function() {
|
2024-03-11 22:23:02 -05:00
|
|
|
this.points.push(this.gameobject.world2this(Mouse.worldpos()));
|
2023-08-25 01:30:39 -05:00
|
|
|
};
|
|
|
|
polygon2d.inputs['C-lm'].doc = "Add a point to location of mouse.";
|
2023-10-06 12:38:49 -05:00
|
|
|
polygon2d.inputs.lm = function(){};
|
|
|
|
polygon2d.inputs.lm.released = function(){};
|
2023-08-25 01:30:39 -05:00
|
|
|
|
2023-11-17 15:16:13 -06:00
|
|
|
polygon2d.inputs['C-M-lm'] = function() {
|
2024-03-11 22:23:02 -05:00
|
|
|
var idx = Math.grab_from_points(Mouse.worldpos(), this.points.map(p => this.gameobject.this2world(p)), 25);
|
2023-08-25 01:30:39 -05:00
|
|
|
if (idx === -1) return;
|
|
|
|
this.points.splice(idx, 1);
|
|
|
|
};
|
2023-11-17 15:16:13 -06:00
|
|
|
polygon2d.inputs['C-M-lm'].doc = "Remove point under mouse.";
|
2023-08-25 01:30:39 -05:00
|
|
|
|
|
|
|
polygon2d.inputs['C-b'] = function() {
|
|
|
|
this.points = this.spoints;
|
|
|
|
this.flipx = false;
|
|
|
|
this.flipy = false;
|
|
|
|
};
|
|
|
|
polygon2d.inputs['C-b'].doc = "Freeze mirroring in place.";
|
|
|
|
|
2023-09-27 17:40:04 -05:00
|
|
|
component.edge2d = Object.copy(collider2d, {
|
2024-03-13 03:51:44 -05:00
|
|
|
toJSON:json_from_whitelist([
|
|
|
|
'sensor',
|
|
|
|
'thickness',
|
|
|
|
'points',
|
|
|
|
'hollow',
|
|
|
|
'hollowt',
|
|
|
|
]),
|
2023-04-22 16:44:26 -05:00
|
|
|
dimensions:2,
|
2023-10-05 17:30:17 -05:00
|
|
|
thickness:0,
|
2023-12-13 08:06:24 -06:00
|
|
|
/* if type === -1, point to point */
|
2023-12-12 08:46:27 -06:00
|
|
|
type: Spline.type.catmull,
|
2023-12-18 06:45:27 -06:00
|
|
|
C: 1, /* when in bezier, continuity required. 0, 1 or 2. */
|
2023-10-29 16:39:45 -05:00
|
|
|
looped: false,
|
2023-12-20 17:20:29 -06:00
|
|
|
angle: 0.5, /* smaller for smoother bezier */
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
flipx: false,
|
|
|
|
flipy: false,
|
2024-03-05 22:23:38 -06:00
|
|
|
points:[],
|
2023-10-05 17:30:17 -05:00
|
|
|
toString() { return "edge2d"; },
|
2023-04-22 16:44:26 -05:00
|
|
|
|
|
|
|
hollow: false,
|
2023-05-27 07:01:17 -05:00
|
|
|
hollowt: 0,
|
2023-10-29 16:39:45 -05:00
|
|
|
|
2023-10-12 17:05:49 -05:00
|
|
|
spoints() {
|
2024-03-05 22:23:38 -06:00
|
|
|
if (!this.points) return [];
|
|
|
|
var spoints = this.points.slice();
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
if (this.flipx) {
|
2023-12-20 17:20:29 -06:00
|
|
|
if (Spline.is_bezier(this.type))
|
|
|
|
spoints.push(Vector.reflect_point(spoints.at(-2), spoints.at(-1)));
|
|
|
|
|
|
|
|
for (var i = spoints.length-1; i >= 0; i--) {
|
2023-04-22 16:44:26 -05:00
|
|
|
var newpoint = spoints[i].slice();
|
|
|
|
newpoint.x = -newpoint.x;
|
|
|
|
spoints.push(newpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
if (this.flipy) {
|
2023-12-20 17:20:29 -06:00
|
|
|
if (Spline.is_bezier(this.type))
|
|
|
|
spoints.push(Vector.reflect(point(spoints.at(-2),spoints.at(-1))));
|
|
|
|
|
2023-04-22 16:44:26 -05:00
|
|
|
for (var i = spoints.length-1; i >= 0; i--) {
|
|
|
|
var newpoint = spoints[i].slice();
|
|
|
|
newpoint.y = -newpoint.y;
|
|
|
|
spoints.push(newpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.hollow) {
|
2024-03-20 14:32:35 -05:00
|
|
|
var hpoints = vector.inflate(spoints, this.hollowt);
|
2023-10-29 16:39:45 -05:00
|
|
|
if (hpoints.length === spoints.length) return spoints;
|
|
|
|
var arr1 = hpoints.filter(function(x,i) { return i % 2 === 0; });
|
|
|
|
var arr2 = hpoints.filter(function(x,i) { return i % 2 !== 0; });
|
|
|
|
return arr1.concat(arr2.reverse());
|
2023-04-22 16:44:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return spoints;
|
|
|
|
},
|
|
|
|
|
2023-11-16 09:27:04 -06:00
|
|
|
setpoints(points) {
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points = points;
|
2023-12-27 07:04:18 -06:00
|
|
|
// this.sync();
|
2023-11-16 09:27:04 -06:00
|
|
|
},
|
|
|
|
|
2023-11-06 07:05:27 -06:00
|
|
|
post() {
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points = [];
|
2023-11-06 07:05:27 -06:00
|
|
|
},
|
|
|
|
|
2023-12-13 08:06:24 -06:00
|
|
|
sample() {
|
2023-10-12 17:05:49 -05:00
|
|
|
var spoints = this.spoints();
|
2024-03-09 18:22:06 -06:00
|
|
|
if (spoints.length === 0) return [];
|
|
|
|
|
2023-12-13 08:06:24 -06:00
|
|
|
if (this.type === -1) {
|
|
|
|
if (this.looped) spoints.push(spoints[0]);
|
|
|
|
return spoints;
|
|
|
|
}
|
2023-12-13 19:53:09 -06:00
|
|
|
|
|
|
|
if (this.type === Spline.type.catmull) {
|
2023-12-18 06:45:27 -06:00
|
|
|
if (this.looped)
|
|
|
|
spoints = Spline.catmull_loop(spoints);
|
|
|
|
else
|
|
|
|
spoints = Spline.catmull_caps(spoints);
|
|
|
|
|
2023-12-13 19:53:09 -06:00
|
|
|
return Spline.sample_angle(this.type, spoints,this.angle);
|
2023-12-12 19:35:34 -06:00
|
|
|
}
|
2023-12-13 19:53:09 -06:00
|
|
|
|
2023-12-20 17:20:29 -06:00
|
|
|
if (this.looped && Spline.is_bezier(this.type))
|
|
|
|
spoints = Spline.bezier_loop(spoints);
|
|
|
|
|
2023-12-12 08:46:27 -06:00
|
|
|
return Spline.sample_angle(this.type, spoints, this.angle);
|
2023-09-27 17:40:04 -05:00
|
|
|
},
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2024-02-27 10:09:15 -06:00
|
|
|
boundingbox() { return bbox.frompoints(this.points.map(x => x.scale(this.gameobject.scale))); },
|
2023-09-27 17:40:04 -05:00
|
|
|
|
2023-10-16 09:40:43 -05:00
|
|
|
hides: ['gameobject', 'id', 'shape'],
|
2024-03-20 09:04:28 -05:00
|
|
|
_enghook: os.make_edge2d,
|
2023-10-11 17:22:41 -05:00
|
|
|
|
2023-05-27 07:01:17 -05:00
|
|
|
/* EDITOR */
|
2023-04-22 16:44:26 -05:00
|
|
|
gizmo() {
|
2023-12-27 10:34:14 -06:00
|
|
|
if (this.type === Spline.type.catmull || this.type === -1) {
|
2024-02-25 17:31:48 -06:00
|
|
|
this.spoints().forEach(x => render.point(this.gameobject.this2screen(x), 3, Color.teal));
|
2024-03-19 23:01:31 -05:00
|
|
|
this.points.forEach((x,i) => render.coordinate(this.gameobject.this2screen(x), i));
|
2023-12-15 12:45:09 -06:00
|
|
|
} else {
|
2024-03-05 22:23:38 -06:00
|
|
|
for (var i = 0; i < this.points.length; i += 3)
|
2024-03-19 23:01:31 -05:00
|
|
|
render.coordinate(this.gameobject.this2screen(this.points[i]), i, Color.teal);
|
2023-12-18 06:45:27 -06:00
|
|
|
|
2024-03-05 22:23:38 -06:00
|
|
|
for (var i = 1; i < this.points.length; i+=3) {
|
2024-03-19 23:01:31 -05:00
|
|
|
render.coordinate(this.gameobject.this2screen(this.points[i]), i, Color.green);
|
|
|
|
render.coordinate(this.gameobject.this2screen(this.points[i+1]), i+1, Color.green);
|
2024-03-05 22:23:38 -06:00
|
|
|
render.line([this.gameobject.this2screen(this.points[i-1]), this.gameobject.this2screen(this.points[i])], Color.yellow);
|
|
|
|
render.line([this.gameobject.this2screen(this.points[i+1]), this.gameobject.this2screen(this.points[i+2])], Color.yellow);
|
2023-12-15 12:45:09 -06:00
|
|
|
}
|
|
|
|
}
|
2023-04-22 16:44:26 -05:00
|
|
|
},
|
2023-05-27 07:01:17 -05:00
|
|
|
|
2024-03-05 22:23:38 -06:00
|
|
|
finish_center(change) { this.points = this.points.map(function(x) { return x.sub(change); }); },
|
2023-05-27 07:01:17 -05:00
|
|
|
|
2023-09-27 17:40:04 -05:00
|
|
|
pick(pos) {
|
2024-03-05 22:23:38 -06:00
|
|
|
var i = Gizmos.pick_gameobject_points(pos, this.gameobject, this.points);
|
|
|
|
var p = this.points[i];
|
2023-12-18 17:12:05 -06:00
|
|
|
if (!p) return undefined;
|
|
|
|
|
2023-12-27 10:34:14 -06:00
|
|
|
if (Spline.is_catmull(this.type) || this.type === -1)
|
2023-12-18 17:12:05 -06:00
|
|
|
return make_point_obj(this,p);
|
|
|
|
|
2023-12-18 06:45:27 -06:00
|
|
|
var that = this.gameobject;
|
|
|
|
var me = this;
|
|
|
|
if (p) {
|
|
|
|
var o = {
|
|
|
|
pos: p,
|
|
|
|
sync: me.sync.bind(me)
|
|
|
|
};
|
2024-03-05 22:23:38 -06:00
|
|
|
if (Spline.bezier_is_handle(this.points,i))
|
2023-12-18 06:45:27 -06:00
|
|
|
o.move = function(d) {
|
|
|
|
d = that.dir_world2this(d);
|
|
|
|
p.x += d.x;
|
|
|
|
p.y += d.y;
|
2024-03-05 22:23:38 -06:00
|
|
|
Spline.bezier_cp_mirror(me.points,i);
|
2023-12-18 06:45:27 -06:00
|
|
|
};
|
|
|
|
else
|
|
|
|
o.move = function(d) {
|
|
|
|
d = that.dir_world2this(d);
|
|
|
|
p.x += d.x;
|
|
|
|
p.y += d.y;
|
2024-03-05 22:23:38 -06:00
|
|
|
var pp = Spline.bezier_point_handles(me.points,i);
|
|
|
|
pp.forEach(ph => me.points[ph] = me.points[ph].add(d));
|
2023-12-18 06:45:27 -06:00
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
2023-09-27 17:40:04 -05:00
|
|
|
},
|
2023-10-29 16:39:45 -05:00
|
|
|
|
2023-12-19 15:34:36 -06:00
|
|
|
rm_node(idx) {
|
2024-03-05 22:23:38 -06:00
|
|
|
if (idx < 0 || idx >= this.points.length) return;
|
2023-12-19 15:34:36 -06:00
|
|
|
if (Spline.is_catmull(this.type))
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points.splice(idx,1);
|
2023-12-19 15:34:36 -06:00
|
|
|
|
|
|
|
if (Spline.is_bezier(this.type)) {
|
2024-03-11 15:11:39 -05:00
|
|
|
assert(Spline.bezier_is_node(this.points, idx), 'Attempted to delete a bezier handle.');
|
2023-12-19 15:34:36 -06:00
|
|
|
if (idx === 0)
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points.splice(idx,2);
|
|
|
|
else if (idx === this.points.length-1)
|
|
|
|
this.points.splice(this.points.length-2,2);
|
2023-12-19 15:34:36 -06:00
|
|
|
else
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points.splice(idx-1,3);
|
2023-12-19 15:34:36 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add_node(pos) {
|
|
|
|
pos = this.gameobject.world2this(pos);
|
|
|
|
var idx = 0;
|
2023-12-27 10:34:14 -06:00
|
|
|
if (Spline.is_catmull(this.type) || this.type === -1) {
|
2024-03-05 22:23:38 -06:00
|
|
|
if (this.points.length >= 2)
|
2024-03-19 14:39:19 -05:00
|
|
|
idx = physics.closest_point(pos, this.points, 400);
|
2023-12-19 15:34:36 -06:00
|
|
|
|
2024-03-05 22:23:38 -06:00
|
|
|
if (idx === this.points.length)
|
|
|
|
this.points.push(pos);
|
2023-12-19 15:34:36 -06:00
|
|
|
else
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points.splice(idx, 0, pos);
|
2023-12-19 15:34:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Spline.is_bezier(this.type)) {
|
2024-03-19 14:39:19 -05:00
|
|
|
idx = physics.closest_point(pos, Spline.bezier_nodes(this.points),400);
|
2023-12-21 10:49:44 -06:00
|
|
|
|
2023-12-19 15:34:36 -06:00
|
|
|
if (idx < 0) return;
|
|
|
|
|
2023-12-21 10:49:44 -06:00
|
|
|
if (idx === 0) {
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points.unshift(pos.slice(), pos.add([-100,0]), Vector.reflect_point(this.points[1], this.points[0]));
|
2023-12-21 10:49:44 -06:00
|
|
|
return;
|
|
|
|
}
|
2024-03-05 22:23:38 -06:00
|
|
|
if (idx === Spline.bezier_node_count(this.points)) {
|
|
|
|
this.points.push(Vector.reflect_point(this.points.at(-2), this.points.at(-1)), pos.add([-100,0]), pos.slice());
|
2023-12-21 10:49:44 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
idx = 2 + (idx-1)*3;
|
|
|
|
var adds = [pos.add([100,0]), pos.slice(), pos.add([-100,0])];
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points.splice(idx, 0, ...adds);
|
2023-12-19 15:34:36 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-10-29 16:39:45 -05:00
|
|
|
pick_all() {
|
|
|
|
var picks = [];
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points.forEach(x =>picks.push(make_point_obj(this,x)));
|
2023-10-29 16:39:45 -05:00
|
|
|
return picks;
|
|
|
|
},
|
2023-08-25 01:30:39 -05:00
|
|
|
});
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2024-03-05 22:23:38 -06:00
|
|
|
component.edge2d.impl = Object.mix(collider2d.impl, {
|
2024-03-20 09:04:28 -05:00
|
|
|
set thickness(x) { edge2d.set_thickness(this.id,x); },
|
|
|
|
get thickness() { return edge2d.get_thickness(this.id); },
|
2024-03-05 22:23:38 -06:00
|
|
|
grow: pointscaler,
|
2023-10-05 17:30:17 -05:00
|
|
|
sync() {
|
|
|
|
var sensor = this.sensor;
|
2023-12-13 08:06:24 -06:00
|
|
|
var points = this.sample();
|
2023-12-12 19:35:34 -06:00
|
|
|
if (!points) return;
|
2024-03-20 09:04:28 -05:00
|
|
|
edge2d.setverts(this.id,points);
|
2023-10-05 17:30:17 -05:00
|
|
|
this.sensor = sensor;
|
|
|
|
},
|
2024-03-05 22:23:38 -06:00
|
|
|
});
|
2023-10-05 17:30:17 -05:00
|
|
|
|
2023-09-27 17:40:04 -05:00
|
|
|
var bucket = component.edge2d;
|
2023-10-29 16:39:45 -05:00
|
|
|
bucket.spoints.doc = "Returns the controls points after modifiers are applied, such as it being hollow or mirrored on its axises.";
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs = {};
|
2023-12-27 07:04:18 -06:00
|
|
|
//bucket.inputs.post = function() { this.sync(); };
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs.h = function() { this.hollow = !this.hollow; };
|
|
|
|
bucket.inputs.h.doc = "Toggle hollow.";
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-10-29 16:39:45 -05:00
|
|
|
bucket.inputs['C-g'] = function() { if (this.hollowt > 0) this.hollowt--; };
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs['C-g'].doc = "Thin the hollow thickness.";
|
2023-10-29 16:39:45 -05:00
|
|
|
bucket.inputs['C-g'].rep = true;
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs['C-f'] = function() { this.hollowt++; };
|
|
|
|
bucket.inputs['C-f'].doc = "Increase the hollow thickness.";
|
2023-10-29 16:39:45 -05:00
|
|
|
bucket.inputs['C-f'].rep = true;
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-10-29 16:39:45 -05:00
|
|
|
bucket.inputs['M-v'] = function() { if (this.thickness > 0) this.thickness--; };
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs['M-v'].doc = "Decrease spline thickness.";
|
|
|
|
bucket.inputs['M-v'].rep = true;
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-10-29 16:39:45 -05:00
|
|
|
bucket.inputs['C-y'] = function() {
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points = this.spoints();
|
2023-08-25 01:30:39 -05:00
|
|
|
this.flipx = false;
|
|
|
|
this.flipy = false;
|
2023-10-29 16:39:45 -05:00
|
|
|
this.hollow = false;
|
2023-08-25 01:30:39 -05:00
|
|
|
};
|
2023-10-29 16:39:45 -05:00
|
|
|
bucket.inputs['C-y'].doc = "Freeze mirroring,";
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs['M-b'] = function() { this.thickness++; };
|
|
|
|
bucket.inputs['M-b'].doc = "Increase spline thickness.";
|
|
|
|
bucket.inputs['M-b'].rep = true;
|
|
|
|
|
2023-11-16 09:27:04 -06:00
|
|
|
bucket.inputs.plus = function() {
|
2023-12-13 08:06:24 -06:00
|
|
|
if (this.angle <= 1) {
|
|
|
|
this.angle = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.angle *= 0.9;
|
2023-11-16 09:27:04 -06:00
|
|
|
};
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs.plus.doc = "Increase the number of samples of this spline.";
|
|
|
|
bucket.inputs.plus.rep = true;
|
|
|
|
|
2023-12-13 08:06:24 -06:00
|
|
|
bucket.inputs.minus = function() { this.angle *= 1.1; };
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs.minus.doc = "Decrease the number of samples on this spline.";
|
|
|
|
bucket.inputs.minus.rep = true;
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2024-03-05 22:23:38 -06:00
|
|
|
bucket.inputs['C-r'] = function() { this.points = this.points.reverse(); };
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs['C-r'].doc = "Reverse the order of the spline's points.";
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-10-29 16:39:45 -05:00
|
|
|
bucket.inputs['C-l'] = function() { this.looped = !this.looped};
|
|
|
|
bucket.inputs['C-l'].doc = "Toggle spline being looped.";
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-12-18 06:45:27 -06:00
|
|
|
bucket.inputs['C-c'] = function() {
|
|
|
|
switch(this.type) {
|
|
|
|
case Spline.type.bezier:
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points = Spline.bezier2catmull(this.points);
|
2023-12-18 06:45:27 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.type = Spline.type.catmull;
|
|
|
|
};
|
|
|
|
|
2023-12-13 08:06:24 -06:00
|
|
|
bucket.inputs['C-c'].doc = "Set type of spline to catmull-rom.";
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-12-18 06:45:27 -06:00
|
|
|
bucket.inputs['C-b'] = function() {
|
|
|
|
switch(this.type) {
|
|
|
|
case Spline.type.catmull:
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points = Spline.catmull2bezier(Spline.catmull_caps(this.points));
|
2023-12-18 06:45:27 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.type = Spline.type.bezier;
|
|
|
|
};
|
2023-12-13 19:53:09 -06:00
|
|
|
|
2023-12-13 08:06:24 -06:00
|
|
|
bucket.inputs['C-o'] = function() { this.type = -1; };
|
|
|
|
bucket.inputs['C-o'].doc = "Set spline to linear.";
|
2023-10-29 16:39:45 -05:00
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs['C-M-lm'] = function() {
|
2023-12-19 15:34:36 -06:00
|
|
|
if (Spline.is_catmull(this.type)) {
|
2024-03-11 22:23:02 -05:00
|
|
|
var idx = Math.grab_from_points(Mouse.worldpos(), this.points.map(p => this.gameobject.this2world(p)), 25);
|
2023-12-19 15:34:36 -06:00
|
|
|
if (idx === -1) return;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
}
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points = this.points.newfirst(idx);
|
2023-08-25 01:30:39 -05:00
|
|
|
};
|
|
|
|
bucket.inputs['C-M-lm'].doc = "Select the given point as the '0' of this spline.";
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2024-03-11 22:23:02 -05:00
|
|
|
bucket.inputs['C-lm'] = function() { this.add_node(Mouse.worldpos()); }
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs['C-lm'].doc = "Add a point to the spline at the mouse position.";
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-11-17 15:16:13 -06:00
|
|
|
bucket.inputs['C-M-lm'] = function() {
|
2023-12-19 15:34:36 -06:00
|
|
|
var idx = -1;
|
|
|
|
if (Spline.is_catmull(this.type))
|
2024-03-11 22:23:02 -05:00
|
|
|
idx = Math.grab_from_points(Mouse.worldpos(), this.points.map(p => this.gameobject.this2world(p)), 25);
|
2023-12-19 15:34:36 -06:00
|
|
|
else {
|
2024-03-05 22:23:38 -06:00
|
|
|
var nodes = Spline.bezier_nodes(this.points);
|
2024-03-11 22:23:02 -05:00
|
|
|
idx = Math.grab_from_points(Mouse.worldpos(), nodes.map(p => this.gameobject.this2world(p)), 25);
|
2023-12-19 15:34:36 -06:00
|
|
|
idx *= 3;
|
|
|
|
}
|
2023-11-06 07:05:27 -06:00
|
|
|
|
2023-12-19 15:34:36 -06:00
|
|
|
this.rm_node(idx);
|
2023-08-25 01:30:39 -05:00
|
|
|
};
|
2023-11-17 15:16:13 -06:00
|
|
|
bucket.inputs['C-M-lm'].doc = "Remove point from the spline.";
|
|
|
|
|
|
|
|
bucket.inputs.lm = function(){};
|
|
|
|
bucket.inputs.lm.released = function(){};
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs.lb = function() {
|
|
|
|
var np = [];
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points.forEach(function(c) {
|
2023-08-25 01:30:39 -05:00
|
|
|
np.push(Vector.rotate(c, Math.deg2rad(-1)));
|
|
|
|
});
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points = np;
|
2023-08-25 01:30:39 -05:00
|
|
|
};
|
|
|
|
bucket.inputs.lb.doc = "Rotate the points CCW.";
|
|
|
|
bucket.inputs.lb.rep = true;
|
|
|
|
|
|
|
|
bucket.inputs.rb = function() {
|
|
|
|
var np = [];
|
|
|
|
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points.forEach(function(c) {
|
2023-08-25 01:30:39 -05:00
|
|
|
np.push(Vector.rotate(c, Math.deg2rad(1)));
|
|
|
|
});
|
|
|
|
|
2024-03-05 22:23:38 -06:00
|
|
|
this.points = np;
|
2023-08-25 01:30:39 -05:00
|
|
|
};
|
|
|
|
bucket.inputs.rb.doc = "Rotate the points CW.";
|
|
|
|
bucket.inputs.rb.rep = true;
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-09-20 17:58:18 -05:00
|
|
|
component.circle2d = Object.copy(collider2d, {
|
2023-10-04 17:57:37 -05:00
|
|
|
radius:10,
|
|
|
|
offset:[0,0],
|
|
|
|
toString() { return "circle2d"; },
|
|
|
|
|
2023-09-20 13:33:11 -05:00
|
|
|
boundingbox() {
|
2024-03-22 09:02:10 -05:00
|
|
|
return bbox.fromcwh([0,0], [this.radius,this.radius]);
|
2023-09-20 13:33:11 -05:00
|
|
|
},
|
2023-10-10 17:37:58 -05:00
|
|
|
|
|
|
|
hides: ['gameobject', 'id', 'shape', 'scale'],
|
2024-03-20 09:04:28 -05:00
|
|
|
_enghook: os.make_circle2d,
|
2023-09-20 17:58:18 -05:00
|
|
|
});
|
2023-05-01 20:58:10 -05:00
|
|
|
|
2023-10-31 08:31:56 -05:00
|
|
|
component.circle2d.impl = Object.mix({
|
2024-03-13 03:51:44 -05:00
|
|
|
toJSON:json_from_whitelist([
|
|
|
|
"pos",
|
|
|
|
"radius",
|
|
|
|
]),
|
|
|
|
|
2024-03-20 09:04:28 -05:00
|
|
|
set radius(x) { circle2d.set_radius(this.id,x); circle2d.sync(this.id); },
|
|
|
|
get radius() { return circle2d.get_radius(this.id); },
|
2023-10-11 17:22:41 -05:00
|
|
|
|
|
|
|
set scale(x) { this.radius = x; },
|
|
|
|
get scale() { return this.radius; },
|
|
|
|
|
2024-03-20 09:04:28 -05:00
|
|
|
set offset(x) { circle2d.set_offset(this.id,x); circle2d.sync(this.id); },
|
|
|
|
get offset() { circle2d.get_offset(this.id); },
|
2023-12-21 10:49:44 -06:00
|
|
|
|
2024-03-20 09:04:28 -05:00
|
|
|
get pos() { return this.offset; },
|
|
|
|
set pos(x) { this.offset = x; },
|
2024-03-05 22:23:38 -06:00
|
|
|
|
|
|
|
grow(x) {
|
|
|
|
if (typeof x === 'number') this.scale *= x;
|
|
|
|
else if (typeof x === 'object') this.scale *= x[0];
|
|
|
|
},
|
2023-12-21 10:49:44 -06:00
|
|
|
|
2023-12-29 19:08:53 -06:00
|
|
|
}, collider2d.impl);
|
2024-02-27 10:09:15 -06:00
|
|
|
|
2024-03-13 03:51:44 -05:00
|
|
|
return {component, SpriteAnim};
|