game-oop-package-zxw 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.
Files changed (4) hide show
  1. package/gameobj.js +44 -0
  2. package/index.js +11 -0
  3. package/item.js +46 -0
  4. package/package.json +13 -0
package/gameobj.js ADDED
@@ -0,0 +1,44 @@
1
+ // 父类:游戏对象
2
+ class GameObj {
3
+ constructor(name) {
4
+ this.name = name;
5
+ }
6
+
7
+ // 父方法:移动
8
+ move() {
9
+ console.log(`${this.name} 进行了移动`);
10
+ }
11
+ }
12
+
13
+ // 子类:NPC
14
+ class Npc extends GameObj {
15
+ constructor() {
16
+ // 复写name
17
+ super("村民");
18
+ // 新增属性:头衔
19
+ this.tittle = "新手向导";
20
+ }
21
+
22
+ // 新增方法:说话
23
+ talk() {
24
+ console.log(`${this.tittle}的${this.name}进行了说话`);
25
+ }
26
+ }
27
+
28
+ // 子类:Enemy
29
+ class Enemy extends GameObj {
30
+ constructor() {
31
+ // 复写name
32
+ super("哥布林");
33
+ // 新增属性:血量
34
+ this.hp = 100;
35
+ }
36
+
37
+ // 新增方法:攻击
38
+ attack() {
39
+ console.log(`血量${this.hp}的${this.name}进行了攻击`);
40
+ }
41
+ }
42
+
43
+ // 导出
44
+ module.exports = { GameObj, Npc, Enemy };
package/index.js ADDED
@@ -0,0 +1,11 @@
1
+ // 导入
2
+ const { Npc, Enemy } = require("./gameObj");
3
+ const { ConsumableItem, EquipmentItem } = require("./item");
4
+
5
+ // 导出 4 个类(满足题目要求)
6
+ module.exports = {
7
+ Npc,
8
+ Enemy,
9
+ ConsumableItem,
10
+ EquipmentItem,
11
+ };
package/item.js ADDED
@@ -0,0 +1,46 @@
1
+ // 父类:基础物品(构造函数)
2
+ function BaseItem(name) {
3
+ this.name = name;
4
+ }
5
+
6
+ // 原型方法:丢弃
7
+ BaseItem.prototype.drop = function () {
8
+ console.log(`${this.name} 被丢弃了`);
9
+ };
10
+
11
+ // 子类:消耗品
12
+ function ConsumableItem() {
13
+ // 继承并覆盖name
14
+ BaseItem.call(this, "消耗物品");
15
+ // 新增属性
16
+ this.consume = 5;
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
+ // 继承并覆盖name
31
+ BaseItem.call(this, "装备");
32
+ // 新增属性:耐久度
33
+ this.naijiu = 50;
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
+ // 导出
46
+ module.exports = { BaseItem, ConsumableItem, EquipmentItem };
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "game-oop-package-zxw",
3
+ "version": "1.0.0",
4
+ "description": "一个基于面向对象的游戏对象npm包,包含Npc、Enemy、Consumeltem、Equipltem类",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [],
10
+ "author": "你的名字",
11
+ "license": "ISC",
12
+ "type": "commonjs"
13
+ }