@tora-editor/tora 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -6
- package/bin/tora.mjs +255 -29
- package/crates/packer/trusted/native-3d-meta.json +3 -3
- package/crates/packer/trusted/native-3d.js +180 -180
- package/crates/packer/trusted/native-meta.json +3 -3
- package/crates/packer/trusted/native.js +5 -5
- package/crates/packer/trusted/release-manifest.json +17 -17
- package/crates/packer/trusted/review-runtime-meta.json +3 -3
- package/crates/packer/trusted/review-runtime.js +182 -182
- package/docs/MIGRATION_0_3.md +39 -0
- package/docs/NATIVE_AGENT_TOOLS.md +12 -363
- package/docs/TORA_AGENT_SETUP.md +24 -106
- package/docs/TORA_CREATION_API.md +63 -365
- package/docs/reference/CORE_AUTHORING.md +8 -2
- package/docs/reference/PLAYABLE_STARTERS.md +1 -1
- package/docs/reference/packs/3d.md +2 -0
- package/package.json +6 -6
- package/packages/creative-project/project-v21.schema.json +108 -1
- package/scripts/lean/editor/entry.js +11 -6
- package/scripts/lean/editor/project-launcher.js +116 -0
- package/scripts/lean/editor/quick-edit-panel.js +479 -0
- package/scripts/lean/editor/review-editor.css +246 -68
- package/scripts/lean/editor/review-editor.js +1731 -1099
- package/scripts/lean/editor-state.mjs +6 -0
- package/scripts/lean/editor.mjs +6 -3
- package/scripts/lean/matrix.mjs +5 -0
- package/scripts/lean/native/agent-tools.mjs +35 -58
- package/scripts/lean/native/api.mjs +148 -134
- package/scripts/lean/native/audio-assets.mjs +30 -0
- package/scripts/lean/native/browser-pool.mjs +34 -0
- package/scripts/lean/native/build-cache.mjs +36 -5
- package/scripts/lean/native/contracts.mjs +8 -6
- package/scripts/lean/native/creative-qa.mjs +66 -12
- package/scripts/lean/native/demos.mjs +11 -20
- package/scripts/lean/native/document.mjs +14 -2
- package/scripts/lean/native/local-bundle.mjs +4 -2
- package/scripts/lean/native/mcp-server.mjs +16 -35
- package/scripts/lean/native/modules/harvest-restore.mjs +3 -284
- package/scripts/lean/native/modules/sort-puzzle.mjs +51 -0
- package/scripts/lean/native/modules/star-salvage.mjs +3 -368
- package/scripts/lean/native/project-bundle.mjs +3 -2
- package/scripts/lean/native/project-index.mjs +0 -2
- package/scripts/lean/native/qa-adapters.mjs +10 -0
- package/scripts/lean/native/quick-edit.mjs +328 -0
- package/scripts/lean/native/review.mjs +203 -572
- package/scripts/lean/native/schema-vocabulary.mjs +10 -0
- package/scripts/lean/native/schema.mjs +1 -1
- package/scripts/lean/native/scope.mjs +45 -0
- package/scripts/lean/native/service.mjs +242 -70
- package/scripts/lean/native/starters.mjs +135 -8
- package/scripts/lean/native/variant-adapters.mjs +8 -0
- package/scripts/lean/native/variants-core.mjs +186 -0
- package/scripts/lean/native/variants.mjs +63 -0
- package/scripts/lean/native/win-cues.mjs +1 -1
- package/skills/tora-playables/SKILL.md +1 -1
- package/scripts/lean/editor/review-intake.js +0 -837
- package/scripts/lean/model-client.mjs +0 -119
- package/scripts/lean/native/author-expansion-proofs.mjs +0 -143
- package/scripts/lean/native/author-fps.mjs +0 -545
- package/scripts/lean/native/author-games.mjs +0 -160
- package/scripts/lean/native/author-harvest.mjs +0 -74
- package/scripts/lean/native/author-primitive-proofs.mjs +0 -875
- package/scripts/lean/native/author-salvage.mjs +0 -71
- package/scripts/lean/native/chat-compose.mjs +0 -287
- package/scripts/lean/native/check-harvest-outcome-audio.mjs +0 -190
- package/scripts/lean/native/check-salvage.mjs +0 -200
- package/scripts/lean/native/compatibility.mjs +0 -34
- package/scripts/lean/native/concept.mjs +0 -633
- package/scripts/lean/native/draw-stroke.mjs +0 -26
- package/scripts/lean/native/erase-mask.mjs +0 -18
- package/scripts/lean/native/glb-loader.mjs +0 -468
- package/scripts/lean/native/harvest-recipe.mjs +0 -21
- package/scripts/lean/native/instance-steering.mjs +0 -24
- package/scripts/lean/native/intake-creator-agent.mjs +0 -98
- package/scripts/lean/native/intake-creator-tools.mjs +0 -218
- package/scripts/lean/native/interaction-gestures.mjs +0 -45
- package/scripts/lean/native/match-board.mjs +0 -138
- package/scripts/lean/native/modules/index.mjs +0 -54
- package/scripts/lean/native/modules/tap-collect.mjs +0 -54
- package/scripts/lean/native/path-network.mjs +0 -132
- package/scripts/lean/native/physics-2d.mjs +0 -154
- package/scripts/lean/native/physics-3d.mjs +0 -259
- package/scripts/lean/native/prepare-audio-collections.mjs +0 -112
- package/scripts/lean/native/prepare-fps-assets.mjs +0 -330
- package/scripts/lean/native/prepare-game-audio.mjs +0 -372
- package/scripts/lean/native/prepare-golf-3d-assets.py +0 -177
- package/scripts/lean/native/prepare-golf-assets.py +0 -211
- package/scripts/lean/native/prepare-harvest-audio.mjs +0 -83
- package/scripts/lean/native/prepare-harvest-images.mjs +0 -31
- package/scripts/lean/native/prepare-primitive-images.py +0 -25
- package/scripts/lean/native/prepare-space-salvage-images.mjs +0 -89
- package/scripts/lean/native/prepare-traffic-assets.py +0 -329
- package/scripts/lean/native/presentation.mjs +0 -335
- package/scripts/lean/native/review-agent-tools.mjs +0 -561
- package/scripts/lean/native/review-connection.mjs +0 -303
- package/scripts/lean/native/review-fields.mjs +0 -24
- package/scripts/lean/native/review-intake.mjs +0 -385
- package/scripts/lean/native/review-intents.mjs +0 -217
- package/scripts/lean/native/review-provider.mjs +0 -580
- package/scripts/lean/native/review-receipts.mjs +0 -76
- package/scripts/lean/native/review-resolver.mjs +0 -429
- package/scripts/lean/native/review-targets.mjs +0 -35
- package/scripts/lean/native/runtime.mjs +0 -1809
- package/scripts/lean/native/salvage-recipe.mjs +0 -20
- package/scripts/lean/native/stage-3d.mjs +0 -1121
- package/scripts/lean/native/verify.mjs +0 -4
- package/scripts/lean/scene-document.mjs +0 -48
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
const clamp=(value,min,max)=>Math.max(min,Math.min(max,value));
|
|
2
|
-
const distance=(a,b)=>Math.hypot(b.x-a.x,b.y-a.y);
|
|
3
|
-
|
|
4
|
-
export function createPathNetwork({id,nodes,edges,gates=[],onEvent=()=>{}}){
|
|
5
|
-
const nodeMap=new Map(nodes.map(node=>[node.id,{...node}]));
|
|
6
|
-
const edgeMap=new Map(edges.map(edge=>[edge.id,{speedScale:1,enabled:true,bidirectional:false,cost:0,...edge}]));
|
|
7
|
-
const gateMap=new Map(gates.map(gate=>[gate.id,{...gate}]));
|
|
8
|
-
const agents=new Map(),agentsById=agents;
|
|
9
|
-
let elapsed=0,tickIndex=0;
|
|
10
|
-
const node=id=>nodeMap.get(id);
|
|
11
|
-
const gateAt=nodeId=>[...gateMap.values()].find(gate=>gate.parameters.nodeId===nodeId);
|
|
12
|
-
const gateOpen=gate=>{
|
|
13
|
-
const p=gate.parameters;if(p.mode==='open')return true;if(p.mode==='closed')return false;
|
|
14
|
-
const period=p.openSeconds+p.closedSeconds,phase=(elapsed+p.phaseOffsetSeconds)%period;
|
|
15
|
-
return phase<p.openSeconds;
|
|
16
|
-
};
|
|
17
|
-
const outgoing=nodeId=>[...edgeMap.values()].filter(edge=>edge.enabled&&(edge.from===nodeId||edge.bidirectional&&edge.to===nodeId)).sort((a,b)=>a.id<b.id?-1:a.id>b.id?1:0);
|
|
18
|
-
const edgeBetween=(from,to)=>[...edgeMap.values()].find(edge=>edge.enabled&&(edge.from===from&&edge.to===to||edge.bidirectional&&edge.from===to&&edge.to===from));
|
|
19
|
-
function route(from,to){
|
|
20
|
-
if(from===to)return [from];
|
|
21
|
-
const distances=new Map([[from,0]]),previous=new Map(),open=new Set([from]);
|
|
22
|
-
while(open.size){
|
|
23
|
-
const current=[...open].sort((a,b)=>(distances.get(a)-distances.get(b))||(a<b?-1:a>b?1:0))[0];open.delete(current);
|
|
24
|
-
if(current===to)break;
|
|
25
|
-
for(const edge of outgoing(current)){
|
|
26
|
-
const next=edge.from===current?edge.to:edge.from,gate=gateAt(next),p=node(next);
|
|
27
|
-
const penalty=gate?gate.parameters.holdSeconds*90+(gateOpen(gate)?0:gate.parameters.closedSeconds*90):0;
|
|
28
|
-
const cost=distances.get(current)+distance(node(current),p)/edge.speedScale+edge.cost+(p.cost??0)+penalty;
|
|
29
|
-
if(cost>=(distances.get(next)??Infinity))continue;
|
|
30
|
-
distances.set(next,cost);previous.set(next,current);open.add(next);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
if(!distances.has(to))return [];
|
|
34
|
-
const result=[to];let current=to;while(current!==from){current=previous.get(current);result.unshift(current);}return result;
|
|
35
|
-
}
|
|
36
|
-
function replan(agent){
|
|
37
|
-
const hasSeg=agent.route.length>=2&&agent.segment<agent.route.length-1;
|
|
38
|
-
if(!hasSeg){
|
|
39
|
-
const current=agent.route[Math.min(agent.segment,agent.route.length-1)]??agent.startNode;
|
|
40
|
-
const fresh=route(current,agent.target);
|
|
41
|
-
agent.route=fresh.length?fresh:[current];agent.segment=0;agent.distance=0;agent.arrived=false;
|
|
42
|
-
agent.active=agent.route.length>=2;return;
|
|
43
|
-
}
|
|
44
|
-
const from=agent.route[agent.segment],next=agent.route[agent.segment+1],segLength=distance(node(from),node(next));
|
|
45
|
-
if(agent.arrived||agent.distance>=segLength-.0001){
|
|
46
|
-
const fresh=route(next,agent.target);
|
|
47
|
-
agent.route=fresh.length?fresh:[next];agent.segment=0;agent.distance=0;agent.arrived=false;
|
|
48
|
-
agent.active=agent.route.length>=2;return;
|
|
49
|
-
}
|
|
50
|
-
const tail=route(next,agent.target);
|
|
51
|
-
agent.route=tail.length?[from,...tail]:[from,next];
|
|
52
|
-
agent.segment=0;
|
|
53
|
-
}
|
|
54
|
-
function addAgent(parameters){
|
|
55
|
-
const initial=route(parameters.startNode,parameters.targetNode),start=node(parameters.startNode);
|
|
56
|
-
const nextPos=initial.length>=2?node(initial[1]):start;
|
|
57
|
-
const heading=initial.length>=2?Math.atan2(nextPos.y-start.y,nextPos.x-start.x):0;
|
|
58
|
-
const direction=Math.floor((((heading%(Math.PI*2))+Math.PI*2+1e-6)%(Math.PI*2))/(Math.PI/2))%4;
|
|
59
|
-
const agent={...parameters,loop:parameters.loop??'none',originNode:parameters.startNode,destinationNode:parameters.targetNode,target:parameters.targetNode,networkId:id,route:initial.length?initial:[parameters.startNode],segment:0,distance:0,x:start.x,y:start.y,heading,frame:[0,1,2,3][direction],flipX:Math.cos(heading)<0,hold:0,arrived:false,active:parameters.active&&initial.length>=2,blockedTicks:0,blockedEmitted:false,speedMultiplier:1};
|
|
60
|
-
agents.set(agent.id,agent);return agent;
|
|
61
|
-
}
|
|
62
|
-
function complete(agent){
|
|
63
|
-
emit('pathArrive',agent,agent.target);
|
|
64
|
-
if(agent.loop==='none'){agent.active=false;return;}
|
|
65
|
-
const from=agent.target;
|
|
66
|
-
const to=agent.loop==='ping-pong'?(from===agent.destinationNode?agent.originNode:agent.destinationNode):(from===agent.originNode?agent.destinationNode:agent.originNode);
|
|
67
|
-
agent.startNode=from;agent.target=to;agent.route=route(from,to);agent.segment=0;agent.distance=0;agent.arrived=false;
|
|
68
|
-
if(agent.route.length<2)agent.active=false;
|
|
69
|
-
}
|
|
70
|
-
function emit(kind,agent,nodeId){onEvent(kind,{componentId:agent.id,nodeId});}
|
|
71
|
-
function arrive(agent,dt){
|
|
72
|
-
const next=agent.route[agent.segment+1],gate=gateAt(next);
|
|
73
|
-
if(gate&&!gateOpen(gate)){agent.blockedTicks++;if(agent.blockedTicks>=30&&!agent.blockedEmitted){agent.blockedEmitted=true;emit('pathBlocked',agent,next);}return;}
|
|
74
|
-
if(!agent.arrived){
|
|
75
|
-
agent.arrived=true;
|
|
76
|
-
if(agent.segment+1===agent.route.length-1){if(next===agent.target)complete(agent);else{agent.active=false;if(!agent.blockedEmitted){agent.blockedEmitted=true;emit('pathBlocked',agent,next);}}return;}
|
|
77
|
-
agent.hold=Math.max(node(next).holdSeconds??0,gate?.parameters.holdSeconds??0);
|
|
78
|
-
}
|
|
79
|
-
if(agent.hold>0){agent.hold=Math.max(0,agent.hold-dt);agent.blockedTicks++;if(agent.blockedTicks>=30&&!agent.blockedEmitted){agent.blockedEmitted=true;emit('pathBlocked',agent,next);}return;}
|
|
80
|
-
agent.segment++;agent.distance=0;agent.arrived=false;agent.blockedTicks=0;agent.blockedEmitted=false;
|
|
81
|
-
}
|
|
82
|
-
function headway(agent,desired){
|
|
83
|
-
if(agent.spacing<=0)return desired;
|
|
84
|
-
const from=agent.route[agent.segment],to=agent.route[agent.segment+1],segLength=distance(node(from),node(to)),remaining=segLength-agent.distance;let allowed=desired;
|
|
85
|
-
for(const other of agentsById.values()){
|
|
86
|
-
if(other.id===agent.id||!other.active||other.route.length<2||other.segment>=other.route.length-1)continue;
|
|
87
|
-
const otherFrom=other.route[other.segment],otherTo=other.route[other.segment+1];let gap=Infinity;
|
|
88
|
-
if(otherFrom===from&&otherTo===to&&(other.distance>agent.distance||other.distance===agent.distance&&other.id<agent.id))gap=other.distance-agent.distance;
|
|
89
|
-
else if(otherFrom===to)gap=remaining+other.distance;
|
|
90
|
-
else if(otherTo===to&&otherFrom!==from){
|
|
91
|
-
const otherRem=distance(node(otherFrom),node(otherTo))-other.distance;
|
|
92
|
-
if(otherRem<agent.spacing&&(otherRem<remaining||otherRem===remaining&&other.id<agent.id))gap=remaining;
|
|
93
|
-
}
|
|
94
|
-
if(gap<Infinity)allowed=Math.min(allowed,Math.max(0,gap-agent.spacing));
|
|
95
|
-
}
|
|
96
|
-
return allowed;
|
|
97
|
-
}
|
|
98
|
-
function position(agent){
|
|
99
|
-
const from=node(agent.route[agent.segment]),to=node(agent.route[agent.segment+1]),segLength=distance(from,to),t=clamp(agent.distance/Math.max(.0001,segLength),0,1);
|
|
100
|
-
let x=from.x+(to.x-from.x)*t,y=from.y+(to.y-from.y)*t,heading=Math.atan2(to.y-from.y,to.x-from.x);
|
|
101
|
-
const radius=Math.min(agent.cornerRadius,segLength/3),nextId=agent.route[agent.segment+2];
|
|
102
|
-
if(radius>0&&nextId){
|
|
103
|
-
const next=node(nextId),nextLength=distance(to,next),r=Math.min(radius,nextLength/3);
|
|
104
|
-
if(agent.distance>segLength-radius){
|
|
105
|
-
const start={x:to.x-(to.x-from.x)/segLength*radius,y:to.y-(to.y-from.y)/segLength*radius},end={x:to.x+(next.x-to.x)/nextLength*r,y:to.y+(next.y-to.y)/nextLength*r};
|
|
106
|
-
const q=clamp((agent.distance-(segLength-radius))/(radius*2),0,.5),u=1-q;
|
|
107
|
-
x=u*u*start.x+2*u*q*to.x+q*q*end.x;y=u*u*start.y+2*u*q*to.y+q*q*end.y;
|
|
108
|
-
const dx=2*u*(to.x-start.x)+2*q*(end.x-to.x),dy=2*u*(to.y-start.y)+2*q*(end.y-to.y);if(Math.abs(dx)+Math.abs(dy)>.001)heading=Math.atan2(dy,dx);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return {x,y,heading};
|
|
112
|
-
}
|
|
113
|
-
function tick(dt){
|
|
114
|
-
elapsed+=dt;tickIndex++;
|
|
115
|
-
const ordered=[...agents.values()].sort((a,b)=>a.id<b.id?-1:a.id>b.id?1:0),offset=ordered.length?tickIndex%ordered.length:0;
|
|
116
|
-
const rotation=[...ordered.slice(offset),...ordered.slice(0,offset)];
|
|
117
|
-
for(const agent of rotation){
|
|
118
|
-
if(!agent.active||agent.route.length<2||agent.segment>=agent.route.length-1)continue;
|
|
119
|
-
const fromId=agent.route[agent.segment],toId=agent.route[agent.segment+1],segLength=distance(node(fromId),node(toId));
|
|
120
|
-
if(agent.distance>=segLength-.0001){arrive(agent,dt);if(!agent.active||agent.route.length<2||agent.segment>=agent.route.length-1)continue;}
|
|
121
|
-
const activeFrom=agent.route[agent.segment],activeTo=agent.route[agent.segment+1],activeLen=distance(node(activeFrom),node(activeTo));
|
|
122
|
-
const remaining=activeLen-agent.distance,edgeScale=edgeBetween(activeFrom,activeTo)?.speedScale??1;
|
|
123
|
-
const desired=Math.min(remaining,agent.speed*edgeScale*Math.max(0,agent.speedMultiplier)*dt),movement=headway(agent,desired);
|
|
124
|
-
if(movement<=.0001){agent.blockedTicks++;if(agent.blockedTicks>=30&&!agent.blockedEmitted){agent.blockedEmitted=true;emit('pathBlocked',agent,activeTo);}}
|
|
125
|
-
else{agent.distance+=movement;agent.blockedTicks=0;agent.blockedEmitted=false;}
|
|
126
|
-
const current=position(agent);agent.x=current.x;agent.y=current.y;agent.heading=current.heading;
|
|
127
|
-
const direction=Math.floor((((agent.heading%(Math.PI*2))+Math.PI*2+1e-6)%(Math.PI*2))/(Math.PI/2))%4;agent.frame=[0,1,2,3][direction];agent.flipX=Math.cos(agent.heading)<0;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
function snapshot(){return {id,nodeCount:nodeMap.size,edgeCount:edgeMap.size,activeAgents:[...agents.values()].filter(agent=>agent.active).length,queuedAgents:[...agents.values()].filter(agent=>agent.active&&agent.blockedTicks>0).length,agents:[...agents.values()].map(agent=>({id:agent.id,x:agent.x,y:agent.y,heading:agent.heading,frame:agent.frame??0,flipX:Boolean(agent.flipX),active:agent.active,targetNode:agent.target,route:[...agent.route],segment:agent.segment,distance:agent.distance,blocked:agent.blockedTicks>0}))};}
|
|
131
|
-
return {addAgent,removeAgent:id=>agents.delete(id),getAgent:id=>agents.get(id),setTarget:(agentId,target)=>{const agent=agents.get(agentId);if(!agent||!node(target))return false;agent.target=target;agent.destinationNode=target;agent.active=true;replan(agent);return true;},setEdge:(edgeId,enabled,cost)=>{const edge=edgeMap.get(edgeId);if(!edge)return false;edge.enabled=enabled;if(cost!==undefined)edge.cost=cost;for(const agent of agents.values())replan(agent);return true;},setGate:(gateId,mode,holdSeconds)=>{const gate=gateMap.get(gateId);if(!gate)return false;gate.parameters={...gate.parameters,mode,holdSeconds};for(const agent of agents.values())replan(agent);return true;},tick,snapshot};
|
|
132
|
-
}
|
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
const length=(x,y)=>Math.hypot(x,y);
|
|
2
|
-
const clamp=(value,min,max)=>Math.max(min,Math.min(max,value));
|
|
3
|
-
const radius=body=>Math.max(1,Math.min(body.width,body.height)/2);
|
|
4
|
-
|
|
5
|
-
function boxVertices(body){
|
|
6
|
-
const cos=Math.cos(body.angle??0),sin=Math.sin(body.angle??0),hw=body.width/2,hh=body.height/2;
|
|
7
|
-
return [[-hw,-hh],[hw,-hh],[hw,hh],[-hw,hh]].map(([lx,ly])=>({x:body.x+lx*cos-ly*sin,y:body.y+lx*sin+ly*cos}));
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function boxBoxContact(a,b){
|
|
11
|
-
const vertsA=boxVertices(a),vertsB=boxVertices(b);
|
|
12
|
-
const axes=[a.angle??0,(a.angle??0)+Math.PI/2,b.angle??0,(b.angle??0)+Math.PI/2];
|
|
13
|
-
let minOverlap=Infinity,bestNx=1,bestNy=0;
|
|
14
|
-
for(const theta of axes){
|
|
15
|
-
const ax=Math.cos(theta),ay=Math.sin(theta);
|
|
16
|
-
let aMin=Infinity,aMax=-Infinity,bMin=Infinity,bMax=-Infinity;
|
|
17
|
-
for(const v of vertsA){const p=v.x*ax+v.y*ay;if(p<aMin)aMin=p;if(p>aMax)aMax=p;}
|
|
18
|
-
for(const v of vertsB){const p=v.x*ax+v.y*ay;if(p<bMin)bMin=p;if(p>bMax)bMax=p;}
|
|
19
|
-
const overlap=Math.min(aMax,bMax)-Math.max(aMin,bMin);
|
|
20
|
-
if(overlap<=0)return null;
|
|
21
|
-
if(overlap<minOverlap){minOverlap=overlap;bestNx=ax;bestNy=ay;}
|
|
22
|
-
}
|
|
23
|
-
if((a.x-b.x)*bestNx+(a.y-b.y)*bestNy<0){bestNx=-bestNx;bestNy=-bestNy;}
|
|
24
|
-
return {nx:bestNx,ny:bestNy,penetration:minOverlap};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function circleBoxContact(circle,box){
|
|
28
|
-
const r=radius(circle),angle=-(box.angle??0),cos=Math.cos(angle),sin=Math.sin(angle);
|
|
29
|
-
const dx=circle.x-box.x,dy=circle.y-box.y,lx=dx*cos-dy*sin,ly=dx*sin+dy*cos,hw=box.width/2,hh=box.height/2;
|
|
30
|
-
const qx=clamp(lx,-hw,hw),qy=clamp(ly,-hh,hh);let ox=lx-qx,oy=ly-qy,distance=length(ox,oy),penetration;
|
|
31
|
-
if(distance>=r)return null;
|
|
32
|
-
if(distance<.0001){
|
|
33
|
-
const faces=[{d:hw-Math.abs(lx),x:lx>=0?1:-1,y:0},{d:hh-Math.abs(ly),x:0,y:ly>=0?1:-1}].sort((a,b)=>a.d-b.d);
|
|
34
|
-
ox=faces[0].x;oy=faces[0].y;penetration=r+faces[0].d;
|
|
35
|
-
}else{ox/=distance;oy/=distance;penetration=r-distance;}
|
|
36
|
-
const ca=Math.cos(box.angle??0),sa=Math.sin(box.angle??0);
|
|
37
|
-
return {nx:ox*ca-oy*sa,ny:ox*sa+oy*ca,penetration};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function ignores(a,b){return Boolean(a.ignoreGroup&&a.ignoreGroup!=='none'&&(a.ignoreGroup==='all'||a.ignoreGroup===(b.collisionGroup??'all')));}
|
|
41
|
-
|
|
42
|
-
function collide(body,other,onEvent){
|
|
43
|
-
if(body.captured||other.captured||ignores(body,other)||ignores(other,body))return;
|
|
44
|
-
let nx=body.x-other.x,ny=body.y-other.y,penetration;
|
|
45
|
-
if(body.shape==='circle'&&other.shape==='circle'){
|
|
46
|
-
const distance=length(nx,ny),overlap=radius(body)+radius(other)-distance;
|
|
47
|
-
if(overlap<=0)return;
|
|
48
|
-
penetration=overlap;
|
|
49
|
-
if(distance>.0001){nx/=distance;ny/=distance;}else{nx=1;ny=0;}
|
|
50
|
-
}else if(body.shape==='circle'&&other.shape==='box'){
|
|
51
|
-
const contact=circleBoxContact(body,other);if(!contact)return;
|
|
52
|
-
nx=contact.nx;ny=contact.ny;penetration=contact.penetration;
|
|
53
|
-
}else if(body.shape==='box'&&other.shape==='circle'){
|
|
54
|
-
const contact=circleBoxContact(other,body);if(!contact)return;
|
|
55
|
-
nx=-contact.nx;ny=-contact.ny;penetration=contact.penetration;
|
|
56
|
-
}else if(body.shape==='box'&&other.shape==='box'){
|
|
57
|
-
const contact=boxBoxContact(body,other);if(!contact)return;
|
|
58
|
-
nx=contact.nx;ny=contact.ny;penetration=contact.penetration;
|
|
59
|
-
}else return;
|
|
60
|
-
const inverseBody=body.mode==='dynamic'?1/(body.mass??1):0,inverseOther=other.mode==='dynamic'?1/(other.mass??1):0,total=inverseBody+inverseOther;
|
|
61
|
-
if(!total)return;
|
|
62
|
-
body.x+=nx*penetration*inverseBody/total;body.y+=ny*penetration*inverseBody/total;
|
|
63
|
-
if(other.mode==='dynamic'){other.x-=nx*penetration*inverseOther/total;other.y-=ny*penetration*inverseOther/total;}
|
|
64
|
-
const rvx=body.vx-(other.vx??0),rvy=body.vy-(other.vy??0),normalSpeed=rvx*nx+rvy*ny;
|
|
65
|
-
if(normalSpeed>=0)return;
|
|
66
|
-
const restitution=Math.max(body.restitution??0,other.restitution??0),impulse=-(1+restitution)*normalSpeed/total;
|
|
67
|
-
body.vx+=nx*impulse*inverseBody;body.vy+=ny*impulse*inverseBody;
|
|
68
|
-
if(other.mode==='dynamic'){other.vx-=nx*impulse*inverseOther;other.vy-=ny*impulse*inverseOther;}
|
|
69
|
-
body.atRest=false;body.restElapsed=0;
|
|
70
|
-
if(other.mode==='dynamic'){other.atRest=false;other.restElapsed=0;}
|
|
71
|
-
if(Math.abs(normalSpeed)>4){
|
|
72
|
-
onEvent('bodyBounce',body,other,{speed:Math.abs(normalSpeed)});
|
|
73
|
-
if(other.mode==='dynamic')onEvent('bodyBounce',other,body,{speed:Math.abs(normalSpeed)});
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function capture(body,zones,dt,onEvent){
|
|
78
|
-
if(body.mode!=='dynamic'||body.captured)return;
|
|
79
|
-
for(const zone of zones){
|
|
80
|
-
const dx=zone.x-body.x,dy=zone.y-body.y,distance=length(dx,dy);
|
|
81
|
-
if(distance>=zone.radius*2.5)continue;
|
|
82
|
-
if(distance>zone.radius&&distance>.001&&zone.pullStrength>0){
|
|
83
|
-
const strength=zone.pullStrength*(1-distance/(zone.radius*2.5));body.vx+=dx/distance*strength*dt;body.vy+=dy/distance*strength*dt;
|
|
84
|
-
}
|
|
85
|
-
if(distance<zone.radius&&length(body.vx,body.vy)<=zone.maxCaptureSpeed){
|
|
86
|
-
body.captured=true;body.atRest=true;body.vx=zone.stopBody?0:body.vx;body.vy=zone.stopBody?0:body.vy;
|
|
87
|
-
if(zone.stopBody){body.x=zone.x;body.y=zone.y;}
|
|
88
|
-
onEvent('zoneCapture',body,zone,{});return;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function projectJoints(joints,bodies){
|
|
94
|
-
for(const joint of joints){
|
|
95
|
-
if(joint.broken)continue;const body=bodies.find(item=>item.id===joint.bodyId),anchor=bodies.find(item=>item.id===joint.anchorId);if(!body||body.mode!=='dynamic')continue;
|
|
96
|
-
const ax=anchor?.x??joint.x??0,ay=anchor?.y??joint.y??0,dx=body.x-ax,dy=body.y-ay,distance=length(dx,dy);if(distance<=joint.lengthPx||distance<.0001)continue;
|
|
97
|
-
const nx=dx/distance,ny=dy/distance;body.x=ax+nx*joint.lengthPx;body.y=ay+ny*joint.lengthPx;const outward=body.vx*nx+body.vy*ny;if(outward>0){body.vx-=nx*outward;body.vy-=ny*outward;}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
export function stepPhysics({bodies,zones=[],joints=[],bounds,dt,onEvent=()=>{}}){
|
|
101
|
-
const dynamic=bodies.filter(body=>body.mode==='dynamic'&&!body.captured);
|
|
102
|
-
const fastest=dynamic.reduce((speed,body)=>Math.max(speed,length(body.vx,body.vy)),0);
|
|
103
|
-
const smallest=dynamic.reduce((size,body)=>Math.min(size,radius(body)),24);
|
|
104
|
-
const steps=Math.min(16,Math.max(1,Math.ceil(fastest*dt/(smallest*.45)))),h=dt/steps;
|
|
105
|
-
for(let step=0;step<steps;step++){
|
|
106
|
-
for(const body of dynamic){
|
|
107
|
-
body.vy+=(body.gravity??0)*h;
|
|
108
|
-
let speed=length(body.vx,body.vy);
|
|
109
|
-
if(speed>body.maxSpeed){const cap=body.maxSpeed/speed;body.vx*=cap;body.vy*=cap;speed=body.maxSpeed;}
|
|
110
|
-
body.x+=body.vx*h;body.y+=body.vy*h;
|
|
111
|
-
const nextSpeed=Math.max(0,speed-(body.friction??0)*h),damping=Math.exp(-(body.linearDamping??0)*h);
|
|
112
|
-
if(speed>.0001){const scale=nextSpeed/speed*damping;body.vx*=scale;body.vy*=scale;}
|
|
113
|
-
if(body.spinToRotation)body.angle=(body.angle??0)+length(body.vx,body.vy)*h/radius(body)*(body.vx<0?-1:1);
|
|
114
|
-
}
|
|
115
|
-
for(let index=0;index<dynamic.length;index++){
|
|
116
|
-
const body=dynamic[index];
|
|
117
|
-
for(const other of bodies)if(other.id!==body.id&&other.mode!=='dynamic')collide(body,other,onEvent);
|
|
118
|
-
for(let other=index+1;other<dynamic.length;other++)collide(dynamic[other],body,onEvent);
|
|
119
|
-
const hx=body.shape==='box'?body.width/2:radius(body),hy=body.shape==='box'?body.height/2:radius(body);
|
|
120
|
-
if(body.wallBounce){
|
|
121
|
-
if(body.x-hx<bounds.left){body.x=bounds.left+hx;if(body.vx<0){onEvent('bodyBounce',body,null,{speed:Math.abs(body.vx)});body.vx=-body.vx*body.restitution;}}
|
|
122
|
-
if(body.x+hx>bounds.right){body.x=bounds.right-hx;if(body.vx>0){onEvent('bodyBounce',body,null,{speed:Math.abs(body.vx)});body.vx=-body.vx*body.restitution;}}
|
|
123
|
-
if(body.y-hy<bounds.top){body.y=bounds.top+hy;if(body.vy<0){onEvent('bodyBounce',body,null,{speed:Math.abs(body.vy)});body.vy=-body.vy*body.restitution;}}
|
|
124
|
-
if(body.y+hy>bounds.bottom){body.y=bounds.bottom-hy;if(body.vy>0){onEvent('bodyBounce',body,null,{speed:Math.abs(body.vy)});body.vy=-body.vy*body.restitution;}}
|
|
125
|
-
}else if(!body.exited&&(body.x+hx<bounds.left||body.x-hx>bounds.right||body.y+hy<bounds.top||body.y-hy>bounds.bottom)){
|
|
126
|
-
body.exited=true;onEvent('boundaryExit',body,null,{});
|
|
127
|
-
}
|
|
128
|
-
capture(body,zones,h,onEvent);
|
|
129
|
-
}
|
|
130
|
-
projectJoints(joints,bodies);
|
|
131
|
-
}
|
|
132
|
-
for(const body of dynamic){
|
|
133
|
-
const speed=length(body.vx,body.vy);
|
|
134
|
-
if(speed<body.restSpeed&&!body.captured){
|
|
135
|
-
body.restElapsed=(body.restElapsed??0)+dt;
|
|
136
|
-
if(body.restElapsed>=body.restDelaySeconds&&!body.atRest){body.atRest=true;body.vx=0;body.vy=0;onEvent('bodyRest',body,null,{});}
|
|
137
|
-
}else if(speed>=body.restSpeed){body.restElapsed=0;body.atRest=false;}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export function predictTrajectory({body,bodies,zones=[],bounds,dots=8,bounces=1}){
|
|
142
|
-
const copy=source=>({...source});
|
|
143
|
-
const simulated=copy(body),others=bodies.filter(item=>item.id!==body.id).map(copy),all=[simulated,...others],points=[];
|
|
144
|
-
let bounceCount=0,finished=false;
|
|
145
|
-
const onEvent=(kind,_body,_other,meta)=>{
|
|
146
|
-
if(kind==='bodyBounce'&&meta.sourceId===body.id)bounceCount++;
|
|
147
|
-
if(kind==='zoneCapture'&&meta.sourceId===body.id)finished=true;
|
|
148
|
-
};
|
|
149
|
-
for(let tick=0;tick<90&&points.length<dots&&!finished&&bounceCount<=bounces;tick++){
|
|
150
|
-
stepPhysics({bodies:all,zones,bounds,dt:1/60,onEvent:(kind,source,other,meta)=>onEvent(kind,source,other,{...meta,sourceId:source.id})});
|
|
151
|
-
if(tick%Math.max(1,Math.floor(90/dots))===0)points.push({x:simulated.x,y:simulated.y});
|
|
152
|
-
}
|
|
153
|
-
return points;
|
|
154
|
-
}
|
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
/** Fixed-step 3D golf/ball physics engine for Tora native 3D scenes. World units are metres; +Y is up. */
|
|
2
|
-
|
|
3
|
-
export const DT_3D = 1 / 120;
|
|
4
|
-
|
|
5
|
-
export function ground3d(body, x, z) {
|
|
6
|
-
const bx = body.boundsX ?? 3;
|
|
7
|
-
const bz = body.boundsZ ?? 7;
|
|
8
|
-
if (Math.abs(x) > bx || Math.abs(z) > bz) return null;
|
|
9
|
-
if (!body.ramp) return 0;
|
|
10
|
-
const h = body.rampHeight ?? 1.1;
|
|
11
|
-
if (z >= 1) return 0;
|
|
12
|
-
if (z <= -1) return h;
|
|
13
|
-
return (1 - z) * (h * 0.5);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function collideWall(body, wall, dt, onEvent) {
|
|
17
|
-
const r = body.radius;
|
|
18
|
-
const c = Math.cos(wall.angle);
|
|
19
|
-
const s = Math.sin(wall.angle);
|
|
20
|
-
const px = body.x - wall.x;
|
|
21
|
-
const pz = body.z - wall.z;
|
|
22
|
-
const x = c * px + s * pz;
|
|
23
|
-
const z = -s * px + c * pz;
|
|
24
|
-
const cx = Math.max(-wall.hx, Math.min(wall.hx, x));
|
|
25
|
-
const cz = Math.max(-wall.hz, Math.min(wall.hz, z));
|
|
26
|
-
let nx = x - cx;
|
|
27
|
-
let nz = z - cz;
|
|
28
|
-
let d = Math.hypot(nx, nz);
|
|
29
|
-
const floor = ground3d(body, body.x, body.z);
|
|
30
|
-
if (d >= r || (floor !== null && body.y > floor + 0.55)) return;
|
|
31
|
-
if (d < 1e-8) {
|
|
32
|
-
if (wall.hx - Math.abs(x) < wall.hz - Math.abs(z)) {
|
|
33
|
-
nx = x < 0 ? -1 : 1;
|
|
34
|
-
nz = 0;
|
|
35
|
-
d = 0;
|
|
36
|
-
} else {
|
|
37
|
-
nx = 0;
|
|
38
|
-
nz = z < 0 ? -1 : 1;
|
|
39
|
-
d = 0;
|
|
40
|
-
}
|
|
41
|
-
} else {
|
|
42
|
-
nx /= d;
|
|
43
|
-
nz /= d;
|
|
44
|
-
}
|
|
45
|
-
const wx = c * nx - s * nz;
|
|
46
|
-
const wz = s * nx + c * nz;
|
|
47
|
-
body.x += wx * (r - d + 0.00001);
|
|
48
|
-
body.z += wz * (r - d + 0.00001);
|
|
49
|
-
const wallVx = wall.vx ?? 0;
|
|
50
|
-
const wallVz = wall.vz ?? 0;
|
|
51
|
-
const dot = (body.vx - wallVx) * wx + (body.vz - wallVz) * wz;
|
|
52
|
-
if (dot < 0) {
|
|
53
|
-
const bounce = 1 + (body.restitution ?? 0.72);
|
|
54
|
-
body.vx -= bounce * dot * wx;
|
|
55
|
-
body.vz -= bounce * dot * wz;
|
|
56
|
-
if (Math.abs(dot) > 0.3 && dt > 0 && onEvent) {
|
|
57
|
-
onEvent('bodyBounce', body, wall.sourceId ? wall : undefined);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function settle(body, onEvent) {
|
|
63
|
-
body.vx = 0;
|
|
64
|
-
body.vy = 0;
|
|
65
|
-
body.vz = 0;
|
|
66
|
-
body.airborne = false;
|
|
67
|
-
body.atRest = true;
|
|
68
|
-
body.restElapsed = 0;
|
|
69
|
-
body.movingTime = 0;
|
|
70
|
-
if (onEvent) onEvent('bodyRest', body);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function recover(body, onEvent) {
|
|
74
|
-
body.x = body.lastSafe.x;
|
|
75
|
-
body.y = body.lastSafe.y;
|
|
76
|
-
body.z = body.lastSafe.z;
|
|
77
|
-
settle(body, onEvent);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Advance all dynamic 3D bodies by dt using 120Hz substeps. */
|
|
81
|
-
export function stepPhysics3d({bodies, walls, zones, dt, onEvent}) {
|
|
82
|
-
if (!Number.isFinite(dt) || dt <= 0) return;
|
|
83
|
-
const substeps = Math.max(1, Math.round(dt / DT_3D));
|
|
84
|
-
const stepDt = dt / substeps;
|
|
85
|
-
for (let s = 0; s < substeps; s++) {
|
|
86
|
-
for (const body of bodies) {
|
|
87
|
-
if (body.mode !== 'dynamic' || body.captured) continue;
|
|
88
|
-
if (body.atRest) continue;
|
|
89
|
-
const bx = body.boundsX ?? 3;
|
|
90
|
-
const bz = body.boundsZ ?? 7;
|
|
91
|
-
if (body.y < -3 || Math.abs(body.x) > bx + 2 || Math.abs(body.z) > bz + 2) {
|
|
92
|
-
recover(body, onEvent);
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
body.movingTime = (body.movingTime ?? 0) + stepDt;
|
|
96
|
-
const r = body.radius;
|
|
97
|
-
const speed3 = Math.hypot(body.vx, body.vy, body.vz);
|
|
98
|
-
const count = Math.max(1, Math.min(16, Math.ceil(speed3 * stepDt / (r * 0.45))));
|
|
99
|
-
const h = stepDt / count;
|
|
100
|
-
if (!body._perimeter || body._pbx !== bx || body._pbz !== bz) {
|
|
101
|
-
body._pbx = bx;
|
|
102
|
-
body._pbz = bz;
|
|
103
|
-
body._perimeter = [
|
|
104
|
-
{x: -(bx + 0.1), z: 0, hx: 0.1, hz: bz + 0.1, angle: 0, vx: 0, vz: 0},
|
|
105
|
-
{x: bx + 0.1, z: 0, hx: 0.1, hz: bz + 0.1, angle: 0, vx: 0, vz: 0},
|
|
106
|
-
{x: 0, z: -(bz + 0.1), hx: bx + 0.1, hz: 0.1, angle: 0, vx: 0, vz: 0},
|
|
107
|
-
{x: 0, z: bz + 0.1, hx: bx + 0.1, hz: 0.1, angle: 0, vx: 0, vz: 0},
|
|
108
|
-
];
|
|
109
|
-
}
|
|
110
|
-
const perimeter = body._perimeter;
|
|
111
|
-
let capturedThisStep = false;
|
|
112
|
-
for (let i = 0; i < count; i++) {
|
|
113
|
-
const floor = ground3d(body, body.x, body.z);
|
|
114
|
-
const supported = floor !== null && body.y <= floor + r + 0.012 && body.vy <= 0.1;
|
|
115
|
-
const oldFloor = floor;
|
|
116
|
-
if (supported) {
|
|
117
|
-
body.y = floor + r;
|
|
118
|
-
body.vy = 0;
|
|
119
|
-
const speed = Math.hypot(body.vx, body.vz);
|
|
120
|
-
const nextSpeed = Math.max(0, speed - (body.friction ?? 1.05) * h);
|
|
121
|
-
if (speed > 0) {
|
|
122
|
-
body.vx *= nextSpeed / speed;
|
|
123
|
-
body.vz *= nextSpeed / speed;
|
|
124
|
-
}
|
|
125
|
-
if (body.ramp && body.z < 1 && body.z > -1) {
|
|
126
|
-
const slope = (body.rampHeight ?? 1.1) * 0.5;
|
|
127
|
-
body.vz += 9.81 * slope / (1 + slope * slope) * h;
|
|
128
|
-
}
|
|
129
|
-
} else {
|
|
130
|
-
body.vy -= 9.81 * h;
|
|
131
|
-
if (!body.airborne) {
|
|
132
|
-
body.airborne = true;
|
|
133
|
-
if (onEvent) onEvent('bodyJump', body);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
body.x += body.vx * h;
|
|
137
|
-
body.z += body.vz * h;
|
|
138
|
-
body.y += body.vy * h;
|
|
139
|
-
const nextFloor = ground3d(body, body.x, body.z);
|
|
140
|
-
if (nextFloor !== null) {
|
|
141
|
-
if (supported && oldFloor !== null) {
|
|
142
|
-
const slope = (body.rampHeight ?? 1.1) * 0.5;
|
|
143
|
-
if (body.ramp && body.z <= -1 && body.z - body.vz * h > -1.5 && body.vz < -0.8) {
|
|
144
|
-
body.vy = -body.vz * slope;
|
|
145
|
-
body.airborne = true;
|
|
146
|
-
if (onEvent) onEvent('bodyJump', body);
|
|
147
|
-
} else {
|
|
148
|
-
body.y = nextFloor + r;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
if (body.y < nextFloor + r) {
|
|
152
|
-
body.y = nextFloor + r;
|
|
153
|
-
body.vy = 0;
|
|
154
|
-
body.airborne = false;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
if (body.y < 2) {
|
|
158
|
-
for (const wall of walls) collideWall(body, wall, h, onEvent);
|
|
159
|
-
for (const wall of perimeter) collideWall(body, wall, h, onEvent);
|
|
160
|
-
}
|
|
161
|
-
body.spin = (body.spin ?? 0) + Math.hypot(body.vx, body.vz) * h / r;
|
|
162
|
-
const horizSpeed = Math.hypot(body.vx, body.vz);
|
|
163
|
-
for (const zone of zones) {
|
|
164
|
-
const dist = Math.hypot(body.x - zone.x, body.z - zone.z);
|
|
165
|
-
if (dist < zone.radius && Math.abs(body.y - (zone.y + r)) < zone.heightTolerance && horizSpeed < zone.maxCaptureSpeed) {
|
|
166
|
-
body.captured = true;
|
|
167
|
-
body.captureElapsed = 0;
|
|
168
|
-
body.captureTarget = {x: zone.x, y: zone.y, z: zone.z};
|
|
169
|
-
body.vx = 0;
|
|
170
|
-
body.vy = 0;
|
|
171
|
-
body.vz = 0;
|
|
172
|
-
body.atRest = true;
|
|
173
|
-
if (onEvent) onEvent('zoneCapture', body, zone);
|
|
174
|
-
capturedThisStep = true;
|
|
175
|
-
break;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
if (capturedThisStep) break;
|
|
179
|
-
if (body.y < -3 || Math.abs(body.x) > bx + 2 || Math.abs(body.z) > bz + 2) {
|
|
180
|
-
recover(body, onEvent);
|
|
181
|
-
capturedThisStep = true;
|
|
182
|
-
break;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
if (capturedThisStep) continue;
|
|
186
|
-
const totalSpeed = Math.hypot(body.vx, body.vy, body.vz);
|
|
187
|
-
if (totalSpeed < (body.restSpeed ?? 0.07)) {
|
|
188
|
-
body.restElapsed = (body.restElapsed ?? 0) + stepDt;
|
|
189
|
-
if (body.restElapsed > (body.restDelaySeconds ?? 0.25)) settle(body, onEvent);
|
|
190
|
-
} else {
|
|
191
|
-
body.restElapsed = 0;
|
|
192
|
-
}
|
|
193
|
-
if (body.movingTime > 18) recover(body, onEvent);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/** Predict dotted 3D trajectory points for aim preview (matches original predict()). */
|
|
199
|
-
export function predictTrajectory3d({body, walls, zones, vx, vz, dots = 15}) {
|
|
200
|
-
const speed = Math.hypot(vx, vz);
|
|
201
|
-
if (!Number.isFinite(speed) || speed < 0.35) return [];
|
|
202
|
-
const scale = Math.min(1, (body.maxSpeed ?? 9) / speed);
|
|
203
|
-
const ghost = {
|
|
204
|
-
...body,
|
|
205
|
-
vx: vx * scale,
|
|
206
|
-
vy: 0,
|
|
207
|
-
vz: vz * scale,
|
|
208
|
-
atRest: false,
|
|
209
|
-
captured: false,
|
|
210
|
-
airborne: false,
|
|
211
|
-
restElapsed: 0,
|
|
212
|
-
movingTime: 0,
|
|
213
|
-
lastSafe: {x: body.x, y: body.y, z: body.z},
|
|
214
|
-
};
|
|
215
|
-
const staticWalls = walls.map(w => ({...w, vx: 0, vz: 0}));
|
|
216
|
-
const ghostList = [ghost];
|
|
217
|
-
const sim = {bodies: ghostList, walls: staticWalls, zones, dt: DT_3D};
|
|
218
|
-
const points = [];
|
|
219
|
-
const steps = Math.max(18, dots * 6);
|
|
220
|
-
for (let i = 0; i < steps; i++) {
|
|
221
|
-
stepPhysics3d(sim);
|
|
222
|
-
if (i % 6 === 0) points.push({x: ghost.x, y: ghost.y, z: ghost.z});
|
|
223
|
-
if (ghost.atRest || ghost.captured) break;
|
|
224
|
-
}
|
|
225
|
-
return points;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/** Push a vertical cylinder (x, z, radius) out of rotated OBB walls and arena perimeter bounds. */
|
|
229
|
-
export function resolveWallSlide3d(x, z, radius, walls = [], boundsX = 12, boundsZ = 12) {
|
|
230
|
-
let px = x, pz = z;
|
|
231
|
-
const r = Math.max(0.05, radius ?? 0.45);
|
|
232
|
-
for (let pass = 0; pass < 2; pass++) {
|
|
233
|
-
for (const wall of walls) {
|
|
234
|
-
const c = Math.cos(wall.angle ?? 0), s = Math.sin(wall.angle ?? 0);
|
|
235
|
-
const dx = px - wall.x, dz = pz - wall.z;
|
|
236
|
-
const lx = c * dx + s * dz, lz = -s * dx + c * dz;
|
|
237
|
-
const cx = Math.max(-wall.hx, Math.min(wall.hx, lx));
|
|
238
|
-
const cz = Math.max(-wall.hz, Math.min(wall.hz, lz));
|
|
239
|
-
let nx = lx - cx, nz = lz - cz;
|
|
240
|
-
const d = Math.hypot(nx, nz);
|
|
241
|
-
if (d >= r) continue;
|
|
242
|
-
if (d < 1e-8) {
|
|
243
|
-
if (wall.hx - Math.abs(lx) < wall.hz - Math.abs(lz)) { nx = lx < 0 ? -1 : 1; nz = 0; }
|
|
244
|
-
else { nx = 0; nz = lz < 0 ? -1 : 1; }
|
|
245
|
-
} else {
|
|
246
|
-
nx /= d;
|
|
247
|
-
nz /= d;
|
|
248
|
-
}
|
|
249
|
-
const wx = c * nx - s * nz, wz = s * nx + c * nz;
|
|
250
|
-
px += wx * (r - d + 1e-4);
|
|
251
|
-
pz += wz * (r - d + 1e-4);
|
|
252
|
-
}
|
|
253
|
-
const maxX = Math.max(0, (boundsX ?? 12) - r);
|
|
254
|
-
const maxZ = Math.max(0, (boundsZ ?? 12) - r);
|
|
255
|
-
px = Math.max(-maxX, Math.min(maxX, px));
|
|
256
|
-
pz = Math.max(-maxZ, Math.min(maxZ, pz));
|
|
257
|
-
}
|
|
258
|
-
return {x: px, z: pz};
|
|
259
|
-
}
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import {chromium} from 'playwright';
|
|
2
|
-
import {readFile,writeFile,mkdir} from 'node:fs/promises';
|
|
3
|
-
import {resolve} from 'node:path';
|
|
4
|
-
import {createHash} from 'node:crypto';
|
|
5
|
-
import {Buffer} from 'node:buffer';
|
|
6
|
-
import process from 'node:process';
|
|
7
|
-
import {analyzeWav} from './audio-analysis.mjs';
|
|
8
|
-
|
|
9
|
-
const root=resolve(import.meta.dirname,'../../..');
|
|
10
|
-
const hash=bytes=>'sha256:'+createHash('sha256').update(bytes).digest('hex');
|
|
11
|
-
const licenseUrl='https://creativecommons.org/publicdomain/zero/1.0/';
|
|
12
|
-
const kenney={
|
|
13
|
-
interface:'kenney-interface-sounds',
|
|
14
|
-
jingles:'kenney-music-jingles',
|
|
15
|
-
};
|
|
16
|
-
const familyDefinitions=[
|
|
17
|
-
{
|
|
18
|
-
id:'casual-warm',label:'Casual Warm',styleTags:['casual','warm','acoustic'],
|
|
19
|
-
ambientSource:{
|
|
20
|
-
id:'tarush-singhal-happy-ukelele',author:'Tarush Singhal',license:'CC0-1.0',
|
|
21
|
-
sourceUrl:'https://opengameart.org/content/happy-ukelele-island-surfing-theme',styleFamily:'casual-warm',
|
|
22
|
-
licenseFile:'assets/free-audio/cozy-warm/source/source-page.html',
|
|
23
|
-
archive:{url:'https://opengameart.org/sites/default/files/ukele.mp3',file:'assets/free-audio/cozy-warm/source/ukele.mp3'},
|
|
24
|
-
},
|
|
25
|
-
cues:[
|
|
26
|
-
{role:'ambient',sourceId:'tarush-singhal-happy-ukelele',input:'assets/free-audio/cozy-warm/source/ukele.mp3',duration:16,valence:'positive',energy:'low',loopable:true,tags:['ukulele','island','music','loop']},
|
|
27
|
-
{role:'tap',sourceId:kenney.interface,input:'assets/kenney-harvest/source/interface-sounds/Audio/select_001.ogg',valence:'neutral',energy:'low',tags:['interface','select','short-cue']},
|
|
28
|
-
{role:'match',sourceId:kenney.interface,input:'assets/kenney-harvest/source/interface-sounds/Audio/confirmation_002.ogg',valence:'positive',energy:'medium',tags:['interface','confirmation','short-cue']},
|
|
29
|
-
{role:'reject',sourceId:kenney.interface,input:'assets/kenney-harvest/source/interface-sounds/Audio/error_003.ogg',valence:'negative',energy:'medium',tags:['interface','error','short-cue']},
|
|
30
|
-
{role:'reward',sourceId:kenney.jingles,input:'assets/kenney-harvest/source/music-jingles/OGG/jingles_SAX/jingles_SAX09.ogg',valence:'positive',energy:'medium',tags:['sax','jingle','reward']},
|
|
31
|
-
{role:'win',sourceId:kenney.jingles,input:'assets/kenney-harvest/source/music-jingles/OGG/jingles_SAX/jingles_SAX03.ogg',valence:'positive',energy:'high',tags:['sax','jingle','fanfare','success']},
|
|
32
|
-
{role:'loss',sourceId:kenney.jingles,input:'assets/kenney-harvest/source/music-jingles/OGG/jingles_SAX/jingles_SAX00.ogg',valence:'negative',energy:'medium',tags:['sax','jingle','failure','timeout']},
|
|
33
|
-
],
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
id:'arcade-pop',label:'Arcade Pop',styleTags:['arcade','pop-punk','chiptune'],
|
|
37
|
-
ambientSource:{
|
|
38
|
-
id:'3xblast-pop-punk-chiptune',author:'3xBlast',license:'CC0-1.0',
|
|
39
|
-
sourceUrl:'https://opengameart.org/content/7-pop-punk-chiptune-tracks',styleFamily:'arcade-pop',
|
|
40
|
-
licenseFile:'assets/free-audio/arcade-pop/source/INFO.txt',
|
|
41
|
-
archive:{url:'https://opengameart.org/sites/default/files/3xblast_-_7_pop-punk_chiptune_tracks_0.zip',file:'assets/free-audio/arcade-pop/source/pop-punk-chiptune.zip'},
|
|
42
|
-
},
|
|
43
|
-
cues:[
|
|
44
|
-
{role:'ambient',sourceId:'3xblast-pop-punk-chiptune',input:'assets/free-audio/arcade-pop/source/3xBlast - A Band of Jellyfish (Looped).ogg',duration:16,valence:'positive',energy:'high',loopable:true,tags:['chiptune','pop-punk','music','loop']},
|
|
45
|
-
{role:'tap',sourceId:kenney.interface,input:'assets/kenney-harvest/source/interface-sounds/Audio/click_001.ogg',valence:'neutral',energy:'medium',tags:['interface','click','short-cue']},
|
|
46
|
-
{role:'match',sourceId:kenney.interface,input:'assets/kenney-harvest/source/interface-sounds/Audio/confirmation_004.ogg',valence:'positive',energy:'high',tags:['interface','confirmation','short-cue']},
|
|
47
|
-
{role:'reject',sourceId:kenney.interface,input:'assets/kenney-harvest/source/interface-sounds/Audio/error_008.ogg',valence:'negative',energy:'high',tags:['interface','error','short-cue']},
|
|
48
|
-
{role:'reward',sourceId:kenney.jingles,input:'assets/kenney-harvest/source/music-jingles/OGG/jingles_PIZZA/jingles_PIZZA09.ogg',valence:'positive',energy:'high',tags:['chiptune','jingle','reward']},
|
|
49
|
-
{role:'win',sourceId:kenney.jingles,input:'assets/kenney-harvest/source/music-jingles/OGG/jingles_PIZZA/jingles_PIZZA03.ogg',valence:'positive',energy:'high',tags:['chiptune','jingle','fanfare','success']},
|
|
50
|
-
{role:'loss',sourceId:kenney.jingles,input:'assets/kenney-harvest/source/music-jingles/OGG/jingles_PIZZA/jingles_PIZZA00.ogg',valence:'negative',energy:'medium',tags:['chiptune','jingle','failure','timeout']},
|
|
51
|
-
],
|
|
52
|
-
},
|
|
53
|
-
];
|
|
54
|
-
const catalogFile=resolve(root,'assets/free-assets/catalog.json');
|
|
55
|
-
const catalog=JSON.parse(await readFile(catalogFile,'utf8'));
|
|
56
|
-
const browser=await chromium.launch();
|
|
57
|
-
const receipts=[];
|
|
58
|
-
try{
|
|
59
|
-
const page=await browser.newPage();
|
|
60
|
-
for(const family of familyDefinitions){
|
|
61
|
-
const outputFolder=resolve(root,'assets/free-audio',family.id,'audio');
|
|
62
|
-
await mkdir(outputFolder,{recursive:true});
|
|
63
|
-
const files=[];
|
|
64
|
-
for(const cue of family.cues){
|
|
65
|
-
const input=await readFile(resolve(root,cue.input));
|
|
66
|
-
const converted=await page.evaluate(async({base64,duration})=>{
|
|
67
|
-
const bytes=Uint8Array.from(globalThis.atob(base64),character=>character.charCodeAt(0));
|
|
68
|
-
const decoder=new globalThis.OfflineAudioContext(1,1,16000);
|
|
69
|
-
const decoded=await decoder.decodeAudioData(bytes.buffer);
|
|
70
|
-
const seconds=Math.min(decoded.duration,duration??decoded.duration);
|
|
71
|
-
const context=new globalThis.OfflineAudioContext(1,Math.ceil(seconds*16000),16000);
|
|
72
|
-
const source=context.createBufferSource();source.buffer=decoded;source.connect(context.destination);source.start();
|
|
73
|
-
const pcm=(await context.startRendering()).getChannelData(0);
|
|
74
|
-
let sourcePeak=0;for(const sample of pcm)sourcePeak=Math.max(sourcePeak,Math.abs(sample));
|
|
75
|
-
const gain=Math.min(1,0.891250938/sourcePeak),fade=duration?320:0;
|
|
76
|
-
const output=new Uint8Array(44+pcm.length*2),view=new DataView(output.buffer);
|
|
77
|
-
const text=(at,value)=>{for(let index=0;index<value.length;index++)output[at+index]=value.charCodeAt(index);};
|
|
78
|
-
text(0,'RIFF');view.setUint32(4,output.length-8,true);text(8,'WAVEfmt ');view.setUint32(16,16,true);view.setUint16(20,1,true);view.setUint16(22,1,true);view.setUint32(24,16000,true);view.setUint32(28,32000,true);view.setUint16(32,2,true);view.setUint16(34,16,true);text(36,'data');view.setUint32(40,pcm.length*2,true);
|
|
79
|
-
for(let index=0;index<pcm.length;index++){
|
|
80
|
-
const envelope=fade?Math.min(1,index/fade,(pcm.length-1-index)/fade):1;
|
|
81
|
-
const sample=Math.max(-1,Math.min(1,pcm[index]*gain*envelope));
|
|
82
|
-
view.setInt16(44+index*2,Math.round(sample*(sample<0?32768:32767)),true);
|
|
83
|
-
}
|
|
84
|
-
let encoded='';for(let index=0;index<output.length;index+=8192)encoded+=String.fromCharCode(...output.subarray(index,index+8192));
|
|
85
|
-
return {base64:globalThis.btoa(encoded),sourceDuration:decoded.duration,sourcePeak,gain};
|
|
86
|
-
},{base64:input.toString('base64'),duration:cue.duration});
|
|
87
|
-
const bytes=Buffer.from(converted.base64,'base64'),audioMeta=analyzeWav(bytes);
|
|
88
|
-
const id=`${family.id}-${cue.role}`,path=`${family.id}/${cue.role}.wav`,file=`assets/free-audio/${family.id}/audio/${cue.role}.wav`;
|
|
89
|
-
await writeFile(resolve(root,file),bytes);
|
|
90
|
-
const args=['decodeAudioData','mono','16000 Hz','16-bit PCM',...(cue.duration?[`first ${cue.duration} seconds`,'20ms source boundary fades']:['full source duration']),...(converted.gain<1?[`gain ${converted.gain} for -1 dBFS ceiling`]:[])];
|
|
91
|
-
const record={
|
|
92
|
-
id,path,file,mime:'audio/wav',sha256:hash(bytes),
|
|
93
|
-
audioMeta:{role:cue.role,valence:cue.valence,energy:cue.energy,styleTags:[...new Set([...family.styleTags,...cue.tags])],loopable:cue.loopable??false,auditionStatus:'analyzed',...audioMeta},
|
|
94
|
-
processing:{kind:'audio-transcode',sourceFile:cue.input,sourceSha256:hash(input),tool:'Web Audio OfflineAudioContext',arguments:args},
|
|
95
|
-
};
|
|
96
|
-
files.push(record);receipts.push({family:family.id,id,role:cue.role,sourceId:cue.sourceId,sourceFile:cue.input,sourceSha256:hash(input),outputFile:file,outputSha256:hash(bytes),sourceDuration:converted.sourceDuration,sourcePeak:converted.sourcePeak,gain:converted.gain,audioMeta:record.audioMeta});
|
|
97
|
-
}
|
|
98
|
-
const newSource=family.ambientSource,{archive,...sourceFields}=newSource;
|
|
99
|
-
const ambientRecord={...sourceFields,licenseUrl,licenseSha256:hash(await readFile(resolve(root,newSource.licenseFile))),archive:{url:archive.url,sha256:hash(await readFile(resolve(root,archive.file)))},files:files.filter(file=>file.audioMeta.role==='ambient')};
|
|
100
|
-
catalog.sources=catalog.sources.filter(source=>source.id!==newSource.id);catalog.sources.push(ambientRecord);
|
|
101
|
-
for(const sourceId of [kenney.interface,kenney.jingles]){
|
|
102
|
-
const source=catalog.sources.find(row=>row.id===sourceId);if(!source)throw Error(`Missing catalog source ${sourceId}`);
|
|
103
|
-
const owned=new Set(files.filter(file=>file.audioMeta.role!=='ambient').map(file=>file.path));
|
|
104
|
-
source.files=source.files.filter(file=>!owned.has(file.path));source.files.push(...files.filter(file=>file.processing.sourceFile.includes(sourceId===kenney.interface?'interface-sounds':'music-jingles')));
|
|
105
|
-
}
|
|
106
|
-
const receiptFile=resolve(root,'assets/free-audio',family.id,'audio-conversion.json');
|
|
107
|
-
await writeFile(receiptFile,JSON.stringify({format:'tora-audio-conversion-1',method:'CC0 source decode, mono PCM conversion, boundary fades and optional -1 dBFS ceiling only',browser:browser.version(),script:'scripts/lean/native/prepare-audio-collections.mjs',family:family.id,cues:receipts.filter(receipt=>receipt.family===family.id)},null,2)+'\n');
|
|
108
|
-
}
|
|
109
|
-
catalog.audioCollections=familyDefinitions.map(family=>({id:family.id,label:family.label,auditionStatus:'analyzed',cues:Object.fromEntries(family.cues.map(cue=>[cue.role,`${family.id}-${cue.role}`]))}));
|
|
110
|
-
await writeFile(catalogFile,JSON.stringify(catalog,null,2)+'\n');
|
|
111
|
-
process.stdout.write(JSON.stringify(receipts.map(({family,id,audioMeta})=>({family,id,durationSec:audioMeta.durationSec,peakDbfs:audioMeta.peakDbfs,clipping:audioMeta.clipping})),null,2)+'\n');
|
|
112
|
-
}finally{await browser.close();}
|