canvasengine 2.0.0-beta.2 → 2.0.0-beta.4

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.
@@ -1,163 +0,0 @@
1
- import { CompositeTilemap, POINT_STRUCT_SIZE, Tilemap, settings } from '@pixi/tilemap';
2
- import { Layer, Tile as TileClass } from '@rpgjs/tiled';
3
- import { createComponent, registerComponent } from '../../engine/reactive';
4
- import { DisplayObject } from '../DisplayObject';
5
- import { Tile } from './Tile';
6
- import { TileSet } from './TileSet';
7
-
8
- settings.use32bitIndex = true
9
-
10
- export class CanvasTileLayer extends DisplayObject(CompositeTilemap) {
11
- private _tiles: any = {}
12
- tiles: (TileClass | null)[]
13
- private _layer: Layer
14
-
15
- static findTileSet(gid: number, tileSets: TileSet[]) {
16
- let tileset: TileSet | undefined
17
- for (let i = tileSets.length - 1; i >= 0; i--) {
18
- tileset = tileSets[i]
19
- if (tileset.firstgid && tileset.firstgid <= gid) {
20
- break;
21
- }
22
- }
23
- return tileset;
24
- }
25
-
26
- /** @internal */
27
- createTile(x: number, y: number, options: any = {}): Tile | undefined {
28
- const { real, filter } = options
29
- const { tilewidth, tileheight, width } = this._layer
30
- if (real) {
31
- x = Math.floor(x / tilewidth)
32
- y = Math.floor(y / tileheight)
33
- }
34
- const i = x + y * width;
35
- const tiledTile = this._layer.getTileByIndex(i)
36
-
37
- if (!tiledTile || (tiledTile && tiledTile.gid == 0)) return
38
-
39
- const tileset = CanvasTileLayer.findTileSet(
40
- tiledTile.gid,
41
- this.tileSets
42
- )
43
-
44
- if (!tileset) return
45
-
46
- const tile = new Tile(
47
- tiledTile,
48
- tileset
49
- )
50
-
51
- tile.x = x * tilewidth;
52
- tile.y =
53
- y * tileheight +
54
- (tileheight -
55
- tile.texture.height);
56
-
57
- tile._x = x;
58
- tile._y = y;
59
-
60
- if (tileset.tileoffset) {
61
- tile.x += tileset.tileoffset.x ?? 0;
62
- tile.y += tileset.tileoffset.y ?? 0;
63
- }
64
-
65
- if (filter) {
66
- const ret = filter(tile)
67
- if (!ret) return
68
- }
69
-
70
- return tile
71
- }
72
-
73
- /** @internal */
74
- changeTile(x: number, y: number) {
75
- const { tilewidth, tileheight } = this._layer
76
- x = Math.floor(x / tilewidth)
77
- y = Math.floor(y / tileheight)
78
- const oldTile: Tile = this._tiles[x + ';' + y]
79
- const newTile = this.createTile(x, y)
80
- if (!oldTile && newTile) {
81
- this._addFrame(newTile, x, y)
82
- }
83
- else {
84
- if (newTile) {
85
- const bufComposite: CompositeTilemap = new CompositeTilemap()
86
- const frame = bufComposite.tile(newTile.texture, newTile.x, newTile.y)
87
- newTile.setAnimation(frame)
88
- this._tiles[x + ';' + y] = newTile
89
- // @ts-ignore
90
- const pointsBufComposite = (bufComposite.children[0] as Tilemap).pointsBuf
91
- // Change Texture (=0, 1 and 7, rotate (=4), animX (=5), animY (=6))
92
- ;[0, 1, 4, 6, 7, 8].forEach((i) => {
93
- if (this.pointsBuf) this.pointsBuf[oldTile.pointsBufIndex + i] = pointsBufComposite[i]
94
- })
95
- // @ts-ignore
96
- this.children[0].modificationMarker = 0
97
- this._addFrame(newTile, x, y)
98
- this['modificationMarker'] = 0
99
- }
100
- else {
101
- delete this._tiles[x + ';' + y]
102
- if (this.pointsBuf) this.pointsBuf.splice(oldTile.pointsBufIndex, POINT_STRUCT_SIZE)
103
- }
104
- }
105
- }
106
-
107
- /** @internal */
108
- get pointsBuf(): number[] | null {
109
- const child = this.children[0] as Tilemap
110
- if (!child) return null
111
- return child['pointsBuf']
112
- }
113
-
114
- private _addFrame(tile: Tile, x: number, y: number) {
115
- const frame = this.tile(tile.texture, tile.x, tile.y, {
116
- rotate: tile.texture.rotate
117
- })
118
- const pb = this.pointsBuf
119
- if (!pb) return null
120
- tile.pointsBufIndex = pb.length - POINT_STRUCT_SIZE
121
- tile.setAnimation(frame)
122
- this._tiles[x + ';' + y] = tile
123
- }
124
-
125
- onMount(args) {
126
- const { props } = args
127
- this.tileSets = props.tilesets
128
- this._layer = new Layer({
129
- ...props
130
- }, this.tileSets)
131
- super.onMount(args)
132
- }
133
-
134
- onUpdate(props) {
135
- super.onUpdate(props)
136
- if (!this.isMounted) return
137
- if (props.tileheight) this._layer.tileheight = props.tileheight
138
- if (props.tilewidth) this._layer.tilewidth = props.tilewidth
139
- if (props.width) this._layer.width = props.width
140
- if (props.height) this._layer.height = props.height
141
- if (props.parallaxX) this._layer.parallaxX = props.parallaxx
142
- if (props.parallaxY) this._layer.parallaxY = props.parallaxy
143
-
144
- this.removeChildren()
145
-
146
- for (let y = 0; y < this._layer.height; y++) {
147
- for (let x = 0; x < this._layer.width; x++) {
148
- const tile = this.createTile(x, y)
149
- if (tile) {
150
- this._addFrame(tile, x, y)
151
- }
152
- }
153
- }
154
- }
155
- }
156
-
157
- export interface CanvasTileLayer extends CompositeTilemap { }
158
-
159
- registerComponent('CompositeTileLayer', CanvasTileLayer)
160
-
161
- export function CompositeTileLayer(props) {
162
- return createComponent('CompositeTileLayer', props)
163
- }
@@ -1,41 +0,0 @@
1
- import { TiledTileset, Tileset as TiledTilesetClass } from "@rpgjs/tiled";
2
- import { Assets, Rectangle, Texture } from "pixi.js";
3
-
4
- export class TileSet extends TiledTilesetClass {
5
- public textures: Texture[] = [];
6
- private tileGroups = {};
7
-
8
- constructor(tileSet: TiledTileset) {
9
- super(tileSet);
10
- }
11
-
12
- loadGroup() {
13
- // for (let tile of this.tileset.tiles) {
14
- // }
15
- }
16
-
17
- /** @internal */
18
- async load(image: string) {
19
- const texture = await Assets.load(image);
20
- for (
21
- let y = this.margin;
22
- y < this.image.height;
23
- y += this.tileheight + this.spacing
24
- ) {
25
- for (
26
- let x = this.margin;
27
- x < this.image.width;
28
- x += this.tilewidth + this.spacing
29
- ) {
30
- this.textures.push(
31
- new Texture({
32
- source: texture.source,
33
- frame: new Rectangle(+x, +y, +this.tilewidth, +this.tileheight),
34
- })
35
- );
36
- }
37
- }
38
- this.loadGroup();
39
- return this;
40
- }
41
- }
@@ -1,80 +0,0 @@
1
- import { TiledLayer, TiledLayerType, TiledMap, TiledParserFile, TiledTileset } from "@rpgjs/tiled"
2
- import { effect, signal } from '@signe/reactive'
3
- import { loop } from "../../engine/reactive"
4
- import { h } from "../../engine/signal"
5
- import { useProps } from "../../hooks/useProps"
6
- import { Container } from "../Container"
7
- import { TilingSprite } from "../TilingSprite"
8
- import { CompositeTileLayer } from "./TileLayer"
9
- import { TileSet } from "./TileSet"
10
-
11
- export function TiledMap(props) {
12
- const { map } = useProps(props)
13
- const layers = signal<TiledLayer[]>([])
14
- const objectLayer = props.objectLayer
15
- let tilesets: TiledTileset[] = []
16
- let mapData: TiledMap = {} as TiledMap
17
-
18
- const parseTmx = async (file: string, relativePath: string = '') => {
19
- if (typeof file !== 'string') {
20
- return file
21
- }
22
- // @ts-ignore
23
- const parser = new TiledParserFile(
24
- file,
25
- {
26
- basePath: '',
27
- staticDir: '',
28
- relativePath
29
- }
30
- )
31
- const data = await parser.parseFilePromise({
32
- getOnlyBasename: false
33
- })
34
-
35
- return data
36
- }
37
-
38
- effect(async () => {
39
- mapData = await parseTmx(map())
40
- for (let tileSet of mapData.tilesets) {
41
- tilesets.push(await new TileSet(tileSet).load(tileSet.image.source))
42
- }
43
- layers.set(mapData.layers)
44
- })
45
-
46
- const createLayer = (layers, props = {}) => {
47
- return h(Container, props, loop(layers, (layer) => {
48
- switch (layer.type) {
49
- case TiledLayerType.Tile:
50
- return h(CompositeTileLayer, {
51
- tilewidth: mapData.tilewidth,
52
- tileheight: mapData.tileheight,
53
- // @ts-ignore
54
- width: mapData.width,
55
- // @ts-ignore
56
- height: mapData.height,
57
- ...layer,
58
- tilesets
59
- })
60
- case TiledLayerType.Image:
61
- const { width, height, source } = layer.image
62
- return h(TilingSprite, {
63
- image: source,
64
- ...layer,
65
- width: layer.repeatx ? layer.width * layer.tilewidth : width,
66
- height: layer.repeaty ? layer.height * layer.tileheight : height
67
- })
68
- case TiledLayerType.Group:
69
- return createLayer(signal(layer.layers), layer)
70
- case TiledLayerType.ObjectGroup:
71
- const child = objectLayer?.(layer)
72
- return h(Container, layer, child)
73
- default:
74
- return h(Container)
75
- }
76
- }))
77
- }
78
-
79
- return createLayer(layers)
80
- }