ytgame-object-system 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/gameobj.js +36 -0
- package/index.js +3 -0
- package/item.js +38 -0
- package/package.json +13 -0
- package/test.js +20 -0
package/gameobj.js
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
// gameobj.js - 基于ES6类的游戏对象系统
|
2
|
+
|
3
|
+
// 父类:游戏对象
|
4
|
+
export class GameObject {
|
5
|
+
constructor(name) {
|
6
|
+
this.name = name;
|
7
|
+
}
|
8
|
+
|
9
|
+
move() {
|
10
|
+
console.log(`${this.name}进行了移动`);
|
11
|
+
}
|
12
|
+
}
|
13
|
+
|
14
|
+
// 子类:NPC
|
15
|
+
export class Npc extends GameObject {
|
16
|
+
constructor() {
|
17
|
+
super('李四'); // 复写name属性
|
18
|
+
this.title = '村长';
|
19
|
+
}
|
20
|
+
|
21
|
+
talk() {
|
22
|
+
console.log(`${this.title}头衔的${this.name}进行了说话`);
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
// 子类:敌人
|
27
|
+
export class Enemy extends GameObject {
|
28
|
+
constructor() {
|
29
|
+
super('王五'); // 复写name属性
|
30
|
+
this.hp = 100;
|
31
|
+
}
|
32
|
+
|
33
|
+
attack() {
|
34
|
+
console.log(`${this.hp}血量的${this.name}进行了攻击`);
|
35
|
+
}
|
36
|
+
}
|
package/index.js
ADDED
package/item.js
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
// item.js - 基于构造函数和原型链的物品系统
|
2
|
+
|
3
|
+
// 父类:基础物品
|
4
|
+
export function BaseItem(name) {
|
5
|
+
this.name = name;
|
6
|
+
}
|
7
|
+
|
8
|
+
BaseItem.prototype.discard = function() {
|
9
|
+
console.log(`${this.name}被丢弃了`);
|
10
|
+
};
|
11
|
+
|
12
|
+
// 子类:消耗物品
|
13
|
+
export function ConsumableItem() {
|
14
|
+
BaseItem.call(this, '消耗物品'); // 继承name属性并覆盖
|
15
|
+
this.consume = 1;
|
16
|
+
}
|
17
|
+
|
18
|
+
// 继承原型链
|
19
|
+
ConsumableItem.prototype = Object.create(BaseItem.prototype);
|
20
|
+
ConsumableItem.prototype.constructor = ConsumableItem;
|
21
|
+
|
22
|
+
ConsumableItem.prototype.use = function() {
|
23
|
+
console.log(`使用了${this.consume}个:${this.name}`);
|
24
|
+
};
|
25
|
+
|
26
|
+
// 子类:装备
|
27
|
+
export function Equipment() {
|
28
|
+
BaseItem.call(this, '装备'); // 继承name属性并覆盖
|
29
|
+
this.durability = 80;
|
30
|
+
}
|
31
|
+
|
32
|
+
// 继承原型链
|
33
|
+
Equipment.prototype = Object.create(BaseItem.prototype);
|
34
|
+
Equipment.prototype.constructor = Equipment;
|
35
|
+
|
36
|
+
Equipment.prototype.equip = function() {
|
37
|
+
console.log(`装备了${this.name},这件装备还有:${this.durability}点耐久`);
|
38
|
+
};
|
package/package.json
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
{
|
2
|
+
"name": "ytgame-object-system",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "游戏对象与物品系统",
|
5
|
+
"main": "index.js",
|
6
|
+
"scripts": {
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
8
|
+
},
|
9
|
+
"author": "Your Name",
|
10
|
+
"license": "MIT",
|
11
|
+
"keywords": ["game", "objects", "inheritance"],
|
12
|
+
"files": ["*.js"]
|
13
|
+
}
|
package/test.js
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
// test.js - 测试代码
|
2
|
+
const { GameObject, Npc, Enemy, BaseItem, ConsumableItem, Equipment } = require('game-object-system');
|
3
|
+
|
4
|
+
// 测试ES6类的游戏对象
|
5
|
+
const npc = new Npc();
|
6
|
+
npc.move(); // 调用父类方法
|
7
|
+
npc.talk(); // 调用子类方法
|
8
|
+
|
9
|
+
const enemy = new Enemy();
|
10
|
+
enemy.move(); // 调用父类方法
|
11
|
+
enemy.attack();// 调用子类方法
|
12
|
+
|
13
|
+
// 测试构造函数的物品系统
|
14
|
+
const consumable = new ConsumableItem();
|
15
|
+
consumable.discard(); // 调用父类方法
|
16
|
+
consumable.use(); // 调用子类方法
|
17
|
+
|
18
|
+
const equipment = new Equipment();
|
19
|
+
equipment.discard(); // 调用父类方法
|
20
|
+
equipment.equip(); // 调用子类方法
|