UNPKG

pixi-fusion

Version:

This module offers a set of common components needed for playing games.

23 lines (18 loc) 735 B
import { Body } from "matter-js"; import { useMemo } from "react"; import { GameObjectPhysicalObjectConfig } from "./GameObjectPhysicalObjectConfig"; import { usePhysicalObject } from "./usePhysicalObject"; import { Nullable } from "../types"; export const usePhysicalObjectFromConfig = <PhysicalObjectType extends Nullable<Body> = Body>({ position = { x: 0, y: 0 }, ...physicalObjectConfig }: GameObjectPhysicalObjectConfig) => { const physicalObject = useMemo(() => { if (physicalObjectConfig) { return Body.create({ position, ...physicalObjectConfig }); } return null; }, []) as PhysicalObjectType; usePhysicalObject({ physicalObject }); return { physicalObject }; };