@inglorious/renderer-2d 0.2.0 → 0.4.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 +4 -6
- package/src/index.js +8 -66
- package/src/rendering-behavior.js +50 -0
- package/src/rendering-system.js +4 -15
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/renderer-2d",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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,16 +18,14 @@
|
|
|
18
18
|
"./*": "./src/*"
|
|
19
19
|
},
|
|
20
20
|
"files": [
|
|
21
|
-
"src"
|
|
22
|
-
"README.md",
|
|
23
|
-
"LICENSE"
|
|
21
|
+
"src"
|
|
24
22
|
],
|
|
25
23
|
"publishConfig": {
|
|
26
24
|
"access": "public"
|
|
27
25
|
},
|
|
28
26
|
"dependencies": {
|
|
29
|
-
"@inglorious/engine": "0.
|
|
30
|
-
"@inglorious/utils": "
|
|
27
|
+
"@inglorious/engine": "0.9.0",
|
|
28
|
+
"@inglorious/utils": "2.1.0"
|
|
31
29
|
},
|
|
32
30
|
"devDependencies": {
|
|
33
31
|
"eslint": "^9.34.0",
|
package/src/index.js
CHANGED
|
@@ -1,68 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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,50 @@
|
|
|
1
|
+
import { track } from "@inglorious/engine/behaviors/input/mouse.js"
|
|
2
|
+
|
|
3
|
+
export function rendering(canvas) {
|
|
4
|
+
const ctx = canvas.getContext("2d")
|
|
5
|
+
|
|
6
|
+
let _onMouseMove = null
|
|
7
|
+
let _onClick = null
|
|
8
|
+
let _onWheel = null
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
init(entity, event, api) {
|
|
12
|
+
const game = api.getEntity("game")
|
|
13
|
+
const [, , width, height] = game.bounds
|
|
14
|
+
|
|
15
|
+
canvas.style.width = `${width}px`
|
|
16
|
+
canvas.style.height = `${height}px`
|
|
17
|
+
const dpi = window.devicePixelRatio
|
|
18
|
+
canvas.width = width * dpi
|
|
19
|
+
canvas.height = height * dpi
|
|
20
|
+
ctx.scale(dpi, dpi)
|
|
21
|
+
|
|
22
|
+
if (game.pixelated) {
|
|
23
|
+
canvas.style.imageRendering = "pixelated"
|
|
24
|
+
ctx.textRendering = "geometricPrecision"
|
|
25
|
+
ctx.imageSmoothingEnabled = false
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const { onMouseMove, onClick, onWheel } = track(canvas, api)
|
|
29
|
+
_onMouseMove = onMouseMove
|
|
30
|
+
_onClick = onClick
|
|
31
|
+
_onWheel = onWheel
|
|
32
|
+
|
|
33
|
+
canvas.addEventListener("mousemove", _onMouseMove)
|
|
34
|
+
canvas.addEventListener("click", _onClick)
|
|
35
|
+
canvas.addEventListener("wheel", _onWheel)
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
destroy() {
|
|
39
|
+
if (_onMouseMove) {
|
|
40
|
+
canvas.removeEventListener("mousemove", _onMouseMove)
|
|
41
|
+
}
|
|
42
|
+
if (_onClick) {
|
|
43
|
+
canvas.removeEventListener("click", _onClick)
|
|
44
|
+
}
|
|
45
|
+
if (_onWheel) {
|
|
46
|
+
canvas.removeEventListener("wheel", _onWheel)
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/rendering-system.js
CHANGED
|
@@ -10,21 +10,9 @@ const DEFAULT_LAYER = 0
|
|
|
10
10
|
const Y = 1
|
|
11
11
|
const Z = 2
|
|
12
12
|
|
|
13
|
-
function
|
|
14
|
-
const
|
|
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()
|
|
@@ -68,7 +56,8 @@ export function createRenderingSystem(ctx) {
|
|
|
68
56
|
b.position[Z] - a.position[Z],
|
|
69
57
|
)
|
|
70
58
|
.forEach((entity) => {
|
|
71
|
-
const
|
|
59
|
+
const type = types[entity.type]
|
|
60
|
+
const { render } = type
|
|
72
61
|
if (render) {
|
|
73
62
|
absolutePosition(render)(entity, ctx, { api })
|
|
74
63
|
}
|