Skip to content

Instantly share code, notes, and snippets.

@3mcd
Last active September 29, 2022 20:10
Show Gist options
  • Save 3mcd/6ebcc553d2e4706978cd68f1527fa7ee to your computer and use it in GitHub Desktop.
Save 3mcd/6ebcc553d2e4706978cd68f1527fa7ee to your computer and use it in GitHub Desktop.
javelin API
import {
changed,
createComponent,
createApp,
createQuery,
createType,
Group,
Format,
createSchema,
useConst,
useQuery,
} from "../src"
enum Id {
Position,
Velocity,
Rotation,
}
interface Vector2 {
x: number
y: number
}
interface Position extends Vector2 {}
interface Velocity extends Vector2 {}
interface Rotation {
angle: number
}
let Vector2 = {x: Format.f32, y: Format.f32}
let Position = createComponent(Id.Position, createSchema<Position>(Vector2))
let Velocity = createComponent(Id.Velocity, createSchema<Velocity>(Vector2))
let Rotation = createComponent(Id.Rotation, createSchema<Rotation>({angle: Format.f32}))
let Point = createType(Position)
let Kinetic = createType(Point, Velocity)
let Transform = createType(Kinetic, Rotation)
createApp()
.addStartupSystem(world => {
world.createEntity(Point, {x: 0, y: 0})
world.createEntity(Kinetic, {x: 0, y: 0}, {x: 0, y: 0})
world.createEntity(Transform, {x: 0, y: 0}, {x: 0, y: 0}, {angle: 0})
})
.addSystemToGroup(Group.First, () => {
console.log("step")
})
.addSystem(() => {
let kinetics = use_const(() =>
createQuery()
.with(Position)
.withOptional(Velocity)
.without(Rotation)
.as(changed(Position), Velocity),
)
useQuery(kinetics).forEach((entity, position, velocity) => {
if (position) {
console.log(
`${entity} changed position${velocity ? ` velocity: ${velocity}` : ""}`,
)
}
})
})
.step()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment