class FruitInventory extends GameObject { closeButton = null; closeBtnX = 120; closeBtnY = -140; state = 0; static Instance = null; g_inv = null; get inventory () { if (!this.g_inv) { let inv = CookieManager.getCookie("fruitInventory"); if (inv.length <= 0) inv = "[]"; this.g_inv = JSON.parse(inv); this.g_inv = this.g_inv || []; } return this.g_inv; } constructor (x, y, sprite, layer) { super(x, y, sprite, layer); FruitInventory.Instance = this; } saveInventory () { CookieManager.setCookie("fruitInventory", JSON.stringify(this.inventory)); } getFruitAmountOfType (type) { let count = 0; for (let i = 0; i < this.inventory.length; i++) { if (this.inventory[i] == type) count++; } return count; } takeFromInventory (type) { for (let i = 0; i < this.inventory.length; i++) { if (this.inventory[i] == type) { this.inventory.splice(i, 1); this.saveInventory(); return new ItemFruit(GameInput.mousePosition.x, GameInput.mousePosition.y, this.scene.game.images.fruit, 0, type); } } this.moveToClose(); return null; } addToInventory (type, amount = 1) { for (let i = 0; i < amount; i++) this.inventory.push(type); this.saveInventory(); } addedToScene () { let pos = this.transform.position.add(new vector(this.closeBtnX, this.closeBtnY)); this.closeButton = this.scene.addObject(new SubBtn(pos.x, pos.y, this.scene.game.images.closeButton)); this.moveToClose(); this.closeButton.isClicked.add(function () { this.moveToClose(); }, this); MenuManager.Instance.isOpened.add(function () { this.moveToClose(); }, this); } update (deltaTime) { super.update(deltaTime); let pos = this.transform.position.add(new vector(this.closeBtnX, this.closeBtnY)); this.closeButton.transform.position = pos; } moveToOpen (onDone) { let context = this; new TWEEN.Tween(this.transform.position) .to({ y: 250 }, 1500) .easing(TWEEN.Easing.Elastic.Out) .onComplete(() => { this.state = MenuManager.States.open; onDone && onDone.call(context); }) .start(); } moveToClose (onDone) { let context = this; new TWEEN.Tween(this.transform.position) .to({ y: -500 }, 1500) .easing(TWEEN.Easing.Elastic.Out) .onComplete(() => { this.state = MenuManager.States.closed; onDone && onDone.call(context); }) .start(); } }