@inglorious/renderer-2d 0.8.3 → 2.0.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.8.3",
3
+ "version": "2.0.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",
@@ -24,18 +24,18 @@
24
24
  "access": "public"
25
25
  },
26
26
  "dependencies": {
27
- "@inglorious/utils": "3.5.3",
28
- "@inglorious/engine": "2.1.3"
27
+ "@inglorious/utils": "3.6.0",
28
+ "@inglorious/engine": "3.0.0"
29
29
  },
30
30
  "peerDependencies": {
31
- "@inglorious/utils": "3.5.3",
32
- "@inglorious/engine": "2.1.3"
31
+ "@inglorious/engine": "3.0.0",
32
+ "@inglorious/utils": "3.6.0"
33
33
  },
34
34
  "devDependencies": {
35
35
  "eslint": "^9.34.0",
36
36
  "prettier": "^3.6.2",
37
37
  "vitest": "^1.6.0",
38
- "@inglorious/eslint-config": "1.0.0"
38
+ "@inglorious/eslint-config": "1.0.1"
39
39
  },
40
40
  "scripts": {
41
41
  "format": "prettier --write '**/*.{js,jsx}'",
@@ -1,7 +1,7 @@
1
1
  import { snap, zero } from "@inglorious/utils/math/vector.js"
2
2
 
3
3
  export function absolutePosition(render) {
4
- return (entity, ctx, { api }) => {
4
+ return (entity, ctx, api) => {
5
5
  const { position = zero() } = entity
6
6
  const [x, y, z] = snap(position)
7
7
 
@@ -5,7 +5,7 @@ const HALF = 2
5
5
  const NO_Y = 0
6
6
  const MAX_HUE = 255
7
7
 
8
- export function renderHitmask(entity, ctx) {
8
+ export function renderHitmask(entity, ctx, api) {
9
9
  const { tileSize, columns, heights } = entity
10
10
 
11
11
  const [tileWidth, tileHeight] = tileSize
@@ -42,7 +42,7 @@ export function renderHitmask(entity, ctx) {
42
42
  backgroundColor: `hsla(${hue}, 100%, 50%, 0.2)`,
43
43
  }
44
44
 
45
- renderRectangle(entity, ctx)
45
+ renderRectangle(entity, ctx, api)
46
46
 
47
47
  ctx.restore()
48
48
  })
@@ -1,10 +1,18 @@
1
1
  const DEFAULT_POSITION = 0
2
+ const DEFAULT_ANCHOR = [DEFAULT_POSITION, DEFAULT_POSITION]
2
3
 
3
- export function renderImage(entity, ctx) {
4
+ export function renderImage(entity, ctx, api) {
4
5
  const { image, sx = DEFAULT_POSITION, sy = DEFAULT_POSITION } = entity
5
- const { id, src, imageSize, tileSize = imageSize } = image
6
+ const {
7
+ id,
8
+ src,
9
+ imageSize,
10
+ tileSize = imageSize,
11
+ anchor = DEFAULT_ANCHOR,
12
+ } = image
6
13
 
7
14
  const [tileWidth, tileHeight] = tileSize
15
+ const [anchorX, anchorY] = anchor
8
16
 
9
17
  const imgParams = [
10
18
  sx * tileWidth,
@@ -19,15 +27,15 @@ export function renderImage(entity, ctx) {
19
27
 
20
28
  ctx.save()
21
29
 
22
- const img = document.getElementById(id)
30
+ ctx.translate(-tileWidth * anchorX, -tileHeight * anchorY)
31
+
32
+ const img = api.getTypes().images.get(id) || document.getElementById(id)
23
33
  if (img) {
24
34
  ctx.drawImage(img, ...imgParams)
35
+ } else if (src) {
36
+ api.getTypes().images.load(id, src)
25
37
  } else {
26
- const newImg = new Image()
27
- newImg.id = id
28
- newImg.style.display = "none"
29
- newImg.src = src
30
- document.body.appendChild(newImg)
38
+ console.warn(`Image '${id}' not found and no src provided for lazy loading`)
31
39
  }
32
40
 
33
41
  ctx.restore()
@@ -13,7 +13,7 @@ const FLIPPED_VERTICALLY_FLAG = 0x40000000
13
13
  // const FLIPPED_DIAGONALLY_FLAG = 0x20000000
14
14
  // const ROTATED_HEXAGONAL_120_FLAG = 0x10000000
15
15
 
16
- export function renderSprite(entity, ctx) {
16
+ export function renderSprite(entity, ctx, api) {
17
17
  const { image, frames, state, value, scale = DEFAULT_SCALE } = entity.sprite
18
18
  const { imageSize, tileSize } = image
19
19
 
@@ -43,7 +43,7 @@ export function renderSprite(entity, ctx) {
43
43
  )
44
44
  ctx.translate(-tileWidth / CENTER_WIDTH, -tileHeight / CENTER_HEIGHT)
45
45
 
46
- renderImage({ image, sx, sy }, ctx)
46
+ renderImage({ image, sx, sy }, ctx, api)
47
47
 
48
48
  ctx.restore()
49
49
  }
@@ -11,7 +11,7 @@ const FLIPPED_VERTICALLY_FLAG = 0x40000000
11
11
  // const FLIPPED_DIAGONALLY_FLAG = 0x20000000
12
12
  // const ROTATED_HEXAGONAL_120_FLAG = 0x10000000
13
13
 
14
- export function renderTilemap(entity, ctx) {
14
+ export function renderTilemap(entity, ctx, api) {
15
15
  const { image, columns, scale = DEFAULT_SCALE, layers } = entity.tilemap
16
16
  const { imageSize, tileSize } = image
17
17
 
@@ -51,12 +51,14 @@ export function renderTilemap(entity, ctx) {
51
51
 
52
52
  ctx.translate(dx, dy)
53
53
 
54
+ ctx.translate(tileWidth / HALF, tileHeight / HALF)
54
55
  ctx.scale(
55
56
  isFlippedHorizontally ? FLIP : NO_FLIP,
56
57
  isFlippedVertically ? FLIP : NO_FLIP,
57
58
  )
59
+ ctx.translate(-tileWidth / HALF, -tileHeight / HALF)
58
60
 
59
- renderImage({ image, sx, sy }, ctx)
61
+ renderImage({ image, sx, sy }, ctx, api)
60
62
 
61
63
  ctx.restore()
62
64
  })
@@ -0,0 +1,22 @@
1
+ import { snap } from "@inglorious/utils/math/vector.js"
2
+
3
+ export function infiniteLoop(render) {
4
+ return (entity, ctx, api) => {
5
+ const { image } = entity
6
+ const { loop } = image || {}
7
+
8
+ if (!loop) {
9
+ render(entity, ctx, api)
10
+ return
11
+ }
12
+
13
+ const [loopX, loopY, loopZ] = snap(loop)
14
+
15
+ render(entity, ctx, api)
16
+
17
+ ctx.save()
18
+ ctx.translate(-loopX, -loopY - loopZ)
19
+ render(entity, ctx, api)
20
+ ctx.restore()
21
+ }
22
+ }
@@ -1,6 +1,8 @@
1
+ import { pipe } from "@inglorious/utils/functions/functions.js"
1
2
  import { to2D } from "@inglorious/utils/math/vector.js"
2
3
 
3
4
  import { absolutePosition } from "./absolute-position.js"
5
+ import { infiniteLoop } from "./infinite-loop.js"
4
6
 
5
7
  const ORIGIN = 0
6
8
  const DEFAULT_ZOOM = 1
@@ -10,6 +12,8 @@ const DEFAULT_LAYER = 0
10
12
  const Y = 1
11
13
  const Z = 2
12
14
 
15
+ const augmentRender = pipe(absolutePosition, infiniteLoop)
16
+
13
17
  export function renderingSystem(canvas) {
14
18
  const ctx = canvas.getContext("2d")
15
19
 
@@ -59,7 +63,8 @@ export function renderingSystem(canvas) {
59
63
  const type = types[entity.type]
60
64
  const { render } = type
61
65
  if (render) {
62
- absolutePosition(render)(entity, ctx, { api })
66
+ const augmentedRender = augmentRender(render)
67
+ augmentedRender(entity, ctx, api)
63
68
  }
64
69
  })
65
70