diff options
| author | ilotterytea <iltsu@alright.party> | 2024-06-08 23:21:59 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-06-09 19:27:33 +0500 |
| commit | 5cd639a94a58fc84c0d3476df23d322759277831 (patch) | |
| tree | 014d496b06523846dfa12c5e6972627c37ccdf85 /core/src/kz/ilotterytea/maxon/pets | |
| parent | 38ffeaa390322ab109bfe933f124091966a57158 (diff) | |
feat: new pet manager
Diffstat (limited to 'core/src/kz/ilotterytea/maxon/pets')
| -rw-r--r-- | core/src/kz/ilotterytea/maxon/pets/Pet.java | 85 | ||||
| -rw-r--r-- | core/src/kz/ilotterytea/maxon/pets/PetManager.java | 52 | ||||
| -rw-r--r-- | core/src/kz/ilotterytea/maxon/pets/PetWidget.java | 105 |
3 files changed, 242 insertions, 0 deletions
diff --git a/core/src/kz/ilotterytea/maxon/pets/Pet.java b/core/src/kz/ilotterytea/maxon/pets/Pet.java new file mode 100644 index 0000000..c39e77e --- /dev/null +++ b/core/src/kz/ilotterytea/maxon/pets/Pet.java @@ -0,0 +1,85 @@ +package kz.ilotterytea.maxon.pets; + +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.utils.GdxRuntimeException; +import kz.ilotterytea.maxon.MaxonConstants; +import kz.ilotterytea.maxon.MaxonGame; +import kz.ilotterytea.maxon.anim.SpriteUtils; +import kz.ilotterytea.maxon.ui.AnimatedImage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Pet { + private final String id, name, description; + private final double price, multiplier; + private final AnimatedImage icon; + private static final Logger logger = LoggerFactory.getLogger(Pet.class); + + private Pet(String id, String name, String description, double price, double multiplier, AnimatedImage icon) { + this.id = id; + this.name = name; + this.description = description; + this.price = price; + this.multiplier = multiplier; + this.icon = icon; + } + + public static Pet create(String id, double price, double multiplier, int iconColumns, int iconRows) { + MaxonGame game = MaxonGame.getInstance(); + TextureRegion[] regions; + + try { + Texture texture = game.assetManager.get("sprites/pets/" + id + ".png", Texture.class); + regions = SpriteUtils.splitToTextureRegions( + texture, + texture.getWidth() / iconColumns, + texture.getHeight() / iconRows, + iconColumns, + iconRows + ); + + } catch (GdxRuntimeException e) { + logger.warn("Failed to load icon spritesheet for ID {}", id); + regions = new TextureRegion[]{new TextureRegion(MaxonConstants.MISSING_TEXTURE)}; + } + + AnimatedImage icon = new AnimatedImage(regions); + + String name = game.locale.TranslatableText("pet." + id + ".name"); + if (name == null) { + name = "pet." + id + ".name"; + } + + String description = game.locale.TranslatableText("pet." + id + ".desc"); + if (description == null) { + description = "pet." + id + ".desc"; + } + + return new Pet(id, name, description, price, multiplier, icon); + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public String getDescription() { + return description; + } + + public double getPrice() { + return price; + } + + public double getMultiplier() { + return multiplier; + } + + public AnimatedImage getIcon() { + return icon; + } +} diff --git a/core/src/kz/ilotterytea/maxon/pets/PetManager.java b/core/src/kz/ilotterytea/maxon/pets/PetManager.java new file mode 100644 index 0000000..4f8f228 --- /dev/null +++ b/core/src/kz/ilotterytea/maxon/pets/PetManager.java @@ -0,0 +1,52 @@ +package kz.ilotterytea.maxon.pets; + +import com.badlogic.gdx.assets.AssetManager; +import com.badlogic.gdx.utils.JsonReader; +import com.badlogic.gdx.utils.JsonValue; +import kz.ilotterytea.maxon.assets.loaders.Text; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashSet; +import java.util.Optional; + +public class PetManager { + private final HashSet<Pet> pets; + private final AssetManager assetManager; + private final Logger logger = LoggerFactory.getLogger(PetManager.class); + + public PetManager(final AssetManager assetManager) { + this.pets = new HashSet<>(); + this.assetManager = assetManager; + } + + public void load() { + pets.clear(); + + String data = assetManager.get("data/pets.json", Text.class).getString(); + JsonValue root = new JsonReader().parse(data); + + for (JsonValue child : root.iterator()) { + String id = child.getString("id"); + double price = child.getDouble("price"); + double multiplier = child.getDouble("multiplier"); + + JsonValue iconData = child.get("icon_data"); + int iconColumns = iconData.getInt("columns"); + int iconRows = iconData.getInt("rows"); + + Pet pet = Pet.create(id, price, multiplier, iconColumns, iconRows); + pets.add(pet); + } + + logger.info("Loaded {} pets", pets.size()); + } + + public Optional<Pet> getPet(String id) { + return pets.stream().filter(x -> x.getId().equals(id)).findFirst(); + } + + public HashSet<Pet> getPets() { + return pets; + } +} diff --git a/core/src/kz/ilotterytea/maxon/pets/PetWidget.java b/core/src/kz/ilotterytea/maxon/pets/PetWidget.java new file mode 100644 index 0000000..01a9dc0 --- /dev/null +++ b/core/src/kz/ilotterytea/maxon/pets/PetWidget.java @@ -0,0 +1,105 @@ +package kz.ilotterytea.maxon.pets; + +import com.badlogic.gdx.graphics.g2d.TextureAtlas; +import com.badlogic.gdx.scenes.scene2d.Actor; +import com.badlogic.gdx.scenes.scene2d.InputEvent; +import com.badlogic.gdx.scenes.scene2d.ui.*; +import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; +import com.badlogic.gdx.utils.Align; +import kz.ilotterytea.maxon.utils.formatters.NumberFormatter; + +public class PetWidget extends Table { + private double price; + private final Label priceLabel, nameLabel; + private final Pet pet; + + private boolean isDisabled = false; + + private final Label.LabelStyle idleStyle, hoverStyle, disabledStyle, availablePriceStyle, disabledPriceStyle; + + public PetWidget( + Skin skin, + Pet pet, + TextureAtlas atlas + ) { + super(skin); + super.setBackground("store_item"); + super.align(Align.left | Align.center); + this.pet = pet; + + super.add(pet.getIcon()).size(64f).pad(6f); + + this.idleStyle = skin.get("store_item", Label.LabelStyle.class); + this.hoverStyle = skin.get("store_item_hover", Label.LabelStyle.class); + this.disabledStyle = skin.get("store_item_disabled", Label.LabelStyle.class); + this.availablePriceStyle = skin.get("store_item_price", Label.LabelStyle.class); + this.disabledPriceStyle = skin.get("store_item_price_disabled", Label.LabelStyle.class); + + this.price = pet.getPrice(); + + Table summary = new Table(skin); + summary.align(Align.left); + + this.nameLabel = new Label(pet.getName(), skin, "store_item"); + nameLabel.setAlignment(Align.left); + + Image priceIcon = new Image(atlas.findRegion("points")); + + this.priceLabel = new Label(NumberFormatter.format((long) price), skin, "store_item_price"); + priceLabel.setAlignment(Align.left); + + summary.add(nameLabel).align(Align.left).grow().row(); + + Table priceTable = new Table(); + priceTable.align(Align.left); + priceTable.add(priceIcon).size(16f, 16f).padRight(5f); + priceTable.add(priceLabel).grow(); + summary.add(priceTable).grow(); + + super.add(summary).align(Align.left).grow(); + + super.addListener(new ClickListener() { + @Override + public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) { + super.enter(event, x, y, pointer, fromActor); + if (!isDisabled) { + nameLabel.setStyle(hoverStyle); + PetWidget.super.setBackground("store_item_hover"); + } + } + @Override + public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) { + super.exit(event, x, y, pointer, toActor); + if (!isDisabled) { + nameLabel.setStyle(idleStyle); + PetWidget.super.setBackground("store_item"); + } + } + }); + } + + public void setPrice(double price) { + this.price = price; + this.priceLabel.setText(NumberFormatter.format((long) price)); + } + + public double getPrice() { + return price; + } + + public boolean isDisabled() { + return isDisabled; + } + + public void setDisabled(boolean disabled) { + isDisabled = disabled; + + priceLabel.setStyle(isDisabled ? disabledPriceStyle : availablePriceStyle); + nameLabel.setStyle(isDisabled ? disabledStyle : idleStyle); + super.setBackground(isDisabled ? "store_item_disabled" : "store_item"); + } + + public Pet getPet() { + return pet; + } +} |
