@tora-editor/tora 0.1.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.
Files changed (131) hide show
  1. package/README.md +107 -58
  2. package/assets/free-audio/arcade-pop/audio/loss.wav +0 -0
  3. package/assets/free-audio/arcade-pop/audio/reward.wav +0 -0
  4. package/assets/free-audio/arcade-pop/audio/win.wav +0 -0
  5. package/assets/free-audio/arcade-pop/source/INFO.txt +11 -11
  6. package/assets/kenney-food-starter/License.txt +28 -0
  7. package/assets/kenney-food-starter/burger.glb +0 -0
  8. package/assets/kenney-food-starter/frying-pan.glb +0 -0
  9. package/assets/kenney-food-starter/plan.json +61 -0
  10. package/assets/kenney-food-starter/plate.glb +0 -0
  11. package/assets/kenney-food-starter/pot.glb +0 -0
  12. package/assets/kenney-harvest/audio/victory.wav +0 -0
  13. package/assets/kenney-harvest/source/music-jingles/OGG/jingles_NES/jingles_NES03.ogg +0 -0
  14. package/assets/kenney-harvest/source/music-jingles/OGG/jingles_PIZZA/jingles_PIZZA03.ogg +0 -0
  15. package/assets/kenney-harvest/source/music-jingles/license.txt +14 -0
  16. package/bin/tora.mjs +322 -28
  17. package/crates/packer/trusted/native-3d-meta.json +4 -3
  18. package/crates/packer/trusted/native-3d.js +181 -181
  19. package/crates/packer/trusted/native-meta.json +3 -3
  20. package/crates/packer/trusted/native.js +5 -5
  21. package/crates/packer/trusted/release-manifest.json +17 -17
  22. package/crates/packer/trusted/review-runtime-meta.json +4 -3
  23. package/crates/packer/trusted/review-runtime.js +182 -182
  24. package/docs/MIGRATION_0_3.md +39 -0
  25. package/docs/NATIVE_AGENT_TOOLS.md +12 -363
  26. package/docs/TORA_AGENT_SETUP.md +24 -95
  27. package/docs/TORA_CREATION_API.md +63 -348
  28. package/docs/reference/CORE_AUTHORING.md +10 -3
  29. package/docs/reference/PLAYABLE_STARTERS.md +20 -0
  30. package/docs/reference/packs/3d.md +11 -3
  31. package/network-rules/mintegral-appgrowth-single-html.json +10 -10
  32. package/package.json +6 -6
  33. package/packages/creative-project/project-v21.schema.json +372 -9
  34. package/scripts/lean/editor/entry.js +11 -6
  35. package/scripts/lean/editor/project-launcher.js +116 -0
  36. package/scripts/lean/editor/quick-edit-panel.js +479 -0
  37. package/scripts/lean/editor/review-editor.css +256 -68
  38. package/scripts/lean/editor/review-editor.js +1834 -1086
  39. package/scripts/lean/editor-state.mjs +6 -0
  40. package/scripts/lean/editor.mjs +6 -3
  41. package/scripts/lean/matrix.mjs +5 -0
  42. package/scripts/lean/native/agent-tools.mjs +39 -62
  43. package/scripts/lean/native/api.mjs +161 -136
  44. package/scripts/lean/native/asset-catalog.mjs +7 -6
  45. package/scripts/lean/native/asset-lineage.mjs +54 -0
  46. package/scripts/lean/native/asset-prep.mjs +173 -40
  47. package/scripts/lean/native/assets.mjs +107 -11
  48. package/scripts/lean/native/audio-assets.mjs +30 -0
  49. package/scripts/lean/native/browser-pool.mjs +34 -0
  50. package/scripts/lean/native/build-cache.mjs +38 -7
  51. package/scripts/lean/native/cc0-inputs.mjs +24 -0
  52. package/scripts/lean/native/cc0-staging.mjs +28 -8
  53. package/scripts/lean/native/compiler.mjs +9 -3
  54. package/scripts/lean/native/contracts.mjs +16 -11
  55. package/scripts/lean/native/creative-qa.mjs +365 -128
  56. package/scripts/lean/native/demos.mjs +53 -18
  57. package/scripts/lean/native/document.mjs +50 -12
  58. package/scripts/lean/native/export-budget.mjs +75 -0
  59. package/scripts/lean/native/local-bundle.mjs +7 -3
  60. package/scripts/lean/native/mcp-server.mjs +42 -74
  61. package/scripts/lean/native/media-validate.mjs +124 -0
  62. package/scripts/lean/native/modules/harvest-restore.mjs +3 -284
  63. package/scripts/lean/native/modules/sort-puzzle.mjs +51 -0
  64. package/scripts/lean/native/modules/star-salvage.mjs +3 -368
  65. package/scripts/lean/native/native-bin.mjs +3 -3
  66. package/scripts/lean/native/outcome-playthrough.mjs +97 -0
  67. package/scripts/lean/native/project-bundle.mjs +6 -5
  68. package/scripts/lean/native/project-index.mjs +0 -2
  69. package/scripts/lean/native/qa-adapters.mjs +10 -0
  70. package/scripts/lean/native/quick-edit.mjs +328 -0
  71. package/scripts/lean/native/remote-cc0-fetcher.mjs +403 -197
  72. package/scripts/lean/native/review.mjs +205 -571
  73. package/scripts/lean/native/schema-vocabulary.mjs +10 -0
  74. package/scripts/lean/native/schema.mjs +7 -5
  75. package/scripts/lean/native/scope.mjs +45 -0
  76. package/scripts/lean/native/service.mjs +378 -79
  77. package/scripts/lean/native/starters.mjs +563 -0
  78. package/scripts/lean/native/variant-adapters.mjs +8 -0
  79. package/scripts/lean/native/variants-core.mjs +186 -0
  80. package/scripts/lean/native/variants.mjs +63 -0
  81. package/scripts/lean/native/win-cues.mjs +96 -0
  82. package/skills/tora-playables/SKILL.md +1 -1
  83. package/scripts/lean/editor/review-intake.js +0 -837
  84. package/scripts/lean/model-client.mjs +0 -119
  85. package/scripts/lean/native/author-expansion-proofs.mjs +0 -240
  86. package/scripts/lean/native/author-games.mjs +0 -160
  87. package/scripts/lean/native/author-harvest.mjs +0 -74
  88. package/scripts/lean/native/author-primitive-proofs.mjs +0 -874
  89. package/scripts/lean/native/author-salvage.mjs +0 -71
  90. package/scripts/lean/native/chat-compose.mjs +0 -287
  91. package/scripts/lean/native/check-harvest-outcome-audio.mjs +0 -191
  92. package/scripts/lean/native/check-salvage.mjs +0 -197
  93. package/scripts/lean/native/compatibility.mjs +0 -34
  94. package/scripts/lean/native/concept.mjs +0 -633
  95. package/scripts/lean/native/draw-stroke.mjs +0 -26
  96. package/scripts/lean/native/erase-mask.mjs +0 -18
  97. package/scripts/lean/native/harvest-recipe.mjs +0 -21
  98. package/scripts/lean/native/instance-steering.mjs +0 -24
  99. package/scripts/lean/native/intake-creator-agent.mjs +0 -98
  100. package/scripts/lean/native/intake-creator-tools.mjs +0 -165
  101. package/scripts/lean/native/interaction-gestures.mjs +0 -45
  102. package/scripts/lean/native/match-board.mjs +0 -138
  103. package/scripts/lean/native/modules/index.mjs +0 -54
  104. package/scripts/lean/native/modules/tap-collect.mjs +0 -54
  105. package/scripts/lean/native/path-network.mjs +0 -132
  106. package/scripts/lean/native/physics-2d.mjs +0 -154
  107. package/scripts/lean/native/physics-3d.mjs +0 -259
  108. package/scripts/lean/native/prepare-audio-collections.mjs +0 -112
  109. package/scripts/lean/native/prepare-game-audio.mjs +0 -372
  110. package/scripts/lean/native/prepare-golf-3d-assets.py +0 -177
  111. package/scripts/lean/native/prepare-golf-assets.py +0 -211
  112. package/scripts/lean/native/prepare-harvest-audio.mjs +0 -83
  113. package/scripts/lean/native/prepare-harvest-images.mjs +0 -31
  114. package/scripts/lean/native/prepare-primitive-images.py +0 -25
  115. package/scripts/lean/native/prepare-space-salvage-images.mjs +0 -89
  116. package/scripts/lean/native/prepare-traffic-assets.py +0 -329
  117. package/scripts/lean/native/presentation.mjs +0 -335
  118. package/scripts/lean/native/review-agent-tools.mjs +0 -561
  119. package/scripts/lean/native/review-connection.mjs +0 -303
  120. package/scripts/lean/native/review-fields.mjs +0 -24
  121. package/scripts/lean/native/review-intake.mjs +0 -385
  122. package/scripts/lean/native/review-intents.mjs +0 -217
  123. package/scripts/lean/native/review-provider.mjs +0 -580
  124. package/scripts/lean/native/review-receipts.mjs +0 -76
  125. package/scripts/lean/native/review-resolver.mjs +0 -429
  126. package/scripts/lean/native/review-targets.mjs +0 -35
  127. package/scripts/lean/native/runtime.mjs +0 -1623
  128. package/scripts/lean/native/salvage-recipe.mjs +0 -20
  129. package/scripts/lean/native/stage-3d.mjs +0 -818
  130. package/scripts/lean/native/verify.mjs +0 -4
  131. package/scripts/lean/scene-document.mjs +0 -48
