refactor: farming

This commit is contained in:
cha0s 2021-05-09 21:15:56 -05:00
parent 4889cf7177
commit 4faf685029
4 changed files with 71 additions and 25 deletions

View File

@ -1,7 +1,10 @@
import {gatherWithLatus} from '@latus/core';
import {decorateWithLatus, gatherWithLatus} from '@latus/core';
export default {
hooks: {
'@avocado/resource/resources.decorate': decorateWithLatus(
require.context('./resources/decorators', false, /\.js$/),
),
'@avocado/traits/traits': gatherWithLatus(
require.context('./traits', false, /\.js$/),
),

View File

@ -0,0 +1,66 @@
import {Rectangle, Vector} from '@avocado/math';
const DIRT = 6;
const TILLED = 7;
export default (Room) => class FarmableRoom extends Room {
#water = {};
adjustWaterAt(target, increase) {
const [tiles] = this.tiles;
if (
'client' === process.env.SIDE
|| TILLED !== tiles.tileAt(target)
) {
return;
}
const hash = Vector.packToUint32(target);
if (!(hash in this.#water)) {
this.#water[hash] = 0;
}
this.#water[hash] += increase;
if (this.#water[hash] < 0) {
// Drought
this.#water[hash] = Math.max(this.#water[hash], -128);
}
else if (this.#water[hash] > 0) {
// Flood
this.#water[hash] = Math.min(this.#water[hash], 127);
}
}
isPlantAt(target) {
const entities = this.entityList.visibleEntities(Rectangle.centerOn(target, [1, 1]));
for (let i = 0; i < entities.length; ++i) {
const entity = entities[i];
if (Vector.equals(entity.position, target) && entity.is('Plant')) {
return true;
}
}
return false;
}
mayPrepareSoilAt(target) {
const [tiles] = this.tiles;
return DIRT === tiles.tileAt(target);
// return -1 !== GRASS.indexOf(tiles.tileAt(target));
}
maySowAt(target) {
const [tiles] = this.tiles;
return (
'client' !== process.env.SIDE
&& TILLED === tiles.tileAt(target)
&& !this.isPlantAt(target)
);
}
prepareSoilAt(target) {
if ('client' !== process.env.SIDE) {
const [tiles] = this.tiles;
tiles.setTileAt(target, TILLED);
}
}
};

View File

@ -1,24 +0,0 @@
import {Rectangle, Vector} from '@avocado/math';
import {Trait} from '@avocado/traits';
export default () => class Seeds extends Trait {
// eslint-disable-next-line class-methods-use-this
methods() {
return {
isPlantAt: (position, list) => {
const entities = list.visibleEntities(Rectangle.centerOn(position, [1, 1]));
for (let i = 0; i < entities.length; ++i) {
const entity = entities[i];
if (Vector.equals(entity.position, position) && entity.is('Plant')) {
return true;
}
}
return false;
},
};
}
};

View File

@ -97,6 +97,7 @@ export default (latus) => class Tool extends Trait {
),
entity: this.entity,
target,
room,
tiles,
wielder,
};