summaryrefslogtreecommitdiff
path: root/core/src/kz/ilotterytea/maxon/pets/PetManager.java
blob: bcb354e128f7da3f158ef57fda92b2362c95683c (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
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.*;

public class PetManager {
    private final ArrayList<Pet> pets;
    private final AssetManager assetManager;
    private final Logger logger = LoggerFactory.getLogger(PetManager.class);

    public PetManager(final AssetManager assetManager) {
        this.pets = new ArrayList<>();
        this.assetManager = assetManager;
    }

    public void load() {
        pets.clear();

        String data = assetManager.get("data/pets.json", Text.class).getString();
        JsonValue root = new JsonReader().parse(data);

        List<Pet> pets = new ArrayList<>();

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

        Collections.sort(pets, (pet1, pet2) -> Double.compare(pet1.getPrice(), pet2.getPrice()));

        this.pets.addAll(pets);
        logger.info("Loaded {} pets", pets.size());
    }

    public Pet getPet(String id) {
        for (Pet pet : pets) {
            if (pet.getId().equals(id)) {
                return pet;
            }
        }
        return null;
    }


    public ArrayList<Pet> getPets() {
        return pets;
    }
}