WarriorJS Docs
  • Играч
  • Аутор
  • Заједница
  • Српски језик (Ћирилица)
    • English
    • العربية
    • Català
    • Čeština
    • Deutsch
    • Ελληνικά
    • Español
    • Français
    • Italiano
    • Polskie
    • Русский
    • Svenska
    • Türkçe
    • 中文
    • 繁體中文
    • Помозите у превођењу
  • GitHub

›Водич

Водич

  • Увод
  • Направите своју кулу
  • Додавање нивоа
  • Дефинисање способности
  • Дефинисање јединица
  • Реструктурирање кода
  • Провера
  • Објављивање

API Аутора

  • Space API
  • Unit API
Translate

Дефинисање способности

Способност је JavaScript функција која прима јединицу која садржи способност као једини параметар и враћа је у JavaScript објекат:

function walk(unit) {
  return {
    // Ability definition.
  };
}

Способност ходања је акција, па прво морамо да укажемо на то:

function walk(unit) {
  return {
    action: true,
  };
}

Затим, морамо да опишемо способност да би играч знао шта она ради:

function walk(unit) {
  return {
    action: true,
    description: 'Move one space in the given direction (forward by default).',
  };
}

И на крају треба да напишемо логику способности у функцији perform. Овде можемо да користимо било који метод из Unit Maker API. Хајде да то и урадимо:

function walk(unit) {
  return {
    action: true,
    description: 'Move one space in the given direction (forward by default).',
    perform(direction = 'forward') {
      const space = unit.getSpaceAt(direction);
      if (space.isEmpty()) {
        unit.move(direction);
        unit.log(`walks ${direction}`);
      } else {
        unit.log(`walks ${direction} and bumps into ${space}`);
      }
    },
  };
}

Способности се додају јединицама са командама у објекту abilities. Сада ћемо ратнику додати способност ходања са командом walk (желимо да играч зада команду позивањем warrior.walk()):

const Level1 = {
  description:
    "You've entered the ancient castle of Eastwatch to escape from a blizzard. But it's deadly cold inside too.",
  tip:
    "Call `warrior.walk()` to walk forward in the Player's `playTurn` method.",
  timeBonus: 15,
  aceScore: 10,
  floor: {
    size: {
      width: 8,
      height: 1,
    },
    stairs: {
      x: 7,
      y: 0,
    },
    warrior: {
      character: '@',
      maxHealth: 20,
      position: {
        x: 0,
        y: 0,
        facing: 'east',
      },
      abilities: {
        walk: walk,
      },
    },
  },
};

За други ниво потребне су нам још две способности: напад и осећај.

Прво ћемо дефинисати способност напада:

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

Затим, дефинишемо способност осећаја. Насупрот нападу осећај је чуло, па можемо изоставити кључ action:

function feel(unit) {
  return {
    description:
      'Return the adjacent space in the given direction (forward by default).',
    perform(direction = 'forward') {
      return unit.getSensedSpaceAt(direction);
    },
  };
}

ВАЖНО: Када враћате једно или више поља помоћу чула, користите unit.getSensedSpaceAt() уместо unit.getSpaceAt(). Прва функција враћа поље које открива само Space Player API док друга открива Space Maker API и замишљена је да се користи интерно, као код способности напада раније.

Коначно, додаћемо ове способности Ратнику на другом нивоу:

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

Сада нећемо додати способност ходања у другом нивоу, јер је Ратник већ у првом нивоу научио ову способност.

Одлично. Пошто је Ратник са разлогом учио да рукује мачем од Валиријског челика. Сада ћемо додати непријатеље са којима може да бори!

← Додавање нивоаДефинисање јединица →
WarriorJS Docs
Docs
PlayerMaker
Community
SpectrumTwitterFollow WarriorJS on Twitter
More
DonateGitHubStar