summaryrefslogtreecommitdiff
path: root/core/src/kz/ilotterytea/maxon/pets/Pet.java
blob: 7428cf37da17acc64f3b7d091f2952ddd96ec853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package kz.ilotterytea.maxon.pets;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g3d.decals.Decal;
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.localization.LineId;
import kz.ilotterytea.maxon.ui.AnimatedImage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class Pet {
    private final String id, name, description;
    private final double price, multiplier;
    private final AnimatedImage icon;
    private final Decal decal;
    private static final Logger logger = LoggerFactory.getLogger(Pet.class);

    private Pet(String id, String name, String description, double price, double multiplier, AnimatedImage icon, Decal decal) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.price = price;
        this.multiplier = multiplier;
        this.icon = icon;
        this.decal = decal;
    }

    public static Pet create(String id, double price, double multiplier, int iconColumns, int iconRows) {
        MaxonGame game = MaxonGame.getInstance();
        ArrayList<TextureRegion> regions;

        try {
            Texture texture = game.assetManager.get("sprites/pets/" + id + ".png", Texture.class);
            regions = SpriteUtils.splitToTextureRegions(
                    texture,
                    texture.getWidth() / iconColumns,
                    texture.getHeight() / iconRows
            );

        } catch (GdxRuntimeException e) {
            logger.warn("Failed to load icon spritesheet for ID {}", id);
            regions = new ArrayList<>(List.of(new TextureRegion(MaxonConstants.MISSING_TEXTURE)));
        }

        AnimatedImage icon = new AnimatedImage(regions, 5);

        String name = game.getLocale().getLine(LineId.fromJson("pet." + id + ".name"));
        if (name == null) {
            name = "pet." + id + ".name";
        }

        String description = game.getLocale().getLine(LineId.fromJson("pet." + id + ".desc"));
        if (description == null) {
            description = "pet." + id + ".desc";
        }

        Decal decal = Decal.newDecal(0.5f, 0.5f, regions.get(0), true);

        return new Pet(id, name, description, price, multiplier, icon, decal);
    }

    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;
    }

    public Decal getDecal() {
        return decal;
    }
}