dev: town

This commit is contained in:
cha0s 2024-07-23 17:04:17 -05:00
parent 5a92be47c1
commit f971295825
7 changed files with 117 additions and 4 deletions

View File

@ -21,9 +21,18 @@ export default class VisibleAabbs extends System {
reindex(entities) {
for (const id of entities) {
if (1 === id) {
const {x, y} = this.ecs.get(1).AreaSize;
if (
!this.hash ||
(
this.hash.area.x !== x
|| this.hash.area.y !== y
)
) {
this.hash = new SpatialHash(this.ecs.get(1).AreaSize);
}
}
}
super.reindex(entities);
for (const id of entities) {
this.updateHash(this.ecs.get(id));

View File

@ -8,6 +8,17 @@ const cache = new LRUCache({
});
export default class ClientEcs extends Ecs {
constructor(specification) {
super(specification);
[
'Colliders',
].forEach((defaultSystem) => {
const System = this.system(defaultSystem);
if (System) {
System.active = true;
}
});
}
async readAsset(uri) {
if (!cache.has(uri)) {
const {promise, resolve, reject} = withResolvers();

View File

@ -169,16 +169,17 @@ export default async function createHomestead(id) {
collisionStartScript: `
ecs.switchEcs(
other,
['houses', ${id}].join('/'),
'town',
{
Position: {
x: 72,
y: 304,
x: 940,
y: 480,
},
},
);
`,
},
Position: {x: 8, y: 432},
Ticking: {},
});

57
app/server/create/town.js Normal file
View File

@ -0,0 +1,57 @@
import data from '../../../public/assets/dev/town.json';
export default async function createTown() {
const area = {x: 60, y: 60};
const entities = [];
entities.push({
AreaSize: {x: area.x * 16, y: area.y * 16},
Ticking: {},
TileLayers: {
layers: [
{
area,
data,
source: '/assets/tileset.json',
tileSize: {x: 16, y: 16},
},
{
area,
data: Array(area.x * area.y).fill(0),
source: '/assets/tileset.json',
tileSize: {x: 16, y: 16},
},
],
},
Time: {},
Water: {water: {}},
});
entities.push({
Collider: {
bodies: [
{
points: [
{x: -8, y: -16},
{x: 7, y: -16},
{x: 7, y: 15},
{x: -8, y: 15},
],
},
],
collisionStartScript: `
ecs.switchEcs(
other,
['homesteads', '0'].join('/'),
{
Position: {
x: 20,
y: 438,
},
},
);
`,
},
Position: {x: 952, y: 480},
Ticking: {},
});
return entities;
}

View File

@ -14,6 +14,7 @@ import createForest from './create/forest.js';
import createHomestead from './create/homestead.js';
import createHouse from './create/house.js';
import createPlayer from './create/player.js';
import createTown from './create/town.js';
const cache = new LRUCache({
max: 128,
@ -257,6 +258,25 @@ export default class Engine {
}
async load() {
let townData;
try {
townData = await this.server.readData('town');
}
catch (error) {
if ('ENOENT' !== error.code) {
throw error;
}
const town = this.createEcs();
for (const entity of await createTown()) {
await town.create(entity);
}
await this.saveEcs('town', town);
townData = await this.server.readData('town');
}
this.ecses['town'] = await this.Ecs.deserialize(
this.createEcs(),
townData,
);
}
async loadEcs(path) {

View File

@ -8,6 +8,7 @@ import createEcs from './create/ecs.js';
import './create/forest.js';
import './create/homestead.js';
import './create/player.js';
import './create/town.js';
import Engine from './engine.js';
@ -111,6 +112,19 @@ if (import.meta.hot) {
await engine.saveEcs('homesteads/0', homestead);
resolve();
});
import.meta.hot.accept('./create/town.js', async ({default: createTown}) => {
const {promise, resolve} = withResolvers();
promises.push(promise);
await before.promise;
delete engine.ecses['town'];
await engine.server.removeData('town');
const town = createEcs(engine.Ecs);
for (const entity of await createTown()) {
await town.create(entity);
}
await engine.saveEcs('town', town);
resolve();
});
import.meta.hot.on('vite:afterUpdate', async () => {
await Promise.all(promises);
postMessage(encode({type: 'ConnectionStatus', payload: 'aborted'}));

File diff suppressed because one or more lines are too long