UNPKG

aura-glass

Version:

A comprehensive glassmorphism design system for React applications with 142+ production-ready components

207 lines (204 loc) 4.76 kB
'use client'; import { useRef, useState, useEffect, useCallback } from 'react'; import { AuraPhysicsEngineAPI } from '../../physics/AuraPhysicsEngine.js'; /** * Hook for managing a physics engine instance */ const usePhysicsEngine = (options = {}) => { const { autoStart = false, gravity, timeScale = 1.0 } = options; const engineRef = useRef(null); const [isRunning, setIsRunning] = useState(false); const [bodyStates, setBodyStates] = useState(new Map()); /** * Initialize engine */ useEffect(() => { engineRef.current = new AuraPhysicsEngineAPI(); if (gravity) { engineRef.current.setGravity(gravity); } engineRef.current.setTimeScale(timeScale); if (autoStart) { engineRef.current.start(); setIsRunning(true); } return () => { if (engineRef.current) { engineRef.current.stop(); engineRef.current.clear(); } }; }, []); /** * Update gravity */ useEffect(() => { if (engineRef.current && gravity) { engineRef.current.setGravity(gravity); } }, [gravity]); /** * Update time scale */ useEffect(() => { if (engineRef.current) { engineRef.current.setTimeScale(timeScale); } }, [timeScale]); /** * Create a physics body */ const createBody = useCallback((id, options) => { if (!engineRef.current) { throw new Error('Physics engine not initialized'); } return engineRef.current.createBody(id, options); }, []); /** * Remove a physics body */ const removeBody = useCallback(id => { engineRef.current?.removeBody(id); setBodyStates(prev => { const next = new Map(prev); next.delete(id); return next; }); }, []); /** * Apply force to a body */ const applyForce = useCallback((id, force) => { engineRef.current?.applyForce(id, force); }, []); /** * Apply impulse to a body */ const applyImpulse = useCallback((id, impulse) => { engineRef.current?.applyImpulse(id, impulse); }, []); /** * Set velocity of a body */ const setVelocity = useCallback((id, velocity) => { engineRef.current?.setVelocity(id, velocity); }, []); /** * Set position of a body */ const setPosition = useCallback((id, position) => { engineRef.current?.setPosition(id, position); }, []); /** * Get body state */ const getBodyState = useCallback(id => { return engineRef.current?.getBodyState(id) || null; }, []); /** * Subscribe to collision events */ const onCollision = useCallback((id, callback) => { if (!engineRef.current) { return () => {}; } return engineRef.current.onCollision(id, callback); }, []); /** * Start the physics engine */ const start = useCallback(() => { engineRef.current?.start(); setIsRunning(true); }, []); /** * Stop the physics engine */ const stop = useCallback(() => { engineRef.current?.stop(); setIsRunning(false); }, []); /** * Force update */ const forceUpdate = useCallback(() => { engineRef.current?.forceUpdate(); }, []); /** * Set gravity */ const setGravity = useCallback(newGravity => { engineRef.current?.setGravity(newGravity); }, []); /** * Set time scale */ const setTimeScale = useCallback(scale => { engineRef.current?.setTimeScale(scale); }, []); /** * Get all body IDs */ const getBodies = useCallback(() => { return engineRef.current?.getBodies() || []; }, []); /** * Clear all bodies */ const clear = useCallback(() => { engineRef.current?.clear(); setBodyStates(new Map()); }, []); /** * Update body states periodically */ useEffect(() => { if (!isRunning || !engineRef.current) return; const interval = setInterval(() => { if (engineRef.current) { const bodies = engineRef.current.getBodies(); const states = new Map(); bodies.forEach(bodyId => { const state = engineRef.current?.getBodyState(bodyId); if (state) { states.set(bodyId, state); } }); setBodyStates(states); } }, 16); // ~60fps return () => clearInterval(interval); }, [isRunning]); return { // Engine control start, stop, forceUpdate, isRunning, // Body management createBody, removeBody, getBodies, clear, // Body manipulation applyForce, applyImpulse, setVelocity, setPosition, getBodyState, // Configuration setGravity, setTimeScale, // Events onCollision, // State bodyStates, engine: engineRef.current }; }; export { usePhysicsEngine }; //# sourceMappingURL=usePhysicsEngine.js.map