summaryrefslogtreecommitdiff
path: root/resources/js/classes/Trainer.js
blob: d63e52d5645dda39ba64a54bb4cfa82af1431a65 (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
class Trainer {
  #monsters = [];
  #inventory = [];

  /**
   * @type {('trainer' | 'monster')}
   */
  type = 'trainer';

  name = '';
  sprite = '';

  /**
   * @type {Monster[]}
   */
  monsters = [];

  /**
   * @type {InventoryItem[]}
   */
  inventory = [];

  /**
   * @type {Monster}
   */
  activeMonster = [];

  /**
   * @param {(Monster[]|Object[])} monsters
   * @param {(InventoryItem[]|Object[])} inventory
   */
  constructor (trainerData) {
    this.#monsters = trainerData.monsters;
    this.#inventory = trainerData.inventory || [];
    this.name = trainerData.name;
    this.sprite = trainerData.sprite;
  }

  async initialize () {
    for (const monsterData of this.#monsters) {
      if (monsterData instanceof Monster) {
        this.monsters.push(monsterData);
      } else {
        const monster = await fetchMonster(monsterData.slug);

        monster.level = monsterData.level || monster.level;

        this.monsters.push(monster);
      }
    }

    this.activeMonster = this.monsters[0];

    for (const itemData of this.#inventory) {
      if (itemData instanceof InventoryItem) {
        this.inventory.push(itemData);
      } else {
        const item = new InventoryItem(await fetchItem(itemData.slug));

        item.quantity = itemData.quantity || 1;
        this.inventory.push(item);
      }
    }
  }
}