refactor: gen

This commit is contained in:
cha0s 2021-03-24 08:18:24 -05:00
parent 6466213461
commit 32bb00b2df
3 changed files with 83 additions and 90 deletions

View File

@ -1,7 +1,6 @@
import {dirname, join} from 'path'; import {dirname, join} from 'path';
import {promisify} from 'util'; import {promisify} from 'util';
import {noise, noiseSeed, Vector} from '@avocado/math';
import {JsonResource} from '@avocado/resource'; import {JsonResource} from '@avocado/resource';
import {createLoop, destroyLoop} from '@avocado/timing'; import {createLoop, destroyLoop} from '@avocado/timing';
import glob from 'glob'; import glob from 'glob';
@ -44,7 +43,7 @@ export default (latus) => class Universe extends JsonResource {
async load(json = {}) { async load(json = {}) {
super.load(json); super.load(json);
const {tps = 60, uri} = json; const {tps = 60, uri} = json;
const {Entity, Room} = latus.get('%resources'); const {Room} = latus.get('%resources');
if (uri) { if (uri) {
const universePath = dirname(uri); const universePath = dirname(uri);
await Promise.all( await Promise.all(
@ -59,94 +58,6 @@ export default (latus) => class Universe extends JsonResource {
this.addRoom(roomUri, await Room.load({extends: join(universePath, roomUri)})); this.addRoom(roomUri, await Room.load({extends: join(universePath, roomUri)}));
}), }),
); );
const room = this.room('players/cha0s/index.room.json');
// const seed = Date.now();
const seed = 69420;
noiseSeed(seed);
const tiles = {
water: [103, 104],
grass: [1, 3, 4, 10, 11, 12],
dirt: [342, 456],
stone: [407, 408, 423, 424],
};
let i = 256;
const w = 2;
const h = 2;
const tree = [];
for (let y = 0; y < h; ++y) {
for (let x = 0; x < w; ++x) {
tree.push(i);
i += 1;
}
i -= w;
i += 16;
}
const rnd = (type, x, y) => {
const l = tiles[type];
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 x = 0; x < 64; ++x) {
room.layer(0).setTileAt([x, y], rnd('water', x, y));
}
}
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) {
v = (1 + noise(Vector.scale([x, y], 15), seed + 1)) / 2;
if (v < 0.15) {
// eslint-disable-next-line no-await-in-loop
const shrub = await Entity.load({extends: '/shrub.entity.json'});
shrub.setPosition(Vector.round(Vector.add(
Vector.add([8, 8], Vector.scale([x, y], 16)),
[Math.random() * 8 - 4, Math.random() * 8 - 4],
)));
room.addEntityToLayer(shrub, 1);
}
else if (v < 0.17) {
// eslint-disable-next-line no-await-in-loop
const tree = await Entity.load({extends: '/tree.entity.json'});
let p = [x, y];
p = Vector.sub(p, Vector.mod(p, [2, 2]));
p = Vector.scale(p, 16);
tree.setPosition(Vector.round(Vector.add(
Vector.add([8, 8], p),
[Math.random() * 8 - 4, Math.random() * 8 - 4],
)));
room.addEntityToLayer(tree, 1);
}
v = (1 + noise(Vector.scale([x, y], 9), seed + 1)) / 2;
if (v < 0.08) {
// eslint-disable-next-line no-await-in-loop
const flower = await Entity.load({extends: '/flower.entity.json'});
flower.setPosition(Vector.round(Vector.add(
Vector.add([8, 8], Vector.scale([x, y], 16)),
[Math.random() * 8 - 4, Math.random() * 8 - 4],
)));
room.addEntityToLayer(flower, 1);
}
}
}
}
} }
this.#tps = tps; this.#tps = tps;
} }

View File

@ -0,0 +1,79 @@
import {noise, noiseSeed, Vector} from '@avocado/math';
export default async (room, latus) => {
const {Entity} = latus.get('%resources');
// const seed = Date.now();
const seed = 69420;
noiseSeed(seed);
const tiles = {
water: [103, 104],
grass: [1, 3, 4, 10, 11, 12],
dirt: [342, 456],
stone: [407, 408, 423, 424],
};
const rnd = (type, x, y) => {
const l = tiles[type];
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 x = 0; x < 64; ++x) {
room.layer(0).setTileAt([x, y], rnd('water', x, y));
}
}
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) {
v = (1 + noise(Vector.scale([x, y], 15), seed + 1)) / 2;
if (v < 0.15) {
// eslint-disable-next-line no-await-in-loop
const shrub = await Entity.load({extends: '/shrub.entity.json'});
shrub.setPosition(Vector.round(Vector.add(
Vector.add([8, 8], Vector.scale([x, y], 16)),
[Math.random() * 8 - 4, Math.random() * 8 - 4],
)));
room.addEntityToLayer(shrub, 1);
}
else if (v < 0.17) {
// eslint-disable-next-line no-await-in-loop
const tree = await Entity.load({extends: '/tree.entity.json'});
let p = [x, y];
p = Vector.sub(p, Vector.mod(p, [2, 2]));
p = Vector.scale(p, 16);
tree.setPosition(Vector.round(Vector.add(
Vector.add([8, 8], p),
[Math.random() * 8 - 4, Math.random() * 8 - 4],
)));
room.addEntityToLayer(tree, 1);
}
v = (1 + noise(Vector.scale([x, y], 9), seed + 1)) / 2;
if (v < 0.08) {
// eslint-disable-next-line no-await-in-loop
const flower = await Entity.load({extends: '/flower.entity.json'});
flower.setPosition(Vector.round(Vector.add(
Vector.add([8, 8], Vector.scale([x, y], 16)),
[Math.random() * 8 - 4, Math.random() * 8 - 4],
)));
room.addEntityToLayer(flower, 1);
}
}
}
}
};

View File

@ -2,6 +2,7 @@ import {Resource} from '@avocado/resource';
import {gatherWithLatus} from '@latus/core'; import {gatherWithLatus} from '@latus/core';
import express from 'express'; import express from 'express';
import alpha from './gen/alpha';
import UniverseInput from './packets/decorators/universe-input'; import UniverseInput from './packets/decorators/universe-input';
export default { export default {
@ -19,6 +20,8 @@ export default {
try { try {
const {Universe} = latus.get('%resources'); const {Universe} = latus.get('%resources');
const universe = await Universe.load({extends: `/universe/${running}/index.universe.json`}); const universe = await Universe.load({extends: `/universe/${running}/index.universe.json`});
// alpha gen
alpha(universe.room('players/cha0s/index.room.json'), latus);
latus.set('%universe', universe); latus.set('%universe', universe);
universe.start(); universe.start();
// eslint-disable-next-line no-console // eslint-disable-next-line no-console