Skip to content

Commit 2d463df

Browse files
committed
Inventory item stack test
1 parent 615be2c commit 2d463df

File tree

7 files changed

+71
-11
lines changed

7 files changed

+71
-11
lines changed

res/class/Constructor.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
/**
2+
* 方块构造器
3+
* @class
4+
*/
15
class BlockConstructor {
26
constructor() {}
37

8+
/**
9+
* 构造方块
10+
* @param {Block} block 方块对象
11+
* @returns {String} DOM
12+
*/
413
static getBlock(block) {
514
if (block.searched) {
615
if (block.type == 'air') {
@@ -77,9 +86,18 @@ class BlockConstructor {
7786
}
7887
}
7988

89+
/**
90+
* 地图构造器
91+
* @class
92+
*/
8093
class MapConstructor {
8194
constructor() {}
8295

96+
/**
97+
* 构造地图
98+
* @param {Stage} stage 楼层对象
99+
* @returns {String} DOM
100+
*/
83101
static getMap(stage) {
84102
let before = `<div id="map" style="--map-size: ${Math.max(stage.size.height, stage.size.width)}; --map-size-height: ${stage.size.height}; --map-size-width: ${stage.size.width};">`;
85103
let str = '';

res/class/LootTable.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,16 @@ class LootTable {
102102
}
103103

104104
weightedRandom(options) {
105-
var i;
105+
let i;
106106

107-
var weights = [];
107+
let weights = [];
108108

109109
for (i = 0; i < options.length; i++) {
110110
options[i].weight = options[i]?.weight != undefined ? options[i].weight : 1;
111111
weights[i] = options[i].weight + (weights[i - 1] || 0);
112112
}
113113

114-
var random = SMath.randomFloat(this.randomSeed) * weights[weights.length - 1];
114+
let random = SMath.randomFloat(this.randomSeed) * weights[weights.length - 1];
115115

116116
for (i = 0; i < weights.length; i++)
117117
if (weights[i] > random)

res/class/Player.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,50 @@ class Player {
163163
/**
164164
* 给予物品
165165
* @param {Item} item 物品
166-
* @returns 状态和数据
166+
* @returns {Object} 状态和数据
167167
*/
168168
give(item) {
169169
if (item instanceof Item == false) return { state: 'fail', failReason: 'item_invalid' };
170-
let index = this.inventory.push(item);
170+
let items = this.inventory.filter(function(e) {
171+
return e.id == item.id;
172+
});
173+
if (items.length <= 0) {
174+
this.inventory.push(item);
175+
} else {
176+
let joined = false;
177+
items.forEach(e => {
178+
let r = e.join(item);
179+
if (r != -1) {
180+
joined = true;
181+
return;
182+
}
183+
});
184+
if (!joined) {
185+
this.inventory.push(item);
186+
}
187+
}
171188
return {
172189
state: 'success',
173190
data: {
174191
item: item,
175-
inventoryIndex: index - 1
192+
}
193+
}
194+
}
195+
196+
/**
197+
* 替换物品
198+
* @param {Number} index 物品栏索引
199+
* @param {Item} item 物品
200+
* @returns {Object} 状态和数据
201+
*/
202+
replaceItem(index, item) {
203+
if (item instanceof Item == false) return { state: 'fail', failReason: 'item_invalid' };
204+
this.inventory[index] = item;
205+
return {
206+
state: 'success',
207+
data: {
208+
item: item,
209+
inventoryIndex: index
176210
}
177211
}
178212
}
@@ -181,7 +215,7 @@ class Player {
181215
* 切换快捷栏物品
182216
* @param {Number} solt 快捷栏槽位
183217
* @param {Number} index 物品栏索引
184-
* @returns 状态和数据
218+
* @returns {Object} 状态和数据
185219
*/
186220
switchHotbarItem(solt, index) {
187221
let item, hotbarItem;

res/data/items.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ let db_items = [
22
{
33
id: 'magnifier',
44
type: 'search'
5+
}, {
6+
id: 'emerald',
7+
type: 'item'
8+
}, {
9+
id: 'monster_crystal',
10+
type: 'item'
511
}, {
612
id: 'sword',
713
type: 'weapon',

res/img/item/item.png

523 Bytes
Loading

res/script/test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ w.create();
1212

1313
let p = new Player(w);
1414
w.playerJoin(p);
15-
let psi = p.give(new Item('magnifier')).data.inventoryIndex;
16-
let pwi = p.give(new Weapon('sword')).data.inventoryIndex;
17-
p.switchHotbarItem(1, pwi);
18-
p.switchHotbarItem(0, psi);
15+
let psi = p.replaceItem(0, new Item('magnifier'));
16+
let pwi = p.replaceItem(1, new Weapon('sword'));
17+
p.switchHotbarItem(1, 1);
18+
p.switchHotbarItem(0, 0);
1919

2020
// let ts_map = [
2121
// ['air', 'air', 'air', 'chest', 'monster'],

res/style/item.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
}
1818

1919
.item-icon[data-item-id='magnifier'] { --icon-pos-x: 0; --icon-pos-y: 0; }
20+
.item-icon[data-item-id='emerald'] { --icon-pos-x: -1; --icon-pos-y: 0; }
21+
.item-icon[data-item-id='monster_crystal'] { --icon-pos-x: -2; --icon-pos-y: 0; }
2022

2123
.item-icon[data-item-id='sword'] { --icon-pos-x: 0; --icon-pos-y: 0; }
2224
.item-icon[data-item-id='dagger'] { --icon-pos-x: -1; --icon-pos-y: 0; }

0 commit comments

Comments
 (0)