WarriorJS Docs
  • Player
  • Maker
  • Komunita
  • Čeština
    • English
    • العربية
    • Català
    • Deutsch
    • Ελληνικά
    • Español
    • Français
    • Italiano
    • Polskie
    • Русский
    • Српски језик (Ћирилица)
    • Svenska
    • Türkçe
    • 中文
    • 繁體中文
    • Pomoc s překladem
  • GitHub

›Průvodce

Průvodce

  • Úvod
  • Tvorba vlastní věže
  • Přidávání úrovní
  • Definování schopností
  • Definování jednotek
  • Refactoring
  • Testování
  • Publikování

API tvůrce

  • API pole
  • API jednotky
Translate

Definování jednotek

Jednotka je JavaScript object:

const WhiteWalker = {
  // Unit definition.
};

Pojďme začít přidáním jména jednotky:

const WhiteWalker = {
  name: 'White Walker',
};

Válečníkovi jsme jméno nedávali, protože je dodáno hráčem během hry.

Stejně jako válečník potřebují ostatní jednotky character a maximum zdraví:

const WhiteWalker = {
  name: 'White Walker',
  character: 'w',
  maxHealth: 12,
};

Pojďme definovat novou attack ability:

function iceCrystalSwordAttack(unit) {
  return {
    action: true,
    description:
      'Attack a unit in the given direction (forward by default) with your ice blade, dealing 3 HP of damage.',
    perform(direction = 'forward') {
      const receiver = unit.getSpaceAt(direction).getUnit();
      if (receiver) {
        unit.log(`attacks ${direction} and hits ${receiver}`);
        unit.damage(receiver, 3);
      } else {
        unit.log(`attacks ${direction} and hits nothing`);
      }
    },
  };
}

A přidejme ji k White Walkerovi. Přidejme také stejnou feel ability, kterou už jsme definovali:

const WhiteWalker = {
  name: 'White Walker',
  character: 'w',
  maxHealth: 12,
  abilities: {
    attack: iceCrystalSwordAttack,
    feel: feel,
  },
};

Nakonec pojďme přidat AI našeho White Walkera. Bude to velice základní AI: White Walker začne svůj tah cítěním každého směru, aby našel nepřítele (válečníka). Pokud ho najde v nějakém směru, v tomto směru zaútočí. Pojďme sepsat tuto logiku do funkce playTurn:

const WhiteWalker = {
  name: 'White Walker',
  character: 'w',
  maxHealth: 12,
  abilities: {
    attack: iceCrystalSwordAttack,
    feel: feel,
  },
  playTurn(whiteWalker) {
    const enemyDirection = ['forward', 'right', 'backward', 'left'].find(
      direction => {
        const unit = whiteWalker.feel(direction).getUnit();
        return unit && unit.isEnemy();
      },
    );
    if (enemyDirection) {
      whiteWalker.attack(enemyDirection);
    }
  },
};

Válečníkovi jsme žádnou AI nesepisovali, protože je také dodána hráčem během hry.

Nyní musíme White Walkera přidat do druhého patra. Jednotky jiné než válečník jsou přidány do units hrací plochy:

const Level2 = {
  description:
    'The cold became more intense. In the distance, you see a pair of deep and blue eyes, a blue that burns like ice.',
  tip:
    "Use `warrior.feel().isEmpty()` to see if there's anything in front of you, and `warrior.attack()` to fight it. Remember, you can only do one action per turn.",
  clue:
    'Add an if/else condition using `warrior.feel().isEmpty()` to decide whether to attack or walk.',
  timeBonus: 20,
  aceScore: 26,
  floor: {
    size: {
      width: 8,
      height: 1,
    },
    stairs: {
      x: 7,
      y: 0,
    },
    warrior: {
      character: '@',
      maxHealth: 20,
      position: {
        x: 0,
        y: 0,
        facing: 'east',
      },
      abilities: {
        attack: valyrianSteelSwordAttack,
        feel: feel,
      },
    },
    units: [
      {
        ...WhiteWalker,
        position: {
          x: 4,
          y: 0,
          facing: 'west',
        },
      },
    ],
  },
};

Tady jsme použili spread properties, abychom sloučili definici jednotky s jejím umístěním na hrací ploše.

Gratulace! Vytvořil jsi svou první věž. Věž je teď plně hratelná, ale možná by zasloužila trochu refactoringu.

← Definování schopnostíRefactoring →
WarriorJS Docs
Docs
PlayerMaker
Community
SpectrumTwitterFollow WarriorJS on Twitter
More
DonateGitHubStar