refactor: nix layer

This commit is contained in:
cha0s 2021-05-09 18:14:18 -05:00
parent 4ef7c33dce
commit 4889cf7177
7 changed files with 66 additions and 90 deletions

View File

@ -35,19 +35,6 @@ export default (latus) => class Tool extends Trait {
this.#targets = [];
}
calculateTargetStart() {
const {wielder} = this.entity;
if (!wielder || !wielder.is('Directional') || !wielder.is('Layered')) {
return [0, 0];
}
const tile = Vector.floor(wielder.tile);
const {distance} = this.params.target;
return Vector.add(
tile,
Vector.directionalProjection(wielder.direction, [0, distance]),
);
}
calculateTargets() {
this.#targets = [];
const {wielder} = this.entity;
@ -67,22 +54,25 @@ export default (latus) => class Tool extends Trait {
}
}
calculateTargetStart() {
const {wielder} = this.entity;
if (!wielder || !wielder.room) {
return [0, 0];
}
const {tileSize} = wielder.room.tiles[0];
const tile = Vector.floor(Vector.div(wielder.position, tileSize));
const {distance} = this.params.target;
return Vector.add(tile, Vector.directionalProjection(wielder.direction, [0, distance]));
}
calculateTargetTotal() {
if ('client' === process.env.SIDE) {
const {wielder} = this.entity;
if (!wielder || !wielder.is('Layered')) {
if (!wielder || !wielder.room) {
return;
}
const {layer} = wielder;
if (!layer) {
return;
}
const {tileset} = layer;
if (!tileset) {
return;
}
const tile = Vector.floor(wielder.tile);
const {tileSize} = tileset;
const {tileSize} = wielder.room.tiles[0];
const tile = Vector.floor(Vector.div(wielder.position, tileSize));
let total = [0, 0, 0, 0];
for (let i = 0; i < this.#targets.length; ++i) {
const target = this.#targets[i];
@ -96,10 +86,19 @@ export default (latus) => class Tool extends Trait {
}
createTargetContext(target) {
const {wielder} = this.entity;
const {room} = wielder;
const [tiles] = room.tiles;
const {tileSize} = tiles;
return {
center: Vector.add(
Vector.mul(target, tileSize),
Vector.scale(tileSize, 0.5),
),
entity: this.entity,
target,
wielder: this.entity.wielder,
tiles,
wielder,
};
}
@ -165,6 +164,7 @@ export default (latus) => class Tool extends Trait {
return {
useTool: () => {
this.calculateTargets();
const promises = this.#targets.map((target) => {
const {Script} = latus.get('%resources');
const context = this.createTargetContext(target);
@ -193,48 +193,17 @@ export default (latus) => class Tool extends Trait {
if ('client' === process.env.SIDE) {
oldWielder.removeRasterSibling(this.#primitives);
}
oldWielder.off(
'activeSlotIndexChanged',
this.onWielderActiveSlotIndexChanged,
);
oldWielder.off(
[
'addedToLayer',
'directionChanged',
'tileOffsetChanged',
],
this.refreshGuide,
);
oldWielder.off('activeSlotIndexChanged', this.onWielderActiveSlotIndexChanged);
}
if (newWielder && newWielder.is('Visible')) {
if ('client' === process.env.SIDE) {
newWielder.addRasterSibling(this.#primitives);
}
newWielder.on(
'activeSlotIndexChanged',
this.onWielderActiveSlotIndexChanged,
this,
);
newWielder.on(
[
'addedToLayer',
'directionChanged',
'tileOffsetChanged',
],
this.refreshGuide,
this,
);
newWielder.on('activeSlotIndexChanged', this.onWielderActiveSlotIndexChanged, this);
}
this.refreshGuide();
this.onWielderActiveSlotIndexChanged();
}
refreshGuide() {
this.calculateTargets();
this.calculateTargetTotal();
this.repositionPrimitives();
}
renderGuide(rectangle, color, expansion) {
const position = Rectangle.position(rectangle);
const size = Rectangle.size(rectangle);
@ -268,14 +237,15 @@ export default (latus) => class Tool extends Trait {
renderGuides() {
const {color} = this.#throbber;
this.#primitives.clear();
this.calculateTargets();
this.calculateTargetTotal();
this.repositionPrimitives();
this.renderGuide(this.#targetTotal, 255, [0, 0]);
this.renderGuide(Rectangle.expand(this.#targetTotal, [2, 2]), color, [1, 1]);
}
renderTick(elapsed) {
// eslint-disable-next-line no-unused-vars
const {lfo} = this.#throbber;
lfo.tick(elapsed);
this.#throbber.lfo.tick(elapsed);
this.renderGuides();
}
@ -295,16 +265,12 @@ export default (latus) => class Tool extends Trait {
repositionPrimitives() {
if ('client' === process.env.SIDE) {
const {wielder} = this.entity;
if (!wielder) {
if (!wielder || !wielder.room) {
return;
}
if (!wielder.is('Layered')) {
return;
}
this.#primitives.position = Vector.add(
wielder.position,
Vector.scale(wielder.tileOffset, -1),
);
const {tileSize} = wielder.room.tiles[0];
const tileOffset = Vector.mod(wielder.position, tileSize);
this.#primitives.position = Vector.add(wielder.position, Vector.scale(tileOffset, -1));
}
}

View File

@ -2,6 +2,7 @@ import './index.scss';
import {
React,
useCallback,
useEffect,
useState,
} from '@latus/react';
@ -13,7 +14,6 @@ import {RoomView} from '@avocado/topdown';
import {useRoom, useSelfEntity} from '@humus/app/client';
const renderer = new Renderer();
const ticker = () => {};
const RoomStage = () => {
const selfEntity = useSelfEntity();
@ -64,13 +64,18 @@ const RoomStage = () => {
camera.off('scaleChanged', onCameraScaleChanged);
};
}, [container, roomView, selfEntity, viewport]);
const ticker = useCallback(
(elapsed) => {
container.renderTick(elapsed);
},
[container],
);
useEffect(() => {
if (!room) {
return undefined;
}
const handle = createLoop((elapsed) => {
room.tick(elapsed);
container.renderTick(elapsed);
});
return () => {
destroyLoop(handle);

View File

@ -3,12 +3,12 @@ import './index.scss';
import {join} from 'path';
import classnames from 'classnames';
import {Image} from '@avocado/graphics';
import {
PropTypes,
React,
useCallback,
useEvent,
useLatus,
useState,
} from '@latus/react';
@ -19,10 +19,8 @@ const ItemSlotComponent = (props) => {
item,
onClick,
} = props;
const latus = useLatus();
const [qty, setQty] = useState(undefined);
useEvent(item, 'qtyChanged', useCallback(() => setQty(item.qty), [item]));
const {Image} = latus.get('%resources');
let backgroundImageUri;
let qtyClass;
if (item) {

View File

@ -54,9 +54,16 @@ export default (latus) => class Universe extends JsonResource {
cwd: join(this.constructor.root, universePath),
},
)
).map(async (roomUri) => {
this.addRoom(roomUri, await Room.load({extends: join(universePath, roomUri)}));
}),
)
.map(async (roomUri) => {
try {
this.addRoom(roomUri, await Room.load({extends: join(universePath, roomUri)}));
}
catch (error) {
error.message = `Couldn't load room ${roomUri}: ${error.message}`;
throw error;
}
}),
);
}
this.#tps = tps;
@ -96,7 +103,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.removeEntity(entity);
const index = this.#players.indexOf(player);
if (-1 !== index) {
this.#players.splice(player, 1);

View File

@ -56,23 +56,23 @@ Plains.generate();
export default async (room, latus) => {
const {Entity} = latus.get('%resources');
room.layer(0).stampAt(
room.tiles[0].stampAt(
[0, 0, w, h],
Plains.matrix,
);
room.layer(1).stampAt(
room.tiles[1].stampAt(
[0, 0, w, h],
Plains.children[0].matrix,
);
room.layer(2).stampAt(
room.tiles[2].stampAt(
[0, 0, w, h],
Plains.children[0].children[0].matrix,
);
room.layer(3).stampAt(
room.tiles[3].stampAt(
[0, 0, w, h],
Plains.children[1].matrix,
);
if (Object.keys(room.layer(3).entities).length > 0) {
if (Object.keys(room.entities).length > 0) {
return;
}
for (let y = 0; y < 64; ++y) {
@ -87,7 +87,7 @@ export default async (room, latus) => {
Vector.add([8, 8], Vector.scale([x, y], 16)),
[Math.random() * 8 - 4, Math.random() * 8 - 4],
)));
room.addEntityToLayer(shrub, 3);
room.addEntity(shrub);
}
else if (v < 0.17) {
// eslint-disable-next-line no-await-in-loop
@ -99,7 +99,7 @@ export default async (room, latus) => {
Vector.add([8, 8], p),
[Math.random() * 8 - 4, Math.random() * 8 - 4],
)));
room.addEntityToLayer(tree, 3);
room.addEntity(tree);
}
v = noise(Vector.scale([x, y], 9), seed + 1);
if (v < 0.08) {
@ -109,7 +109,7 @@ export default async (room, latus) => {
Vector.add([8, 8], Vector.scale([x, y], 16)),
[Math.random() * 8 - 4, Math.random() * 8 - 4],
)));
room.addEntityToLayer(flower, 3);
room.addEntity(flower);
}
}
}

View File

@ -9,7 +9,7 @@ export default {
hooks: {
'@latus/core/hmr': (path, latus) => {
if ('@humus/universe/server' === path) {
alpha(latus.get('%universe').room('players/cha0s/index.room.json'), latus);
alpha(latus.get('%universe').room('players/cha0s/rooms/water/0.room.json'), latus);
}
},
'@latus/core/server/up': async (latus) => {
@ -25,7 +25,7 @@ export default {
try {
const {Universe} = latus.get('%resources');
const universe = await Universe.load({extends: `/universe/${running}/index.universe.json`});
alpha(universe.room('players/cha0s/index.room.json'), latus);
alpha(universe.room('players/cha0s/rooms/water/0.room.json'), latus);
latus.set('%universe', universe);
universe.start();
// eslint-disable-next-line no-console

View File

@ -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, 1);
room.removeEntity(this.entity);
}
if (this.entity.currentRoom) {
const room = this.#universe.room(this.entity.currentRoom);
room.addEntityToLayer(this.entity, 0);
room.addEntity(this.entity);
this.entity.startInforming(room);
}
}