Source file: /examples/transform/2d/translate.js

import {
  createCamera2D,
  BasicMaterial,
  Mesh,
  Position2D,
  World,
  Query,
  EntityCommands,
  VirtualClock,
  Meshed,
  createBasicMesh2D,
  BasicMaterialAssets,
  MeshAssets,
  App,
  AppSchedule,
  Canvas2DRendererPlugin,
  DOMWindowPlugin,
  DefaultPlugin,
  FPSDebugger
} from 'wima'

const app = new App()

app
  .registerPlugin(new DefaultPlugin())
  .registerPlugin(new DOMWindowPlugin())
  .registerPlugin(new Canvas2DRendererPlugin())
  .registerSystem({ schedule: AppSchedule.Startup, system: addmesh })
  .registerSystem({ schedule: AppSchedule.Startup, system: addDefaultCamera2D })
  .registerSystem({ schedule: AppSchedule.Update, system: updateMesh })
  .registerDebugger(new FPSDebugger())
  .run()

/**
 * @param {World} world
 */
function addDefaultCamera2D(world) {
  const commands = new EntityCommands(world)

  commands
    .spawn()
    .insertPrefab([...createCamera2D()])
    .build()
}

/**
 * @param {World} world
 */
function addmesh(world) {
  const commands = new EntityCommands(world)
  const meshes = world.getResource(MeshAssets)
  const materials = world.getResource(BasicMaterialAssets)

  const mesh = meshes.add(Mesh.quad2D(0.2, 0.2))
  const material = materials.add(new BasicMaterial())

  commands
    .spawn()
    .insertPrefab([
      ...createBasicMesh2D(mesh, material)])
    .build()
}

/**
 * @param {World} world
 */
function updateMesh(world) {
  const query = new Query(world, [Position2D, Meshed])
  const clock = world.getResource(VirtualClock)
  const dt = clock.getElapsed()

  query.each(([position]) => {
    const amplitude = 0.7

    position.x = Math.sin(dt) * amplitude
    position.y = Math.cos(dt) * amplitude
  })
}