From 09511e8ed5006d6246ed2323119764f5bfda9b02 Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 30 May 2019 09:12:17 -0500 Subject: [PATCH] fun: tomato plant and seeds! --- common/traits/plant.trait.js | 90 +++++++++++++++++++++++++ common/traits/trait-plant.packet.js | 11 +++ resource/seeds.png | Bin 0 -> 864 bytes resource/tomato-stage-0.png | Bin 0 -> 204 bytes resource/tomato-stage-1.png | Bin 0 -> 216 bytes resource/tomato-stage-2.png | Bin 0 -> 241 bytes resource/tomato-stage-3.png | Bin 0 -> 313 bytes resource/tomato-stage-4.png | Bin 0 -> 296 bytes server/create-entity-for-connection.js | 4 ++ server/create-fixtures.js | 4 ++ server/fixtures/tomato-plant.entity.js | 76 +++++++++++++++++++++ server/fixtures/tomato-seeds.entity.js | 82 ++++++++++++++++++++++ 12 files changed, 267 insertions(+) create mode 100644 common/traits/plant.trait.js create mode 100644 common/traits/trait-plant.packet.js create mode 100644 resource/seeds.png create mode 100644 resource/tomato-stage-0.png create mode 100644 resource/tomato-stage-1.png create mode 100644 resource/tomato-stage-2.png create mode 100644 resource/tomato-stage-3.png create mode 100644 resource/tomato-stage-4.png create mode 100644 server/fixtures/tomato-plant.entity.js create mode 100644 server/fixtures/tomato-seeds.entity.js diff --git a/common/traits/plant.trait.js b/common/traits/plant.trait.js new file mode 100644 index 0000000..bf56d42 --- /dev/null +++ b/common/traits/plant.trait.js @@ -0,0 +1,90 @@ +import {behaviorItemFromJSON, createContext} from '@avocado/behavior'; +import {compose, Property} from '@avocado/core'; +import {StateProperty, Trait} from '@avocado/entity'; + +import {TraitPlantPacket} from './trait-plant.packet'; + +const decorate = compose( + StateProperty('growthStage', { + track: true, + }), +); + +export class Plant extends decorate(Trait) { + + static defaultParams() { + return { + growthCondition: { + type: 'condition', + operator: 'or', + operands: [], + }, + stageSpecs: {}, + }; + } + + static defaultState() { + return { + growthStage: 0, + }; + } + + static type() { + return 'plant'; + } + + constructor(entity, params, state) { + super(entity, params, state); + this.growthCondition = behaviorItemFromJSON(this.params.growthCondition); + this.growthConditionContext = createContext(); + this.growthConditionContext.set('entity', this.entity); + this.growthElapsed = 0; + } + + acceptPacket(packet) { + if (packet instanceof TraitPlantPacket) { + this.entity.growthStage = packet.data.growthStage; + } + } + + packetsForUpdate() { + return this.createTraitPacketUpdates(TraitPlantPacket); + } + + listeners() { + return { + + growthStageChanged: (oldStage, newStage) => { + const stageSpec = this.params.stageSpecs[newStage]; + this.entity.currentImage = stageSpec.image; + }, + + }; + } + + methods() { + return { + + growToNextStage: () => { + const growthStage = this.entity.growthStage; + const stageSpec = this.params.stageSpecs[growthStage]; + this.growthElapsed = stageSpec.growAt; + this.entity.growthStage = growthStage + 1; + }, + + }; + } + + tick(elapsed) { + if (this.growthCondition.get(this.growthConditionContext)) { + const growthStage = this.entity.growthStage; + const stageSpec = this.params.stageSpecs[growthStage]; + // TODO variance + this.growthElapsed += elapsed; + if (this.growthElapsed >= stageSpec.growAt) { + this.entity.growthStage = growthStage + 1; + } + } + } + +} diff --git a/common/traits/trait-plant.packet.js b/common/traits/trait-plant.packet.js new file mode 100644 index 0000000..e539c6a --- /dev/null +++ b/common/traits/trait-plant.packet.js @@ -0,0 +1,11 @@ +import {EntityPacket} from '@avocado/entity'; + +export class TraitPlantPacket extends EntityPacket { + + static get schema() { + const schema = super.schema; + schema.data.growthStage = 'uint8'; + return schema; + } + +} diff --git a/resource/seeds.png b/resource/seeds.png new file mode 100644 index 0000000000000000000000000000000000000000..45ba9d9e2e9ec38c8bed8b5960d7100e268920af GIT binary patch literal 864 zcmV-m1E2hfP)e zSad^gZEa<4bO1wgWnpw>WFU8GbZ8()Nlj2!fese{00PKKL_t(I%Z-#vNK|nc#ee@h zlg&7DeT-UnG%d}mTs_$W3LGQBWbc zi4tjg5T<74o$-~gnS1Bo%E6fo`j%&LIOlxl`-GH|)Dc4b4bB@WrAWo>3laS2?j$ce zlM7ewQc+sQ)tfii)zrwu*d+UQZbAqlQ^~}fA|ZrqC||0%@z(GI80<}jEk2qq2=aankH^n z7A}_yU*&2(ziLa0KoLSDSkn~Ap{4+r&YoiL`YNzs_qOer(HJG=I$PIP(b{^QaOfQ_ zm&!k_-8+8*e;|Nm2~@WO!!SroQz_Mph|X9TkuZv)5Vt_nG}6mz684g;)z#IZ>pH6H z#O+L{tlY=zw;f&~&CN&9G>z$49LLImD;uar60~9XJW7HVN>2!j95r#2}uInt= zz4@RJLVkOBp9#}O!4eNLYSP`^jazjP3WW#{8`K2Xv3gAf8LQR;IHZ)be^~>_%gw=C ztdpIslBPQF=I0U_i;|x1rnAdv6~ATE2$I;~^s>lgU{V zuU5eL$Pgxy(iO$rxpfM*{_1?l<0l;nRZSb~dH=bW98Wf;73XVjKXm~= zBNLNErzY|H{lsE1GW`vSBeqK^Me4Pm4;wVp2eH`~A#Bs=#f2I=GDh0000{)GJi literal 0 HcmV?d00001 diff --git a/resource/tomato-stage-0.png b/resource/tomato-stage-0.png new file mode 100644 index 0000000000000000000000000000000000000000..aa565d8a3dd16a13249e1aa61ee72bb1420af01c GIT binary patch literal 204 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4S-PBL8Ax$_Vt!CBxD zST%s^_uLH>k}PqJC~?lu%}vcKVQ?-=O)N=G zQ7F$W$xv|j^bH7awo5Ftou2ConFF{ sY@LM9j|4t5U1XaeH21J6j|U^ei_@% literal 0 HcmV?d00001 diff --git a/resource/tomato-stage-1.png b/resource/tomato-stage-1.png new file mode 100644 index 0000000000000000000000000000000000000000..68fae6ed679adb0a416b0844e83d894b63d2f827 GIT binary patch literal 216 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4S-PBL8Ax$_Vt!CBxD zSZ?e4m6`&DBuiW)N}Tg^b5rw57@Uhz6H8K4 z6v{J8G8EiBeFMT9`NV;W96VhdLo7}w&#C`p#jM)U+2~|&g2%(;dI}IPSOaT; literal 0 HcmV?d00001 diff --git a/resource/tomato-stage-2.png b/resource/tomato-stage-2.png new file mode 100644 index 0000000000000000000000000000000000000000..f3fcc5b13f873c08ff985119b54be0c7eeb878d6 GIT binary patch literal 241 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4S-PBL8Ax$_Vt!CBxD zSg)ZSKSvuVBw6AbQR1ARo12W7-Dfcc~1Q&D`wS(&PFGL6FeR!*HeIa$$D;ymr~p} z_MB+){@B9)?|FVdQ&MBb@08w^JFaQ7m literal 0 HcmV?d00001 diff --git a/resource/tomato-stage-3.png b/resource/tomato-stage-3.png new file mode 100644 index 0000000000000000000000000000000000000000..c05ac6e2dc610270bbc37e19fcff03f3d99f6a72 GIT binary patch literal 313 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4S-PBL8Ax$_Vt!CBxD zSbCYGe8 zD3oWGWGJ|M`UZqI@`(c#E%bD846!(!Jg5GX6|-tXXQPwB2eu6g#X@IICBKBP`g?ro z4u?c#Wk>NnoYjXusJ-Frdc@S)+mnB2p23wFlK+ptZu+Ud-q>JHQo@n6>eDMX_pt13 z;P$C_z`y-x(hps?Co9qqE|<9b-Zbsl|D}g_{8Qd7bhJUr`0;<9G=b+y#vHTFB|a{7 zNpaqAl;NC#hspJn1R%H&zp^ddI8;dJ@rCm~43d#nif?R5 Bay;j;N`W#P5VYM^e=XTr2U6Y-_qM<{ryp&@*`0_`g2p zo`a#o)jfi%{|m-7oDk+=j;Y&MyJ?=_UG1J9K}u&{{@^((#n$<377qyg(!R=ZjOi$6 dXW@%