refactor: simpification & semantics
This commit is contained in:
parent
b6688ff2bb
commit
b03abf5907
|
@ -1,3 +1,3 @@
|
|||
export {TilesView} from './tiles-view';
|
||||
export {TilesRenderer} from './tiles-renderer';
|
||||
export {Tiles} from './tiles';
|
||||
export {Tileset} from './tileset';
|
||||
|
|
50
packages/topdown/tiles-renderer.js
Normal file
50
packages/topdown/tiles-renderer.js
Normal file
|
@ -0,0 +1,50 @@
|
|||
import {Canvas, Container, Sprite} from '@avocado/graphics';
|
||||
import {Rectangle, Vector} from '@avocado/math';
|
||||
|
||||
export class TilesRenderer {
|
||||
|
||||
constructor(tiles, tileset) {
|
||||
this.tiles = tiles;
|
||||
this.tileset = tileset;
|
||||
}
|
||||
|
||||
renderChunk(renderer, rectangle) {
|
||||
// No rectangle? Render all.
|
||||
if (!rectangle) {
|
||||
rectangle = Rectangle.compose([0, 0], this.tiles.size);
|
||||
}
|
||||
const slice = this.tiles.slice(rectangle);
|
||||
if (!slice) {
|
||||
return undefined;
|
||||
}
|
||||
// Precalcs.
|
||||
const size = Rectangle.size(rectangle);
|
||||
const tileSize = this.tileset.tileSize;
|
||||
const rowWidth = size[0] * tileSize[0];
|
||||
// Render all tiles.
|
||||
const container = new Container();
|
||||
const position = [0, 0];
|
||||
for (let i = 0; i < slice.length; ++i) {
|
||||
const index = slice[i];
|
||||
const sprite = new Sprite(this.tileset.subimages[index]);
|
||||
sprite.position = position;
|
||||
container.addChild(sprite);
|
||||
// Only adds, please.
|
||||
position[0] += tileSize[0];
|
||||
if (rowWidth === position[0]) {
|
||||
position[0] = 0;
|
||||
position[1] += tileSize[1];
|
||||
}
|
||||
}
|
||||
// Blit to canvas.
|
||||
const canvasSize = Vector.mul(size, tileSize);
|
||||
const canvas = new Canvas(canvasSize);
|
||||
canvas.renderWith(container, renderer);
|
||||
container.destroy();
|
||||
// Convert to image and return.
|
||||
const image = canvas.toImage();
|
||||
canvas.destroy();
|
||||
return image;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,72 +0,0 @@
|
|||
import {compose} from '@avocado/core';
|
||||
import {Canvas, Container, Renderable, Sprite} from '@avocado/graphics';
|
||||
import {Vector} from '@avocado/math';
|
||||
|
||||
export class TilesView extends Renderable {
|
||||
|
||||
constructor(tiles, tileset) {
|
||||
super();
|
||||
this.tiles = tiles;
|
||||
this.tileset = tileset;
|
||||
this.canvas = new Canvas();
|
||||
this.sprite = undefined;
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.container.destroy();
|
||||
}
|
||||
|
||||
indexMapFromSlice(slice) {
|
||||
const indexes = new Map();
|
||||
for (const i in slice) {
|
||||
const tile = slice[i];
|
||||
if (!indexes.get(tile)) {
|
||||
indexes.set(tile, new Set());
|
||||
}
|
||||
indexes.get(tile).add(parseInt(i));
|
||||
}
|
||||
return indexes;
|
||||
}
|
||||
|
||||
get internal() {
|
||||
return this.canvas.internal;
|
||||
}
|
||||
|
||||
renderWith(renderer) {
|
||||
const size = this.tiles.size;
|
||||
const tileSize = Vector.mul(this.tileset.tileSize, this.scale);
|
||||
const rowWidth = size[0] * tileSize[0];
|
||||
const canvasSize = Vector.mul(size, tileSize);
|
||||
this.canvas.size = canvasSize;
|
||||
const slice = this.tiles.slice([0, 0, size[0], size[1]]);
|
||||
const indexMap = this.indexMapFromSlice(slice);
|
||||
const container = new Container();
|
||||
for (const [tile, indexes] of indexMap.entries()) {
|
||||
let iterator = indexes.values();
|
||||
let result = iterator.next();
|
||||
while (!result.done) {
|
||||
const position = [0, 0];
|
||||
const validIndex = result.value;
|
||||
for (const indexString in slice) {
|
||||
const index = parseInt(indexString);
|
||||
if (index === validIndex) {
|
||||
const sprite = new Sprite(this.tileset.subimages[tile]);
|
||||
sprite.scale = this.scale;
|
||||
sprite.position = position;
|
||||
container.addChild(sprite);
|
||||
}
|
||||
position[0] += tileSize[0];
|
||||
if (rowWidth === position[0]) {
|
||||
position[1] += tileSize[1];
|
||||
position[0] = 0;
|
||||
}
|
||||
}
|
||||
result = iterator.next();
|
||||
}
|
||||
}
|
||||
this.canvas.renderWith(container, renderer);
|
||||
container.destroy();
|
||||
return this.canvas.toImage();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user