@@ -1,54 +0,0 @@
1
- import {assertValue,fieldLimits,record} from '../review-fields.mjs';
2
-
3
- const moments=[{id:'opening',label:'Opening'},{id:'end-card',label:'End card'}];
4
- function resolveRoles(index){
5
- const target=index.component('target'),feedback=index.binding('target.feedback');
6
- const event=feedback&&index.event(feedback.eventId);
7
- if(target?.contract!=='native.button.v1'||event?.kind!=='click'||event.componentId!==target.id||
8
- !feedback.actions.some(action=>action.kind==='add'&&action.variableId==='score'))
9
- return {status:'unsupported',module:null,unresolved:['module']};
10
- const cta=index.component('success.cta'),label=index.component('success.cta.label');
11
- const store=index.binding('success.store'),click=store&&index.event(store.eventId),pulse=index.behavior('feedback.pulse');
12
- const primaryCta=cta?.contract==='native.button.v1'&&label?.contract==='native.text.v1'&&label.parentId===cta.id&&
13
- click?.kind==='click'&&click.componentId===cta.id&&store.actions.some(action=>action.kind==='store')
14
- ?{componentId:cta.id,labelId:label.id,bindingId:store.id}:null;
15
- const feedbackTiming=pulse?.kind==='animation'&&pulse.componentId===target.id&&pulse.parameters.property==='scale'&&
16
- feedback.actions.some(action=>action.kind==='animate'&&action.behaviorId===pulse.id)?{behaviorId:pulse.id}:null;
17
- const roles={primaryCta,feedbackTiming},unresolved=Object.keys(roles).filter(key=>!roles[key]);
18
- return {status:unresolved.length?'unresolved':'resolved',module:'tap-collect',version:1,roles,unresolved};
19
- }
20
- function reviewTargets(project,resolution){
21
- if(resolution.status!=='resolved')return {module:'tap-collect',moments:[],targets:[]};
22
- const {primaryCta,feedbackTiming}=resolution.roles;
23
- return {module:'tap-collect',moments,targets:[
24
- {id:'cta',label:'Install button',moment:'end-card',componentIds:[primaryCta.componentId],
25
- fields:[{id:'text',label:'Button wording',value:record(project,'components',primaryCta.labelId).parameters.text,...fieldLimits.text}]},
26
- {id:'tap-feedback',label:'Tap feedback',moment:'opening',componentIds:['target'],
27
- fields:[{id:'feedback-duration',label:'Tap feedback duration (s)',value:record(project,'gameplay',feedbackTiming.behaviorId).parameters.seconds,...fieldLimits['feedback-duration']}]},
28
- ]};
29
- }
30
- function reviewOperations(project,targetId,fields,changes,baseline,resolution){
31
- if(!changes||typeof changes!=='object'||Array.isArray(changes))throw Error('E_REVIEW_ARGUMENT');
32
- const keys=Object.keys(changes);
33
- if(!keys.length||keys.some(key=>!fields.includes(key)))throw Error('E_REVIEW_SCOPE');
34
- const operations=[],expected=globalThis.structuredClone(project);
35
- for(const field of keys){
36
- assertValue(field,changes[field]);
37
- if(field==='text'){
38
- const label=record(expected,'components',resolution.roles?.primaryCta?.labelId);
39
- if(!label)throw Error('E_REVIEW_ROLE_UNRESOLVED');
40
- label.parameters.text=changes[field];operations.push({kind:'setParameter',id:label.id,key:'text',value:changes[field]});
41
- }else if(field==='feedback-duration'){
42
- if(targetId!=='tap-feedback'||!reviewTargets(baseline,resolution).targets.some(target=>target.id===targetId))throw Error('E_REVIEW_SCOPE');
43
- const pulse=record(expected,'gameplay','feedback.pulse');pulse.parameters.seconds=changes[field];
44
- operations.push({kind:'putBehavior',behavior:globalThis.structuredClone(pulse)});
45
- }else throw Error('E_REVIEW_SCOPE');
46
- }
47
- return {operations,expected};
48
- }
49
- export const tapCollectModule=Object.freeze({
50
- id:'tap-collect',version:1,supportsIntake:false,
51
- matches:index=>Boolean(index.component('target')||index.component('success.cta')),
52
- resolveRoles,reviewTargets,reviewOperations,canTransferOutcomeCue:()=>false,
53
- timingFields:()=>({'feedback-duration':{target:'tap-feedback',targets:['tap-feedback'],moment:'opening',label:'tap feedback duration'}}),
54
- });
@@ -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
- }