Constructionは直訳すると「工事」ですね。一体何を工事するのでしょうか?日本語にしてみましょう。
チュートリアル全記事は以下になります。
- Loop and import(ループとインポート)
- Simple move(単純な移動)
- First attack(初めての攻撃)
- Creeps bodies(クリープの各パーツ)
- Store and transfer(エネルギーのストアと転送)
- Terrain(地形)
- Spawn creeps(クリープを生成)
- Harvest energy(エネルギーの採取)
- Construction(工事・建築)
- Final test(最終試験)
Construction ー工事ー
クリープをスポーン(生む・生成)するだけでなく、構造(著者注:タワーなどの建造物)を構築することもできます! (構築を)達成するには、いくらかのエネルギーと労働クリープ(著者注:WORK
)が必要です。
まず、createConstructionSite()
メソッドを使用して、ConstructionSite
というオブジェクトをゲーム内(著者注:マップ内)のどこかに配置します。建てたい建造物の座標とプロトタイプを指定する必要があります。
import { createConstructionSite } from '/game/utils';
import { StructureTower } from '/game/prototypes';
export function loop() {
var constructionSite = createConstructionSite({x: 50, y: 55}, StructureTower);
}
クリープ(エネルギーを積んでいる必要あり)は建設現場に近づいて建造物をbuild
(建築)しなければなりません:
creep.build(constructionSite);
ビルドアクションが成功するたびに、クリープのstore
からエネルギーが消費されるため、エネルギー供給と建設現場の間を行ったり来たりする必要があります。 完了すると新しい建築物が表示されます。
目的:タワーを構築します。
サンプルコード
import { prototypes, utils } from '/game';
import { RESOURCE_ENERGY, ERR_NOT_IN_RANGE } from '/game/constants';
export function loop() {
const creep = utils.getObjectsByPrototype(prototypes.Creep).find(i => i.my);
if(!creep.store[RESOURCE_ENERGY]) {
const container = utils.findClosestByPath(creep, utils.getObjectsByPrototype(prototypes.StructureContainer));
if(creep.withdraw(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(container);
}
} else {
const constructionSite = utils.getObjectsByPrototype(prototypes.ConstructionSite).find(i => i.my);
if(!constructionSite) {
utils.createConstructionSite(50,55, prototypes.StructureTower);
} else {
if(creep.build(constructionSite) == ERR_NOT_IN_RANGE) {
creep.moveTo(constructionSite);
}
}
}
}
まずはcreep.store[RESOURCE_ENERGY]
でエネルギーがストア(蓄え)されているかどうが判別し、無ければcontainer
(コンテナ)に近づいてwithdraw()
していますね。エネルギーを保有している場合は、createConstructionSite
でタワー(StructureTower
)を建てて、build()
する流れです。
ConstructionSite
の有無によってもcreateConstructionSite()
とbuild()
の動作を分けている点もポイントみたいです。build()
は有効範囲内でないといけないので、moveTo()
でクリープを造っているタワーに近づけているのも注意です。