fun: gen room for now
This commit is contained in:
parent
74a5342ae7
commit
ad87c6f499
|
@ -1,6 +1,7 @@
|
|||
import {dirname, join} from 'path';
|
||||
import {promisify} from 'util';
|
||||
|
||||
import {noise, noiseSeed, Vector} from '@avocado/math';
|
||||
import {JsonResource} from '@avocado/resource';
|
||||
import {createLoop, destroyLoop} from '@avocado/timing';
|
||||
import glob from 'glob';
|
||||
|
@ -32,12 +33,6 @@ export default (latus) => class Universe extends JsonResource {
|
|||
user,
|
||||
});
|
||||
this.#players.push(player);
|
||||
setTimeout(() => {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
entity.setPosition([20, 20]);
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
entity.currentRoom = 'rooms/town.room.json';
|
||||
}, 5000);
|
||||
return player;
|
||||
}
|
||||
|
||||
|
@ -49,8 +44,7 @@ export default (latus) => class Universe extends JsonResource {
|
|||
async load(json = {}) {
|
||||
super.load(json);
|
||||
const {tps = 60, uri} = json;
|
||||
const {Room} = latus.get('%resources');
|
||||
// console.log(Object.getPrototypeOf(Object.getPrototypeOf(Room)).toString());
|
||||
const {Entity, Room} = latus.get('%resources');
|
||||
if (uri) {
|
||||
const universePath = dirname(uri);
|
||||
await Promise.all(
|
||||
|
@ -65,6 +59,93 @@ export default (latus) => class Universe extends JsonResource {
|
|||
this.addRoom(roomUri, await Room.load({extends: join(universePath, roomUri)}));
|
||||
}),
|
||||
);
|
||||
const room = this.room('players/cha0s/index.room.json');
|
||||
const seed = Date.now();
|
||||
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;
|
||||
}
|
||||
|
@ -103,7 +184,7 @@ export default (latus) => class Universe extends JsonResource {
|
|||
const {entity} = player;
|
||||
const room = this.room(entity.currentRoom);
|
||||
entity.stopInforming(room);
|
||||
room.removeEntityFromLayer(entity, 0);
|
||||
room.removeEntityFromLayer(entity, 1);
|
||||
const index = this.#players.indexOf(player);
|
||||
if (-1 !== index) {
|
||||
this.#players.splice(player, 1);
|
||||
|
|
|
@ -25,11 +25,11 @@ export default () => class Universed extends decorate(Trait) {
|
|||
if (oldRoom) {
|
||||
const room = this.#universe.room(oldRoom);
|
||||
this.entity.stopInforming(room);
|
||||
room.removeEntityFromLayer(this.entity, 0);
|
||||
room.removeEntityFromLayer(this.entity, 1);
|
||||
}
|
||||
if (this.entity.currentRoom) {
|
||||
const room = this.#universe.room(this.entity.currentRoom);
|
||||
room.addEntityToLayer(this.entity, 0);
|
||||
room.addEntityToLayer(this.entity, 1);
|
||||
this.entity.startInforming(room);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user