edbentley / replay

A cross-platform JS game engine inspired by React
https://replay.js.org
MIT License
298 stars 11 forks source link

Mutable Sprites #106

Closed edbentley closed 2 years ago

edbentley commented 2 years ago

This PR introduces Mutable Sprites, an alternative API to reduce the amount of GC to be collected.

Breaking changes:

import { makeMutableSprite, t2 } from "@replay/core";
import { Player } from "./player";

export const Level = makeMutableSprite({
  init() {
    return { playerX: 0 };
  },

  loop({ state }) {
    state.playerX++;
  },

  render({ state }) {
    return [
      t2.circle(
        {
          radius: 20,
          color: "purple",
        },
        (thisProps) => {
          thisProps.x = state.playerX;
        }
      ),
      Player.Single(
        {
          rotation: 10,
          color: "red",
        },
        (thisProps) => {
          thisProps.x = state.playerX;
        }
      ),
    ];
  },
});