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

›Guida

Guida

  • Introduzione
  • Creare la tua torre
  • Aggiungere livelli
  • Definire le abilità
  • Definendo le Unità
  • Refactoring
  • Test
  • Pubblicare

API creatore

  • API relative allo spazio
  • API dell'unità
Translate

Definire le abilità

Un'abilità è una funzione JavaScript che riceve l'unità che possiede l'abilità come unico parametro e restituisce un oggetto JavaScript:

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

L'abilità camminare è un'azione, quindi prima di tutto indichiamo questo:

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

Poi, dobbiamo scrivere una descrizione per l'abilità in modo che il giocatore sappia cosa fa:

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

Come ultima cosa, dobbiamo scrivere la logica dell'abilità all'interno della funzione perform. Qui, possiamo utilizzare qualsiasi metodo presente nella Unit Maker API. Facciamolo:

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

Le abilità vengono aggiunte alle unità sotto una chiave presente nell'oggetto abilities. Andiamo ad aggiungere al Guerriero l'abilità di camminare sotto la chiave walk (perché vogliamo che il giocatore richiami quest'abilità utilizzando 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,
      },
    },
  },
};

Per il secondo livello, dobbiamo aggiungere altre due abilità: attaccare e percepire.

Per prima cosa, andiamo a definire l'abilità attacco:

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

Poi, andiamo a definire l'abilità di percepire. Al contrario dell'attaccare, percepire è uno dei sensi, quindi possiamo anche omettere la chiave action:

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

IMPORTANTE: quando i sensi vi ritornano uno o molteplici spazi, utilizzare unit.getSensedSpaceAt() al posto di unit.getSpaceAt(). Il primo restituisce uno spazio che espone solamente le Space Player API, mentre il secondo espone le Space Maker API ed è fatto per essere usato internamente, come l'abilità attaccare di prima.

Infine, aggiungiamoli al Guerriero del secondo livello:

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

Nel secondo livello non aggiungeremo nuovamente l'abilità cammina poiché il Guerriero ha già imparato quest'abilità nel primo livello.

Tutto questo è molto bello, ma il nostro Guerriero non porta con se una spada forgiata con l'acciaio di Valyria senza alcun motivo. Aggiungiamo un nemico da combattere!

← Aggiungere livelliDefinendo le Unità →
WarriorJS Docs
Docs
PlayerMaker
Community
SpectrumTwitterFollow WarriorJS on Twitter
More
DonateGitHubStar