Game

Game is a runtime of game. Through the methods on the Game instance, the game is controlled to pause and continue. By adding the System to the Game, the game can support different abilities, which are displayed by adding components to the GameObject.

Create a game

  1. <style>
  2. #canvas {
  3. width: 100%;
  4. height: auto;
  5. }
  6. </style>
  7. <canvas id="canvas"></canvas>
  1. import {Game} from '@eva/eva.js'
  2. import {RendererSystem} from '@eva/plugin-renderer'
  3. // Create a rendering system
  4. const rendererSystem = new RendererSystem({
  5. canvas: document.querySelector('#canvas'), // Optional, automatically generate canvas and hang on game.canvas
  6. width: 750, // required
  7. height: 1000, // required
  8. transparent: false, // optional
  9. resolution: window.devicePixelRatio / 2, // Optional, if it is 2 times the image design, it can be divided by 2
  10. enableScroll: true, // Enable page scrolling
  11. renderType: 0 // 0: automatic judgment, 1: WebGL, 2: Canvas, it is recommended to use Canvas below android6.1 ios9, business judgment is required.
  12. })
  13. // Initialize the game
  14. const game = new Game({
  15. frameRate: 60, // optional
  16. autoStart: true, // optional
  17. systems: [rendererSystem]
  18. })

Add system

There are two ways to add a system. One is to pass in the systems parameter of the constructor when the Game is instantiated. For example, the rendererSystem rendering capability is necessary and can be added in this way. The other is to call the addSystem method on the game instance after the game is created. Eva.js provides many systems. These systems are used as plug-ins in a package. For example, if we want to detect the frame rate, we can use the @eva/plugin-stats plug-in.

  1. import {StatsSystem} from '@eva/plugin-stats'
  2. const statsSystem = new StatsSystem({
  3. show: true, // Set here whether to display the fps panel
  4. style: {
  5. x: 0, // The values ​​here are all percentages of the screen width, unit vw
  6. y: 50,
  7. width: 20,
  8. height: 12
  9. }
  10. })
  11. game.addSystem(statsSystem)

Get system

  1. import {StatsSystem} from '@eva/plugin-stats'
  2. const stats = game.getSystem(StatsSystem) // Get system through class
  3. // or
  4. const stats = game.getSystem('StatsSystem') // Get system by system name

Start the game

  1. game.start()

Pause the game

It is recommended to pause the game when the app exits to the background and start after returning

  1. game.pause()

Multi-scene management

Switch scene

  1. import {Scene} from '@eva/eva.js'
  2. const scene = new Scene('bg')
  3. game.loadScene({
  4. scene,
  5. mode: LOAD_SCENE_MODE.SINGLE
  6. })

Render to multiple canvases

In the project, the game will be rendered on a default canvas by default. When we need to render on multiple canvases, we can render the scene on another canvas.

  1. import {Scene, LOAD_SCENE_MODE} from '@eva/eva.js'
  2. const scene = new Scene('bg')
  3. game.loadScene({
  4. scene,
  5. mode: LOAD_SCENE_MODE.MULTI_CANVAS,
  6. params: {
  7. // This is the same as the RendererSystem parameter
  8. canvas: document.querySelector('#canvas'), //Optional, automatically generate canvas and hang on game.canvas
  9. width: 750, //Required
  10. height: 1000, // required
  11. transparent: false, // optional
  12. resolution: window.devicePixelRatio / 2, // Optional, if it is 2 times the image design, it can be divided by 2
  13. enableScroll: true, // Enable page scrolling
  14. renderType: 0
  15. // 0: automatic judgment, 1: WebGL, 2: Canvas, it is recommended to use Canvas under android6.1 ios9, business judgment is required.
  16. }
  17. })

Ticker

add function in per frame

It is recommended that the update method in the Component add the function to be executed per frame, or you can use the add method, which will be executed after the lateUpdate of all systems is executed

  1. game.ticker.add((e: UpdateParams)=>{
  2. })

修改游戏播放速度

  1. game.ticker.setPlaybackRate(1.5) // 1.5倍速播放