cloud-ch5-game-lib 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.
Binary file
package/gameobj.js ADDED
@@ -0,0 +1,37 @@
1
+ // 1. 使用class语法糖定义游戏对象
2
+
3
+ class GameObj {
4
+ constructor(name) {
5
+ this.name = name;
6
+ }
7
+
8
+ move() {
9
+ console.log(`${this.name}进行了移动`);
10
+ }
11
+ }
12
+
13
+ class NPC extends GameObj {
14
+ constructor(name, title) {
15
+ super(name);
16
+ this.name = 'NPC-' + name;
17
+ this.title = title || '平民';
18
+ }
19
+
20
+ talk() {
21
+ console.log(`${this.title}的${this.name}进行了说话`);
22
+ }
23
+ }
24
+
25
+ class Enemy extends GameObj {
26
+ constructor(name, hp) {
27
+ super(name);
28
+ this.name = 'Enemy-' + name;
29
+ this.hp = hp || 100;
30
+ }
31
+
32
+ attack() {
33
+ console.log(`${this.hp}血量的${this.name}进行了攻击`);
34
+ }
35
+ }
36
+
37
+ module.exports = { NPC, Enemy };
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ // 4. 统一导出
2
+ const { NPC, Enemy } = require('./gameobj');
3
+ const { 消耗物品类, 装备类 } = require('./item');
4
+
5
+ module.exports = { NPC, Enemy, 消耗物品类, 装备类 };
package/item.js ADDED
@@ -0,0 +1,37 @@
1
+ // 2. 使用构造函数+原型链定义物品类
2
+
3
+ function 基础物品(name) {
4
+ this.name = name;
5
+ }
6
+
7
+ 基础物品.prototype.丢弃 = function () {
8
+ console.log(`${this.name}被丢弃了`);
9
+ };
10
+
11
+ function 消耗物品类(name, consume) {
12
+ 基础物品.call(this, name);
13
+ this.name = '消耗物品';
14
+ this.consume = consume || 1;
15
+ }
16
+
17
+ 消耗物品类.prototype = Object.create(基础物品.prototype);
18
+ 消耗物品类.prototype.constructor = 消耗物品类;
19
+
20
+ 消耗物品类.prototype.使用 = function () {
21
+ console.log(`使用了${this.consume}个:${this.name}`);
22
+ };
23
+
24
+ function 装备类(name, naijiu) {
25
+ 基础物品.call(this, name);
26
+ this.name = '装备';
27
+ this.naijiu = naijiu || 50;
28
+ }
29
+
30
+ 装备类.prototype = Object.create(基础物品.prototype);
31
+ 装备类.prototype.constructor = 装备类;
32
+
33
+ 装备类.prototype.装备 = function () {
34
+ console.log(`装备了${this.name},这件装备还有:${this.naijiu}点耐久`);
35
+ };
36
+
37
+ module.exports = { 消耗物品类, 装备类 };
package/package.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "cloud-ch5-game-lib",
3
+ "version": "1.0.0",
4
+ "description": "游戏对象类库 - 第五章作业",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test.js"
8
+ },
9
+ "license": "MIT"
10
+ }
package/test.js ADDED
@@ -0,0 +1,22 @@
1
+ // 6. 测试脚本
2
+ const { NPC, Enemy, 消耗物品类, 装备类 } = require('./index');
3
+
4
+ console.log('=== NPC ===');
5
+ var a = new NPC('张三', '商人');
6
+ a.move();
7
+ a.talk();
8
+
9
+ console.log('\n=== Enemy ===');
10
+ var b = new Enemy('哥布林', 80);
11
+ b.move();
12
+ b.attack();
13
+
14
+ console.log('\n=== 消耗物品类 ===');
15
+ var c = new 消耗物品类('药水', 3);
16
+ c.丢弃();
17
+ c.使用();
18
+
19
+ console.log('\n=== 装备类 ===');
20
+ var d = new 装备类('铁剑', 60);
21
+ d.丢弃();
22
+ d.装备();