From 4faf685029b248a80b5cd42182303e759b2f6c69 Mon Sep 17 00:00:00 2001 From: cha0s Date: Sun, 9 May 2021 21:15:56 -0500 Subject: [PATCH] refactor: farming --- packages/farm/src/index.js | 5 +- .../farm/src/resources/decorators/room.js | 66 +++++++++++++++++++ packages/farm/src/traits/seeds.js | 24 ------- packages/farm/src/traits/tool.js | 1 + 4 files changed, 71 insertions(+), 25 deletions(-) create mode 100644 packages/farm/src/resources/decorators/room.js delete mode 100644 packages/farm/src/traits/seeds.js diff --git a/packages/farm/src/index.js b/packages/farm/src/index.js index be0f06b..33d9d01 100644 --- a/packages/farm/src/index.js +++ b/packages/farm/src/index.js @@ -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$/), ), diff --git a/packages/farm/src/resources/decorators/room.js b/packages/farm/src/resources/decorators/room.js new file mode 100644 index 0000000..528fc21 --- /dev/null +++ b/packages/farm/src/resources/decorators/room.js @@ -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); + } + } + +}; diff --git a/packages/farm/src/traits/seeds.js b/packages/farm/src/traits/seeds.js deleted file mode 100644 index 2e5a2c6..0000000 --- a/packages/farm/src/traits/seeds.js +++ /dev/null @@ -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; - }, - - }; - } - -}; diff --git a/packages/farm/src/traits/tool.js b/packages/farm/src/traits/tool.js index 9e0feff..43558f0 100644 --- a/packages/farm/src/traits/tool.js +++ b/packages/farm/src/traits/tool.js @@ -97,6 +97,7 @@ export default (latus) => class Tool extends Trait { ), entity: this.entity, target, + room, tiles, wielder, };