diff options
| author | ilotterytea <iltsu@alright.party> | 2024-05-30 00:48:46 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-05-30 00:48:46 +0500 |
| commit | cc40d196da82d113dc8d039198ca70562533ced1 (patch) | |
| tree | a05172e983e1983aabf17ac62cfcfe2653199fcc /core | |
| parent | a99ad4295f6415578b700f9dede0e15eea77c7ae (diff) | |
feat: item purchase functionality
Diffstat (limited to 'core')
| -rw-r--r-- | core/src/com/ilotterytea/maxoning/screens/game/shop/ShopUI.java | 59 | ||||
| -rw-r--r-- | core/src/com/ilotterytea/maxoning/ui/PurchaseItem.java | 46 |
2 files changed, 95 insertions, 10 deletions
diff --git a/core/src/com/ilotterytea/maxoning/screens/game/shop/ShopUI.java b/core/src/com/ilotterytea/maxoning/screens/game/shop/ShopUI.java index 2f87bad..a4d6135 100644 --- a/core/src/com/ilotterytea/maxoning/screens/game/shop/ShopUI.java +++ b/core/src/com/ilotterytea/maxoning/screens/game/shop/ShopUI.java @@ -27,6 +27,8 @@ public class ShopUI { private final MaxonSavegame savegame; private Label pointsLabel, multiplierLabel; + private final ArrayList<PurchaseItem> purchaseItems = new ArrayList<>(); + public ShopUI(final MaxonSavegame savegame, Stage stage, Skin skin, TextureAtlas atlas) { this.savegame = savegame; @@ -168,12 +170,31 @@ public class ShopUI { table.setBackground("shop_list"); for (final MaxonItem item : this.items) { - int amount = (int) savegame.inv.stream().filter(c -> c == item.id).count(); - - double price = item.price * java.lang.Math.pow(1.15f, amount + this.multiplier.getMultiplier()); - item.price = (float) price; PurchaseItem purchaseItem = new PurchaseItem(this.skin, item); - + purchaseItem.addListener(new ClickListener() { + @Override + public void clicked(InputEvent event, float x, float y) { + super.clicked(event, x, y); + + if (purchaseItem.isDisabled()) { + return; + } + + if (mode == ShopMode.BUY) { + savegame.points -= (long) purchaseItem.getPrice(); + for (int i = 0; i < multiplier.getMultiplier(); i++) { + savegame.inv.add(purchaseItem.getItem().id); + } + } else { + savegame.points += (long) purchaseItem.getPrice(); + for (int i = 0; i < multiplier.getMultiplier(); i++) { + savegame.inv.remove(Integer.valueOf(purchaseItem.getItem().id)); + } + } + } + }); + + purchaseItems.add(purchaseItem); table.add(purchaseItem).growX().padBottom(5f).row(); } @@ -188,9 +209,37 @@ public class ShopUI { this.table.add(scrollPaneTable).grow().row(); } + private void updatePurchaseItems() { + for (final PurchaseItem item : this.purchaseItems) { + int amount = (int) savegame.inv.stream().filter(c -> c == item.getItem().id).count(); + double price = item.getItem().price * java.lang.Math.pow(1.15f, amount + multiplier.getMultiplier()); + + if (mode == ShopMode.SELL) { + price /= 4; + } + + item.setPrice(price); + + if (mode == ShopMode.BUY) { + if (price > savegame.points || savegame.points - price < 0) { + item.setDisabled(true); + } else if (item.isDisabled()) { + item.setDisabled(false); + } + } else { + if (amount - multiplier.getMultiplier() < 0) { + item.setDisabled(true); + } else if (item.isDisabled()) { + item.setDisabled(false); + } + } + } + } + public void render() { this.pointsLabel.setText(String.valueOf(savegame.points)); this.multiplierLabel.setText(String.format("%s/s", savegame.multiplier)); + updatePurchaseItems(); } public void update() { diff --git a/core/src/com/ilotterytea/maxoning/ui/PurchaseItem.java b/core/src/com/ilotterytea/maxoning/ui/PurchaseItem.java index 7f6d44c..fb69d4f 100644 --- a/core/src/com/ilotterytea/maxoning/ui/PurchaseItem.java +++ b/core/src/com/ilotterytea/maxoning/ui/PurchaseItem.java @@ -9,6 +9,12 @@ import com.ilotterytea.maxoning.MaxonConstants; import com.ilotterytea.maxoning.player.MaxonItem; public class PurchaseItem extends Table { + private double price; + private final Label priceLabel; + private final MaxonItem item; + + private boolean isDisabled = false; + public PurchaseItem( Skin skin, MaxonItem item @@ -19,31 +25,61 @@ public class PurchaseItem extends Table { super.add(item.icon).size(64f).pad(6f); + this.price = item.price; + this.item = item; + Table summary = new Table(); summary.align(Align.topLeft); Label name = new Label(item.name, skin, "item_title"); name.setAlignment(Align.left); - Label desc = new Label(String.format("%s SQP (%s/click)", MaxonConstants.DECIMAL_FORMAT.format(item.price), MaxonConstants.DECIMAL_FORMAT.format(item.multiplier)), skin, "item_price"); - desc.setAlignment(Align.left); + this.priceLabel = new Label(String.format("%s SQP (%s/click)", MaxonConstants.DECIMAL_FORMAT.format(price), MaxonConstants.DECIMAL_FORMAT.format(item.multiplier)), skin, "item_price"); + this.priceLabel.setAlignment(Align.left); summary.add(name).align(Align.left).row(); - summary.add(desc).grow(); + summary.add(this.priceLabel).grow(); super.add(summary).grow(); super.addListener(new ClickListener() { @Override public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) { - PurchaseItem.super.setBackground("shop_item_hover"); super.enter(event, x, y, pointer, fromActor); + if (!isDisabled) { + PurchaseItem.super.setBackground("shop_item_hover"); + } } @Override public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) { - PurchaseItem.super.setBackground("shop_item"); super.exit(event, x, y, pointer, toActor); + if (!isDisabled) { + PurchaseItem.super.setBackground("shop_item"); + } } }); } + + public void setPrice(double price) { + this.price = price; + this.priceLabel.setText(String.format("%s SQP (%s/click)", MaxonConstants.DECIMAL_FORMAT.format(price), MaxonConstants.DECIMAL_FORMAT.format(item.multiplier))); + } + + public double getPrice() { + return price; + } + + public boolean isDisabled() { + return isDisabled; + } + + public void setDisabled(boolean disabled) { + isDisabled = disabled; + + super.setBackground(isDisabled ? "bg" : "shop_item"); + } + + public MaxonItem getItem() { + return item; + } } |
