Plug-in development

Generally, most of the logic can be realized through script components, involving general capabilities at the engine level or business level, and can be developed in the form of plug-ins. The plug-ins are mainly composed of Component and System.

The rendering of Eva.js is based on PixiJS. Generally, plugins such as Img/Sprite/Spine actually create Pixi rendering objects, and plugins such as Stats/EvaX/Transition do not depend on Pixi. No matter what kind of plug-in, it outputs Component and System to the engine, but there are some differences in the development plan. Next, I will explain the simplest plug-in development method.

We provide a plugin template, you can click on Use this Template to directly use the template for development, with the necessary scaffolding inside.

Basic

Development

After reading this, I believe that everyone already knows about Eva.js and knows how to use it in the project. Here is a simple way to use a plug-in.

  1. import {Demo, DemoSystem} from'.lib'
  2. const game = new Game({
  3. systems: [new DemoSystem()]
  4. })
  5. const go = new GameObject()
  6. go.addComponent(new Demo())
  7. game.scene.addChild(go)

We can see that the plug-in is composed of Component and System, and a plug-in does not necessarily contain only one Component.

Therefore, the development of plug-ins needs to implement the Component and System that are exposed to users.

Plug-in operation logic

Component (Component) can give game object capabilities, we record some configurations and properties on the component. System is used to read the data on the component and realize the corresponding ability of the component.

After the system is added to the game instance, the system performs a series of corresponding operations when the components it needs to care about are added, removed, and changed to achieve some functions.

For example, in the Img plug-in, when the Img is added to the game object, a Pixi Sprite object will be created in the System and mounted on the Pixi Container corresponding to the GameObject. When the resource of the Img component changes, the System will modify the corresponding The texture on the sprite.

Next, I will explain how to design a component and how the System monitors component changes.

Build and release specifications

Development Practice

Take @eva/plugin-a11y plug-in as an example to give a detailed introduction to Eva.js plug-in development.

@eva/plugin-a11y is used to add accessibility to game objects. In DOM development, an accessible reader can read the content of HTML elements. Currently, drawing elements in Canvas cannot achieve accessibility. The @eva/plugin-a11y plug-in automatically adds by locating the position of the game object Auxiliary DOM allows game objects to be focused by an accessible reader, allowing the game to have accessibility features.

First design the Component, both need to give the ability of the game object.

Usage

  1. import {A11y, A11ySystem} from '@eva/plugin-a11y'
  2. const game = new Game({
  3. systems: [new A11ySystem()]
  4. })
  5. const go = new GameObject()
  6. go.addComponent(new A11y({
  7. hint:'Content to be read aloud'
  8. }))
  9. game.scene.addChild(go)

Component Design

-Determine the component name: A11y -Design component parameters: -hint what needs to be read

  1. import {Component} from '@eva/eva.js'
  2. export default class A11y extends Component {
  3. static componentName: string ='A11y' // Here is the name of the Component, which is used by the System to monitor changes
  4. /**
  5. * Accessible label reading content
  6. */
  7. public hint: string
  8. /**
  9. * Initialization method, the parameters of the constructor will be passed here
  10. */
  11. init(param = {hint:''}) {
  12. const {hint} = param
  13. this.hint = hint
  14. }
  15. }

System Design

-Determine the components to be monitored and which parameter changes need to be monitored -Determine the system name -Implement logic according to component changes

Step1 Determine the components and parameters to be monitored

  1. import {System, decorators} from '@eva/eva.js'
  2. @decorators.componentObserver({
  3. A11y: ['hint'] // monitor the hint attribute changes of the A11y component
  4. })
  5. class A11ySystem extends System {
  6. }

In the above code, we pass the name of the component that needs to be monitored for changes and the monitoring properties into @decorators.componentObserver in order to create the listener.

If you only need to add or remove monitoring components, you can leave out specific properties, such as

  1. @decorators.componentObserver({
  2. A11y: [] // Monitor the hint attribute changes of the A11y component
  3. })

If the monitored attribute is not directly mounted on the component object, there is a level of nesting

For example, monitor the size attribute under the style attribute of component A

