refactor: generation

This commit is contained in:
cha0s 2021-03-28 10:01:19 -05:00
parent 402f8539f5
commit dba763879e
3 changed files with 65 additions and 44 deletions

View File

@ -96,7 +96,7 @@ export default (latus) => class Universe extends JsonResource {
const {entity} = player; const {entity} = player;
const room = this.room(entity.currentRoom); const room = this.room(entity.currentRoom);
entity.stopInforming(room); entity.stopInforming(room);
room.removeEntityFromLayer(entity, 1); room.removeEntityFromLayer(entity, 2);
const index = this.#players.indexOf(player); const index = this.#players.indexOf(player);
if (-1 !== index) { if (-1 !== index) {
this.#players.splice(player, 1); this.#players.splice(player, 1);

View File

@ -1,47 +1,68 @@
import {noise, noiseSeed, Vector} from '@avocado/math'; import {
Generator,
noise as rawNoise,
noiseSeed,
Vector,
} from '@avocado/math';
const noise = ([x, y]) => (1 + rawNoise([x, y])) / 2;
const indexComputer = (indices, randomizer) => (
([x, y]) => indices[Math.floor(indices.length * randomizer([x, y]))]
);
const [w, h] = [64, 64];
const Plains = new Generator({
calculate: indexComputer(
[1, 3, 4, 10, 11, 12],
([x, y]) => noise(Vector.scale([x, y], 30)),
),
covers: () => true,
size: [w, h],
children: [
new Generator({
calculate: indexComputer(
[342, 456],
([x, y]) => noise(Vector.scale([x, y], 30)),
),
covers: ([x, y]) => noise(Vector.scale([x, y], 10)) < 0.2,
size: [w, h],
}),
new Generator({
calculate: indexComputer(
[103, 104],
([x, y]) => noise(Vector.scale([x, y], 30)),
),
covers: ([x, y]) => noise(Vector.scale([x, y], 5)) < 0.2,
size: [w, h],
}),
],
});
const seed = 69420;
noiseSeed(seed);
Plains.generate();
export default async (room, latus) => { export default async (room, latus) => {
const {Entity} = latus.get('%resources'); const {Entity} = latus.get('%resources');
// const seed = Date.now(); room.layer(0).stampAt(
const seed = 69420; [0, 0, w, h],
noiseSeed(seed); Plains.matrix,
const tiles = { );
water: [103, 104], room.layer(1).stampAt(
grass: [1, 3, 4, 10, 11, 12], [0, 0, w, h],
dirt: [342, 456], Plains.children[0].matrix,
stone: [407, 408, 423, 424], );
}; room.layer(2).stampAt(
const rnd = (type, x, y) => { [0, 0, w, h],
const l = tiles[type]; Plains.children[1].matrix,
const v = (1 + noise(Vector.scale([x, y], 30), seed)) / 2; );
return l[Math.floor(v * l.length)];
};
const tile = (x, y) => {
let v = (1 + noise(Vector.scale([x, y], 5), seed)) / 2;
if (v < 0.2) {
return 0;
}
v = (1 + noise(Vector.scale([x, y], 10), seed + 1)) / 2;
if (v < 0.2) {
return rnd('dirt', x, y);
}
return rnd('grass', x, y);
};
for (let y = 0; y < 64; ++y) { for (let y = 0; y < 64; ++y) {
for (let x = 0; x < 64; ++x) { for (let x = 0; x < 64; ++x) {
room.layer(0).setTileAt([x, y], rnd('water', x, y)); let v = noise(Vector.scale([x, y], 5), seed);
}
}
for (let y = 0; y < 64; ++y) {
for (let x = 0; x < 64; ++x) {
room.layer(1).setTileAt([x, y], tile(x, y));
}
}
for (let y = 0; y < 64; ++y) {
for (let x = 0; x < 64; ++x) {
let v = (1 + noise(Vector.scale([x, y], 5), seed)) / 2;
if (v > 0.2) { if (v > 0.2) {
v = (1 + noise(Vector.scale([x, y], 15), seed + 1)) / 2; v = noise(Vector.scale([x, y], 15), seed + 1);
if (v < 0.15) { if (v < 0.15) {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
const shrub = await Entity.load({extends: '/shrub.entity.json'}); const shrub = await Entity.load({extends: '/shrub.entity.json'});
@ -49,7 +70,7 @@ export default async (room, latus) => {
Vector.add([8, 8], Vector.scale([x, y], 16)), Vector.add([8, 8], Vector.scale([x, y], 16)),
[Math.random() * 8 - 4, Math.random() * 8 - 4], [Math.random() * 8 - 4, Math.random() * 8 - 4],
))); )));
room.addEntityToLayer(shrub, 1); room.addEntityToLayer(shrub, 2);
} }
else if (v < 0.17) { else if (v < 0.17) {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
@ -61,9 +82,9 @@ export default async (room, latus) => {
Vector.add([8, 8], p), Vector.add([8, 8], p),
[Math.random() * 8 - 4, Math.random() * 8 - 4], [Math.random() * 8 - 4, Math.random() * 8 - 4],
))); )));
room.addEntityToLayer(tree, 1); room.addEntityToLayer(tree, 2);
} }
v = (1 + noise(Vector.scale([x, y], 9), seed + 1)) / 2; v = noise(Vector.scale([x, y], 9), seed + 1);
if (v < 0.08) { if (v < 0.08) {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
const flower = await Entity.load({extends: '/flower.entity.json'}); const flower = await Entity.load({extends: '/flower.entity.json'});
@ -71,7 +92,7 @@ export default async (room, latus) => {
Vector.add([8, 8], Vector.scale([x, y], 16)), Vector.add([8, 8], Vector.scale([x, y], 16)),
[Math.random() * 8 - 4, Math.random() * 8 - 4], [Math.random() * 8 - 4, Math.random() * 8 - 4],
))); )));
room.addEntityToLayer(flower, 1); room.addEntityToLayer(flower, 2);
} }
} }
} }

View File

@ -29,7 +29,7 @@ export default () => class Universed extends decorate(Trait) {
} }
if (this.entity.currentRoom) { if (this.entity.currentRoom) {
const room = this.#universe.room(this.entity.currentRoom); const room = this.#universe.room(this.entity.currentRoom);
room.addEntityToLayer(this.entity, 1); room.addEntityToLayer(this.entity, 2);
this.entity.startInforming(room); this.entity.startInforming(room);
} }
} }