2023-10-10 17:37:58 -05:00
|
|
|
function assign_impl(obj, impl)
|
|
|
|
{
|
|
|
|
var tmp = {};
|
|
|
|
for (var key in impl)
|
|
|
|
if (typeof obj[key] !== 'undefined' && typeof obj[key] !== 'function')
|
|
|
|
tmp[key] = obj[key];
|
|
|
|
|
|
|
|
Object.mixin(obj, impl);
|
|
|
|
|
2023-10-26 11:48:02 -05:00
|
|
|
if (obj.sync) obj.sync();
|
|
|
|
|
2023-10-10 17:37:58 -05:00
|
|
|
for (var key in tmp)
|
|
|
|
obj[key] = tmp[key];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
hides: ['gameobject', 'id'],
|
|
|
|
|
|
|
|
make(go) {
|
|
|
|
var nc = Object.create(this);
|
|
|
|
nc.gameobject = go;
|
|
|
|
Object.assign(nc, this._enghook(go.body));
|
2023-10-26 11:48:02 -05:00
|
|
|
nc.sync();
|
2023-10-10 17:37:58 -05:00
|
|
|
assign_impl(nc,this.impl);
|
|
|
|
Object.hide(nc, ...this.hides);
|
|
|
|
return nc;
|
|
|
|
},
|
2023-04-22 16:44:26 -05:00
|
|
|
|
|
|
|
kill() { Log.info("Kill not created for this component yet"); },
|
2023-10-10 17:37:58 -05:00
|
|
|
sync() {},
|
2023-04-22 16:44:26 -05:00
|
|
|
gui() { },
|
|
|
|
gizmo() { },
|
|
|
|
|
|
|
|
prepare_center() {},
|
|
|
|
finish_center() {},
|
2023-09-20 17:58:18 -05:00
|
|
|
extend(spec) {
|
|
|
|
return Object.copy(this, spec);
|
2023-04-22 16:44:26 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-10-04 17:57:37 -05:00
|
|
|
component.sprite = Object.copy(component, {
|
2023-09-21 12:50:39 -05:00
|
|
|
pos:[0,0],
|
|
|
|
color:[1,1,1],
|
|
|
|
layer:0,
|
|
|
|
enabled:true,
|
2023-09-26 17:07:51 -05:00
|
|
|
path: "",
|
2023-10-05 08:02:12 -05:00
|
|
|
rect: {s0:0, s1: 1, t0: 0, t1: 1},
|
2023-09-21 12:50:39 -05:00
|
|
|
toString() { return "sprite"; },
|
2023-10-10 17:37:58 -05:00
|
|
|
_enghook: make_sprite,
|
2023-10-04 17:57:37 -05:00
|
|
|
});
|
2023-09-21 12:50:39 -05:00
|
|
|
|
2023-10-04 17:57:37 -05:00
|
|
|
component.sprite.impl = {
|
2023-09-26 17:07:51 -05:00
|
|
|
set path(x) {
|
2023-10-05 17:30:17 -05:00
|
|
|
cmd(12,this.id,prototypes.resani(this.gameobject.__proto__.toString(), x),this.rect);
|
2023-09-26 17:07:51 -05:00
|
|
|
},
|
|
|
|
get path() {
|
2023-10-05 17:30:17 -05:00
|
|
|
return prototypes.resavi(this.gameobject.__proto__.toString(), cmd(116,this.id));
|
2023-09-26 17:07:51 -05:00
|
|
|
},
|
2023-10-10 17:37:58 -05:00
|
|
|
toString() { return "sprite"; },
|
2023-10-02 17:03:01 -05:00
|
|
|
hide() { this.enabled = false; },
|
|
|
|
show() { this.enabled = true; },
|
2023-09-21 12:50:39 -05:00
|
|
|
asset(str) { this.path = str; },
|
2023-09-20 17:58:18 -05:00
|
|
|
get enabled() { return cmd(114,this.id); },
|
|
|
|
set enabled(x) { cmd(20,this.id,x); },
|
|
|
|
set color(x) { cmd(96,this.id,x); },
|
|
|
|
get color() {return undefined; },
|
|
|
|
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 undefined; },
|
|
|
|
|
|
|
|
boundingbox() {
|
2023-09-21 12:50:39 -05:00
|
|
|
return cwh2bb([0,0],[0,0]);
|
2023-09-20 17:58:18 -05:00
|
|
|
var dim = this.dimensions();
|
|
|
|
dim = dim.scale(this.gameobject.scale);
|
|
|
|
var realpos = this.pos.copy();
|
|
|
|
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); },
|
2023-09-14 17:37:04 -05:00
|
|
|
dimensions() { return cmd(64,this.path); },
|
|
|
|
width() { return cmd(64,this.path).x; },
|
|
|
|
height() { return cmd(64,this.path).y; },
|
2023-10-04 17:57:37 -05:00
|
|
|
};
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-09-23 12:35:02 -05:00
|
|
|
Object.freeze(sprite);
|
|
|
|
|
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."
|
|
|
|
};
|
|
|
|
|
2023-09-13 16:49:22 -05:00
|
|
|
sprite.inputs = {};
|
|
|
|
sprite.inputs.kp9 = function() { this.pos = [0,0]; };
|
|
|
|
sprite.inputs.kp8 = function() { this.pos = [-0.5, 0]; };
|
|
|
|
sprite.inputs.kp7 = function() { this.pos = [-1,0]; };
|
|
|
|
sprite.inputs.kp6 = function() { this.pos = [0,-0.5]; };
|
|
|
|
sprite.inputs.kp5 = function() { this.pos = [-0.5,-0.5]; };
|
|
|
|
sprite.inputs.kp4 = function() { this.pos = [-1,-0.5]; };
|
|
|
|
sprite.inputs.kp3 = function() { this.pos = [0, -1]; };
|
|
|
|
sprite.inputs.kp2 = function() { this.pos = [-0.5,-1]; };
|
|
|
|
sprite.inputs.kp1 = function() { this.pos = [-1,-1]; };
|
2023-09-23 12:35:02 -05:00
|
|
|
Object.seal(sprite);
|
2023-09-13 16:49:22 -05:00
|
|
|
|
2023-09-25 08:21:02 -05:00
|
|
|
var aseframeset2anim = function(frameset, meta)
|
|
|
|
{
|
|
|
|
var anim = {};
|
|
|
|
anim.frames = [];
|
|
|
|
anim.path = meta.image;
|
|
|
|
var dim = meta.size;
|
|
|
|
|
|
|
|
var ase_make_frame = function(ase_frame,i) {
|
|
|
|
var f = ase_frame.frame;
|
|
|
|
var frame = {};
|
|
|
|
frame.rect = {
|
|
|
|
s0: f.x/dim.w,
|
|
|
|
s1: (f.x+f.w)/dim.w,
|
|
|
|
t0: f.y/dim.h,
|
|
|
|
t1: (f.y+f.h)/dim.h
|
|
|
|
};
|
|
|
|
frame.time = ase_frame.duration / 1000;
|
|
|
|
anim.frames.push(frame);
|
|
|
|
};
|
|
|
|
|
|
|
|
frameset.forEach(ase_make_frame);
|
2023-09-25 16:34:48 -05:00
|
|
|
anim.dim = [frameset[0].sourceSize.x, frameset[0].sourceSize.y];
|
2023-09-25 08:21:02 -05:00
|
|
|
anim.loop = true;
|
|
|
|
return anim;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ase2anim = function(ase)
|
|
|
|
{
|
|
|
|
var json = IO.slurp(ase);
|
|
|
|
json = JSON.parse(json);
|
|
|
|
var frames = Array.isArray(json.frames) ? json.frames : Object.values(json.frames);
|
|
|
|
return aseframeset2anim(json.frames, json.meta);
|
|
|
|
}
|
|
|
|
|
|
|
|
var ase2anims = function(ase)
|
|
|
|
{
|
|
|
|
var json = IO.slurp(ase);
|
|
|
|
json = JSON.parse(json);
|
|
|
|
var anims = {};
|
|
|
|
var frames = Array.isArray(json.frames) ? json.frames : Object.values(json.frames);
|
|
|
|
for (var tag of json.meta.frameTags)
|
|
|
|
anims[tag.name] = aseframeset2anim(frames.slice(tag.from, tag.to+1), json.meta);
|
|
|
|
|
|
|
|
return anims;
|
|
|
|
}
|
|
|
|
|
2023-09-24 11:26:44 -05:00
|
|
|
var gif2anim = function(gif)
|
|
|
|
{
|
|
|
|
var anim = {};
|
|
|
|
anim.frames = [];
|
|
|
|
anim.path = gif;
|
|
|
|
var frames = cmd(139,gif);
|
|
|
|
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;
|
2023-09-25 16:34:48 -05:00
|
|
|
var dim = cmd(64,gif);
|
|
|
|
dim.y /= frames;
|
|
|
|
anim.dim = dim;
|
2023-09-24 11:26:44 -05:00
|
|
|
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);
|
|
|
|
}
|
2023-09-25 16:34:48 -05:00
|
|
|
anim.dim = cmd(64,strip);
|
|
|
|
anim.dim.x /= frames;
|
2023-09-24 11:26:44 -05:00
|
|
|
return anim;
|
|
|
|
}
|
|
|
|
|
2023-04-22 16:44:26 -05:00
|
|
|
/* Container to play sprites and anim2ds */
|
2023-09-20 17:58:18 -05:00
|
|
|
component.char2d = Object.copy(sprite, {
|
2023-09-23 12:35:02 -05:00
|
|
|
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; },
|
2023-09-24 11:26:44 -05:00
|
|
|
|
|
|
|
boundingbox() {
|
2023-09-25 16:34:48 -05:00
|
|
|
var dim = this.curplaying.dim.slice();
|
2023-09-24 11:26:44 -05:00
|
|
|
dim = dim.scale(this.gameobject.scale);
|
2023-09-25 16:34:48 -05:00
|
|
|
var realpos = this.pos.slice();
|
|
|
|
realpos.x = realpos.x * dim.x + (dim.x/2);
|
|
|
|
realpos.y = realpos.y * dim.y + (dim.y/2);
|
2023-09-24 11:26:44 -05:00
|
|
|
return cwh2bb(realpos,dim);
|
|
|
|
},
|
|
|
|
|
|
|
|
sync() {
|
|
|
|
if (this.path)
|
|
|
|
cmd(12,this.id,this.path,this.rect);
|
|
|
|
},
|
|
|
|
|
|
|
|
kill() { cmd(9,this.id); },
|
2023-09-23 12:35:02 -05:00
|
|
|
ur: {
|
|
|
|
|
|
|
|
},
|
2023-09-24 11:26:44 -05:00
|
|
|
|
2023-09-23 12:35:02 -05:00
|
|
|
make(go) {
|
|
|
|
var char = Object.create(this);
|
2023-09-25 16:34:48 -05:00
|
|
|
char.gameobject = go;
|
|
|
|
Object.assign(char, make_sprite(go.body));
|
2023-04-22 16:44:26 -05:00
|
|
|
char.frame = 0;
|
2023-09-24 11:26:44 -05:00
|
|
|
char.timer = timer.make(char.advance.bind(char), 1);
|
2023-04-22 16:44:26 -05:00
|
|
|
char.timer.loop = true;
|
|
|
|
return char;
|
|
|
|
},
|
|
|
|
|
|
|
|
frame: 0,
|
|
|
|
|
|
|
|
play(name) {
|
2023-09-24 11:26:44 -05:00
|
|
|
if (!(name in this)) {
|
2023-04-22 16:44:26 -05:00
|
|
|
Log.info("Can't find an animation named " + name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-24 11:26:44 -05:00
|
|
|
if (this.curplaying === this[name]) {
|
2023-04-28 12:49:18 -05:00
|
|
|
this.timer.start();
|
|
|
|
return;
|
|
|
|
}
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-09-24 11:26:44 -05:00
|
|
|
this.curplaying = this[name];
|
2023-04-22 16:44:26 -05:00
|
|
|
this.frame = 0;
|
2023-09-24 11:26:44 -05:00
|
|
|
this.timer.time = this.curplaying.frames[this.frame].time;
|
|
|
|
this.timer.start();
|
2023-04-22 16:44:26 -05:00
|
|
|
this.setsprite();
|
|
|
|
},
|
|
|
|
|
|
|
|
setsprite() {
|
2023-09-24 11:26:44 -05:00
|
|
|
cmd(12, this.id, this.curplaying.path, this.curplaying.frames[this.frame].rect);
|
2023-04-22 16:44:26 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
advance() {
|
2023-09-24 11:26:44 -05:00
|
|
|
this.frame = (this.frame + 1) % this.curplaying.frames.length;
|
2023-04-22 16:44:26 -05:00
|
|
|
this.setsprite();
|
2023-04-28 12:49:18 -05:00
|
|
|
|
|
|
|
if (this.frame === 0 && !this.curplaying.loop)
|
|
|
|
this.timer.pause();
|
2023-04-22 16:44:26 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
devance() {
|
|
|
|
this.frame = (this.frame - 1);
|
|
|
|
if (this.frame === -1) this.frame = this.curplaying.frames-1;
|
|
|
|
this.setsprite();
|
|
|
|
},
|
2023-04-28 12:49:18 -05:00
|
|
|
|
|
|
|
setframe(frame) {
|
|
|
|
this.frame = frame;
|
|
|
|
this.setsprite();
|
|
|
|
},
|
2023-04-22 16:44:26 -05:00
|
|
|
|
|
|
|
pause() {
|
|
|
|
this.timer.pause();
|
|
|
|
},
|
|
|
|
|
|
|
|
stop() {
|
2023-04-28 12:49:18 -05:00
|
|
|
this.setframe(0);
|
2023-04-22 16:44:26 -05:00
|
|
|
this.timer.stop();
|
|
|
|
},
|
|
|
|
|
|
|
|
kill() {
|
|
|
|
this.timer.kill();
|
|
|
|
cmd(9, this.id);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-05-27 07:01:17 -05:00
|
|
|
/* Returns points specifying this geometry, with ccw */
|
|
|
|
var Geometry = {
|
|
|
|
box(w, h) {
|
|
|
|
w /= 2;
|
|
|
|
h /= 2;
|
|
|
|
|
|
|
|
var points = [
|
|
|
|
[w,h],
|
|
|
|
[-w,h],
|
|
|
|
[-w,-h],
|
|
|
|
[w,-h]
|
|
|
|
];
|
|
|
|
|
|
|
|
return points;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
register_hit(fn, obj) {
|
|
|
|
register_collide(1, fn, obj, this.gameobject.body, this.shape);
|
|
|
|
},
|
2023-10-11 17:22:41 -05:00
|
|
|
|
|
|
|
impl: {
|
|
|
|
set sensor(x) { cmd(18,this.shape,x); },
|
2023-05-27 07:01:17 -05:00
|
|
|
get sensor() { return cmd(21,this.shape); },
|
2023-10-11 17:22:41 -05:00
|
|
|
// set enabled(x) { cmd(22,this.shape,x); },
|
|
|
|
// get enabled() { return cmd(23,this.shape); }
|
|
|
|
},
|
|
|
|
|
2023-04-22 16:44:26 -05:00
|
|
|
});
|
|
|
|
|
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, {
|
2023-10-05 17:30:17 -05:00
|
|
|
toString() { return "poly2d"; },
|
2023-10-10 17:37:58 -05:00
|
|
|
flipx: false,
|
|
|
|
flipy: false,
|
2023-09-23 12:35:02 -05:00
|
|
|
|
|
|
|
boundingbox() {
|
|
|
|
return points2bb(this.spoints);
|
|
|
|
},
|
2023-10-10 17:37:58 -05:00
|
|
|
|
|
|
|
hides: ['id', 'shape', 'gameobject'],
|
|
|
|
_enghook: make_poly2d,
|
2023-10-05 17:30:17 -05:00
|
|
|
points:[],
|
|
|
|
|
2023-05-27 07:01:17 -05:00
|
|
|
/* EDITOR */
|
2023-04-22 16:44:26 -05:00
|
|
|
get spoints() {
|
|
|
|
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() {
|
|
|
|
this.spoints.forEach(function(x) {
|
|
|
|
Debug.point(world2screen(this.gameobject.this2world(x)), 3, Color.green);
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
this.points.forEach(function(x, i) {
|
|
|
|
Debug.numbered_point(this.gameobject.this2world(x), i);
|
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
pick(pos) {
|
2023-10-05 17:30:17 -05:00
|
|
|
if (!Object.hasOwn(this,'points'))
|
|
|
|
this.points = deep_copy(this.__proto__.points);
|
|
|
|
|
2023-09-27 12:36:32 -05:00
|
|
|
var p = Gizmos.pick_gameobject_points(pos, this.gameobject, this.points);
|
|
|
|
if (p) {
|
|
|
|
return {
|
|
|
|
set pos(n) {
|
|
|
|
p.x = n.x;
|
|
|
|
p.y = n.y;
|
|
|
|
},
|
|
|
|
get pos() { return p; },
|
|
|
|
sync: this.sync.bind(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
2023-04-22 16:44:26 -05:00
|
|
|
},
|
2023-10-05 17:30:17 -05:00
|
|
|
});
|
|
|
|
|
2023-10-16 09:40:43 -05:00
|
|
|
component.polygon2d.impl = {
|
|
|
|
set sensor(x) { cmd(18,this.shape,x); },
|
|
|
|
get sensor() { return cmd(21,this.shape); },
|
|
|
|
|
2023-10-05 17:30:17 -05:00
|
|
|
sync() {
|
|
|
|
cmd_poly2d(0, this.id, this.spoints);
|
|
|
|
},
|
2023-04-22 16:44:26 -05:00
|
|
|
query() {
|
|
|
|
return cmd(80, this.shape);
|
|
|
|
},
|
2023-10-16 09:40:43 -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-09-13 16:49:22 -05:00
|
|
|
polygon2d.inputs.post = function() { this.sync(); };
|
2023-08-25 01:30:39 -05:00
|
|
|
polygon2d.inputs.f10 = function() {
|
|
|
|
this.points = sortpointsccw(this.points);
|
|
|
|
};
|
|
|
|
polygon2d.inputs.f10.doc = "Sort all points to be CCW order.";
|
|
|
|
|
|
|
|
polygon2d.inputs['C-lm'] = function() {
|
|
|
|
this.points.push(this.gameobject.world2this(Mouse.worldpos));
|
|
|
|
};
|
|
|
|
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
|
|
|
|
|
|
|
polygon2d.inputs['S-lm'] = function() {
|
2023-09-13 16:49:22 -05:00
|
|
|
var idx = 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);
|
|
|
|
};
|
|
|
|
polygon2d.inputs['S-lm'].doc = "Remove point under mouse.";
|
|
|
|
|
|
|
|
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-10-05 17:30:17 -05:00
|
|
|
//Object.freeze(polygon2d);
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-09-27 17:40:04 -05:00
|
|
|
component.edge2d = Object.copy(collider2d, {
|
2023-04-22 16:44:26 -05:00
|
|
|
dimensions:2,
|
2023-10-05 17:30:17 -05:00
|
|
|
thickness:0,
|
2023-04-22 16:44:26 -05:00
|
|
|
/* open: 0
|
|
|
|
clamped: 1
|
|
|
|
beziers: 2
|
|
|
|
looped: 3
|
|
|
|
*/
|
2023-10-29 16:39:45 -05:00
|
|
|
type: Spline.type.clamped,
|
|
|
|
looped: false,
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
flipx: false,
|
|
|
|
flipy: false,
|
2023-10-12 17:05:49 -05:00
|
|
|
cpoints:[],
|
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() {
|
|
|
|
if (!this.cpoints) return [];
|
2023-04-22 16:44:26 -05:00
|
|
|
var spoints = this.cpoints.slice();
|
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
if (this.flipx) {
|
2023-04-22 16:44:26 -05:00
|
|
|
for (var i = spoints.length-1; i >= 0; i--) {
|
|
|
|
var newpoint = spoints[i].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
|
|
|
for (var i = spoints.length-1; i >= 0; i--) {
|
|
|
|
var newpoint = spoints[i].slice();
|
|
|
|
newpoint.y = -newpoint.y;
|
|
|
|
spoints.push(newpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.hollow) {
|
2023-10-29 16:39:45 -05:00
|
|
|
var hpoints = inflate_cpv(spoints, spoints.length, this.hollowt);
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
|
|
|
sample(n) {
|
2023-10-12 17:05:49 -05:00
|
|
|
var spoints = this.spoints();
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-10-29 16:39:45 -05:00
|
|
|
var degrees = 2;
|
|
|
|
|
|
|
|
if (n < spoints.length) n = spoints.length;
|
2023-04-22 16:44:26 -05:00
|
|
|
|
|
|
|
if (spoints.length === 2)
|
|
|
|
return spoints;
|
|
|
|
if (spoints.length < 2)
|
|
|
|
return [];
|
2023-10-29 16:39:45 -05:00
|
|
|
if (this.samples === spoints.length) {
|
|
|
|
if (this.looped) return spoints.wrapped(1);
|
2023-04-22 16:44:26 -05:00
|
|
|
return spoints;
|
|
|
|
}
|
2023-10-29 16:39:45 -05:00
|
|
|
|
2023-04-22 16:44:26 -05:00
|
|
|
/*
|
|
|
|
order = degrees+1
|
|
|
|
knots = spoints.length + order
|
|
|
|
assert knots%order != 0
|
|
|
|
*/
|
|
|
|
|
2023-10-29 16:39:45 -05:00
|
|
|
if (this.looped)
|
|
|
|
return Spline.sample(degrees, this.dimensions, Spline.type.open, spoints.wrapped(this.degrees), n);
|
2023-10-12 17:05:49 -05:00
|
|
|
|
2023-10-29 16:39:45 -05:00
|
|
|
return Spline.sample(degrees, this.dimensions, this.type, spoints, n);
|
2023-09-27 17:40:04 -05:00
|
|
|
},
|
2023-04-22 16:44:26 -05:00
|
|
|
|
|
|
|
samples: 10,
|
|
|
|
|
2023-09-27 17:40:04 -05:00
|
|
|
boundingbox() {
|
|
|
|
return points2bb(this.points.map(x => x.scale(this.gameobject.scale)));
|
|
|
|
},
|
|
|
|
|
2023-10-16 09:40:43 -05:00
|
|
|
hides: ['gameobject', 'id', 'shape'],
|
2023-10-11 17:22:41 -05:00
|
|
|
_enghook: make_edge2d,
|
|
|
|
|
2023-05-27 07:01:17 -05:00
|
|
|
/* EDITOR */
|
2023-04-22 16:44:26 -05:00
|
|
|
gizmo() {
|
2023-10-12 17:05:49 -05:00
|
|
|
this.spoints().forEach(function(x) {
|
2023-04-22 16:44:26 -05:00
|
|
|
Debug.point(world2screen(this.gameobject.this2world(x)), 3, Color.green);
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
this.cpoints.forEach(function(x, i) {
|
|
|
|
Debug.numbered_point(this.gameobject.this2world(x), i);
|
|
|
|
}, this);
|
|
|
|
},
|
2023-05-27 07:01:17 -05:00
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
finish_center(change) {
|
|
|
|
this.cpoints = this.cpoints.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) {
|
|
|
|
var p = Gizmos.pick_gameobject_points(pos, this.gameobject, this.cpoints);
|
|
|
|
if (p) return {
|
|
|
|
set pos(n) { p.x = n.x; p.y = n.y; },
|
|
|
|
get pos() { return p; },
|
|
|
|
sync: this.sync.bind(this),
|
|
|
|
};
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
},
|
2023-10-29 16:39:45 -05:00
|
|
|
|
|
|
|
pick_all() {
|
|
|
|
var picks = [];
|
|
|
|
this.cpoints.forEach(function(x) {
|
|
|
|
picks.push({
|
|
|
|
set pos(n) { x.x = n.x; x.y = n.y; },
|
|
|
|
get pos() { return x; },
|
|
|
|
sync: this.sync.bind(this),
|
|
|
|
});
|
|
|
|
}, this);
|
|
|
|
return picks;
|
|
|
|
},
|
2023-08-25 01:30:39 -05:00
|
|
|
});
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-10-16 09:40:43 -05:00
|
|
|
component.edge2d.impl = {
|
2023-10-26 11:48:02 -05:00
|
|
|
set sensor(x) { cmd(18,this.shape,x); },
|
|
|
|
get sensor() { return cmd(21,this.shape); },
|
2023-10-16 09:40:43 -05:00
|
|
|
|
2023-10-05 17:30:17 -05:00
|
|
|
set thickness(x) {
|
|
|
|
cmd_edge2d(1,this.id,x);
|
|
|
|
},
|
|
|
|
get thickness() { return cmd(112,this.id); },
|
|
|
|
sync() {
|
2023-10-29 16:39:45 -05:00
|
|
|
if (this.samples < this.spoints().length) this.samples = this.spoints().length;
|
2023-10-05 17:30:17 -05:00
|
|
|
var sensor = this.sensor;
|
2023-10-11 17:22:41 -05:00
|
|
|
var points = this.sample(this.samples);
|
|
|
|
cmd_edge2d(0,this.id,points);
|
2023-10-05 17:30:17 -05:00
|
|
|
this.sensor = sensor;
|
|
|
|
},
|
2023-10-16 09:40:43 -05: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-10-29 16:39:45 -05: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() {
|
2023-10-12 17:05:49 -05:00
|
|
|
this.cpoints = 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;
|
|
|
|
|
|
|
|
bucket.inputs.plus = function() { this.samples++; };
|
|
|
|
bucket.inputs.plus.doc = "Increase the number of samples of this spline.";
|
|
|
|
bucket.inputs.plus.rep = true;
|
|
|
|
|
2023-10-29 16:39:45 -05:00
|
|
|
bucket.inputs.minus = function() { if (this.samples > this.spoints().length) this.samples--;};
|
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
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs['C-r'] = function() { this.cpoints = this.cpoints.reverse(); };
|
|
|
|
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-10-29 16:39:45 -05:00
|
|
|
bucket.inputs['C-c'] = function() { this.type = Spline.type.clamped; };
|
|
|
|
bucket.inputs['C-c'].doc = "Set type of spline to clamped.";
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-10-29 16:39:45 -05:00
|
|
|
bucket.inputs['C-o'] = function() { this.type = Spline.type.open; };
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs['C-o'].doc = "Set spline to open.";
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-10-29 16:39:45 -05:00
|
|
|
bucket.inputs['C-b'] = function() { this.type = Spline.type.bezier; };
|
|
|
|
bucket.inputs['C-b'].doc = "Set spline to bezier.";
|
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs['C-M-lm'] = function() {
|
|
|
|
var idx = grab_from_points(Mouse.worldpos, this.cpoints.map(this.gameobject.world2this,this.gameobject), 25);
|
|
|
|
if (idx === -1) return;
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
this.cpoints = this.cpoints.newfirst(idx);
|
|
|
|
};
|
|
|
|
bucket.inputs['C-M-lm'].doc = "Select the given point as the '0' of this spline.";
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-10-11 17:22:41 -05:00
|
|
|
bucket.inputs.lm = function(){};
|
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
bucket.inputs['C-lm'] = function() {
|
|
|
|
var idx = 0;
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
if (this.cpoints.length >= 2) {
|
|
|
|
idx = cmd(59, screen2world(Mouse.pos).sub(this.gameobject.pos), this.cpoints, 1000);
|
|
|
|
if (idx === -1) return;
|
|
|
|
}
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
if (idx === this.cpoints.length)
|
|
|
|
this.cpoints.push(this.gameobject.world2this(screen2world(Mouse.pos)));
|
|
|
|
else
|
|
|
|
this.cpoints.splice(idx, 0, this.gameobject.world2this(screen2world(Mouse.pos)));
|
|
|
|
};
|
|
|
|
bucket.inputs['C-lm'].doc = "Add a point to the spline at the mouse position.";
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-10-11 17:22:41 -05:00
|
|
|
bucket.inputs['C-M-lm'] = function() {
|
|
|
|
var idx = cmd(59, Mouse.worldpos.sub(this.gameobject.pos), this.cpoints, 250);
|
|
|
|
Log.warn(idx);
|
|
|
|
// var idx = grab_from_points(screen2world(Mouse.pos), this.cpoints.map(function(x) {return x.add(this.gameobject.pos); }, this), 25);
|
2023-08-25 01:30:39 -05:00
|
|
|
if (idx === -1) return;
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
this.cpoints.splice(idx, 1);
|
|
|
|
};
|
2023-10-11 17:22:41 -05:00
|
|
|
bucket.inputs['C-M-lm'].doc = "Remove point from the spline.";
|
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
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
this.cpoints.forEach(function(c) {
|
|
|
|
np.push(Vector.rotate(c, Math.deg2rad(-1)));
|
|
|
|
});
|
2023-04-22 16:44:26 -05:00
|
|
|
|
2023-08-25 01:30:39 -05:00
|
|
|
this.cpoints = np;
|
|
|
|
};
|
|
|
|
bucket.inputs.lb.doc = "Rotate the points CCW.";
|
|
|
|
bucket.inputs.lb.rep = true;
|
|
|
|
|
|
|
|
bucket.inputs.rb = function() {
|
|
|
|
var np = [];
|
|
|
|
|
|
|
|
this.cpoints.forEach(function(c) {
|
|
|
|
np.push(Vector.rotate(c, Math.deg2rad(1)));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.cpoints = np;
|
|
|
|
};
|
|
|
|
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() {
|
|
|
|
var diameter = this.radius*2*this.gameobject.scale;
|
|
|
|
return cwh2bb(this.offset.scale(this.gameobject.scale), [this.radius,this.radius]);
|
|
|
|
},
|
2023-10-10 17:37:58 -05:00
|
|
|
|
|
|
|
hides: ['gameobject', 'id', 'shape', 'scale'],
|
|
|
|
_enghook: make_circle2d,
|
2023-09-20 17:58:18 -05:00
|
|
|
});
|
2023-05-01 20:58:10 -05:00
|
|
|
|
2023-10-16 09:40:43 -05:00
|
|
|
component.circle2d.impl = {
|
|
|
|
set sensor(x) { cmd(18,this.shape,x); },
|
|
|
|
get sensor() { return cmd(21,this.shape); },
|
|
|
|
|
2023-10-11 17:22:41 -05:00
|
|
|
set radius(x) { cmd_circle2d(0,this.id,x); },
|
|
|
|
get radius() { return cmd_circle2d(2,this.id); },
|
|
|
|
|
|
|
|
set scale(x) { this.radius = x; },
|
|
|
|
get scale() { return this.radius; },
|
|
|
|
|
|
|
|
set offset(x) { cmd_circle2d(1,this.id,x); },
|
|
|
|
get offset() { return cmd_circle2d(3,this.id); },
|
2023-10-16 09:40:43 -05:00
|
|
|
};
|
2023-10-11 17:22:41 -05:00
|
|
|
|
2023-05-01 20:58:10 -05:00
|
|
|
/* ASSETS */
|
|
|
|
|
|
|
|
var Texture = {
|
|
|
|
mipmaps(path, x) {
|
|
|
|
cmd(94, path, x);
|
|
|
|
},
|
|
|
|
|
|
|
|
sprite(path, x) {
|
|
|
|
cmd(95, path, x);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
var Resources = {
|
|
|
|
load(path) {
|
|
|
|
if (path in this)
|
|
|
|
return this[path];
|
|
|
|
|
|
|
|
var src = {};
|
|
|
|
this[path] = src;
|
|
|
|
src.path = path;
|
|
|
|
|
|
|
|
if (!IO.exists(`${path}.asset`))
|
|
|
|
return this[path];
|
|
|
|
|
|
|
|
var data = JSON.parse(IO.slurp(`${path}.asset`));
|
|
|
|
Object.assign(src,data);
|
|
|
|
return this[path];
|
|
|
|
|
|
|
|
},
|
|
|
|
};
|