vehicle-path2 1.0.3 → 1.0.5
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/dist/react/hooks/useAnimation.d.ts +1 -0
- package/dist/react/hooks/useVehicleSimulation.d.ts +1 -0
- package/dist/react.cjs +1 -1
- package/dist/react.js +1 -1
- package/dist/useVehicleEvents-B1_CZyWq.cjs +3 -0
- package/dist/useVehicleEvents-Cv-YgfXB.js +931 -0
- package/dist/vehicle-path.cjs +1 -1
- package/dist/vehicle-path.js +1 -1
- package/package.json +1 -1
- package/dist/useVehicleEvents-GQ7zeGg6.cjs +0 -3
- package/dist/useVehicleEvents-Lf_yySwI.js +0 -924
|
@@ -36,6 +36,7 @@ export interface UseAnimationProps {
|
|
|
36
36
|
*/
|
|
37
37
|
export declare function useAnimation({ vehicles, lines, vehicleQueues, getVehicleQueues, wheelbase, tangentMode, curves, eventEmitter }: UseAnimationProps): {
|
|
38
38
|
movingVehicles: Vehicle[];
|
|
39
|
+
getMovingVehicles: () => Vehicle[];
|
|
39
40
|
prepare: () => boolean;
|
|
40
41
|
tick: (distance: number) => boolean;
|
|
41
42
|
reset: () => void;
|
|
@@ -35,6 +35,7 @@ export interface UseVehicleSimulationResult {
|
|
|
35
35
|
curves: Curve[];
|
|
36
36
|
vehicles: Vehicle[];
|
|
37
37
|
movingVehicles: Vehicle[];
|
|
38
|
+
getMovingVehicles: () => Vehicle[];
|
|
38
39
|
vehicleQueues: Map<string, GotoCommand[]>;
|
|
39
40
|
error: string | null;
|
|
40
41
|
addLine: (line: SceneLineInput) => SimulationResult;
|
package/dist/react.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./useVehicleEvents-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./useVehicleEvents-B1_CZyWq.cjs");exports.VehicleEventContext=e.VehicleEventContext;exports.VehicleEventProvider=e.VehicleEventProvider;exports.useAnimation=e.useAnimation;exports.useCreateVehicleEventEmitter=e.useCreateVehicleEventEmitter;exports.useInitialMovement=e.useInitialMovement;exports.useMovement=e.useMovementQueue;exports.useMovementQueue=e.useMovementQueue;exports.useMovementSequence=e.useMovementSequence;exports.useScene=e.useScene;exports.useSceneDefinition=e.useSceneDefinition;exports.useVehicleEvent=e.useVehicleEvent;exports.useVehicleEventEmitter=e.useVehicleEventEmitter;exports.useVehicleMovement=e.useAnimation;exports.useVehicleSimulation=e.useVehicleSimulation;exports.useVehicles=e.useVehicles;
|
package/dist/react.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { V as t, a as i, u as a, b as n, c as u, d as c, d as o, e as v, f as l, g as m, h, i as V, u as r, j as E, k as M } from "./useVehicleEvents-
|
|
1
|
+
import { V as t, a as i, u as a, b as n, c as u, d as c, d as o, e as v, f as l, g as m, h, i as V, u as r, j as E, k as M } from "./useVehicleEvents-Cv-YgfXB.js";
|
|
2
2
|
export {
|
|
3
3
|
t as VehicleEventContext,
|
|
4
4
|
i as VehicleEventProvider,
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";const o=require("react"),A=require("./core.cjs"),R=require("./vehicle-helpers-82D2V4MI.cjs"),te=require("react/jsx-runtime");function U(s){return Array.isArray(s)?{x:s[0],y:s[1]}:s}function G(s){return{id:s.id,start:U(s.start),end:U(s.end)}}function z(s){const c=s.fromIsPercentage!==!1,I=s.toIsPercentage!==!1;return{fromLineId:s.from,toLineId:s.to,fromOffset:s.fromPosition,fromIsPercentage:s.fromPosition!==void 0?c:void 0,toOffset:s.toPosition,toIsPercentage:s.toPosition!==void 0?I:void 0}}function N(s){const c=s.position??0,I=s.isPercentage!==!1;return{vehicleId:s.id,lineId:s.lineId,offset:c,isPercentage:I}}function H(s){const c=s.isPercentage!==!1,I=s.targetPosition??1;return{vehicleId:s.vehicleId,targetLineId:s.targetLineId,targetOffset:I,isPercentage:c,awaitConfirmation:s.wait,payload:s.payload}}function se(s){const c=[],I=new Set;for(const r of s.lines)I.has(r.id)&&c.push(`Duplicate line ID: ${r.id}`),I.add(r.id);if(s.connections)for(const r of s.connections){I.has(r.from)||c.push(`Connection references non-existent line: ${r.from}`),I.has(r.to)||c.push(`Connection references non-existent line: ${r.to}`);const m=r.fromIsPercentage!==!1,t=r.toIsPercentage!==!1;!m&&r.fromPosition===void 0&&c.push("fromPosition is required when fromIsPercentage is false"),!t&&r.toPosition===void 0&&c.push("toPosition is required when toIsPercentage is false"),r.fromPosition!==void 0&&(m&&(r.fromPosition<0||r.fromPosition>1)?c.push(`Invalid fromPosition: ${r.fromPosition} (must be 0-1 for percentage)`):!m&&r.fromPosition<0&&c.push(`Invalid fromPosition: ${r.fromPosition} (must be >= 0 for absolute distance)`)),r.toPosition!==void 0&&(t&&(r.toPosition<0||r.toPosition>1)?c.push(`Invalid toPosition: ${r.toPosition} (must be 0-1 for percentage)`):!t&&r.toPosition<0&&c.push(`Invalid toPosition: ${r.toPosition} (must be >= 0 for absolute distance)`))}return{valid:c.length===0,errors:c}}function J(){const[s,c]=o.useState([]),[I,r]=o.useState([]),[m,t]=o.useState(null),d=o.useCallback((n,v)=>{c(n),r(v),t(null)},[]),C=o.useCallback(n=>{const v=se(n);if(!v.valid)return t(v.errors.join("; ")),{success:!1,errors:v.errors};const u=n.lines.map(G),g=n.connections?.map(z)||[];return d(u,g),{success:!0}},[d]),y=o.useCallback(n=>{if(s.some(u=>u.id===n.id)){const u=`Line with ID '${n.id}' already exists`;return t(u),{success:!1,error:u}}return c(u=>[...u,G(n)]),t(null),{success:!0}},[s]),q=o.useCallback((n,v)=>{if(s.findIndex(g=>g.id===n)===-1){const g=`Line with ID '${n}' not found`;return t(g),{success:!1,error:g}}return c(g=>g.map(M=>M.id!==n?M:{...M,start:v.start?U(v.start):M.start,end:v.end?U(v.end):M.end})),t(null),{success:!0}},[s]),k=o.useCallback(n=>{if(!s.some(u=>u.id===n)){const u=`Line with ID '${n}' not found`;return t(u),{success:!1,error:u}}return c(u=>u.filter(g=>g.id!==n)),r(u=>u.filter(g=>g.fromLineId!==n&&g.toLineId!==n)),t(null),{success:!0}},[s]),b=o.useCallback(n=>{const v=s.some(f=>f.id===n.from),u=s.some(f=>f.id===n.to);if(!v){const f=`Line '${n.from}' not found`;return t(f),{success:!1,error:f}}if(!u){const f=`Line '${n.to}' not found`;return t(f),{success:!1,error:f}}const g=n.fromIsPercentage!==!1,M=n.toIsPercentage!==!1;if(!g&&n.fromPosition===void 0){const f="fromPosition is required when fromIsPercentage is false";return t(f),{success:!1,error:f}}if(!M&&n.toPosition===void 0){const f="toPosition is required when toIsPercentage is false";return t(f),{success:!1,error:f}}if(I.some(f=>f.fromLineId===n.from&&f.toLineId===n.to)){const f=`Connection from '${n.from}' to '${n.to}' already exists`;return t(f),{success:!1,error:f}}return r(f=>[...f,z(n)]),t(null),{success:!0}},[s,I]),p=o.useCallback((n,v,u)=>{const g=I.findIndex(e=>e.fromLineId===n&&e.toLineId===v);if(g===-1){const e=`Connection from '${n}' to '${v}' not found`;return t(e),{success:!1,error:e}}const M=I[g],Q=u.fromIsPercentage??M.fromIsPercentage,f=u.toIsPercentage??M.toIsPercentage;let $;u.fromOffset!==void 0?$=u.fromOffset:M.fromOffset!==void 0&&($=M.fromOffset);let L;if(u.toOffset!==void 0?L=u.toOffset:M.toOffset!==void 0&&(L=M.toOffset),$!==void 0){if(Q!==!1&&($<0||$>1)){const e=`Invalid fromOffset: ${$} (must be 0-1 for percentage)`;return t(e),{success:!1,error:e}}if(Q===!1&&$<0){const e=`Invalid fromOffset: ${$} (must be >= 0 for absolute distance)`;return t(e),{success:!1,error:e}}}if(L!==void 0){if(f!==!1&&(L<0||L>1)){const e=`Invalid toOffset: ${L} (must be 0-1 for percentage)`;return t(e),{success:!1,error:e}}if(f===!1&&L<0){const e=`Invalid toOffset: ${L} (must be >= 0 for absolute distance)`;return t(e),{success:!1,error:e}}}if(Q===!1&&$===void 0){const e="fromOffset is required when fromIsPercentage is false";return t(e),{success:!1,error:e}}if(f===!1&&L===void 0){const e="toOffset is required when toIsPercentage is false";return t(e),{success:!1,error:e}}const E={from:n,to:v,fromPosition:$,fromIsPercentage:Q,toPosition:L,toIsPercentage:f};return r(e=>e.map((i,l)=>l===g?z(E):i)),t(null),{success:!0}},[I]),P=o.useCallback((n,v)=>{if(!I.some(g=>g.fromLineId===n&&g.toLineId===v)){const g=`Connection from '${n}' to '${v}' not found`;return t(g),{success:!1,error:g}}return r(g=>g.filter(M=>!(M.fromLineId===n&&M.toLineId===v))),t(null),{success:!0}},[I]),a=o.useCallback(()=>{c([]),r([]),t(null)},[]);return{lines:s,curves:I,setScene:C,addLine:y,updateLine:q,removeLine:k,addConnection:b,updateConnection:p,removeConnection:P,clear:a,error:m,_loadScene:d}}function B({lines:s,wheelbase:c}){const[I,r]=o.useState([]),[m,t]=o.useState(null),d=o.useRef([]),C=o.useCallback(p=>{d.current=p,r(p),t(null)},[]),y=o.useCallback(p=>{const P=Array.isArray(p)?p:[p],a=[];for(const g of P)d.current.some(Q=>Q.id===g.id)&&a.push(`Vehicle with ID '${g.id}' already exists`);if(a.length>0)return t(a.join("; ")),{success:!1,errors:a};const n=P.map(N),{vehicles:v,errors:u}=R.validateAndCreateVehicles(n,s,c);return u.length>0?(t(u.join("; ")),{success:!1,errors:u}):(d.current=[...d.current,...v],r(d.current),t(null),{success:!0})},[s,c]),q=o.useCallback((p,P)=>{const a=d.current.findIndex(L=>L.id===p);if(a===-1){const L=`Vehicle with ID '${p}' not found`;return t(L),{success:!1,error:L}}const n=d.current[a];if(n.state!=="idle"){const L=`Cannot update vehicle '${p}' while it is ${n.state}. Vehicle must be idle.`;return t(L),{success:!1,error:L}}const v=P.lineId??n.lineId;if(!s.find(L=>L.id===v)){const L=`Line '${v}' not found`;return t(L),{success:!1,error:L}}let g,M;P.lineId!==void 0&&P.position===void 0?(g=0,M=!0):P.position!==void 0?(g=P.position,M=P.isPercentage??!0):(g=n.offset,M=n.isPercentage);const Q={vehicleId:p,lineId:v,offset:g,isPercentage:M},{vehicles:f,errors:$}=R.validateAndCreateVehicles([Q],s,c);return $.length>0?(t($.join("; ")),{success:!1,error:$.join("; ")}):(d.current=d.current.map((L,E)=>E===a?f[0]:L),r(d.current),t(null),{success:!0})},[s,c]),k=o.useCallback(p=>{if(!d.current.some(a=>a.id===p)){const a=`Vehicle with ID '${p}' not found`;return t(a),{success:!1,error:a}}return d.current=d.current.filter(a=>a.id!==p),r(d.current),t(null),{success:!0}},[]),b=o.useCallback(()=>{d.current=[],r([]),t(null)},[]);return{vehicles:I,addVehicles:y,updateVehicle:q,removeVehicle:k,clear:b,error:m,_loadVehicles:C}}function W({vehicles:s,lines:c}){const[I,r]=o.useState(new Map),[m,t]=o.useState(null),d=o.useRef(new Map),C=o.useCallback(()=>d.current,[]),y=o.useCallback(b=>{d.current=b,r(b),t(null)},[]),q=o.useCallback((b,p)=>{if(!s.some(f=>f.id===b)){const f=`Vehicle '${b}' not found`;return t(f),{success:!1,error:f}}const a=c.find(f=>f.id===p.targetLineId);if(!a){const f=`Line '${p.targetLineId}' not found`;return t(f),{success:!1,error:f}}const n=p.isPercentage!==!1,v=A.distance(a.start,a.end);if(!n&&p.targetPosition===void 0){const f="targetPosition is required when isPercentage is false";return t(f),{success:!1,error:f}}const u=p.targetPosition??1;if(n){if(u<0||u>1){const f=`Invalid targetPosition: ${u} (must be 0-1 for percentage)`;return t(f),{success:!1,error:f}}}else{if(u<0){const f=`Invalid targetPosition: ${u} (must be >= 0 for absolute distance)`;return t(f),{success:!1,error:f}}if(u>v){const f=`Position ${u} exceeds line length ${v}`;return t(f),{success:!1,error:f}}}const g=H({vehicleId:b,...p}),M=new Map(d.current),Q=M.get(b)||[];return M.set(b,[...Q,g]),d.current=M,r(M),t(null),{success:!0}},[s,c]),k=o.useCallback(b=>{if(b!==void 0){if(!s.some(a=>a.id===b)){const a=`Vehicle '${b}' not found`;return t(a),{success:!1,error:a}}const P=new Map(d.current);P.delete(b),d.current=P,r(P)}else d.current=new Map,r(new Map);return t(null),{success:!0}},[s]);return{vehicleQueues:I,getVehicleQueues:C,queueMovement:q,clearQueue:k,error:m,_loadQueues:y}}function Z({vehicles:s,lines:c,vehicleQueues:I,getVehicleQueues:r,wheelbase:m,tangentMode:t,curves:d,eventEmitter:C}){const[y,q]=o.useState([]),k=o.useRef([]),b=o.useCallback(E=>{typeof E=="function"?b(e=>{const i=E(e);return k.current=i,i}):(k.current=E,q(E))},[]),p=o.useCallback(()=>k.current,[]),P=o.useMemo(()=>({wheelbase:m,tangentMode:t}),[m,t]),a=o.useMemo(()=>new Map(c.map(E=>[E.id,E])),[c]),n=o.useRef(new Map);o.useEffect(()=>{const{movingVehicles:E,stateMap:e}=A.initializeAllVehicles(s,a);n.current=e;const i=setTimeout(()=>{b(E)},0);return()=>clearTimeout(i)},[s,a]);const v=o.useRef(null);o.useEffect(()=>{c.length>0&&(v.current=A.buildGraph(c,d,P))},[c,d,P]);const u=o.useRef(0),g=o.useRef(!1),M=o.useCallback(E=>{if(!g.current)return!1;let e=!1;for(const[,l]of n.current)if(l.vehicle.state==="moving"){e=!0;break}if(!e)return!1;const i=[];for(const[l,h]of n.current){if(h.vehicle.state!=="moving"||!h.execution)continue;const x=h.execution;let S;if(x.front.currentSegmentIndex<x.path.segments.length){const T=x.path.segments[x.front.currentSegmentIndex];if(T.type==="line"){const V=a.get(T.lineId);V&&(S=Math.sqrt(Math.pow(V.end.x-V.start.x,2)+Math.pow(V.end.y-V.start.y,2)))}}const O=A.updateAxlePosition(h.vehicle.rear,x.rear,x.path,E,a,x.curveDataMap),D=A.updateAxlePosition(h.vehicle.front,x.front,x.path,E,a,x.curveDataMap,S);if(h.vehicle={...h.vehicle,rear:O.axleState,front:D.axleState},h.execution.rear=O.execution,h.execution.front=D.execution,O.completed){const T={linesMap:a,config:P,vehicleQueues:I,curves:d,graphRef:v,prepareCommandPath:A.prepareCommandPath,onCommandComplete:j=>i.push({type:"commandComplete",data:j}),onCommandStart:j=>i.push({type:"commandStart",data:j})},V=A.handleArrival(h,T);h.vehicle=V.vehicle,V.newExecution!==void 0&&(h.execution=V.newExecution),V.vehicle.state!=="moving"&&i.push({type:"stateChange",data:{vehicleId:l,from:"moving",to:V.vehicle.state}});const w=V.vehicle.rear.position,_=V.vehicle.front.position;i.push({type:"positionUpdate",data:{vehicleId:l,rear:w,front:_,center:{x:(w.x+_.x)/2,y:(w.y+_.y)/2},angle:Math.atan2(_.y-w.y,_.x-w.x)}})}}if(b(l=>l.map(h=>{const x=n.current.get(h.id);return x?x.vehicle:h})),C&&i.length>0){const l=u.current;setTimeout(()=>{u.current===l&&i.forEach(({type:h,data:x})=>{C.emit(h,x)})},0)}for(const[,l]of n.current)if(l.vehicle.state==="moving")return!0;return g.current=!1,!1},[a,d,P,I,C]),Q=o.useCallback(()=>{if(g.current)return!0;const E=v.current;if(!E)return!1;const e=r?r():I,i=[];let l=!1;for(const[h,x]of n.current){const S=x.vehicle,O=e.get(h);if(!O||O.length===0)continue;const D=O[0],T={graph:E,linesMap:a,curves:d,config:P},V=A.prepareCommandPath(S,D,T);if(!V){console.warn(`No path found for vehicle ${h}`);continue}const w=A.calculateFrontAxlePosition(V.path,0,0,m);n.current.set(h,{...x,execution:{path:V.path,curveDataMap:V.curveDataMap,currentCommandIndex:0,rear:{currentSegmentIndex:0,segmentDistance:0},front:w?{currentSegmentIndex:w.segmentIndex,segmentDistance:w.segmentDistance}:{currentSegmentIndex:0,segmentDistance:0}},vehicle:{...S,state:"moving"}}),l=!0,S.state!=="moving"&&i.push({id:h,fromState:S.state,command:D,startPosition:{lineId:S.rear.lineId,absoluteOffset:S.rear.absoluteOffset,position:S.rear.position}})}if(!l)return!1;if(g.current=!0,b(h=>h.map(x=>{const S=n.current.get(x.id);return S?S.vehicle:x})),C&&i.length>0){const h=u.current;setTimeout(()=>{u.current===h&&i.forEach(({id:x,fromState:S,command:O,startPosition:D})=>{C.emit("commandStart",{vehicleId:x,command:O,commandIndex:0,startPosition:D}),C.emit("stateChange",{vehicleId:x,from:S,to:"moving"})})},0)}return!0},[a,d,I,r,P,m,C]),f=o.useCallback(()=>{u.current++,g.current=!1;const{movingVehicles:E,stateMap:e}=A.initializeAllVehicles(s,a);n.current=e,b(E)},[s,a]),$=o.useCallback(E=>{const e=n.current.get(E);if(!e||e.vehicle.state!=="waiting")return!1;const i=I.get(E),l=e.execution;if(!l)return!1;const h=l.currentCommandIndex+1;if(i&&h<i.length){const x=v.current;if(x){const S=i[h],O={graph:x,linesMap:a,curves:d,config:P},D=A.prepareCommandPath(e.vehicle,S,O);if(D){const T=A.calculateFrontAxlePosition(D.path,0,0,m);if(e.execution={path:D.path,curveDataMap:D.curveDataMap,currentCommandIndex:h,rear:{currentSegmentIndex:0,segmentDistance:0},front:T?{currentSegmentIndex:T.segmentIndex,segmentDistance:T.segmentDistance}:{currentSegmentIndex:0,segmentDistance:0}},e.vehicle={...e.vehicle,state:"moving"},b(V=>V.map(w=>w.id===E?e.vehicle:w)),C){const V=u.current;setTimeout(()=>{u.current===V&&C.emit("stateChange",{vehicleId:E,from:"waiting",to:"moving"})},0)}return!0}}}if(e.vehicle={...e.vehicle,state:"idle"},e.execution=null,b(x=>x.map(S=>S.id===E?e.vehicle:S)),C){const x=u.current;setTimeout(()=>{u.current===x&&C.emit("stateChange",{vehicleId:E,from:"waiting",to:"idle"})},0)}return!0},[I,a,d,P,m,C]),L=o.useCallback(()=>{for(const[,E]of n.current)if(E.vehicle.state==="moving")return!0;return!1},[]);return{movingVehicles:y,getMovingVehicles:p,prepare:Q,tick:M,reset:f,continueVehicle:$,isMoving:L,isPrepared:g.current}}function re({wheelbase:s,tangentMode:c="proportional-40",eventEmitter:I}){const r=J(),m=B({lines:r.lines,wheelbase:s}),t=W({vehicles:m.vehicles,lines:r.lines,curves:r.curves}),d=Z({vehicles:m.vehicles,lines:r.lines,vehicleQueues:t.vehicleQueues,getVehicleQueues:t.getVehicleQueues,wheelbase:s,tangentMode:c,curves:r.curves,eventEmitter:I}),C=o.useCallback(e=>m.vehicles.filter(i=>i.lineId===e||i.rear.lineId===e),[m.vehicles]),y=o.useCallback(e=>C(e).length>0,[C]),q=o.useCallback(e=>{const i=r.addLine(e);return i.success?{success:!0}:{success:!1,error:i.error}},[r]),k=o.useCallback((e,i)=>{const l=r.updateLine(e,i);return l.success?{success:!0}:{success:!1,error:l.error}},[r]),b=o.useCallback(e=>{const i=[],l=C(e);l.length>0&&(i.push({type:"vehicle_on_removed_line",message:`${l.length} vehicle(s) are on line '${e}'`,details:{lineId:e,vehicleIds:l.map(S=>S.id)}}),l.forEach(S=>{m.removeVehicle(S.id),t.clearQueue(S.id)}));const h=r.curves.filter(S=>S.fromLineId===e||S.toLineId===e);h.length>0&&i.push({type:"orphaned_connection",message:`${h.length} connection(s) will be removed`,details:{lineId:e,connectionCount:h.length}});const x=r.removeLine(e);return x.success?{success:!0,warnings:i.length>0?i:void 0}:{success:!1,error:x.error}},[r,C,m,t]),p=o.useCallback(()=>{r.clear(),m.clear(),t.clearQueue()},[r,m,t]),P=o.useCallback((e,i,l)=>{const h=r.addConnection({from:e,to:i,fromPosition:l?.fromOffset,fromIsPercentage:l?.fromIsPercentage,toPosition:l?.toOffset,toIsPercentage:l?.toIsPercentage});return h.success?{success:!0}:{success:!1,error:h.error}},[r]),a=o.useCallback((e,i,l)=>{const h=r.updateConnection(e,i,l);return h.success?{success:!0}:{success:!1,error:h.error}},[r]),n=o.useCallback((e,i)=>{const l=r.removeConnection(e,i);return l.success?{success:!0}:{success:!1,error:l.error}},[r]),v=o.useCallback(e=>{const i=m.addVehicles(e);return i.success?{success:!0}:{success:!1,error:i.errors?.join("; ")}},[m]),u=o.useCallback((e,i)=>{const l=m.updateVehicle(e,i);return l.success?{success:!0}:{success:!1,error:l.error}},[m]),g=o.useCallback(e=>{const i=[],l=t.vehicleQueues.get(e);l&&l.length>0&&(i.push({type:"movement_queue_cleared",message:`${l.length} queued movement(s) will be cleared for vehicle '${e}'`,details:{vehicleId:e}}),t.clearQueue(e));const h=m.removeVehicle(e);return h.success?{success:!0,warnings:i.length>0?i:void 0}:{success:!1,error:h.error}},[m,t]),M=o.useCallback(()=>{m.clear(),t.clearQueue()},[m,t]),Q=o.useCallback(e=>{const i={targetLineId:e.lineId,targetPosition:e.position??1,isPercentage:e.isPercentage,wait:e.wait,payload:e.payload},l=t.queueMovement(e.id,i);return l.success?{success:!0}:{success:!1,error:l.error}},[t]),f=o.useCallback(e=>{const i=t.clearQueue(e);return i.success?{success:!0}:{success:!1,error:i.error}},[t]),$=o.useCallback(e=>{const i=[],l=[],{scene:h,vehicles:x,movements:S}=R.parseAllDSL(e);h.errors.length>0&&l.push(...h.errors),x.errors.length>0&&l.push(...x.errors),S.errors.length>0&&l.push(...S.errors);const O=h.data.lines.map(G),D=(h.data.connections||[]).map(z),T=x.data.map(N),{vehicles:V,errors:w}=R.validateAndCreateVehicles(T,O,s);w.length>0&&l.push(...w);const _=new Map;for(const j of S.data){const Y=_.get(j.vehicleId)||[];Y.push(H(j)),_.set(j.vehicleId,Y)}return r._loadScene(O,D),m._loadVehicles(V),t._loadQueues(_),l.length>0&&i.push({type:"dsl_parse_error",message:`DSL loading had ${l.length} error(s)`,details:{errors:l}}),{success:!0,warnings:i.length>0?i:void 0}},[r,m,t,s]),L=o.useCallback(e=>{const i=[],l=[],h=e.lines.map(G),x=(e.connections||[]).map(z),S=(e.vehicles||[]).map(N),{vehicles:O,errors:D}=R.validateAndCreateVehicles(S,h,s);D.length>0&&l.push(...D);const T=new Map;for(const V of e.movements||[]){const w=T.get(V.vehicleId)||[];w.push(H({vehicleId:V.vehicleId,targetLineId:V.targetLineId,targetPosition:V.targetPosition,isPercentage:V.isPercentage,wait:V.wait,payload:V.payload})),T.set(V.vehicleId,w)}return r._loadScene(h,x),m._loadVehicles(O),t._loadQueues(T),l.length>0&&i.push({type:"dsl_parse_error",message:`JSON loading had ${l.length} error(s)`,details:{errors:l}}),{success:!0,warnings:i.length>0?i:void 0}},[r,m,t,s]),E=o.useMemo(()=>r.error||m.error||t.error,[r.error,m.error,t.error]);return{lines:r.lines,curves:r.curves,vehicles:m.vehicles,movingVehicles:d.movingVehicles,getMovingVehicles:d.getMovingVehicles,vehicleQueues:t.vehicleQueues,error:E,addLine:q,updateLine:k,removeLine:b,clearScene:p,connect:P,updateConnection:a,disconnect:n,addVehicles:v,updateVehicle:u,removeVehicle:g,clearVehicles:M,goto:Q,clearQueue:f,prepare:d.prepare,tick:d.tick,reset:d.reset,continueVehicle:d.continueVehicle,isMoving:d.isMoving,loadFromDSL:$,loadFromJSON:L,getVehiclesOnLine:C,hasVehiclesOnLine:y}}function F(s){return s.map(c=>({id:c.id,start:c.start,end:c.end}))}function K(s){return s.map(c=>({from:c.fromLineId,to:c.toLineId,fromPosition:c.fromOffset,fromIsPercentage:c.fromIsPercentage,toPosition:c.toOffset,toIsPercentage:c.toIsPercentage}))}function ne(){const[s,c]=o.useState(""),[I,r]=o.useState(null),{lines:m,curves:t,setScene:d}=J(),C=o.useRef(!1),y=o.useRef("");o.useEffect(()=>{y.current=s},[s]),o.useEffect(()=>{if(C.current)return;const p={lines:F(m),connections:t.length>0?K(t):void 0},P=R.generateSceneDSL(p);P!==y.current&&(C.current=!0,c(P),setTimeout(()=>{C.current=!1},50))},[m,t]);const q=o.useCallback(p=>{C.current=!0,c(p);try{const{data:P,errors:a}=R.parseSceneDSL(p);a.length>0&&r(a.join("; "));const n=d(P);!n.success&&n.errors?r(v=>v?`${v}; ${n.errors.join("; ")}`:n.errors.join("; ")):a.length===0&&r(null)}catch(P){r(P instanceof Error?P.message:"Invalid scene definition")}setTimeout(()=>{C.current=!1},50)},[d]),k=o.useCallback(p=>{const P=typeof p=="function"?p(m):p,a=K(t);d({lines:F(P),connections:a.length>0?a:void 0});const n={lines:F(P),connections:a.length>0?a:void 0},v=R.generateSceneDSL(n);C.current=!0,c(v),setTimeout(()=>{C.current=!1},50)},[m,t,d]),b=o.useCallback(p=>{const P=typeof p=="function"?p(t):p,a=K(P);d({lines:F(m),connections:a.length>0?a:void 0});const n={lines:F(m),connections:a.length>0?a:void 0},v=R.generateSceneDSL(n);C.current=!0,c(v),setTimeout(()=>{C.current=!1},50)},[m,t,d]);return{lines:m,curves:t,sceneDefinitionText:s,sceneError:I,isDebouncing:!1,debounceKey:0,setLines:k,setCurves:b,setSceneDefinitionText:q}}function oe({lines:s,wheelbase:c}){const[I,r]=o.useState(""),[m,t]=o.useState(null),{vehicles:d,addVehicles:C,clear:y,error:q}=B({lines:s,wheelbase:c}),k=o.useRef(!1),b=o.useCallback(p=>{k.current=!0,r(p);try{const{data:P,errors:a}=R.parseVehiclesDSL(p),n=[...a];y();for(const v of P){const u=C(v);!u.success&&u.errors&&n.push(...u.errors)}n.length>0?t(n.join(`
|
|
2
|
+
`)):t(null)}catch(P){t(P instanceof Error?P.message:"Invalid initial movement")}setTimeout(()=>{k.current=!1},50)},[C,y]);return{vehicles:d,initialMovementText:I,movementError:m||q,isDebouncing:!1,debounceKey:0,setInitialMovementText:b}}function ce({lines:s,vehicles:c}){const[I,r]=o.useState(""),[m,t]=o.useState([]),[d,C]=o.useState(null),{vehicleQueues:y,queueMovement:q,clearQueue:k,error:b}=W({vehicles:c,lines:s}),p=o.useRef(!1),P=o.useCallback(a=>{p.current=!0,r(a);try{const{data:n,errors:v}=R.parseMovementDSL(a),u=[...v];k();for(const g of n){const M=q(g.vehicleId,{targetLineId:g.targetLineId,targetPosition:g.targetPosition,isPercentage:g.isPercentage,wait:g.wait,payload:g.payload});!M.success&&M.error&&u.push(M.error)}t(n),u.length>0?C(u.join(`
|
|
3
|
+
`)):C(null)}catch(n){C(n instanceof Error?n.message:"Invalid movement sequence"),t([])}setTimeout(()=>{p.current=!1},50)},[q,k]);return{movementSequenceText:I,gotoCommands:m,vehicleQueues:y,sequenceError:d||b,isDebouncing:!1,debounceKey:0,setMovementSequenceText:P}}const X=o.createContext(null);function ee(){const s=o.useContext(X);if(!s)throw new Error("useVehicleEventEmitter must be used within a VehicleEventProvider");return s}function ie(){return o.useMemo(()=>new R.VehicleEventEmitter,[])}function ae(s,c,I=[]){const r=ee();o.useEffect(()=>r.on(s,c),[r,s,...I])}function ue({children:s}){const c=o.useMemo(()=>new R.VehicleEventEmitter,[]);return te.jsx(X.Provider,{value:c,children:s})}exports.VehicleEventContext=X;exports.VehicleEventProvider=ue;exports.useAnimation=Z;exports.useCreateVehicleEventEmitter=ie;exports.useInitialMovement=oe;exports.useMovementQueue=W;exports.useMovementSequence=ce;exports.useScene=J;exports.useSceneDefinition=ne;exports.useVehicleEvent=ae;exports.useVehicleEventEmitter=ee;exports.useVehicleSimulation=re;exports.useVehicles=B;
|