@inglorious/renderer-2d 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/renderer-2d",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "A renderer for the Inglorious Engine, using the Context2D of the Canvas API.",
5
5
  "author": "IceOnFire <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "license": "MIT",
@@ -18,14 +18,14 @@
18
18
  "./*": "./src/*"
19
19
  },
20
20
  "files": [
21
- "src/*"
21
+ "src"
22
22
  ],
23
23
  "publishConfig": {
24
24
  "access": "public"
25
25
  },
26
26
  "dependencies": {
27
- "@inglorious/engine": "0.8.0",
28
- "@inglorious/utils": "2.0.0"
27
+ "@inglorious/engine": "0.10.0",
28
+ "@inglorious/utils": "2.2.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "eslint": "^9.34.0",
@@ -6,11 +6,11 @@ export function absolutePosition(render) {
6
6
  const [x, y, z] = snap(position)
7
7
 
8
8
  const game = api.getEntity("game")
9
- const [, , , screenHeight] = game.bounds
9
+ const [, gameHeight] = game.size
10
10
 
11
11
  ctx.save()
12
12
 
13
- ctx.translate(x, screenHeight - y - z)
13
+ ctx.translate(x, gameHeight - y - z)
14
14
  render(entity, ctx, api)
15
15
 
16
16
  ctx.restore()
package/src/index.js CHANGED
@@ -1,68 +1,10 @@
1
- import { track } from "@inglorious/engine/behaviors/input/mouse.js"
2
-
3
- import { createRenderingSystem } from "./rendering-system.js"
4
-
5
- export class Renderer2D {
6
- _onKeyPress = null
7
- _onMouseMove = null
8
- _onClick = null
9
- _onWheel = null
10
-
11
- constructor(canvas) {
12
- this.canvas = canvas
13
- this.ctx = canvas.getContext("2d")
14
- }
15
-
16
- getSystems() {
17
- return [createRenderingSystem(this.ctx)]
18
- }
19
-
20
- init(engine) {
21
- const game = engine._api.getEntity("game")
22
- const [, , width, height] = game.bounds
23
-
24
- this.canvas.style.width = `${width}px`
25
- this.canvas.style.height = `${height}px`
26
- const dpi = window.devicePixelRatio
27
- this.canvas.width = width * dpi
28
- this.canvas.height = height * dpi
29
- this.ctx.scale(dpi, dpi)
30
-
31
- if (game.pixelated) {
32
- this.canvas.style.imageRendering = "pixelated"
33
- this.ctx.textRendering = "geometricPrecision"
34
- this.ctx.imageSmoothingEnabled = false
35
- }
36
-
37
- this._onKeyPress = (event) => {
38
- if (event.key === "p") {
39
- engine.isRunning ? engine.stop() : engine.start()
40
- }
41
- }
42
- document.addEventListener("keypress", this._onKeyPress)
43
-
44
- const { onMouseMove, onClick, onWheel } = track(this.canvas, engine._api)
45
- this._onMouseMove = onMouseMove
46
- this._onClick = onClick
47
- this._onWheel = onWheel
48
-
49
- this.canvas.addEventListener("mousemove", this._onMouseMove)
50
- this.canvas.addEventListener("click", this._onClick)
51
- this.canvas.addEventListener("wheel", this._onWheel)
52
- }
53
-
54
- destroy() {
55
- if (this._onKeyPress) {
56
- document.removeEventListener("keypress", this._onKeyPress)
57
- }
58
- if (this._onMouseMove) {
59
- this.canvas.removeEventListener("mousemove", this._onMouseMove)
60
- }
61
- if (this._onClick) {
62
- this.canvas.removeEventListener("click", this._onClick)
63
- }
64
- if (this._onWheel) {
65
- this.canvas.removeEventListener("wheel", this._onWheel)
66
- }
1
+ import { rendering } from "./rendering-behavior"
2
+ import { renderingSystem } from "./rendering-system"
3
+
4
+ export function createRenderer(canvas) {
5
+ return {
6
+ types: { renderer: [rendering(canvas)] },
7
+ entities: { renderer: { type: "renderer" } },
8
+ systems: [renderingSystem(canvas)],
67
9
  }
68
10
  }
@@ -0,0 +1,69 @@
1
+ import { track } from "@inglorious/engine/behaviors/input/mouse.js"
2
+
3
+ const ORIGIN = 0
4
+ const HALF = 2
5
+
6
+ export function rendering(canvas) {
7
+ const ctx = canvas.getContext("2d")
8
+
9
+ let _onMouseMove = null
10
+ let _onClick = null
11
+ let _onWheel = null
12
+
13
+ return {
14
+ init(entity, event, api) {
15
+ const game = api.getEntity("game")
16
+ const [gameWidth, gameHeight] = game.size
17
+
18
+ const canvasWidth = canvas.width
19
+ const canvasHeight = canvas.height
20
+
21
+ const scaleX = canvasWidth / gameWidth
22
+ const scaleY = canvasHeight / gameHeight
23
+ const scale = Math.min(scaleX, scaleY)
24
+ const scaledGameWidth = gameWidth * scale
25
+ const scaledGameHeight = gameHeight * scale
26
+
27
+ const offsetX = (canvasWidth - scaledGameWidth) / HALF
28
+ const offsetY = (canvasHeight - scaledGameHeight) / HALF
29
+
30
+ const dpi = window.devicePixelRatio
31
+
32
+ ctx.clearRect(ORIGIN, ORIGIN, canvasWidth, canvasHeight)
33
+ ctx.fillStyle = "black"
34
+ ctx.fillRect(ORIGIN, ORIGIN, canvasWidth, canvasHeight)
35
+
36
+ ctx.translate(offsetX, offsetY)
37
+ ctx.scale(dpi * scale, dpi * scale)
38
+
39
+ if (game.pixelated) {
40
+ canvas.style.imageRendering = "pixelated"
41
+ ctx.textRendering = "geometricPrecision"
42
+ ctx.imageSmoothingEnabled = false
43
+ }
44
+
45
+ const { onMouseMove, onClick, onWheel } = track(canvas, api)
46
+ _onMouseMove = onMouseMove
47
+ _onClick = onClick
48
+ _onWheel = onWheel
49
+
50
+ canvas.addEventListener("mousemove", _onMouseMove)
51
+ canvas.addEventListener("click", _onClick)
52
+ canvas.addEventListener("wheel", _onWheel)
53
+ },
54
+
55
+ destroy(entity, id) {
56
+ if (id !== entity.id) return
57
+
58
+ if (_onMouseMove) {
59
+ canvas.removeEventListener("mousemove", _onMouseMove)
60
+ }
61
+ if (_onClick) {
62
+ canvas.removeEventListener("click", _onClick)
63
+ }
64
+ if (_onWheel) {
65
+ canvas.removeEventListener("wheel", _onWheel)
66
+ }
67
+ },
68
+ }
69
+ }
@@ -10,30 +10,18 @@ const DEFAULT_LAYER = 0
10
10
  const Y = 1
11
11
  const Z = 2
12
12
 
13
- function getRenderFunction(types, entity) {
14
- const typeInfo = types[entity.type]
15
- if (!typeInfo) {
16
- return null
17
- }
18
-
19
- // A type can be a single object or an array of behaviors
20
- const behaviorWithRender = Array.isArray(typeInfo)
21
- ? typeInfo.find((b) => b.render)
22
- : typeInfo
23
-
24
- return behaviorWithRender?.render
25
- }
13
+ export function renderingSystem(canvas) {
14
+ const ctx = canvas.getContext("2d")
26
15
 
27
- export function createRenderingSystem(ctx) {
28
16
  return {
29
17
  update(state, dt, api) {
30
18
  const types = api.getTypes()
31
19
  const { game, ...worldEntities } = state.entities
32
20
 
33
21
  // 1. Clear canvas
34
- const [, , width, height] = game.bounds
22
+ const [gameWidth, gameHeight] = game.size
35
23
  ctx.fillStyle = game.backgroundColor || "lightgrey"
36
- ctx.fillRect(ORIGIN, ORIGIN, width, height)
24
+ ctx.fillRect(ORIGIN, ORIGIN, gameWidth, gameHeight)
37
25
 
38
26
  // 2. Find active camera
39
27
  const camera = Object.values(state.entities).find(
@@ -49,11 +37,11 @@ export function createRenderingSystem(ctx) {
49
37
 
50
38
  // Center the view on the camera and apply zoom.
51
39
  // The order of operations is crucial here.
52
- ctx.translate(width / HALF, height / HALF)
40
+ ctx.translate(gameWidth / HALF, gameHeight / HALF)
53
41
  ctx.scale(zoom, zoom)
54
42
  // This vertical translation compensates for the coordinate system flip
55
43
  // that happens inside the absolutePosition decorator.
56
- ctx.translate(ORIGIN, -height)
44
+ ctx.translate(ORIGIN, -gameHeight)
57
45
  // This translation moves the world relative to the camera.
58
46
  ctx.translate(-cameraX, cameraZ)
59
47
  }
@@ -68,7 +56,8 @@ export function createRenderingSystem(ctx) {
68
56
  b.position[Z] - a.position[Z],
69
57
  )
70
58
  .forEach((entity) => {
71
- const render = getRenderFunction(types, entity)
59
+ const type = types[entity.type]
60
+ const { render } = type
72
61
  if (render) {
73
62
  absolutePosition(render)(entity, ctx, { api })
74
63
  }