Simplify components in js
This commit is contained in:
parent
eadfd1dc38
commit
33fdd97bdd
1
Makefile
1
Makefile
|
@ -132,6 +132,7 @@ else
|
||||||
CXXFLAGS += -std=c++11
|
CXXFLAGS += -std=c++11
|
||||||
LDFLAGS += -framework Cocoa -framework QuartzCore -framework AudioToolbox -framework Metal -framework MetalKit
|
LDFLAGS += -framework Cocoa -framework QuartzCore -framework AudioToolbox -framework Metal -framework MetalKit
|
||||||
INFO :=$(INFO)_macos
|
INFO :=$(INFO)_macos
|
||||||
|
STEAMAPI := steam_api
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
|
@ -358,13 +358,6 @@ Object.readonly = function(o, name, msg)
|
||||||
Object.defineProperty(o, name, prop);
|
Object.defineProperty(o, name, prop);
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.extend = function(from)
|
|
||||||
{
|
|
||||||
var n = {};
|
|
||||||
Object.mixin(n, from);
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.mixin = function(target, source)
|
Object.mixin = function(target, source)
|
||||||
{
|
{
|
||||||
if (typeof source !== 'object') return target;
|
if (typeof source !== 'object') return target;
|
||||||
|
@ -810,6 +803,13 @@ Object.defineProperty(String.prototype, 'name', {
|
||||||
return this.fromlast('/').tolast('.'); }
|
return this.fromlast('/').tolast('.'); }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(String.prototype, 'set_name', {
|
||||||
|
value: function(name) {
|
||||||
|
var dir = this.dir();
|
||||||
|
return this.dir() + name + "." + this.ext();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Object.defineProperty(String.prototype, 'base', {
|
Object.defineProperty(String.prototype, 'base', {
|
||||||
value: function() { return this.fromlast('/'); }
|
value: function() { return this.fromlast('/'); }
|
||||||
});
|
});
|
||||||
|
@ -1337,16 +1337,19 @@ Math.snap = function(val, grid) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Math.angledist = function (a1, a2) {
|
Math.angledist = function (a1, a2) {
|
||||||
a1 = Math.turn2deg(a1);
|
a1 = a1%1;
|
||||||
a2 = Math.turn2deg(a2);
|
a2 = a2%1;
|
||||||
var dist = a2 - a1;
|
var dist = a2 - a1;
|
||||||
var wrap = dist >= 0 ? dist+360 : dist-360;
|
if (dist == 0) return dist;
|
||||||
wrap %= 360;
|
|
||||||
|
|
||||||
if (Math.abs(dist) < Math.abs(wrap))
|
if (dist > 0) {
|
||||||
return Math.deg2turn(dist);
|
if (dist > 0.5) return dist-1;
|
||||||
|
return dist;
|
||||||
|
}
|
||||||
|
|
||||||
return Math.deg2turn(wrap);
|
if (dist < -0.5) return dist+1;
|
||||||
|
|
||||||
|
return dist;
|
||||||
};
|
};
|
||||||
Math.angledist.doc = "Find the shortest angle between two angles.";
|
Math.angledist.doc = "Find the shortest angle between two angles.";
|
||||||
Math.TAU = Math.PI*2;
|
Math.TAU = Math.PI*2;
|
||||||
|
@ -1544,8 +1547,8 @@ Math.sortpointsccw = function(points)
|
||||||
var cm = points2cm(points);
|
var cm = points2cm(points);
|
||||||
var cmpoints = points.map(function(x) { return x.sub(cm); });
|
var cmpoints = points.map(function(x) { return x.sub(cm); });
|
||||||
var ccw = cmpoints.sort(function(a,b) {
|
var ccw = cmpoints.sort(function(a,b) {
|
||||||
aatan = Math.atan2(a.y, a.x);
|
var aatan = Math.atan2(a.y, a.x);
|
||||||
batan = Math.atan2(b.y, b.x);
|
var batan = Math.atan2(b.y, b.x);
|
||||||
return aatan - batan;
|
return aatan - batan;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1604,6 +1607,8 @@ yaml.tojson = function(yaml)
|
||||||
return yaml;
|
return yaml;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Math.sign = function(n) { return n >= 0 ? 1 : -1; }
|
||||||
|
|
||||||
return {
|
return {
|
||||||
convert,
|
convert,
|
||||||
time,
|
time,
|
||||||
|
|
|
@ -1,44 +1,4 @@
|
||||||
var component = {
|
var component = {};
|
||||||
components: [],
|
|
||||||
toString() {
|
|
||||||
if ('gameobject' in this)
|
|
||||||
return this.name + " on " + this.gameobject;
|
|
||||||
else
|
|
||||||
return this.name;
|
|
||||||
},
|
|
||||||
name: "component",
|
|
||||||
component: true,
|
|
||||||
enabled: true,
|
|
||||||
enable() { this.enabled = true; },
|
|
||||||
disable() { this.enabled = false; },
|
|
||||||
|
|
||||||
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');
|
|
||||||
},
|
|
||||||
|
|
||||||
make(go) {
|
|
||||||
var nc = Object.create(this);
|
|
||||||
nc.gameobject = go;
|
|
||||||
Object.mixin(nc, this._enghook(go, nc));
|
|
||||||
assign_impl(nc,this.impl);
|
|
||||||
Object.hide(nc, 'gameobject', 'id');
|
|
||||||
nc.post();
|
|
||||||
nc.make = undefined;
|
|
||||||
return nc;
|
|
||||||
},
|
|
||||||
|
|
||||||
kill() { console.info("Kill not created for this component yet"); },
|
|
||||||
sync(){},
|
|
||||||
post(){},
|
|
||||||
gui(){},
|
|
||||||
gizmo(){},
|
|
||||||
|
|
||||||
finish_center() {},
|
|
||||||
extend(spec) { return Object.copy(this, spec); },
|
|
||||||
};
|
|
||||||
|
|
||||||
var make_point_obj = function(o, p)
|
var make_point_obj = function(o, p)
|
||||||
{
|
{
|
||||||
|
@ -53,41 +13,8 @@ var make_point_obj = function(o, p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var assign_impl = function(obj, impl)
|
|
||||||
{
|
|
||||||
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];
|
|
||||||
}
|
|
||||||
|
|
||||||
function json_from_whitelist(whitelist)
|
|
||||||
{
|
|
||||||
return function() {
|
|
||||||
var o = {};
|
|
||||||
for (var p of whitelist)
|
|
||||||
o[p] = this[p];
|
|
||||||
return o;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var sprite = {
|
var sprite = {
|
||||||
loop: true,
|
loop: true,
|
||||||
toJSON:json_from_whitelist([
|
|
||||||
"path",
|
|
||||||
"pos",
|
|
||||||
"scale",
|
|
||||||
"angle",
|
|
||||||
"color",
|
|
||||||
"emissive",
|
|
||||||
"parallax",
|
|
||||||
"frame"
|
|
||||||
]),
|
|
||||||
anim:{},
|
anim:{},
|
||||||
playing: 0,
|
playing: 0,
|
||||||
play(str = 0) {
|
play(str = 0) {
|
||||||
|
@ -155,7 +82,6 @@ var sprite = {
|
||||||
this.anim_done = undefined;
|
this.anim_done = undefined;
|
||||||
delete allsprites[this.guid];
|
delete allsprites[this.guid];
|
||||||
},
|
},
|
||||||
toString() { return "sprite"; },
|
|
||||||
move(d) { this.pos = this.pos.add(d); },
|
move(d) { this.pos = this.pos.add(d); },
|
||||||
grow(x) {
|
grow(x) {
|
||||||
this.scale = this.scale.scale(x);
|
this.scale = this.scale.scale(x);
|
||||||
|
@ -181,15 +107,6 @@ var sprite = {
|
||||||
height() { return this.dimensions().y; },
|
height() { return this.dimensions().y; },
|
||||||
};
|
};
|
||||||
globalThis.allsprites = {};
|
globalThis.allsprites = {};
|
||||||
sprite.make = function(go)
|
|
||||||
{
|
|
||||||
var sp = Object.create(sprite);
|
|
||||||
sp.go = go;
|
|
||||||
sp.gameobject = go;
|
|
||||||
sp.guid = prosperon.guid();
|
|
||||||
allsprites[sp.guid] = sp;
|
|
||||||
return sp;
|
|
||||||
}
|
|
||||||
|
|
||||||
sprite.doc = {
|
sprite.doc = {
|
||||||
path: "Path to the texture.",
|
path: "Path to the texture.",
|
||||||
|
@ -225,9 +142,18 @@ sprite.inputs.kp3 = function() { this.setanchor("ur"); }
|
||||||
sprite.inputs.kp2 = function() { this.setanchor("um"); }
|
sprite.inputs.kp2 = function() { this.setanchor("um"); }
|
||||||
sprite.inputs.kp1 = function() { this.setanchor("ul"); }
|
sprite.inputs.kp1 = function() { this.setanchor("ul"); }
|
||||||
|
|
||||||
component.sprite = sprite;
|
component.sprite = function(obj) {
|
||||||
|
var sp = Object.create(sprite);
|
||||||
|
sp.gameobject = obj;
|
||||||
|
sp.guid = prosperon.guid();
|
||||||
|
allsprites[sp.guid] = sp;
|
||||||
|
return sp;
|
||||||
|
}
|
||||||
|
sprite.shade = [1,1,1,1];
|
||||||
|
|
||||||
//Object.seal(sprite);
|
Object.mixin(os.make_seg2d(), {
|
||||||
|
sync() { this.set_endpoints(this.points[0], this.points[1]); }
|
||||||
|
});
|
||||||
|
|
||||||
/* sprite anim returns a data structure for the given file path
|
/* sprite anim returns a data structure for the given file path
|
||||||
frames: array of frames
|
frames: array of frames
|
||||||
|
@ -364,18 +290,7 @@ SpriteAnim.strip.doc = 'Given a path and number of frames, converts a horizontal
|
||||||
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.';
|
SpriteAnim.find.doc = 'Given a path, find the relevant animation for the file.';
|
||||||
|
|
||||||
/* For all colliders, "shape" is a pointer to a phys2d_shape, "id" is a pointer to the shape data */
|
var collider2d = {};
|
||||||
var collider2d = Object.copy(component, {
|
|
||||||
impl: {
|
|
||||||
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); }
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
Object.hide(collider2d.impl, 'enabled');
|
|
||||||
|
|
||||||
collider2d.inputs = {};
|
collider2d.inputs = {};
|
||||||
collider2d.inputs['M-s'] = function() { this.sensor = !this.sensor; }
|
collider2d.inputs['M-s'] = function() { this.sensor = !this.sensor; }
|
||||||
collider2d.inputs['M-s'].doc = "Toggle if this collider is a sensor.";
|
collider2d.inputs['M-s'].doc = "Toggle if this collider is a sensor.";
|
||||||
|
@ -383,27 +298,11 @@ 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'] = function() { this.enabled = !this.enabled; }
|
||||||
collider2d.inputs['M-t'].doc = "Toggle if this collider is enabled.";
|
collider2d.inputs['M-t'].doc = "Toggle if this collider is enabled.";
|
||||||
|
|
||||||
component.polygon2d = Object.copy(collider2d, {
|
Object.mix(os.make_poly2d(), {
|
||||||
toJSON:json_from_whitelist([
|
|
||||||
'points',
|
|
||||||
'sensor'
|
|
||||||
]),
|
|
||||||
toString() { return "polygon2d"; },
|
|
||||||
flipx: false,
|
|
||||||
flipy: false,
|
|
||||||
|
|
||||||
boundingbox() {
|
boundingbox() {
|
||||||
return bbox.frompoints(this.spoints());
|
return bbox.frompoints(this.spoints());
|
||||||
},
|
},
|
||||||
|
|
||||||
hides: ['id', 'shape', 'gameobject'],
|
|
||||||
_enghook: os.make_poly2d,
|
|
||||||
points:[],
|
|
||||||
setpoints(points) {
|
|
||||||
this.points = points;
|
|
||||||
this.sync();
|
|
||||||
},
|
|
||||||
|
|
||||||
/* EDITOR */
|
/* EDITOR */
|
||||||
spoints() {
|
spoints() {
|
||||||
var spoints = this.points.slice();
|
var spoints = this.points.slice();
|
||||||
|
@ -411,7 +310,7 @@ component.polygon2d = Object.copy(collider2d, {
|
||||||
if (this.flipx) {
|
if (this.flipx) {
|
||||||
spoints.forEach(function(x) {
|
spoints.forEach(function(x) {
|
||||||
var newpoint = x.slice();
|
var newpoint = x.slice();
|
||||||
newpoint.x = -newpoint.x;
|
newpoint.x = -newpoint.x;
|
||||||
spoints.push(newpoint);
|
spoints.push(newpoint);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -419,8 +318,8 @@ component.polygon2d = Object.copy(collider2d, {
|
||||||
if (this.flipy) {
|
if (this.flipy) {
|
||||||
spoints.forEach(function(x) {
|
spoints.forEach(function(x) {
|
||||||
var newpoint = x.slice();
|
var newpoint = x.slice();
|
||||||
newpoint.y = -newpoint.y;
|
newpoint.y = -newpoint.y;
|
||||||
spoints.push(newpoint);
|
spoints.push(newpoint);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return spoints;
|
return spoints;
|
||||||
|
@ -449,63 +348,71 @@ function pointscaler(x) {
|
||||||
this.points = this.points.map(p => p.mult(x));
|
this.points = this.points.map(p => p.mult(x));
|
||||||
}
|
}
|
||||||
|
|
||||||
component.polygon2d.impl = Object.mix(collider2d.impl, {
|
Object.mixin(os.make_poly2d(), {
|
||||||
sync() { poly2d.setverts(this.id,this.spoints()); },
|
sync() {
|
||||||
query() { return physics.shape_query(this.shape); },
|
this.setverts(this.points);
|
||||||
|
},
|
||||||
grow: pointscaler,
|
grow: pointscaler,
|
||||||
});
|
});
|
||||||
|
|
||||||
var polygon2d = component.polygon2d;
|
var polyinputs = Object.create(collider2d.inputs);
|
||||||
|
os.make_poly2d().inputs = polyinputs;
|
||||||
|
|
||||||
polygon2d.inputs = {};
|
polyinputs = {};
|
||||||
//polygon2d.inputs.post = function() { this.sync(); };
|
polyinputs.f10 = function() {
|
||||||
polygon2d.inputs.f10 = function() {
|
|
||||||
this.points = Math.sortpointsccw(this.points);
|
this.points = Math.sortpointsccw(this.points);
|
||||||
};
|
};
|
||||||
polygon2d.inputs.f10.doc = "Sort all points to be CCW order.";
|
polyinputs.f10.doc = "Sort all points to be CCW order.";
|
||||||
|
|
||||||
polygon2d.inputs['C-lm'] = function() {
|
polyinputs['C-lm'] = function() {
|
||||||
this.points.push(this.gameobject.world2this(input.mouse.worldpos()));
|
this.points.push(this.gameobject.world2this(input.mouse.worldpos()));
|
||||||
};
|
};
|
||||||
polygon2d.inputs['C-lm'].doc = "Add a point to location of mouse.";
|
polyinputs['C-lm'].doc = "Add a point to location of mouse.";
|
||||||
polygon2d.inputs.lm = function(){};
|
polyinputs.lm = function(){};
|
||||||
polygon2d.inputs.lm.released = function(){};
|
polyinputs.lm.released = function(){};
|
||||||
|
|
||||||
polygon2d.inputs['C-M-lm'] = function() {
|
polyinputs['C-M-lm'] = function() {
|
||||||
var idx = Math.grab_from_points(input.mouse.worldpos(), this.points.map(p => this.gameobject.this2world(p)), 25);
|
var idx = Math.grab_from_points(input.mouse.worldpos(), this.points.map(p => this.gameobject.this2world(p)), 25);
|
||||||
if (idx === -1) return;
|
if (idx === -1) return;
|
||||||
this.points.splice(idx, 1);
|
this.points.splice(idx, 1);
|
||||||
};
|
};
|
||||||
polygon2d.inputs['C-M-lm'].doc = "Remove point under mouse.";
|
polyinputs['C-M-lm'].doc = "Remove point under mouse.";
|
||||||
|
|
||||||
polygon2d.inputs['C-b'] = function() {
|
polyinputs['C-b'] = function() {
|
||||||
this.points = this.spoints;
|
this.points = this.spoints;
|
||||||
this.flipx = false;
|
this.flipx = false;
|
||||||
this.flipy = false;
|
this.flipy = false;
|
||||||
};
|
};
|
||||||
polygon2d.inputs['C-b'].doc = "Freeze mirroring in place.";
|
polyinputs['C-b'].doc = "Freeze mirroring in place.";
|
||||||
|
|
||||||
component.edge2d = Object.copy(collider2d, {
|
var edge2d = {
|
||||||
toJSON:json_from_whitelist([
|
|
||||||
'sensor',
|
|
||||||
'thickness',
|
|
||||||
'points',
|
|
||||||
'hollow',
|
|
||||||
'hollowt',
|
|
||||||
'angle',
|
|
||||||
]),
|
|
||||||
dimensions:2,
|
dimensions:2,
|
||||||
thickness:0,
|
thickness:1,
|
||||||
/* if type === -1, point to point */
|
/* if type === -1, point to point */
|
||||||
type: Spline.type.catmull,
|
type: Spline.type.catmull,
|
||||||
C: 1, /* when in bezier, continuity required. 0, 1 or 2. */
|
C: 1, /* when in bezier, continuity required. 0, 1 or 2. */
|
||||||
looped: false,
|
looped: false,
|
||||||
angle: 0.5, /* smaller for smoother bezier */
|
angle: 0.5, /* smaller for smoother bezier */
|
||||||
|
elasticity: 0,
|
||||||
|
friction: 0,
|
||||||
|
sync() {
|
||||||
|
var ppp = this.sample();
|
||||||
|
this.segs ??= [];
|
||||||
|
var count = ppp.length-1;
|
||||||
|
this.segs.length = count;
|
||||||
|
for (var i = 0; i < count; i++) {
|
||||||
|
this.segs[i] ??= os.make_seg2d(this.body);
|
||||||
|
this.segs[i].set_endpoints(ppp[i],ppp[i+1]);
|
||||||
|
this.segs[i].set_neighbors(ppp[i],ppp[i+1]);
|
||||||
|
this.segs[i].radius = this.thickness;
|
||||||
|
this.segs[i].elasticity = this.elasticity;
|
||||||
|
this.segs[i].friction = this.friction;
|
||||||
|
this.segs[i].collide = this.collide;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
flipx: false,
|
flipx: false,
|
||||||
flipy: false,
|
flipy: false,
|
||||||
points:[],
|
|
||||||
toString() { return "edge2d"; },
|
|
||||||
|
|
||||||
hollow: false,
|
hollow: false,
|
||||||
hollowt: 0,
|
hollowt: 0,
|
||||||
|
@ -544,12 +451,10 @@ component.edge2d = Object.copy(collider2d, {
|
||||||
return arr1.concat(arr2.reverse());
|
return arr1.concat(arr2.reverse());
|
||||||
}
|
}
|
||||||
|
|
||||||
return spoints;
|
if (this.looped)
|
||||||
},
|
spoints = spoints.wrapped(1);
|
||||||
|
|
||||||
setpoints(points) {
|
return spoints;
|
||||||
this.points = points;
|
|
||||||
// this.sync();
|
|
||||||
},
|
},
|
||||||
|
|
||||||
post() {
|
post() {
|
||||||
|
@ -582,9 +487,6 @@ component.edge2d = Object.copy(collider2d, {
|
||||||
|
|
||||||
boundingbox() { return bbox.frompoints(this.points.map(x => x.scale(this.gameobject.scale))); },
|
boundingbox() { return bbox.frompoints(this.points.map(x => x.scale(this.gameobject.scale))); },
|
||||||
|
|
||||||
hides: ['gameobject', 'id', 'shape'],
|
|
||||||
_enghook: os.make_edge2d,
|
|
||||||
|
|
||||||
/* EDITOR */
|
/* EDITOR */
|
||||||
gizmo() {
|
gizmo() {
|
||||||
if (this.type === Spline.type.catmull || this.type === -1) {
|
if (this.type === Spline.type.catmull || this.type === -1) {
|
||||||
|
@ -660,12 +562,12 @@ component.edge2d = Object.copy(collider2d, {
|
||||||
var idx = 0;
|
var idx = 0;
|
||||||
if (Spline.is_catmull(this.type) || this.type === -1) {
|
if (Spline.is_catmull(this.type) || this.type === -1) {
|
||||||
if (this.points.length >= 2)
|
if (this.points.length >= 2)
|
||||||
idx = physics.closest_point(pos, this.points, 400);
|
idx = physics.closest_point(pos, this.points, 400);
|
||||||
|
|
||||||
if (idx === this.points.length)
|
if (idx === this.points.length)
|
||||||
this.points.push(pos);
|
this.points.push(pos);
|
||||||
else
|
else
|
||||||
this.points.splice(idx, 0, pos);
|
this.points.splice(idx, 0, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Spline.is_bezier(this.type)) {
|
if (Spline.is_bezier(this.type)) {
|
||||||
|
@ -675,11 +577,11 @@ component.edge2d = Object.copy(collider2d, {
|
||||||
|
|
||||||
if (idx === 0) {
|
if (idx === 0) {
|
||||||
this.points.unshift(pos.slice(), pos.add([-100,0]), Vector.reflect_point(this.points[1], this.points[0]));
|
this.points.unshift(pos.slice(), pos.add([-100,0]), Vector.reflect_point(this.points[1], this.points[0]));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (idx === Spline.bezier_node_count(this.points)) {
|
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());
|
this.points.push(Vector.reflect_point(this.points.at(-2), this.points.at(-1)), pos.add([-100,0]), pos.slice());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
idx = 2 + (idx-1)*3;
|
idx = 2 + (idx-1)*3;
|
||||||
var adds = [pos.add([100,0]), pos.slice(), pos.add([-100,0])];
|
var adds = [pos.add([100,0]), pos.slice(), pos.add([-100,0])];
|
||||||
|
@ -692,71 +594,64 @@ component.edge2d = Object.copy(collider2d, {
|
||||||
this.points.forEach(x =>picks.push(make_point_obj(this,x)));
|
this.points.forEach(x =>picks.push(make_point_obj(this,x)));
|
||||||
return picks;
|
return picks;
|
||||||
},
|
},
|
||||||
});
|
};
|
||||||
|
|
||||||
component.edge2d.impl = Object.mix(collider2d.impl, {
|
component.edge2d = function(obj) {
|
||||||
set thickness(x) { edge2d.set_thickness(this.id,x); },
|
if (!obj.body) obj.rigidify();
|
||||||
get thickness() { return edge2d.get_thickness(this.id); },
|
var edge = Object.create(edge2d);
|
||||||
grow: pointscaler,
|
edge.body = obj.body;
|
||||||
sync() {
|
return edge;
|
||||||
var sensor = this.sensor;
|
}
|
||||||
var points = this.sample();
|
|
||||||
if (!points) return;
|
|
||||||
edge2d.setverts(this.id,points);
|
|
||||||
this.sensor = sensor;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
var bucket = component.edge2d;
|
edge2d.spoints.doc = "Returns the controls points after modifiers are applied, such as it being hollow or mirrored on its axises.";
|
||||||
bucket.spoints.doc = "Returns the controls points after modifiers are applied, such as it being hollow or mirrored on its axises.";
|
edge2d.inputs = {};
|
||||||
bucket.inputs = {};
|
edge2d.inputs.h = function() { this.hollow = !this.hollow; };
|
||||||
bucket.inputs.h = function() { this.hollow = !this.hollow; };
|
edge2d.inputs.h.doc = "Toggle hollow.";
|
||||||
bucket.inputs.h.doc = "Toggle hollow.";
|
|
||||||
|
|
||||||
bucket.inputs['C-g'] = function() { if (this.hollowt > 0) this.hollowt--; };
|
edge2d.inputs['C-g'] = function() { if (this.hollowt > 0) this.hollowt--; };
|
||||||
bucket.inputs['C-g'].doc = "Thin the hollow thickness.";
|
edge2d.inputs['C-g'].doc = "Thin the hollow thickness.";
|
||||||
bucket.inputs['C-g'].rep = true;
|
edge2d.inputs['C-g'].rep = true;
|
||||||
|
|
||||||
bucket.inputs['C-f'] = function() { this.hollowt++; };
|
edge2d.inputs['C-f'] = function() { this.hollowt++; };
|
||||||
bucket.inputs['C-f'].doc = "Increase the hollow thickness.";
|
edge2d.inputs['C-f'].doc = "Increase the hollow thickness.";
|
||||||
bucket.inputs['C-f'].rep = true;
|
edge2d.inputs['C-f'].rep = true;
|
||||||
|
|
||||||
bucket.inputs['M-v'] = function() { if (this.thickness > 0) this.thickness--; };
|
edge2d.inputs['M-v'] = function() { if (this.thickness > 0) this.thickness--; };
|
||||||
bucket.inputs['M-v'].doc = "Decrease spline thickness.";
|
edge2d.inputs['M-v'].doc = "Decrease spline thickness.";
|
||||||
bucket.inputs['M-v'].rep = true;
|
edge2d.inputs['M-v'].rep = true;
|
||||||
|
|
||||||
bucket.inputs['C-y'] = function() {
|
edge2d.inputs['C-y'] = function() {
|
||||||
this.points = this.spoints();
|
this.points = this.spoints();
|
||||||
this.flipx = false;
|
this.flipx = false;
|
||||||
this.flipy = false;
|
this.flipy = false;
|
||||||
this.hollow = false;
|
this.hollow = false;
|
||||||
};
|
};
|
||||||
bucket.inputs['C-y'].doc = "Freeze mirroring,";
|
edge2d.inputs['C-y'].doc = "Freeze mirroring,";
|
||||||
bucket.inputs['M-b'] = function() { this.thickness++; };
|
edge2d.inputs['M-b'] = function() { this.thickness++; };
|
||||||
bucket.inputs['M-b'].doc = "Increase spline thickness.";
|
edge2d.inputs['M-b'].doc = "Increase spline thickness.";
|
||||||
bucket.inputs['M-b'].rep = true;
|
edge2d.inputs['M-b'].rep = true;
|
||||||
|
|
||||||
bucket.inputs.plus = function() {
|
edge2d.inputs.plus = function() {
|
||||||
if (this.angle <= 1) {
|
if (this.angle <= 1) {
|
||||||
this.angle = 1;
|
this.angle = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.angle *= 0.9;
|
this.angle *= 0.9;
|
||||||
};
|
};
|
||||||
bucket.inputs.plus.doc = "Increase the number of samples of this spline.";
|
edge2d.inputs.plus.doc = "Increase the number of samples of this spline.";
|
||||||
bucket.inputs.plus.rep = true;
|
edge2d.inputs.plus.rep = true;
|
||||||
|
|
||||||
bucket.inputs.minus = function() { this.angle *= 1.1; };
|
edge2d.inputs.minus = function() { this.angle *= 1.1; };
|
||||||
bucket.inputs.minus.doc = "Decrease the number of samples on this spline.";
|
edge2d.inputs.minus.doc = "Decrease the number of samples on this spline.";
|
||||||
bucket.inputs.minus.rep = true;
|
edge2d.inputs.minus.rep = true;
|
||||||
|
|
||||||
bucket.inputs['C-r'] = function() { this.points = this.points.reverse(); };
|
edge2d.inputs['C-r'] = function() { this.points = this.points.reverse(); };
|
||||||
bucket.inputs['C-r'].doc = "Reverse the order of the spline's points.";
|
edge2d.inputs['C-r'].doc = "Reverse the order of the spline's points.";
|
||||||
|
|
||||||
bucket.inputs['C-l'] = function() { this.looped = !this.looped};
|
edge2d.inputs['C-l'] = function() { this.looped = !this.looped};
|
||||||
bucket.inputs['C-l'].doc = "Toggle spline being looped.";
|
edge2d.inputs['C-l'].doc = "Toggle spline being looped.";
|
||||||
|
|
||||||
bucket.inputs['C-c'] = function() {
|
edge2d.inputs['C-c'] = function() {
|
||||||
switch(this.type) {
|
switch(this.type) {
|
||||||
case Spline.type.bezier:
|
case Spline.type.bezier:
|
||||||
this.points = Spline.bezier2catmull(this.points);
|
this.points = Spline.bezier2catmull(this.points);
|
||||||
|
@ -765,9 +660,9 @@ bucket.inputs['C-c'] = function() {
|
||||||
this.type = Spline.type.catmull;
|
this.type = Spline.type.catmull;
|
||||||
};
|
};
|
||||||
|
|
||||||
bucket.inputs['C-c'].doc = "Set type of spline to catmull-rom.";
|
edge2d.inputs['C-c'].doc = "Set type of spline to catmull-rom.";
|
||||||
|
|
||||||
bucket.inputs['C-b'] = function() {
|
edge2d.inputs['C-b'] = function() {
|
||||||
switch(this.type) {
|
switch(this.type) {
|
||||||
case Spline.type.catmull:
|
case Spline.type.catmull:
|
||||||
this.points = Spline.catmull2bezier(Spline.catmull_caps(this.points));
|
this.points = Spline.catmull2bezier(Spline.catmull_caps(this.points));
|
||||||
|
@ -776,10 +671,10 @@ bucket.inputs['C-b'] = function() {
|
||||||
this.type = Spline.type.bezier;
|
this.type = Spline.type.bezier;
|
||||||
};
|
};
|
||||||
|
|
||||||
bucket.inputs['C-o'] = function() { this.type = -1; };
|
edge2d.inputs['C-o'] = function() { this.type = -1; };
|
||||||
bucket.inputs['C-o'].doc = "Set spline to linear.";
|
edge2d.inputs['C-o'].doc = "Set spline to linear.";
|
||||||
|
|
||||||
bucket.inputs['C-M-lm'] = function() {
|
edge2d.inputs['C-M-lm'] = function() {
|
||||||
if (Spline.is_catmull(this.type)) {
|
if (Spline.is_catmull(this.type)) {
|
||||||
var idx = Math.grab_from_points(input.mouse.worldpos(), this.points.map(p => this.gameobject.this2world(p)), 25);
|
var idx = Math.grab_from_points(input.mouse.worldpos(), this.points.map(p => this.gameobject.this2world(p)), 25);
|
||||||
if (idx === -1) return;
|
if (idx === -1) return;
|
||||||
|
@ -789,12 +684,12 @@ bucket.inputs['C-M-lm'] = function() {
|
||||||
|
|
||||||
this.points = this.points.newfirst(idx);
|
this.points = this.points.newfirst(idx);
|
||||||
};
|
};
|
||||||
bucket.inputs['C-M-lm'].doc = "Select the given point as the '0' of this spline.";
|
edge2d.inputs['C-M-lm'].doc = "Select the given point as the '0' of this spline.";
|
||||||
|
|
||||||
bucket.inputs['C-lm'] = function() { this.add_node(input.mouse.worldpos()); }
|
edge2d.inputs['C-lm'] = function() { this.add_node(input.mouse.worldpos()); }
|
||||||
bucket.inputs['C-lm'].doc = "Add a point to the spline at the mouse position.";
|
edge2d.inputs['C-lm'].doc = "Add a point to the spline at the mouse position.";
|
||||||
|
|
||||||
bucket.inputs['C-M-lm'] = function() {
|
edge2d.inputs['C-M-lm'] = function() {
|
||||||
var idx = -1;
|
var idx = -1;
|
||||||
if (Spline.is_catmull(this.type))
|
if (Spline.is_catmull(this.type))
|
||||||
idx = Math.grab_from_points(input.mouse.worldpos(), this.points.map(p => this.gameobject.this2world(p)), 25);
|
idx = Math.grab_from_points(input.mouse.worldpos(), this.points.map(p => this.gameobject.this2world(p)), 25);
|
||||||
|
@ -806,12 +701,12 @@ bucket.inputs['C-M-lm'] = function() {
|
||||||
|
|
||||||
this.rm_node(idx);
|
this.rm_node(idx);
|
||||||
};
|
};
|
||||||
bucket.inputs['C-M-lm'].doc = "Remove point from the spline.";
|
edge2d.inputs['C-M-lm'].doc = "Remove point from the spline.";
|
||||||
|
|
||||||
bucket.inputs.lm = function(){};
|
edge2d.inputs.lm = function(){};
|
||||||
bucket.inputs.lm.released = function(){};
|
edge2d.inputs.lm.released = function(){};
|
||||||
|
|
||||||
bucket.inputs.lb = function() {
|
edge2d.inputs.lb = function() {
|
||||||
var np = [];
|
var np = [];
|
||||||
|
|
||||||
this.points.forEach(function(c) {
|
this.points.forEach(function(c) {
|
||||||
|
@ -820,10 +715,10 @@ bucket.inputs.lb = function() {
|
||||||
|
|
||||||
this.points = np;
|
this.points = np;
|
||||||
};
|
};
|
||||||
bucket.inputs.lb.doc = "Rotate the points CCW.";
|
edge2d.inputs.lb.doc = "Rotate the points CCW.";
|
||||||
bucket.inputs.lb.rep = true;
|
edge2d.inputs.lb.rep = true;
|
||||||
|
|
||||||
bucket.inputs.rb = function() {
|
edge2d.inputs.rb = function() {
|
||||||
var np = [];
|
var np = [];
|
||||||
|
|
||||||
this.points.forEach(function(c) {
|
this.points.forEach(function(c) {
|
||||||
|
@ -832,37 +727,27 @@ bucket.inputs.rb = function() {
|
||||||
|
|
||||||
this.points = np;
|
this.points = np;
|
||||||
};
|
};
|
||||||
bucket.inputs.rb.doc = "Rotate the points CW.";
|
edge2d.inputs.rb.doc = "Rotate the points CW.";
|
||||||
bucket.inputs.rb.rep = true;
|
edge2d.inputs.rb.rep = true;
|
||||||
|
|
||||||
component.circle2d = Object.copy(collider2d, {
|
/* CIRCLE */
|
||||||
radius:10,
|
|
||||||
offset:[0,0],
|
|
||||||
toString() { return "circle2d"; },
|
|
||||||
|
|
||||||
boundingbox() {
|
function shape_maker(maker) {
|
||||||
return bbox.fromcwh([0,0], [this.radius,this.radius]);
|
return function(obj) {
|
||||||
},
|
if (!obj.body) obj.rigidify();
|
||||||
|
return maker(obj.body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
component.circle2d = shape_maker(os.make_circle2d);
|
||||||
|
component.poly2d = shape_maker(os.make_poly2d);
|
||||||
|
component.seg2d = shape_maker(os.make_seg2d);
|
||||||
|
|
||||||
hides: ['gameobject', 'id', 'shape', 'scale'],
|
Object.mix(os.make_circle2d(), {
|
||||||
_enghook: os.make_circle2d,
|
boundingbox() { return bbox.fromcwh(this.offset, [this.radius,this.radius]); },
|
||||||
});
|
|
||||||
|
|
||||||
component.circle2d.impl = Object.mix({
|
|
||||||
toJSON:json_from_whitelist([
|
|
||||||
"pos",
|
|
||||||
"radius",
|
|
||||||
]),
|
|
||||||
|
|
||||||
set radius(x) { circle2d.set_radius(this.id,x); circle2d.sync(this.id); },
|
|
||||||
get radius() { return circle2d.get_radius(this.id); },
|
|
||||||
|
|
||||||
set scale(x) { this.radius = x; },
|
set scale(x) { this.radius = x; },
|
||||||
get scale() { return this.radius; },
|
get scale() { return this.radius; },
|
||||||
|
|
||||||
set offset(x) { circle2d.set_offset(this.id,x); circle2d.sync(this.id); },
|
|
||||||
get offset() { circle2d.get_offset(this.id); },
|
|
||||||
|
|
||||||
get pos() { return this.offset; },
|
get pos() { return this.offset; },
|
||||||
set pos(x) { this.offset = x; },
|
set pos(x) { this.offset = x; },
|
||||||
|
|
||||||
|
@ -870,7 +755,6 @@ component.circle2d.impl = Object.mix({
|
||||||
if (typeof x === 'number') this.scale *= x;
|
if (typeof x === 'number') this.scale *= x;
|
||||||
else if (typeof x === 'object') this.scale *= x[0];
|
else if (typeof x === 'object') this.scale *= x[0];
|
||||||
},
|
},
|
||||||
|
});
|
||||||
}, collider2d.impl);
|
|
||||||
|
|
||||||
return {component, SpriteAnim};
|
return {component, SpriteAnim};
|
||||||
|
|
|
@ -33,6 +33,11 @@ var entity = {
|
||||||
return undefined;
|
return undefined;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
rigidify() {
|
||||||
|
this.body = os.make_body(this.transform);
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
path_from(o) {
|
path_from(o) {
|
||||||
var p = this.toString();
|
var p = this.toString();
|
||||||
var c = this.master;
|
var c = this.master;
|
||||||
|
@ -178,9 +183,8 @@ var entity = {
|
||||||
for (var [prop, p] of Object.entries(ent)) {
|
for (var [prop, p] of Object.entries(ent)) {
|
||||||
if (!p) continue;
|
if (!p) continue;
|
||||||
if (typeof p !== 'object') continue;
|
if (typeof p !== 'object') continue;
|
||||||
if (component.isComponent(p)) continue;
|
|
||||||
if (!p.comp) continue;
|
if (!p.comp) continue;
|
||||||
ent[prop] = component[p.comp].make(ent);
|
ent[prop] = component[p.comp](ent);
|
||||||
Object.merge(ent[prop], p);
|
Object.merge(ent[prop], p);
|
||||||
ent.components[prop] = ent[prop];
|
ent.components[prop] = ent[prop];
|
||||||
};
|
};
|
||||||
|
@ -347,9 +351,8 @@ dup(diff) {
|
||||||
for (var key in this.components) {
|
for (var key in this.components) {
|
||||||
this.components[key].kill?.();
|
this.components[key].kill?.();
|
||||||
this.components[key].gameobject = undefined;
|
this.components[key].gameobject = undefined;
|
||||||
this[key].enabled = false;
|
this.components[key].enabled = false;
|
||||||
delete this.components[key];
|
delete this.components[key];
|
||||||
delete this[key];
|
|
||||||
}
|
}
|
||||||
delete this.components;
|
delete this.components;
|
||||||
|
|
||||||
|
@ -393,15 +396,14 @@ dup(diff) {
|
||||||
return this.objects[newname];
|
return this.objects[newname];
|
||||||
},
|
},
|
||||||
|
|
||||||
add_component(comp, data, name = comp.toString()) {
|
add_component(comp, data) {
|
||||||
if (typeof comp.make !== 'function') return;
|
var name = prosperon.guid();
|
||||||
name = obj_unique_name(name, this);
|
this.components[name] = comp(this);
|
||||||
this[name] = comp.make(this);
|
if (data) {
|
||||||
this[name].comp = comp.toString();
|
Object.assign(this.components[name], data);
|
||||||
this.components[name] = this[name];
|
this.components[name].sync?.();
|
||||||
if (data)
|
}
|
||||||
Object.assign(this[name], data);
|
return this.components[name];
|
||||||
return this[name];
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ JSValue arb2js(cpArbiter *arb)
|
||||||
cpShape *shape2;
|
cpShape *shape2;
|
||||||
cpArbiterGetShapes(arb, &shape1, &shape2);
|
cpArbiterGetShapes(arb, &shape1, &shape2);
|
||||||
|
|
||||||
JSValue j = cpShape2js(shape2);
|
JSValue j = *(JSValue*)cpShapeGetUserData(shape2);
|
||||||
|
|
||||||
JSValue jg = body2go(body2)->ref;
|
JSValue jg = body2go(body2)->ref;
|
||||||
|
|
||||||
|
@ -69,8 +69,8 @@ void register_hit(cpArbiter *arb, gameobject *go, const char *name)
|
||||||
|
|
||||||
cpShape *s1, *s2;
|
cpShape *s1, *s2;
|
||||||
cpArbiterGetShapes(arb, &s1, &s2);
|
cpArbiterGetShapes(arb, &s1, &s2);
|
||||||
JSValue j1 = cpShape2js(s1);
|
JSValue j1 = *(JSValue*)cpShapeGetUserData(s1);
|
||||||
JSValue j2 = cpShape2js(s2);
|
JSValue j2 = *(JSValue*)cpShapeGetUserData(s2);
|
||||||
cb = JS_GetPropertyStr(js, j1, name);
|
cb = JS_GetPropertyStr(js, j1, name);
|
||||||
if (!JS_IsUndefined(cb)) {
|
if (!JS_IsUndefined(cb)) {
|
||||||
JSValue jarb = arb2js(arb);
|
JSValue jarb = arb2js(arb);
|
||||||
|
|
|
@ -32,6 +32,7 @@ void gameobject_apply(gameobject *go) { *go->t = go2t(go); }
|
||||||
static void velocityFn(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
|
static void velocityFn(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
|
||||||
{
|
{
|
||||||
gameobject *go = body2go(body);
|
gameobject *go = body2go(body);
|
||||||
|
gameobject_apply(go);
|
||||||
cpVect pos = cpBodyGetPosition(body);
|
cpVect pos = cpBodyGetPosition(body);
|
||||||
HMM_Vec2 g = warp_force((HMM_Vec3){pos.x, pos.y, 0}, go->warp_mask).xy;
|
HMM_Vec2 g = warp_force((HMM_Vec3){pos.x, pos.y, 0}, go->warp_mask).xy;
|
||||||
if (!go) {
|
if (!go) {
|
||||||
|
@ -91,9 +92,3 @@ void gameobject_free(gameobject *go) {
|
||||||
cpBodyFree(go->body);
|
cpBodyFree(go->body);
|
||||||
free(go);
|
free(go);
|
||||||
}
|
}
|
||||||
|
|
||||||
void gameobject_setpos(gameobject *go, cpVect vec) {
|
|
||||||
if (!go || !go->body) return;
|
|
||||||
cpBodySetPosition(go->body, vec);
|
|
||||||
// phys2d_reindex_body(go->body);
|
|
||||||
}
|
|
||||||
|
|
|
@ -1324,17 +1324,6 @@ JSC_CCALL(physics_box_query,
|
||||||
return ret;
|
return ret;
|
||||||
)
|
)
|
||||||
|
|
||||||
static void shape_query_fn(cpShape *shape, cpContactPointSet *points, JSValue *cb)
|
|
||||||
{
|
|
||||||
JSValue go = JS_DupValue(js,shape2go(shape)->ref);
|
|
||||||
script_call_sym(*cb, 1, &go);
|
|
||||||
JS_FreeValue(js, go);
|
|
||||||
}
|
|
||||||
|
|
||||||
JSC_CCALL(physics_shape_query,
|
|
||||||
//cpSpaceShapeQuery(space, ((struct phys2d_shape*)js2ptr(argv[0]))->shape, shape_query_fn, &argv[1]);
|
|
||||||
)
|
|
||||||
|
|
||||||
static void ray_query_fn(cpShape *shape, float t, cpVect n, float a, JSValue *cb)
|
static void ray_query_fn(cpShape *shape, float t, cpVect n, float a, JSValue *cb)
|
||||||
{
|
{
|
||||||
JSValue argv[3] = {
|
JSValue argv[3] = {
|
||||||
|
@ -1429,7 +1418,6 @@ static const JSCFunctionListEntry js_physics_funcs[] = {
|
||||||
MIST_FUNC_DEF(physics, point_query_nearest, 2),
|
MIST_FUNC_DEF(physics, point_query_nearest, 2),
|
||||||
MIST_FUNC_DEF(physics, ray_query, 4),
|
MIST_FUNC_DEF(physics, ray_query, 4),
|
||||||
MIST_FUNC_DEF(physics, box_query, 2),
|
MIST_FUNC_DEF(physics, box_query, 2),
|
||||||
MIST_FUNC_DEF(physics, shape_query, 1),
|
|
||||||
MIST_FUNC_DEF(physics, closest_point, 3),
|
MIST_FUNC_DEF(physics, closest_point, 3),
|
||||||
MIST_FUNC_DEF(physics, make_damp, 0),
|
MIST_FUNC_DEF(physics, make_damp, 0),
|
||||||
MIST_FUNC_DEF(physics, make_gravity, 0),
|
MIST_FUNC_DEF(physics, make_gravity, 0),
|
||||||
|
@ -1591,7 +1579,6 @@ JSC_GETSET(gameobject, warp_mask, bitmask)
|
||||||
JSC_CCALL(gameobject_sleeping, return boolean2js(cpBodyIsSleeping(js2gameobject(self)->body)))
|
JSC_CCALL(gameobject_sleeping, return boolean2js(cpBodyIsSleeping(js2gameobject(self)->body)))
|
||||||
JSC_CCALL(gameobject_sleep, cpBodySleep(js2gameobject(self)->body))
|
JSC_CCALL(gameobject_sleep, cpBodySleep(js2gameobject(self)->body))
|
||||||
JSC_CCALL(gameobject_wake, cpBodyActivate(js2gameobject(self)->body))
|
JSC_CCALL(gameobject_wake, cpBodyActivate(js2gameobject(self)->body))
|
||||||
JSC_CCALL(gameobject_selfsync, gameobject_apply(js2gameobject(self)))
|
|
||||||
|
|
||||||
void body_shape_fn(cpBody *body, cpShape *shape, JSValue *fn) {
|
void body_shape_fn(cpBody *body, cpShape *shape, JSValue *fn) {
|
||||||
JSValue v = *(JSValue*)cpShapeGetUserData(shape);
|
JSValue v = *(JSValue*)cpShapeGetUserData(shape);
|
||||||
|
@ -1600,8 +1587,7 @@ void body_shape_fn(cpBody *body, cpShape *shape, JSValue *fn) {
|
||||||
|
|
||||||
JSC_CCALL(gameobject_eachshape,
|
JSC_CCALL(gameobject_eachshape,
|
||||||
gameobject *g = js2gameobject(self);
|
gameobject *g = js2gameobject(self);
|
||||||
JSValue fn = argv[0];
|
cpBodyEachShape(g->body, body_shape_fn, &argv[0]);
|
||||||
cpBodyEachShape(g->body, body_shape_fn, &fn);
|
|
||||||
)
|
)
|
||||||
|
|
||||||
void body_constraint_fn(cpBody *body, cpConstraint *c, JSValue *fn) {
|
void body_constraint_fn(cpBody *body, cpConstraint *c, JSValue *fn) {
|
||||||
|
@ -1648,7 +1634,6 @@ static const JSCFunctionListEntry js_gameobject_funcs[] = {
|
||||||
MIST_FUNC_DEF(gameobject, impulse, 1),
|
MIST_FUNC_DEF(gameobject, impulse, 1),
|
||||||
MIST_FUNC_DEF(gameobject, force, 1),
|
MIST_FUNC_DEF(gameobject, force, 1),
|
||||||
MIST_FUNC_DEF(gameobject, force_local, 2),
|
MIST_FUNC_DEF(gameobject, force_local, 2),
|
||||||
MIST_FUNC_DEF(gameobject, selfsync, 0),
|
|
||||||
MIST_FUNC_DEF(gameobject, eachshape, 1),
|
MIST_FUNC_DEF(gameobject, eachshape, 1),
|
||||||
MIST_FUNC_DEF(gameobject, eachconstraint, 1),
|
MIST_FUNC_DEF(gameobject, eachconstraint, 1),
|
||||||
MIST_FUNC_DEF(gameobject, eacharbiter, 1),
|
MIST_FUNC_DEF(gameobject, eacharbiter, 1),
|
||||||
|
@ -2096,7 +2081,7 @@ JSC_CCALL(os_reindex_static, cpSpaceReindexStatic(space));
|
||||||
JSC_CCALL(os_gc, script_gc());
|
JSC_CCALL(os_gc, script_gc());
|
||||||
JSC_SSCALL(os_eval, ret = script_eval(str, str2))
|
JSC_SSCALL(os_eval, ret = script_eval(str, str2))
|
||||||
|
|
||||||
JSC_CCALL(os_make_gameobject,
|
JSC_CCALL(os_make_body,
|
||||||
gameobject *g = MakeGameobject();
|
gameobject *g = MakeGameobject();
|
||||||
g->t = js2transform(argv[0]);
|
g->t = js2transform(argv[0]);
|
||||||
ret = gameobject2js(g);
|
ret = gameobject2js(g);
|
||||||
|
@ -2146,6 +2131,16 @@ JSC_CCALL(cpShape_body,
|
||||||
return JS_DupValue(js,gameobject2js(go));
|
return JS_DupValue(js,gameobject2js(go));
|
||||||
)
|
)
|
||||||
|
|
||||||
|
static void shape_query_fn(cpShape *shape, cpContactPointSet *points, JSValue *cb)
|
||||||
|
{
|
||||||
|
JSValue v = *(JSValue*)cpShapeGetUserData(shape);
|
||||||
|
script_call_sym(*cb, 1, &v);
|
||||||
|
}
|
||||||
|
|
||||||
|
JSC_CCALL(cpShape_query,
|
||||||
|
cpSpaceShapeQuery(space, js2cpShape(self), shape_query_fn, &argv[0]);
|
||||||
|
)
|
||||||
|
|
||||||
static const JSCFunctionListEntry js_cpShape_funcs[] = {
|
static const JSCFunctionListEntry js_cpShape_funcs[] = {
|
||||||
CGETSET_ADD(cpShape, sensor),
|
CGETSET_ADD(cpShape, sensor),
|
||||||
CGETSET_ADD(cpShape, friction),
|
CGETSET_ADD(cpShape, friction),
|
||||||
|
@ -2156,6 +2151,7 @@ static const JSCFunctionListEntry js_cpShape_funcs[] = {
|
||||||
CGETSET_ADD(cpShape, mass),
|
CGETSET_ADD(cpShape, mass),
|
||||||
MIST_FUNC_DEF(cpShape, area, 0),
|
MIST_FUNC_DEF(cpShape, area, 0),
|
||||||
MIST_FUNC_DEF(cpShape, body, 0),
|
MIST_FUNC_DEF(cpShape, body, 0),
|
||||||
|
MIST_FUNC_DEF(cpShape, query, 1),
|
||||||
};
|
};
|
||||||
|
|
||||||
JSValue js_circle2d_set_radius(JSContext *js, JSValue self, JSValue val) {
|
JSValue js_circle2d_set_radius(JSContext *js, JSValue self, JSValue val) {
|
||||||
|
@ -2203,12 +2199,13 @@ JSC_CCALL(os_make_circle2d,
|
||||||
JSC_CCALL(poly2d_setverts,
|
JSC_CCALL(poly2d_setverts,
|
||||||
cpShape *s = js2cpShape(self);
|
cpShape *s = js2cpShape(self);
|
||||||
HMM_Vec2 *v = js2cpvec2arr(argv[0]);
|
HMM_Vec2 *v = js2cpvec2arr(argv[0]);
|
||||||
|
gameobject *go = shape2go(s);
|
||||||
cpTransform t = {0};
|
cpTransform t = {0};
|
||||||
t.a = 1;
|
t.a = go->t->scale.x;
|
||||||
t.b = 0;
|
t.b = 0;
|
||||||
t.tx = 0;
|
t.tx = 0;
|
||||||
t.c = 0;
|
t.c = 0;
|
||||||
t.d = 1;
|
t.d = go->t->scale.y;
|
||||||
t.ty = 0;
|
t.ty = 0;
|
||||||
cpPolyShapeSetVerts(s, arrlen(v), v, t);
|
cpPolyShapeSetVerts(s, arrlen(v), v, t);
|
||||||
arrfree(v);
|
arrfree(v);
|
||||||
|
@ -2235,9 +2232,8 @@ JSC_CCALL(os_make_poly2d,
|
||||||
)
|
)
|
||||||
|
|
||||||
JSC_CCALL(seg2d_set_endpoints,
|
JSC_CCALL(seg2d_set_endpoints,
|
||||||
HMM_Vec2 a = js2vec2(argv[0]);
|
cpSegmentShapeSetEndpoints(js2cpShape(self), js2vec2(argv[0]).cp, js2vec2(argv[1]).cp);
|
||||||
HMM_Vec2 b = js2vec2(argv[1]);
|
cpSpaceReindexShape(space, js2cpShape(self));
|
||||||
cpSegmentShapeSetEndpoints(js2cpShape(self), a.cp, b.cp);
|
|
||||||
)
|
)
|
||||||
|
|
||||||
JSValue js_seg2d_set_radius(JSContext *js, JSValue self, JSValue val) {
|
JSValue js_seg2d_set_radius(JSContext *js, JSValue self, JSValue val) {
|
||||||
|
@ -2252,10 +2248,15 @@ JSC_CCALL(seg2d_set_neighbors,
|
||||||
cpSegmentShapeSetNeighbors(js2cpShape(self), prev.cp, next.cp);
|
cpSegmentShapeSetNeighbors(js2cpShape(self), prev.cp, next.cp);
|
||||||
)
|
)
|
||||||
|
|
||||||
|
JSC_CCALL(seg2d_a, return cvec22js(cpSegmentShapeGetA(js2cpShape(self))))
|
||||||
|
JSC_CCALL(seg2d_b, return cvec22js(cpSegmentShapeGetB(js2cpShape(self))))
|
||||||
|
|
||||||
static const JSCFunctionListEntry js_seg2d_funcs[] = {
|
static const JSCFunctionListEntry js_seg2d_funcs[] = {
|
||||||
MIST_FUNC_DEF(seg2d, set_endpoints, 2),
|
MIST_FUNC_DEF(seg2d, set_endpoints, 2),
|
||||||
CGETSET_ADD(seg2d, radius),
|
CGETSET_ADD(seg2d, radius),
|
||||||
MIST_FUNC_DEF(seg2d, set_neighbors, 2)
|
MIST_FUNC_DEF(seg2d, set_neighbors, 2),
|
||||||
|
MIST_FUNC_DEF(seg2d, a, 0),
|
||||||
|
MIST_FUNC_DEF(seg2d, b, 0),
|
||||||
};
|
};
|
||||||
|
|
||||||
JSC_CCALL(os_make_seg2d,
|
JSC_CCALL(os_make_seg2d,
|
||||||
|
@ -2496,7 +2497,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
|
||||||
MIST_FUNC_DEF(os, reindex_static, 0),
|
MIST_FUNC_DEF(os, reindex_static, 0),
|
||||||
MIST_FUNC_DEF(os, gc, 0),
|
MIST_FUNC_DEF(os, gc, 0),
|
||||||
MIST_FUNC_DEF(os, eval, 2),
|
MIST_FUNC_DEF(os, eval, 2),
|
||||||
MIST_FUNC_DEF(os, make_gameobject, 1),
|
MIST_FUNC_DEF(os, make_body, 1),
|
||||||
MIST_FUNC_DEF(os, make_circle2d, 2),
|
MIST_FUNC_DEF(os, make_circle2d, 2),
|
||||||
MIST_FUNC_DEF(os, make_poly2d, 2),
|
MIST_FUNC_DEF(os, make_poly2d, 2),
|
||||||
MIST_FUNC_DEF(os, make_seg2d, 1),
|
MIST_FUNC_DEF(os, make_seg2d, 1),
|
||||||
|
|
|
@ -98,7 +98,10 @@ static JSClassDef js_##TYPE##_class = {\
|
||||||
#TYPE,\
|
#TYPE,\
|
||||||
.finalizer = js_##TYPE##_finalizer,\
|
.finalizer = js_##TYPE##_finalizer,\
|
||||||
};\
|
};\
|
||||||
TYPE *js2##TYPE (JSValue val) { return JS_GetOpaque(val,js_##TYPE##_id); }\
|
TYPE *js2##TYPE (JSValue val) { \
|
||||||
|
assert(JS_GetClassID(val) == js_##TYPE##_id); \
|
||||||
|
return JS_GetOpaque(val,js_##TYPE##_id); \
|
||||||
|
}\
|
||||||
JSValue TYPE##2js(TYPE *n) { \
|
JSValue TYPE##2js(TYPE *n) { \
|
||||||
JSValue j = JS_NewObjectClass(js,js_##TYPE##_id);\
|
JSValue j = JS_NewObjectClass(js,js_##TYPE##_id);\
|
||||||
YughSpam("Created " #TYPE " at %p", n); \
|
YughSpam("Created " #TYPE " at %p", n); \
|
||||||
|
|
Loading…
Reference in a new issue