定義技能
技能(ability) 是一個JavaScript函數,當單位具有作為唯一參數的技能,這函數能接收它並傳回一個JavaScript物件:
function walk(unit) {
return {
// Ability definition.
};
}
步行技能 (walk) 是一個行動,先讓我們指出這個特性:
function walk(unit) {
return {
action: true,
};
}
然後我們需要寫上這個技能的描述,所以玩家能夠知道它的功能:
function walk(unit) {
return {
action: true,
description: 'Move one space in the given direction (forward by default).',
};
}
來到最後但同樣重要的步驟,我們需要在perform
函數寫上這技能的邏輯推理, Here, we can use any of the methods in the 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 ability)添加到勇士的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,
},
},
},
};
在第二層,我們需要再添加兩個技能:攻擊(attack) 及感應(feel)。
首先,讓我們定義攻擊技能:
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`);
}
},
};
}
然後,讓我們定義感應技能,與攻擊相反的是,感應是一個感官技(sense),所以我們可以略去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()
。 The former returns a space that exposes only the Space Player API, whereas the latter exposes the Space Maker API and is meant to be used internally, like in the attack ability before.
最後,讓我們添加這些技能給第二層的勇士:
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,
},
},
},
};
我們不需要加添加步行技能給第二層的勇士,因為他已經在第一層學會了這技能。
非常好!但勇士仍未有機會揮動他的瓦雷利亞鋼劍,就讓我們添加一名敵人給他戰鬥吧!