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

›指南

指南

  • 簡介
  • 創造你的層塔
  • 增加塔層
  • 定義技能
  • 定義單位
  • 重構
  • 測試
  • 發佈

製作者 API

  • 空間 API
  • 單位 API
Translate

定義單位

單位(unit) 是一個JavaScript物件:

const WhiteWalker = {
  // Unit definition.
};

讓我們先開始為這單位添加名字:

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

我們沒有為勇士添加名字,因為這名字是由玩家在遊戲開始時添加的。

就像勇士一樣,其他單位都需要一個字符及最高體力值:

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

讓我們定義一個新的攻擊技能:

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

將這技能添加給異鬼(White Walker),也讓我們添加之前已定義的感應技能(feel ability):

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

最後,我們需要定義異鬼的人工智能(AI), 這人工技能只需要很基本:異鬼在每回合開始時會感應每個方向尋找敵人(勇士), 如果他在任何方向找到敵人,就會向著那個方向攻擊。 讓我們在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);
    }
  },
};

我們同樣沒有為勇士寫上人工智能,因為玩家會在遊戲中慢慢的補上。

現在我們需要將異鬼添加到第二層,勇士外的單位都會添加到那層的units陣列:

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

在這裡,我們使用了擴充屬性(spread properties) 合併單位定義及他在樓層的位置。

恭喜你!你已經創造了你的第一座塔!這個時候的層塔已經可以玩了,但它仍可使用一些重構。

← 定義技能重構 →
WarriorJS Docs
Docs
PlayerMaker
Community
SpectrumTwitterFollow WarriorJS on Twitter
More
DonateGitHubStar