dev: town
This commit is contained in:
parent
5a92be47c1
commit
f971295825
|
@ -21,7 +21,16 @@ export default class VisibleAabbs extends System {
|
|||
reindex(entities) {
|
||||
for (const id of entities) {
|
||||
if (1 === id) {
|
||||
this.hash = new SpatialHash(this.ecs.get(1).AreaSize);
|
||||
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);
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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
57
app/server/create/town.js
Normal 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;
|
||||
}
|
|
@ -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) {
|
||||
|
|
|
@ -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'}));
|
||||
|
|
1
public/assets/dev/town.json
Normal file
1
public/assets/dev/town.json
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user