refactor: more components

This commit is contained in:
Azgaar 2022-07-03 16:30:34 +03:00
parent e739698346
commit c1b3a3bbbb
4 changed files with 23 additions and 11 deletions

View file

@ -1,3 +1,4 @@
// @ts-nocheck global variables
import {TIME} from "config/logging";
import {normalize} from "utils/numberUtils";

View file

@ -1,3 +1,4 @@
// @ts-nocheck global variables
import {TIME} from "config/logging";
import {UINT16_MAX} from "constants";
import {createTypedArray} from "utils/arrayUtils";
@ -8,7 +9,7 @@ import {rn} from "utils/numberUtils";
export function reGraph() {
TIME && console.time("reGraph");
const {cells: gridCells, points, features} = grid;
const newCells = {p: [], g: [], h: []}; // store new data
const newCells: {p: number[][]; g: number[]; h: number[]} = {p: [], g: [], h: []}; // store new data
const spacing2 = grid.spacing ** 2;
for (const i of gridCells.i) {
@ -36,13 +37,13 @@ export function reGraph() {
}
}
function addNewPoint(i, x, y, height) {
function addNewPoint(i: number, x: number, y: number, height: number) {
newCells.p.push([x, y]);
newCells.g.push(i);
newCells.h.push(height);
}
function getCellArea(i) {
function getCellArea(i: number) {
const area = Math.abs(d3.polygonArea(getPackPolygon(i)));
return Math.min(area, UINT16_MAX);
}

View file

@ -1,11 +1,12 @@
// @ts-nocheck global variables
import {INFO} from "config/logging";
import {byId} from "utils/shorthands";
import {heightmapTemplates} from "config/heightmap-templates";
import {locked} from "scripts/options/lock";
import {getInputValue} from "utils/nodeUtils";
// show map stats on generation complete
export function showStatistics() {
const heightmap = byId("templateInput").value;
const heightmap = getInputValue("templateInput");
const isTemplate = heightmap in heightmapTemplates;
const heightmapType = isTemplate ? "template" : "precreated";
const isRandomTemplate = isTemplate && !locked("template") ? "random " : "";

View file

@ -8,16 +8,25 @@ export function unique<T>(array: T[]) {
return [...new Set(array)];
}
interface ICreateTypedArray {
interface ICreateTypesArrayLength {
maxValue: number;
length: number;
from?: ArrayLike<number>;
}
export function createTypedArray({maxValue, length, from}: ICreateTypedArray) {
const typedArray = getTypedArray(maxValue);
if (!from) return new typedArray(length);
return typedArray.from(from);
interface ICreateTypesArrayFrom {
maxValue: number;
from: ArrayLike<number>;
}
export function createTypedArray(params: ICreateTypesArrayLength | ICreateTypesArrayFrom) {
const typedArray = getTypedArray(params.maxValue);
if ("from" in params) {
typedArray.from(params.from);
} else if ("length" in params) {
return new typedArray(params.length);
}
return typedArray;
}
function getTypedArray(maxValue: number) {