Skip to content

Commit

Permalink
feat: refactors, some minor api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac-mason committed Aug 26, 2023
1 parent 23fb4a6 commit b49fb7e
Show file tree
Hide file tree
Showing 47 changed files with 1,577 additions and 1,574 deletions.
6 changes: 6 additions & 0 deletions .changeset/dirty-flies-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@arancini/core": minor
"arancini": minor
---

feat: rename world.destroy() to world.reset(), add world.destroy(entity: Entity)
6 changes: 6 additions & 0 deletions .changeset/funny-pigs-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@arancini/core": minor
"arancini": minor
---

feat: refactor query manager
5 changes: 5 additions & 0 deletions .changeset/great-flowers-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@arancini/react": patch
---

feat: export createECS return type, 'ECS'
5 changes: 5 additions & 0 deletions .changeset/olive-dancers-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@arancini/react": patch
---

feat: refactor useQuery component
6 changes: 6 additions & 0 deletions .changeset/shy-rice-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@arancini/core": minor
"arancini": minor
---

feat: world.entities is now a list, was a map
17 changes: 17 additions & 0 deletions .changeset/twelve-ghosts-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
"arancini": minor
"@arancini/core": minor
---

feat: class components are no longer object pooled by default, they must opted in with the `@objectPooled` annotation, or by setting the `objectPooled` property on the component definition

```ts
@objectPooled()
class MyComponent extends Component { /* ... */ }

// or

= class MyComponent extends Component { /* ... */ };

MyComponent.objectPooled = true;
```
8 changes: 4 additions & 4 deletions apps/benchmarks/src/arancini.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { World, Component, System } from 'arancini'
import { World, Component, System } from '@arancini/core'

class Position extends Component {
construct() {
this.x = 0
this.y = 0
}
}
Position.objectPooled = true

class Velocity extends Component {
construct() {
this.dx = Math.random() - 0.5
this.dy = Math.random() - 0.5
}
}
Velocity.objectPooled = true

let updateCount = 0

class MovementSystem extends System {
onInit() {
this.movement = this.query([Velocity, Position])
}
movement = this.query([Velocity, Position])

onUpdate() {
for (let i = 0; i < this.movement.entities.length; i++) {
Expand Down
18 changes: 6 additions & 12 deletions apps/react-demo/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { Canvas, useFrame } from '@react-three/fiber'
import { Component, System, World } from 'arancini'
import { createECS } from 'arancini/react'
import { Object3D, Vector3, Vector3Tuple } from 'three'
import { Vector3, Vector3Tuple } from 'three'

class ThreeComponent extends Component {
object!: Object3D

construct(object: Object3D) {
this.object = object
}
}
const Object3DComponent = Component.object<THREE.Object3D>('Object3D')

class AngularVelocity extends Component {
linvel!: Vector3
Expand All @@ -20,11 +14,11 @@ class AngularVelocity extends Component {
}

class LinearVelocitySystem extends System {
linvel = this.query([AngularVelocity, ThreeComponent])
linvel = this.query([AngularVelocity, Object3DComponent])

onUpdate(delta: number): void {
for (const entity of this.linvel) {
const { object } = entity.get(ThreeComponent)
const object = entity.get(Object3DComponent)
const { linvel } = entity.get(AngularVelocity)

object.rotation.x += linvel.x * delta
Expand All @@ -36,7 +30,7 @@ class LinearVelocitySystem extends System {

const world = new World()

world.registerComponent(ThreeComponent)
world.registerComponent(Object3DComponent)
world.registerComponent(AngularVelocity)
world.registerSystem(LinearVelocitySystem)
world.init()
Expand All @@ -51,7 +45,7 @@ const App = () => {
return (
<>
<ECS.Entity>
<ECS.Component type={ThreeComponent}>
<ECS.Component type={Object3DComponent}>
<mesh>
<boxGeometry />
<meshStandardMaterial color="orange" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import React, { useEffect } from 'react'

import './find-the-bomb.css'

const GameState = Component.object<{ clicks: number; foundBomb: boolean }>('GameState')
const GameState = Component.object<{ clicks: number; foundBomb: boolean }>(
'GameState'
)

const Emoji = Component.object<{
revealed: boolean
Expand All @@ -13,7 +15,9 @@ const Emoji = Component.object<{

const Position = Component.object<{ x: number; y: number }>('Position')

const DistanceToTarget = Component.object<{ distance: number }>('DistanceToTarget')
const DistanceToTarget = Component.object<{ distance: number }>(
'DistanceToTarget'
)

const Target = Component.tag('Target')

Expand Down Expand Up @@ -219,7 +223,7 @@ export const FindTheBomb = () => {
}

return () => {
world.destroy()
world.reset()
}
}

Expand Down Expand Up @@ -257,7 +261,7 @@ export const FindTheBomb = () => {

return () => {
running = false
world.destroy()
world.reset()
}
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component } from '@arancini/core'
import { Component, objectPooled } from '@arancini/core'
import { Vector2 } from './utils'

@objectPooled()
export class Movement extends Component {
velocity: Vector2
acceleration: Vector2
Expand All @@ -11,6 +12,7 @@ export class Movement extends Component {
}
}

@objectPooled()
export class Circle extends Component {
position: Vector2
radius: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const OverlappingCircles = () => {

return () => {
running = false
world.destroy()
world.reset()
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const PlayerInventoryEvents = () => {

return () => {
running = false
world.destroy()
world.reset()
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export const RandomColorChangingWalkers = () => {

return () => {
running = false
world.destroy()
world.reset()
}
})

Expand Down
Loading

1 comment on commit b49fb7e

@vercel
Copy link

@vercel vercel bot commented on b49fb7e Aug 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.