It can be written like this:

  1. @decorators.componentObserver({
  2. A: [{
  3. prop: ['style','size']
  4. }]
  5. })

If you want to monitor the style attribute in depth, you can write

  1. @decorators.componentObserver({
  2. A: [{
  3. prop: ['style'],
  4. deep: true
  5. }]
  6. })

If you want to monitor changes in multiple components, you can write like this

  1. @decorators.componentObserver({
  2. A: [{
  3. prop: ['style'],
  4. deep: true
  5. }]
  6. B: ['props']
  7. })

Step2 Set the system name

Set a name for System

  1. import {System, decorators} from '@eva/eva.js'
  2. @decorators.componentObserver({
  3. A11y: ['hint'] // monitor the hint attribute changes of the A11y component
  4. })
  5. class A11ySystem extends System {
  6. static systemName ='A11ySystem';
  7. }

Step3 Implement logic according to component changes

Before that, we did some monitoring configuration, so how do we get the corresponding changes?

We know that System has an update life cycle, and we can get the changes of the current frame Component during the life cycle.

  1. import {System, decorators, ComponentChanged} from '@eva/eva.js'
  2. @decorators.componentObserver({
  3. A11y: ['hint'] // monitor the hint attribute changes of the A11y component
  4. })
  5. class A11ySystem extends System {
  6. static systemName ='A11ySystem';
  7. private elemMap = new Map()
  8. update () {
  9. const changes: ComponentChanged[] = this.componentObserver.clear() // Get all the major changes of the components that need to be monitored in the current frame, and clean up
  10. for (const changed of changes) {
  11. switch (changed.type) {
  12. case OBSERVER_TYPE.ADD:
  13. this.add(changed);
  14. break;
  15. case OBSERVER_TYPE.CHANGE:
  16. this.change(changed)
  17. break;
  18. case OBSERVER_TYPE.REMOVE:
  19. this.remove(changed);
  20. break;
  21. }
  22. }
  23. }
  24. add(changed) {
  25. if (changed.componentName ==='A11y') {// If there are multiple components, they need to be processed separately
  26. const component = changed.component as A11y
  27. const elem = document.createElement('div')
  28. elem.setAttribute('aria-label', component.hint);
  29. this.elemMap.set(component, elem)
  30. document.body.append(elem) // add to body
  31. }
  32. }
  33. remove(changed) {
  34. if (changed.componentName ==='A11y') {// If there are multiple components, they need to be processed separately
  35. const component = changed.component as A11y
  36. const elem = this.elemMap.get(component)
  37. elem.remove() // remove elem
  38. }
  39. }
  40. change(changed) {
  41. if (changed.componentName ==='A11y') {// If there are multiple components, they need to be processed separately
  42. if (changed.prop?.prop[0] ==='hint'){ //If there are multiple monitoring properties that need to be processed separately
  43. const component = changed.component as A11y
  44. elem.setAttribute('aria-label', component.hint);
  45. }
  46. }
  47. }
  48. }

The corresponding type of ComponentChanged is like this, you can refer to it, you don’t need to implement it in the code

  1. export interface PureObserverProp {
  2. deep: boolean;
  3. prop: string[];
  4. }
  5. export enum ObserverType {
  6. ADD ='ADD',
  7. REMOVE='REMOVE',
  8. CHANGE ='CHANGE',
  9. }
  10. export interface ComponentChanged {
  11. type: ObserverType;
  12. component: Component;
  13. componentName: string;
  14. prop?: PureObserverProp;
  15. gameObject?: GameObject;
  16. systemName?: string;
  17. }

Now we have created the DOM and placed it on the body. In terms of ability, we have completed the specific functions, because screen readers can already read the elements in the game, but it seems that some content is currently lacking, such as: unable By triggering DOM click events to trigger clicks in the game, the DOM has no width, height and positioning.

If you want to implement these functions, you have to get other components under the current component to implement the functions. If you want to trigger a click event, you need to determine whether Event component is installed. If it is installed, you can follow The event bound to Event triggers the corresponding event. If you want to get the width and height position, you can get the Transform component of the game object

