Skip to content

Commit

Permalink
feat(sandbox): remove use of @arancini/systems
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac-mason committed Apr 22, 2024
1 parent 08a99c0 commit c911c92
Show file tree
Hide file tree
Showing 29 changed files with 318 additions and 355 deletions.
5 changes: 0 additions & 5 deletions .changeset/brown-parrots-hear.md

This file was deleted.

2 changes: 1 addition & 1 deletion .changeset/cool-rocks-pump.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"@p2-es/sandbox": patch
---

chore: update arancini to v6
chore: update arancini to v6, stop using @arancini/systems
4 changes: 2 additions & 2 deletions apps/p2-es-demos/src/concave.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div id="app" style="width: 100%; height: 100vh;"></div>

<script type="module">
import { Sandbox, Tools } from '@p2-es/sandbox'
import { Sandbox, Tool } from '@p2-es/sandbox'
import * as p2 from 'p2-es'

new Sandbox(
Expand Down Expand Up @@ -73,7 +73,7 @@
world,
// Enable shape drawing
tools: {
default: Tools.POLYGON,
default: Tool.POLYGON,
},
}
},
Expand Down
30 changes: 15 additions & 15 deletions packages/p2-es-sandbox/src/controls.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Leva, button, useControls } from 'leva'
import { ButtonInput } from 'leva/plugin'
import React, { useEffect } from 'react'
import { SandboxSettings, Tool, Tools, useSingletonQuery } from './ecs'
import { SandboxSettingsStore } from './state/sandbox-settings'
import { SandboxSettings, SandboxSettingsStore, usePhysicsWorldStore } from './state'
import { Tool } from './tools'
import { levaTheme } from './ui'

