|
20 | 20 | ## Summary
|
21 | 21 |
|
22 | 22 | Focus on stability, strict TypeScript usage, and Vite-enhanced testing while reusing existing code.
|
23 |
| -This dev container includes an up-to-date version of Git, built from source as needed, pre-installed and available on the `PATH`. |
24 |
| -This dev container includes `node`, `npm` and `eslint` pre-installed and available on the `PATH` for Node.js and JavaScript development. |
25 |
| -This dev container includes the GitHub CLI (`gh`), which is pre-installed and available on the `PATH`. IMPORTANT: `gh api -f` does not support object values, use multiple `-f` flags with hierarchical keys and string values instead. When using GitHub actions `actions/upload-artifact` or `actions/download-artifact` use v4 or later. |
| 23 | + |
| 24 | +## PixiJS with React Guidelines |
| 25 | + |
| 26 | +- **Leverage `@pixi/react` Components:** |
| 27 | + - Utilize components like `Stage`, `Container`, `Sprite`, `Graphics`, `Text` from `@pixi/react` for declarative scene graph construction. |
| 28 | + - Refer to the official `@pixi/react` documentation: https://react.pixijs.io/getting-started/ |
| 29 | +- **Extend PixiJS Objects:** |
| 30 | + - Use the `extend` function from `@pixi/react` to make PixiJS display objects (e.g., `Graphics`, `Sprite`) available as React components. |
| 31 | + - Example: `extend({ Graphics, Sprite });` |
| 32 | +- **Component-Based Architecture:** |
| 33 | + - Structure your game elements as reusable React components. |
| 34 | + - Manage state within components using React hooks (`useState`, `useReducer`). |
| 35 | + - Encapsulate PixiJS drawing logic within these components, often using `useCallback` for draw functions passed to `<pixiGraphics />`. |
| 36 | +- **Game Loop and State Updates:** |
| 37 | + - Use the `useTick` hook from `@pixi/react` for logic that needs to run every frame (e.g., animations, physics). |
| 38 | + - Manage game state with React's state management (e.g., `useState`, `useContext`, or external libraries like Zustand/Redux if complexity grows). |
| 39 | + - Ensure state updates correctly trigger re-renders of PixiJS components. |
| 40 | +- **PixiJS Core API:** |
| 41 | + - For advanced features or direct manipulation not covered by `@pixi/react` abstractions, you can still access the core PixiJS API. |
| 42 | + - Refer to the PixiJS API documentation: https://pixijs.download/release/docs/index.html |
| 43 | +- **Event Handling:** |
| 44 | + - Use `@pixi/react`'s event props (e.g., `onClick`, `onPointerDown`) on Pixi components, similar to DOM event handling in React. |
| 45 | + - Ensure `interactive={true}` is set on components that need to respond to pointer events. |
| 46 | +- **Strict Typing with PixiJS:** |
| 47 | + - When interacting with PixiJS objects directly or defining props for Pixi-React components, use precise TypeScript types provided by `pixi.js` and `@pixi/react`. |
| 48 | + - For instance, when using `Graphics`, type the draw callback parameter explicitly: `draw={(g: PIXI.Graphics) => { ... }}`. |
| 49 | + |
| 50 | +### Example: Simple Player Component |
| 51 | + |
| 52 | +```tsx |
| 53 | +import { Sprite, useTick } from "@pixi/react"; |
| 54 | +import { Texture } from "pixi.js"; |
| 55 | +import { useState, useCallback } from "react"; |
| 56 | + |
| 57 | +interface PlayerProps { |
| 58 | + initialX: number; |
| 59 | + initialY: number; |
| 60 | + texture: Texture; |
| 61 | +} |
| 62 | + |
| 63 | +export function Player({ |
| 64 | + initialX, |
| 65 | + initialY, |
| 66 | + texture, |
| 67 | +}: PlayerProps): JSX.Element { |
| 68 | + const [position, setPosition] = useState({ x: initialX, y: initialY }); |
| 69 | + |
| 70 | + useTick((delta: number) => { |
| 71 | + // Example movement logic |
| 72 | + // setPosition(prev => ({ x: prev.x + 1 * delta, y: prev.y })); |
| 73 | + }); |
| 74 | + |
| 75 | + const handleClick = useCallback(() => { |
| 76 | + console.log("Player clicked!"); |
| 77 | + // Update state or trigger game logic |
| 78 | + }, []); |
| 79 | + |
| 80 | + return ( |
| 81 | + <Sprite |
| 82 | + texture={texture} |
| 83 | + x={position.x} |
| 84 | + y={position.y} |
| 85 | + anchor={0.5} |
| 86 | + interactive={true} |
| 87 | + cursor="pointer" |
| 88 | + onPointerDown={handleClick} |
| 89 | + /> |
| 90 | + ); |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +https://react.pixijs.io/getting-started/ |
| 95 | +https://pixijs.download/release/docs/index.html |
0 commit comments