To increase the monitoring of the Event component, do the corresponding operations in the above add and remove methods.

  1. @decorators.componentObserver({
  2. A11y: ['hint'] // monitor the hint attribute changes of the A11y component
  3. Event: [] // Event add delete monitor
  4. })
  5. class A11ySystem extends System {
  6. }

For the position and width and height, you can get the Transform corresponding to the GameObject when the A11y component is added. Here is just an example

  1. add(changed) {
  2. if (changed.componentName ==='A11y') {// If there are multiple components, they need to be processed separately
  3. const component = changed.component as A11y
  4. const elem = document.createElement('div')
  5. elem.setAttribute('aria-label', component.hint);
  6. this.elemMap.set(component, elem)
  7. document.body.append(elem) // add to body
  8. const transform = changed.gameObject.transform
  9. elem.style.width = transform.size.width +'px'
  10. elem.style.height = transform.size.width +'px'
  11. elem.style.x = transform.position.x +'px'
  12. elem.style.y = transform.position.y +'px'
  13. }
  14. }

Plug-in based on PixiJS

Take the picture component as an example:

  1. import {
  2. GameObject,
  3. decorators,
  4. resource,
  5. ComponentChanged,
  6. RESOURCE_TYPE,
  7. OBSERVER_TYPE,
  8. } from '@eva/eva.js';
  9. import {
  10. RendererManager,
  11. ContainerManager,
  12. RendererSystem,
  13. Renderer,
  14. } from '@eva/plugin-renderer';
  15. @decorators.componentObserver({
  16. Img: [{prop: ['resource'], deep: false}],
  17. })
  18. export default class Img extends Renderer {// Based on PixiJS rendering plug-in, our System needs to inherit from a unified Renderer class
  19. rendererSystem: RendererSystem;
  20. init() {// Get the rendererSystem in init to add Pixi objects later, and the current system needs to be registered in the rendererManager.
  21. this.rendererSystem = this.game.getSystem(RendererSystem) as RendererSystem;
  22. this.rendererSystem.rendererManager.register(this);
  23. }
  24. rendererUpdate(gameObject: GameObject) {// rendererUpdate replaces the Update method, because update has been implemented in the Renderer class
  25. const {width, height} = gameObject.transform.size;
  26. if (this.imgs[gameObject.id]) {
  27. this.imgs[gameObject.id].sprite.width = width;
  28. this.imgs[gameObject.id].sprite.height = height;
  29. }
  30. }
  31. async componentChanged(changed: ComponentChanged) {// The update method is implemented in the Renderer class, and the component change corresponding to Img is passed to componentChanged
  32. if (changed.componentName ==='Img') {
  33. const component: ImgComponent = changed.component as ImgComponent;
  34. if (changed.type === OBSERVER_TYPE.ADD) {
  35. const sprite = new Sprite(null);
  36. resource.getResource(component.resource).then(({instance}) => {
  37. if (!instance) {
  38. console.error(
  39. `GameObject:${changed.gameObject.name}'s Img resource load error`,
  40. );
  41. }
  42. sprite.image = instance;
  43. });
  44. this.imgs[changed.gameObject.id] = sprite;
  45. this.containerManager
  46. .getContainer(changed.gameObject.id)
  47. .addChildAt(sprite.sprite, 0); // Put the created Pixi rendering object into the Pixi container corresponding to the GameObject
  48. } else if (changed.type === OBSERVER_TYPE.CHANGE) {
  49. const {instance} = await resource.getResource(component.resource);
  50. if (!instance) {
  51. console.error(
  52. `GameObject:${changed.gameObject.name}'s Img resource load error`,
  53. );
  54. }
  55. this.imgs[changed.gameObject.id].image = instance;
  56. } else if (changed.type === OBSERVER_TYPE.REMOVE) {
  57. const sprite = this.imgs[changed.gameObject.id];
  58. this.containerManager
  59. .getContainer(changed.gameObject.id)
  60. .removeChild(sprite.sprite);
  61. delete this.imgs[changed.gameObject.id];
  62. }
  63. }
  64. }

life cycle

image.png

to sum up

Through the combination of Component and System, we can implement a variety of common plug-ins. In daily development, we only need the capabilities provided by CustomComponent to develop game logic.