avocado-old/packages/topdown/tiles.js

131 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-03-27 01:05:29 -05:00
import * as I from 'immutable';
2019-04-28 23:45:03 -05:00
import {compose, EventEmitter} from '@avocado/core';
2019-03-25 19:03:34 -05:00
import {Rectangle, Vector} from '@avocado/math';
2019-04-07 11:43:50 -05:00
import {Synchronized} from '@avocado/state';
2019-03-25 19:03:34 -05:00
const decorate = compose(
EventEmitter,
2019-03-25 19:03:34 -05:00
Vector.Mixin('size', 'width', 'height', {
default: [0, 0],
}),
2019-04-16 17:52:56 -05:00
Synchronized,
2019-03-25 19:03:34 -05:00
);
2019-04-16 17:52:56 -05:00
export class Tiles extends decorate(class {}) {
2019-03-25 19:03:34 -05:00
2019-03-27 01:05:29 -05:00
constructor() {
super();
this.data = I.List();
2019-03-27 01:05:29 -05:00
}
2019-04-12 20:16:31 -05:00
forEachTile(fn) {
let [x, y] = [0, 0];
let [width, height] = this.size;
let i = 0;
for (let k = 0; k < height; ++k) {
for (let j = 0; j < width; ++j) {
fn(this.data.get(i), x, y, i);
++i;
++x;
}
x = 0;
++y;
}
}
fromJSON(json) {
if (json.size) {
this.size = json.size;
}
if (json.data) {
this.data = I.fromJS(json.data);
}
return this;
}
2019-04-07 11:43:50 -05:00
patchStateStep(key, step) {
if ('data' === key) {
const oldData = this.data;
for (const i in step.value) {
const index = parseInt(i);
this.data = this.data.set(index, step.value[i]);
}
2019-04-07 11:43:50 -05:00
if (oldData !== this.data) {
this.emit('dataChanged');
}
return;
2019-03-27 01:05:29 -05:00
}
2019-04-07 11:43:50 -05:00
super.patchStateStep(key, step);
2019-03-27 01:05:29 -05:00
}
2019-03-25 19:03:34 -05:00
get rectangle() {
return Rectangle.compose([0, 0], this.size);
}
2019-03-25 20:49:16 -05:00
setTileAt(x, y, tile) {
const oldTile = this.tileAt(x, y);
if (oldTile === tile) {
return;
}
2019-03-25 20:49:16 -05:00
const index = y * this.width + x;
if (index < 0 || index >= this.data.size) {
2019-03-25 20:49:16 -05:00
return;
}
this.data = this.data.set(index, tile);
this.emit('dataChanged');
2019-03-25 20:49:16 -05:00
}
2019-03-25 19:03:34 -05:00
slice(rectangle) {
const tilesRectangle = this.rectangle;
2019-03-25 20:49:42 -05:00
// Get intersection.
2019-03-25 19:03:34 -05:00
if (!Rectangle.intersects(rectangle, tilesRectangle)) {
return [];
}
2019-03-25 20:49:42 -05:00
let [x, y, sliceWidth, sliceHeight] = Rectangle.intersection(
rectangle,
tilesRectangle,
);
// No muls in the loop.
let sliceRow = y * sliceWidth;
const dataWidth = this.width;
let dataRow = y * dataWidth;
// Copy slice.
const slice = new Array(sliceWidth * sliceHeight);
for (let j = 0; j < sliceHeight; ++j) {
for (let i = 0; i < sliceWidth; ++i) {
slice[sliceRow + x] = this.data.get(dataRow + x);
2019-03-25 19:03:34 -05:00
x++;
}
2019-03-25 20:49:42 -05:00
sliceRow += sliceWidth;
dataRow += dataWidth;
x -= sliceWidth;
2019-03-25 19:03:34 -05:00
}
return slice;
}
2019-04-07 11:43:50 -05:00
synchronizedChildren() {
return [
'width',
'height',
'data',
];
2019-03-27 01:05:29 -05:00
}
tick(elapsed) {
this.tickSynchronized(elapsed);
}
2019-03-25 19:03:34 -05:00
tileAt(x, y) {
return this.data.get(y * this.width + x);
2019-03-25 19:03:34 -05:00
}
toJSON() {
2019-03-25 20:50:11 -05:00
return {
size: [...this.size],
data: [...this.data.toJS()],
2019-03-25 20:50:11 -05:00
};
2019-03-25 19:03:34 -05:00
}
}