refactor: noise

This commit is contained in:
cha0s 2024-07-10 14:17:53 -05:00
parent ed5d12b4a8
commit c848b07668
4 changed files with 72 additions and 9 deletions

View File

@ -1,4 +1,3 @@
import {simplex2D} from '@leodeslf/simplex-noise';
import {Texture} from '@pixi/core';
import {Container} from '@pixi/react';
import {Sprite} from '@pixi/sprite';
@ -8,7 +7,7 @@ import {RESOLUTION} from '@/constants.js';
import {usePacket} from '@/context/client.js';
import {useEcs, useEcsTick} from '@/context/ecs.js';
import {useMainEntity} from '@/context/main-entity.js';
import {bresenham, TAU} from '@/util/math.js';
import {bresenham, createNoise2D, TAU} from '@/util/math.js';
import Entities from './entities.jsx';
import TargetingGhost from './targeting-ghost.jsx';
@ -18,6 +17,8 @@ import Water from './water.jsx';
const NIGHTNESS = 0.1;
const simplex2D = createNoise2D();
function calculateDarkness(hour) {
let darkness = 0;
if (hour >= 21 || hour < 4) {

View File

@ -1,3 +1,6 @@
import alea from 'alea';
import {createNoise2D as createSimplexNoise2D} from 'simplex-noise';
export const {
abs,
acos,
@ -110,6 +113,14 @@ export function clamp(n, min, max) {
return Math.max(min, Math.min(max, n));
}
export function createNoise2D(seed = 0) {
return createSimplexNoise2D(createRandom(seed));
}
export function createRandom(seed = 0) {
return alea(seed);
}
export function distance({x: lx, y: ly}, {x: rx, y: ry}) {
const xd = lx - rx;
const yd = ly - ry;
@ -167,6 +178,50 @@ export function floodwalk2D(eligible, data, {x, y, w, h}, {diagonal = false} = {
return points;
}
export class Generator {
constructor({
calculate,
children = [],
covers,
size: {w, h},
}) {
this.calculate = calculate;
this.children = children;
this.covers = covers;
this.size = {w, h};
this.matrix = new Array(w * h).fill(0);
}
compute(i, position) {
if (!this.covers(position)) {
return;
}
this.matrix[i] = this.calculate(position);
if (!this.children) {
return;
}
for (let j = 0; j < this.children.length; j++) {
this.children[j].compute(i, position);
}
}
generate() {
const {w, h} = this.size;
let i = 0;
const position = {x: 0, y: 0};
for (let y = 0; y < h; ++y) {
for (let x = 0; x < w; ++x) {
this.compute(i++, position);
position.x += 1;
}
position.x -= w;
position.y += 1;
}
}
}
export function intersects(l, r) {
if (l.x0 > r.x1) return false;
if (l.y0 > r.y1) return false;

18
package-lock.json generated
View File

@ -6,7 +6,6 @@
"": {
"name": "silphius-next",
"dependencies": {
"@leodeslf/simplex-noise": "^1.0.0",
"@msgpack/msgpack": "^3.0.0-beta2",
"@pixi/filter-adjustment": "^5.1.1",
"@pixi/filter-color-matrix": "^7.4.2",
@ -20,6 +19,7 @@
"@remix-run/node": "^2.9.2",
"@remix-run/react": "^2.9.2",
"acorn": "^8.12.0",
"alea": "^1.0.1",
"compression": "^1.7.4",
"express": "^4.18.2",
"idb-keyval": "^6.2.1",
@ -30,6 +30,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-tabs": "^6.0.2",
"simplex-noise": "^4.0.1",
"ws": "^8.17.0"
},
"devDependencies": {
@ -3027,11 +3028,6 @@
"resolved": "https://registry.npmjs.org/@juggle/resize-observer/-/resize-observer-3.4.0.tgz",
"integrity": "sha512-dfLbk+PwWvFzSxwk3n5ySL0hfBog779o8h68wK/7/APo/7cgyWp5jcXockbxdk5kFRkbeXWm4Fbi9FrdN381sA=="
},
"node_modules/@leodeslf/simplex-noise": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@leodeslf/simplex-noise/-/simplex-noise-1.0.0.tgz",
"integrity": "sha512-hsp+CfDnT9jxUjDUqiV2+eLkkdAKz6szlpcyMyXvrgs+LO8a0Gepyri1V+c6FbOfqc3ReWB282GtIE8y3Idi1A=="
},
"node_modules/@mdx-js/mdx": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/@mdx-js/mdx/-/mdx-2.3.0.tgz",
@ -7453,6 +7449,11 @@
"url": "https://github.com/sponsors/epoberezkin"
}
},
"node_modules/alea": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/alea/-/alea-1.0.1.tgz",
"integrity": "sha512-QU+wv+ziDXaMxRdsQg/aH7sVfWdhKps5YP97IIwFkHCsbDZA3k87JXoZ5/iuemf4ntytzIWeScrRpae8+lDrXA=="
},
"node_modules/ansi-regex": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz",
@ -16513,6 +16514,11 @@
"integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==",
"dev": true
},
"node_modules/simplex-noise": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/simplex-noise/-/simplex-noise-4.0.1.tgz",
"integrity": "sha512-zl/+bdSqW7HJOQ0oDbxrNYaF4F5ik0i7M6YOYmEoIJNtg16NpvWaTTM1Y7oV/7T0jFljawLgYPS81Uu2rsfo1A=="
},
"node_modules/sisteransi": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.5.tgz",

View File

@ -13,7 +13,6 @@
"test": "vitest app"
},
"dependencies": {
"@leodeslf/simplex-noise": "^1.0.0",
"@msgpack/msgpack": "^3.0.0-beta2",
"@pixi/filter-adjustment": "^5.1.1",
"@pixi/filter-color-matrix": "^7.4.2",
@ -27,6 +26,7 @@
"@remix-run/node": "^2.9.2",
"@remix-run/react": "^2.9.2",
"acorn": "^8.12.0",
"alea": "^1.0.1",
"compression": "^1.7.4",
"express": "^4.18.2",
"idb-keyval": "^6.2.1",
@ -37,6 +37,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-tabs": "^6.0.2",
"simplex-noise": "^4.0.1",
"ws": "^8.17.0"
},
"devDependencies": {