Skip to content

Commit 89c7f3f

Browse files
Finish blocks for inventory class
1 parent 2361292 commit 89c7f3f

File tree

5 files changed

+102
-5
lines changed

5 files changed

+102
-5
lines changed

.github/makecode/blocks.png

9.55 KB
Loading

.github/makecode/blocksdiff.png

-116 KB
Loading

inventory.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ enum ToolbarNumberAttribute {
3232
MaxItems
3333
}
3434

35+
enum InventoryNumberAttribute {
36+
//% block="selected index"
37+
SelectedIndex,
38+
//% block="max items"
39+
MaxItems
40+
}
41+
3542
//% color="#7732B3"
3643
//% group="['Item', 'Toolbar', 'Inventory']"
3744
namespace Inventory {
@@ -395,6 +402,16 @@ namespace Inventory {
395402
return this._items;
396403
}
397404

405+
/**
406+
* Get the items in the inventory. Only rewrapped for blocks.
407+
*/
408+
//% block="%Inventory(Inventory) get items"
409+
//% weight=80
410+
//% group="Inventory"
411+
public get_items() {
412+
return this.items;
413+
}
414+
398415
/**
399416
* Set the items in the inventory.
400417
*/
@@ -403,6 +420,14 @@ namespace Inventory {
403420
this.update();
404421
}
405422

423+
//% block="%Inventory(Inventory) set items to %new_items"
424+
//% new_items.shadow="lists_create_with"
425+
//% weight=90
426+
//% group="Inventory"
427+
public set_items(new_items: Item[]) {
428+
this.items = new_items;
429+
}
430+
406431
/**
407432
* Get the maximum amount of items in the inventory.
408433
*/
@@ -417,6 +442,35 @@ namespace Inventory {
417442
this._max_items = new_max;
418443
this.update();
419444
}
445+
446+
/**
447+
* Set the selected index or max items. Only rewrapped for blocks.
448+
*/
449+
//% block="%Inventory(Inventory) set %attribute to %value"
450+
//% weight=70
451+
//% group="Inventory"
452+
public set_number(attribute: InventoryNumberAttribute, value: number) {
453+
if (attribute == InventoryNumberAttribute.SelectedIndex) {
454+
this.selected = value;
455+
} else if (attribute == InventoryNumberAttribute.MaxItems) {
456+
this.max_items = value;
457+
}
458+
}
459+
460+
/**
461+
* Get the selected index or max items. Only rewrapped for blocks.
462+
*/
463+
//% block="%Inventory(Inventory) get %attribute"
464+
//% weight=60
465+
//% group="Inventory"
466+
public get_number(attribute: InventoryNumberAttribute) {
467+
if (attribute == InventoryNumberAttribute.SelectedIndex) {
468+
return this.selected;
469+
} else if (attribute == InventoryNumberAttribute.MaxItems) {
470+
return this.max_items;
471+
}
472+
return -1;
473+
}
420474

421475
/**
422476
* Get the text in the inventory.
@@ -425,6 +479,16 @@ namespace Inventory {
425479
return this._text;
426480
}
427481

482+
/**
483+
* Get the text in the inventory. Only rewrapped for blocks.
484+
*/
485+
//% block="%Inventory(Inventory) get text"
486+
//% weight=40
487+
//% group="Inventory"
488+
public get_text() {
489+
return this.text;
490+
}
491+
428492
/**
429493
* Set the text in the inventory.
430494
*/
@@ -433,11 +497,25 @@ namespace Inventory {
433497
this.update();
434498
}
435499

500+
/**
501+
* Set the text in the inventory. Only rewrapped for blocks.
502+
*/
503+
//% block="%Inventory(Inventory) set text to %new_text"
504+
//% weight=50
505+
//% group="Inventory"
506+
public set_text(new_text: string) {
507+
this.text = new_text;
508+
}
509+
436510
/**
437511
* Set a specific part of the inventory to a specific color.
438512
* @param attribute: A property of the InventoryColorAttribute enum.
439513
* @param color: A number which should be the new color of the attribute.
440514
*/
515+
//% block="%Inventory(Inventory) set color of %attribute to %color"
516+
//% color.shadow=colorindexpicker
517+
//% weight=30
518+
//% group="Inventory"
441519
public set_color(attribute: InventoryColorAttribute, color: number) {
442520
if (attribute == InventoryColorAttribute.InventoryOutline) {
443521
this._inv_outline_color = color;
@@ -456,6 +534,9 @@ namespace Inventory {
456534
* @param attribute: A property of the InventoryColorAttribute enum.
457535
* @return: The color (which is a number) of the attribute, otherwise -1.
458536
*/
537+
//% block="%Inventory(Inventory) get color of %attribute"
538+
//% weight=20
539+
//% group="Inventory"
459540
public get_color(attribute: InventoryColorAttribute) {
460541
if (attribute == InventoryColorAttribute.InventoryOutline) {
461542
return this._inv_outline_color;
@@ -472,6 +553,9 @@ namespace Inventory {
472553
/**
473554
* Update the image of the inventory.
474555
*/
556+
//% block="%Inventory(Inventory) force redraw of inventory"
557+
//% weight=10
558+
//% group="Inventory"
475559
public update() {
476560
let image_size: number = 16;
477561
let padding: number = 1;
@@ -501,4 +585,17 @@ namespace Inventory {
501585
this.setImage(new_image);
502586
}
503587
}
588+
589+
/**
590+
* Create a new inventory - for blocks.
591+
*/
592+
//% block="create inventory with items %items and max items %max_items"
593+
//% blockSetVariable=inventory
594+
//% items.shadow="lists_create_with"
595+
//% max_items.dfl=3
596+
//% weight=100
597+
//% group="Inventory"
598+
export function create_inventory(items: Item[], max_items: number) {
599+
return new Inventory(items, max_items);
600+
}
504601
}

main.blocks

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="O5Ms-MSjX1ckdlYxmHl3">inv_contents</variable><variable id="M~w)hn!2hv3g!ud^v)r0">inv</variable><variable id="%8P,Y?##mLxxx6@=UhAU">toolbar</variable></variables><block type="pxt-on-start" id="ciDH1w$_E|!vhd`j^0sG" x="0" y="0"><statement name="HANDLER"><block type="spriteutilextsetconsolevisible" id="|T*_ny1D!z/Yh^A{]U$G"><value name="on"><shadow type="toggleOnOff" id="nE`=msm,zJ8ER}cl5v!!"><field name="on">true</field></shadow></value><next><block type="gamesetbackgroundcolor" id="e!G^88Ni_#|ccyMk)[2g"><value name="color"><shadow type="colorindexpicker" id="XU]8DOaDwJ`7sTrN^{8I"><field name="index">7</field></shadow></value><next><block type="variables_set" id="KZ{yU=MsC|-b0lzS|jQe"><field name="VAR" id="%8P,Y?##mLxxx6@=UhAU">toolbar</field><value name="VALUE"><shadow type="math_number" id="lkHS#qy-$u}oN5upI2z{" disabled="true"><field name="NUM">0</field></shadow><block type="Inventory_create_toolbar" id="%o*fP`cGc*8aS1oG-Zap"><value name="items"><block type="lists_create_with" id="/^Bj^T,}KepfeRcLoFPz"><mutation items="3"/><value name="ADD0"><block type="Inventory_create_item" id="@]Aa$x`#WYQtR7)nD!TD"><mutation xmlns="http://www.w3.org/1999/xhtml" _expanded="0" _input_init="false"></mutation><value name="name"><shadow type="text" id="=Gi{Lp);Uh+]^0}c*Omj"><field name="TEXT">Apple</field></shadow></value><value name="image"><shadow type="screen_image_picker" id="C*Jj0V6f(`UxDmM[KZQr"><field name="img">img`
1+
<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="O5Ms-MSjX1ckdlYxmHl3">inv_contents</variable><variable id="%8P,Y?##mLxxx6@=UhAU">toolbar</variable><variable id="{5=8(#8[mpRK`SUegF[~">inventory</variable></variables><block type="pxt-on-start" id="ciDH1w$_E|!vhd`j^0sG" x="0" y="0"><statement name="HANDLER"><block type="spriteutilextsetconsolevisible" id="|T*_ny1D!z/Yh^A{]U$G"><value name="on"><shadow type="toggleOnOff" id="nE`=msm,zJ8ER}cl5v!!"><field name="on">true</field></shadow></value><next><block type="gamesetbackgroundcolor" id="e!G^88Ni_#|ccyMk)[2g"><value name="color"><shadow type="colorindexpicker" id="XU]8DOaDwJ`7sTrN^{8I"><field name="index">7</field></shadow></value><next><block type="variables_set" id="KZ{yU=MsC|-b0lzS|jQe"><field name="VAR" id="%8P,Y?##mLxxx6@=UhAU">toolbar</field><value name="VALUE"><shadow type="math_number" id="lkHS#qy-$u}oN5upI2z{" disabled="true"><field name="NUM">0</field></shadow><block type="Inventory_create_toolbar" id="%o*fP`cGc*8aS1oG-Zap"><value name="items"><block type="lists_create_with" id="/^Bj^T,}KepfeRcLoFPz"><mutation items="3"/><value name="ADD0"><block type="Inventory_create_item" id="@]Aa$x`#WYQtR7)nD!TD"><mutation xmlns="http://www.w3.org/1999/xhtml" _expanded="0" _input_init="false"></mutation><value name="name"><shadow type="text" id="=Gi{Lp);Uh+]^0}c*Omj"><field name="TEXT">Apple</field></shadow></value><value name="image"><shadow type="screen_image_picker" id="C*Jj0V6f(`UxDmM[KZQr"><field name="img">img`
22
. . . . . . . e c 7 . . . . . .
33
. . . . e e e c 7 7 e e . . . .
44
. . c e e e e c 7 e 2 2 e e . .
@@ -66,4 +66,4 @@ c e e 2 2 2 2 2 2 2 2 2 2 4 2 e
6666
. . 2 e e 2 2 2 2 2 4 4 2 e . .
6767
. . . 2 2 e e 4 4 4 2 e e . . .
6868
. . . . . 2 2 e e e e . . . . .
69-
`</field><data>{"commentRefs":[],"fieldData":{"img":null}}</data></shadow></value></block></value></block></statement><next><block type="typescript_statement" id="Ep?G!P=daZi?#cc|D~Q{" editable="false"><mutation xmlns="http://www.w3.org/1999/xhtml" line0="let inv = new Inventory.Inventory(inv_contents, 32);" numlines="1" declaredvars="inv"></mutation><next><block type="Sprite_blockCombine_set" id=";QnO;`_85r}zr={!EbzJ"><field name="property">Sprite.left@set</field><value name="mySprite"><block type="variables_get" id=";EsAn;RYMrU=KO7jOJuf"><field name="VAR" id="M~w)hn!2hv3g!ud^v)r0">inv</field></block></value><value name="value"><shadow type="math_number" id="IoY1s#NPn/:~H0KGR4t3"><field name="NUM">4</field></shadow></value><next><block type="Sprite_blockCombine_set" id="1b[P,lbS@A`ddAx@3-No"><field name="property">Sprite.top@set</field><value name="mySprite"><block type="variables_get" id="#Y;O5:@lisQq#8*z.SA#"><field name="VAR" id="M~w)hn!2hv3g!ud^v)r0">inv</field></block></value><value name="value"><shadow type="math_number" id="{D9zZZIi@-mfdtD}#+UX"><field name="NUM">4</field></shadow></value></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></statement></block></xml>
69+
`</field><data>{"commentRefs":[],"fieldData":{"img":null}}</data></shadow></value></block></value></block></statement><next><block type="variables_set" id="6*fnw9@VSz6[m|bfgvep"><field name="VAR" id="{5=8(#8[mpRK`SUegF[~">inventory</field><value name="VALUE"><shadow xmlns="http://www.w3.org/1999/xhtml" type="math_number"><field name="NUM">0</field></shadow><block type="Inventory_create_inventory" id="e!}NYYxw5}]SAk:)rY(;"><value name="items"><block type="variables_get" id="LQEB#dO[e-;9pM0$_1Z9"><field name="VAR" id="O5Ms-MSjX1ckdlYxmHl3">inv_contents</field></block></value><value name="max_items"><shadow type="math_number" id="u#PGL%1M6q%k,(UL}}wm"><field name="NUM">32</field></shadow></value></block></value><next><block type="Sprite_blockCombine_set" id="1b[P,lbS@A`ddAx@3-No"><field name="property">Sprite.top@set</field><value name="mySprite"><block type="variables_get" id="#Y;O5:@lisQq#8*z.SA#"><field name="VAR" id="{5=8(#8[mpRK`SUegF[~">inventory</field></block></value><value name="value"><shadow type="math_number" id="{D9zZZIi@-mfdtD}#+UX"><field name="NUM">4</field></shadow></value><next><block type="Sprite_blockCombine_set" id=";QnO;`_85r}zr={!EbzJ"><field name="property">Sprite.left@set</field><value name="mySprite"><block type="variables_get" id=";EsAn;RYMrU=KO7jOJuf"><field name="VAR" id="{5=8(#8[mpRK`SUegF[~">inventory</field></block></value><value name="value"><shadow type="math_number" id="IoY1s#NPn/:~H0KGR4t3"><field name="NUM">4</field></shadow></value></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></next></block></statement></block></xml>

main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ for (let index = 0; index < 30; index++) {
7575
. . . . . 2 2 e e e e . . . . .
7676
`))
7777
}
78-
let inv = new Inventory.Inventory(inv_contents, 32);
79-
inv.left = 4
80-
inv.top = 4
78+
let inventory = Inventory.create_inventory(inv_contents, 32)
79+
inventory.top = 4
80+
inventory.left = 4

0 commit comments

Comments
 (0)