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

›提示&技巧

游戏

  • 概述
  • 目的
  • 游戏玩法
  • 视角
  • 得分
  • 史诗模式
  • Towers

概念

  • 单位
  • 勇士
  • 技能
  • 空格

玩家 API

  • 空间 API
  • 单元 API
  • 转变 API

提示&技巧

  • 一般
  • JavaScript
  • 人工智能
  • 命令行界面

命令行界面

  • Install
  • Options
Translate

JavaScript 提示

  • 不要简单地用大量代码填充 playTurn 方法, **使用方法和类 ** 来组织代码。例如:
class Player {
  playTurn(warrior) {
    if (this.isInjured(warrior)) {
      warrior.rest();
    }
  }

  isInjured(warrior) {
    return warrior.health() < 20;
  }
}
  • 如果希望在每个级别的开头执行一些代码,则在Player中 定义一个 constructor 类,如下:
class Player {
  constructor() {
    // This code will be executed only once, at the beginning of the level.
    this.health = 20;
  }

  // ...
}
  • 你可以直接在一个感知动作(sense)后调用空间API的方法。 For example, the "feel" sense in the "Baby Steps" tower returns one space. You can call isEmpty() on this to determine if the space is clear before walking there:
class Player {
  playTurn(warrior) {
    if (warrior.feel().isEmpty()) {
      warrior.walk();
    }
  }
}
  • Some senses (like "look" and "listen" in the "Baby Steps" tower) return an array of spaces instead, so you might find many of the Array prototype methods really useful. Here is an example of the Array.prototype.find method:
class Player {
  // ...

  isEnemyInSight(warrior) {
    const spaceWithUnit = warrior.look().find(space => space.isUnit());
    return spaceWithUnit && spaceWithUnit.getUnit().isEnemy();
  }
}
← 一般提示AI Tips →
WarriorJS Docs
Docs
PlayerMaker
Community
SpectrumTwitterFollow WarriorJS on Twitter
More
DonateGitHubStar