game-classes-tky
Version:
游戏角色和物品的JavaScript类库(NPC、敌人、装备等)
38 lines (32 loc) • 815 B
JavaScript
// 父类 GameObj
class GameObj {
constructor(name) {
this.name = name;
}
move() {
console.log(`${this.name}进行了移动`);
}
}
// NPC 子类
class Npc extends GameObj {
constructor(name, title) {
super(name);
this.title = title;
this.name = "友好的" + name; // 复写name属性
}
talk() {
console.log(`${this.title}头衔的${this.name}进行了说话`);
}
}
// Enemy 子类
class Enemy extends GameObj {
constructor(name, hp) {
super(name);
this.hp = hp;
this.name = "危险的" + name; // 复写name属性
}
attack() {
console.log(`${this.hp}血量的${this.name}进行了攻击`);
}
}
module.exports = { GameObj, Npc, Enemy };