my-game-classes-dilunce 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.
@@ -0,0 +1,40 @@
1
+ // 父类: gameobj
2
+ class GameObj {
3
+ constructor(name) {
4
+ this.name = name;
5
+ }
6
+
7
+ move() {
8
+ console.log(`${this.name} 进行了移动`);
9
+ }
10
+ }
11
+
12
+ // NPC 子类
13
+ class NPC extends GameObj {
14
+ constructor(id, name) {
15
+ super(name); // 调用父类构造器
16
+ this.id = id;
17
+ this.tittle = "资深村民"; // 随意初始化头衔
18
+ this.name = name; // 复写name属性为自己的名字
19
+ }
20
+
21
+ talk() {
22
+ console.log(`${this.tittle}头衔的${this.name} 进行了说话`);
23
+ }
24
+ }
25
+
26
+ // Enemy 子类
27
+ class Enemy extends GameObj {
28
+ constructor(id, name) {
29
+ super(name);
30
+ this.id = id;
31
+ this.hp = 100; // 随意初始化血量
32
+ this.name = name; // 复写name属性为自己的名字
33
+ }
34
+
35
+ attack() {
36
+ console.log(`${this.hp}血量的${this.name} 进行了攻击`);
37
+ }
38
+ }
39
+
40
+ module.exports = { GameObj, NPC, Enemy };
@@ -0,0 +1,45 @@
1
+ // 父类: 基础物品 (构造函数写法)
2
+ function BaseItem(name) {
3
+ this.name = name;
4
+ }
5
+
6
+ // 使用原型链添加丢弃方法
7
+ BaseItem.prototype.discard = function() {
8
+ console.log(`${this.name} 被丢弃了`);
9
+ };
10
+
11
+ // 子类: 消耗物品类
12
+ function ConsumableItem() {
13
+ // 使用 call 继承父类的 name 属性
14
+ BaseItem.call(this, "消耗物品");
15
+ this.consume = 5; // 一次消耗数量,随意给值
16
+ this.name = "消耗物品"; // 覆盖name属性
17
+ }
18
+
19
+ // 继承父类的方法
20
+ ConsumableItem.prototype = Object.create(BaseItem.prototype);
21
+ ConsumableItem.prototype.constructor = ConsumableItem;
22
+
23
+ // 新增自己的方法
24
+ ConsumableItem.prototype.use = function() {
25
+ console.log(`使用了 ${this.consume} 个: ${this.name}`);
26
+ };
27
+
28
+ // 子类: 装备类
29
+ function EquipmentItem() {
30
+ // 使用 call 继承父类的 name 属性
31
+ BaseItem.call(this, "装备");
32
+ this.naijiu = 40; // 耐久度,随意给值
33
+ this.name = "装备"; // 覆盖name属性
34
+ }
35
+
36
+ // 继承父类的方法
37
+ EquipmentItem.prototype = Object.create(BaseItem.prototype);
38
+ EquipmentItem.prototype.constructor = EquipmentItem;
39
+
40
+ // 新增自己的方法
41
+ EquipmentItem.prototype.equip = function() {
42
+ console.log(`装备了 ${this.name},这件装备还有: ${this.naijiu} 点耐久`);
43
+ };
44
+
45
+ module.exports = { BaseItem, ConsumableItem, EquipmentItem };
package/index.js ADDED
@@ -0,0 +1,16 @@
1
+ // 导入 1 和 2 的类
2
+ const { GameObj, NPC, Enemy } = require('./classes/gameobj');
3
+ const { BaseItem, ConsumableItem, EquipmentItem } = require('./classes/item');
4
+
5
+ // 导出总共 4 个类
6
+ module.exports = {
7
+ // 从 gameobj.js 导出
8
+ GameObj,
9
+ NPC,
10
+ Enemy,
11
+ // 从 item.js 导出
12
+ BaseItem,
13
+ ConsumableItem,
14
+ EquipmentItem
15
+ };
16
+ //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
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "my-game-classes-dilunce",
3
+ "version": "1.0.0",
4
+ "description": "一个游戏角色和物品的类库,包含NPC、敌人、消耗品和装备类",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test.js"
8
+ },
9
+ "keywords": ["game", "classes", "npc", "enemy", "item"],
10
+ "author": "Your Name",
11
+ "license": "ISC"
12
+ }
package/test.js ADDED
@@ -0,0 +1,21 @@
1
+ const { NPC, Enemy, ConsumableItem, EquipmentItem } = require('./index');
2
+
3
+ console.log("========== 测试 NPC 类 ==========");
4
+ const npc = new NPC(1, "张三");
5
+ npc.move(); // 父类方法
6
+ npc.talk(); // 子类特有方法
7
+
8
+ console.log("\n========== 测试 Enemy 类 ==========");
9
+ const enemy = new Enemy(2, "李四");
10
+ enemy.move(); // 父类方法
11
+ enemy.attack(); // 子类特有方法
12
+
13
+ console.log("\n========== 测试 ConsumableItem 类 ==========");
14
+ const potion = new ConsumableItem();
15
+ potion.discard(); // 父类方法
16
+ potion.use(); // 子类特有方法
17
+
18
+ console.log("\n========== 测试 EquipmentItem 类 ==========");
19
+ const sword = new EquipmentItem();
20
+ sword.discard(); // 父类方法
21
+ sword.equip(); // 子类特有方法