type ButtonGroupControlsProps = {
Expand Down Expand Up @@ -48,7 +48,7 @@ export type ControlsProps = {
}

export const Controls = ({ scene, scenes, setScene, tool, setTool, settings, setSettings, reset }: ControlsProps) => {
const physicsWorld = useSingletonQuery('physicsWorld')
const { world: physics } = usePhysicsWorldStore()

useButtonGroupControls('Scene', {
options: scenes.map((s, idx) => ({
Expand All @@ -62,10 +62,10 @@ export const Controls = ({ scene, scenes, setScene, tool, setTool, settings, set

useButtonGroupControls('Tool', {
options: [
{ name: 'Pick/Pan [q]', value: Tools.PICK_PAN },
{ name: 'Polygon [d]', value: Tools.POLYGON },
{ name: 'Circle [a]', value: Tools.CIRCLE },
{ name: 'Rectangle [f]', value: Tools.RECTANGLE },
{ name: 'Pick/Pan [q]', value: Tool.PICK_PAN },
{ name: 'Polygon [d]', value: Tool.POLYGON },
{ name: 'Circle [a]', value: Tool.CIRCLE },
{ name: 'Rectangle [f]', value: Tool.RECTANGLE },
],
current: tool,
onChange: (value) => setTool(value as Tool),
Expand Down Expand Up @@ -104,12 +104,12 @@ export const Controls = ({ scene, scenes, setScene, tool, setTool, settings, set
}, [settings.paused, settings.physicsStepsPerSecond, settings.maxSubSteps])

const manualStep = () => {
if (!physicsWorld) return
if (!physics) return

setSettings({ paused: true })

const timeStep = 1 / settings.physicsStepsPerSecond
physicsWorld.step(timeStep, timeStep)
physics.step(timeStep, timeStep)
}

useControls(
Expand All @@ -122,7 +122,7 @@ export const Controls = ({ scene, scenes, setScene, tool, setTool, settings, set
reset()
}),
},
[physicsWorld, settings, reset]
[physics, settings, reset]
)

const [, setRendering] = useControls('Rendering', () => ({
Expand Down Expand Up @@ -187,19 +187,19 @@ export const Controls = ({ scene, scenes, setScene, tool, setTool, settings, set
const key = event.key.toLowerCase()

if (key === 'q') {
return setTool(Tools.PICK_PAN)
return setTool(Tool.PICK_PAN)
}

if (key === 'd') {
return setTool(Tools.POLYGON)
return setTool(Tool.POLYGON)
}

if (key === 'a') {
return setTool(Tools.CIRCLE)
return setTool(Tool.CIRCLE)
}

if (key === 'f') {
return setTool(Tools.RECTANGLE)
return setTool(Tool.RECTANGLE)
}

if (key === 'p' || key === ' ') {
Expand Down Expand Up @@ -236,7 +236,7 @@ export const Controls = ({ scene, scenes, setScene, tool, setTool, settings, set
return () => {
window.removeEventListener('keydown', handler)
}
}, [physicsWorld, settings, reset])
}, [physics, settings, reset])

return <Leva fill flat theme={levaTheme} titleBar={false} />
}
21 changes: 0 additions & 21 deletions packages/p2-es-sandbox/src/ecs/context.tsx

This file was deleted.

20 changes: 1 addition & 19 deletions packages/p2-es-sandbox/src/ecs/entity.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
import * as p2 from 'p2-es'
import { Graphics } from 'pixi.js'

export type Sprite = {
graphics: Graphics
drawnSleeping: boolean | null
drawnLineColor: number | null
drawnFillColor: number | null
dirty: boolean
}

export const createSprite = () => {
return {
graphics: new Graphics(),
drawnSleeping: null,
drawnLineColor: null,
drawnFillColor: null,
dirty: false,
}
}
import { Sprite } from '../pixi/sprite'

export type Entity = {
physicsBody?: p2.Body
Expand Down
5 changes: 1 addition & 4 deletions packages/p2-es-sandbox/src/ecs/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export * from './entity'
export * from './context'
export * from './pointer-system'
export * from './renderer-systems'
export * from './tool-systems'
export * from './utils'
export * from './world'
86 changes: 0 additions & 86 deletions packages/p2-es-sandbox/src/ecs/physics-system.ts

This file was deleted.

17 changes: 5 additions & 12 deletions packages/p2-es-sandbox/src/ecs/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { QueryFn } from 'arancini'
import { useMemo } from 'react'
import { useECS } from './context'
import { Entity } from './entity'
import { react, world } from './world'

export const useSingletonQuery = <C extends keyof Entity>(component: C) => {
const {
world,
react: { useQuery },
} = useECS()
export const useQuery = <Q extends QueryFn<any, any>>(queryFn: Q) => {
const query = useMemo(() => world.query(queryFn), [world])

const singletonComponentQuery = useMemo(() => world.query((e) => e.has(component)), [world])

const query = useQuery(singletonComponentQuery)

return useMemo(() => query.first?.[component] ?? null, [query.version])
return react.useQuery(query)
}
6 changes: 6 additions & 0 deletions packages/p2-es-sandbox/src/ecs/world.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { World } from 'arancini'
import { createReactAPI } from 'arancini/react'
import { Entity } from './entity'

export const world = new World<Entity>()
export const react = createReactAPI(world)
File renamed without changes.
File renamed without changes.
32 changes: 6 additions & 26 deletions packages/p2-es-sandbox/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import { World } from 'arancini'
import { createReactAPI } from 'arancini/react'
import { Executor } from 'arancini/systems'
import React from 'react'
import * as ReactDOM from 'react-dom/client'
import { ThemeProvider } from 'styled-components'
import {
Entity,
PhysicsAABBRendererSystem,
PhysicsBodyRendererSystem,
PhysicsContactRendererSystem,
PhysicsSpringRendererSystem,
} from './ecs'
import { PhysicsSystem } from './ecs/physics-system'
import { world } from './ecs'
import { createPixiApp } from './pixi'
import { Sandbox as SandboxComponent } from './sandbox'
import { SandboxConfig, SandboxContext, SandboxFunction, Scenes } from './sandbox-api'
import { useECS } from './state/ecs'
import { usePixiStore } from './state/pixi'
import { usePixiStore } from './state'
import { styledComponentsTheme } from './ui'

const CONSOLE_MESSAGE = `
Expand All @@ -39,7 +28,7 @@ body.addShape(new p2.Circle({
world.addBody(body);
`

export { Tools, type Tool } from './ecs'
export { Tool } from './tools'
export type { SandboxConfig, SandboxContext, SandboxFunction, Scenes }

export type SandboxProps = {
Expand All @@ -64,22 +53,11 @@ export class Sandbox {

mount(domElement: HTMLElement): this {
if (!this.root) {
const world = new World<Entity>()
const executor = new Executor(world)
const react = createReactAPI(world)
useECS.setState({ world, executor, react })
world.clear()

const { destroy: destroyPixi, ...pixi } = createPixiApp()
usePixiStore.setState(pixi)

executor.add(PhysicsSystem)
executor.add(PhysicsSpringRendererSystem)
executor.add(PhysicsContactRendererSystem)
executor.add(PhysicsBodyRendererSystem)
executor.add(PhysicsAABBRendererSystem)

executor.init()

this.cleanup = () => {
destroyPixi()
}
Expand All @@ -106,6 +84,8 @@ export class Sandbox {

unmount(): void {
if (this.root) {
world.clear()

this.cleanup?.()
this.cleanup = undefined

Expand Down
Loading

0 comments on commit c911c92

Please sign in to comment.