-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap_Level_1.cpp
More file actions
331 lines (301 loc) · 11 KB
/
Copy pathMap_Level_1.cpp
File metadata and controls
331 lines (301 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "Map_Level_1.h"
Map_Level_1::Map_Level_1()
{
//初始化地图元素
level_1_Scene = new QGraphicsScene;
filePath = "../Map/level_1.txt";
map_1 = InitByFile(filePath);
map_data = map_1; // 将地图数据同时存储到基类的map_data中
level_1_Scene->addPixmap(QPixmap(":/back/img/background/leve_1_back.jpg"));
int tileSize = 50;
for(int row = 0;row<map_1.size();++row)
{
for(int col = 0;col<map_1[row].size();++col)
{
QGraphicsRectItem *tile = new QGraphicsRectItem(col * tileSize, row * tileSize, tileSize, tileSize);
switch (map_1[row][col])
{
case bedrock:
{
QPixmap pixmap(":/level_1/img/block/bedrock.jpg");
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
pixmapItem->setPos(col*tileSize,row*tileSize);
level_1_Scene->addItem(pixmapItem);
break;
}
case Road:
{
QPixmap pixmap(" ");
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
pixmapItem->setPos(col*tileSize,row*tileSize);
level_1_Scene->addItem(pixmapItem);
break;
}
case soil:
{
QPixmap pixmap(":/level_1/img/block/soil.png");
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
pixmapItem->setPos(col*tileSize,row*tileSize);
level_1_Scene->addItem(pixmapItem);
break;
}
case grass:
{
QPixmap pixmap(":/level_1/img/block/grass.jpg");
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
pixmapItem->setPos(col*tileSize,row*tileSize);
level_1_Scene->addItem(pixmapItem);
break;
}
case Cobblestone:
{
QPixmap pixmap(":/level_1/img/block/Cobblestone.png");
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
pixmapItem->setPos(col*tileSize,row*tileSize);
level_1_Scene->addItem(pixmapItem);
break;
}
case Log:
{
QPixmap pixmap(":/level_1/img/block/Log.png");
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
pixmapItem->setPos(col*tileSize,row*tileSize);
level_1_Scene->addItem(pixmapItem);
break;
}
case leaf:
{
QPixmap pixmap(":/level_1/img/block/leaf.png");
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
pixmapItem->setPos(col*tileSize,row*tileSize);
level_1_Scene->addItem(pixmapItem);
break;
}
case Plank:
{
QPixmap pixmap(":/level_1/img/block/Planks.png");
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
pixmapItem->setPos(col*tileSize,row*tileSize);
level_1_Scene->addItem(pixmapItem);
break;
}
case TransmitBlock:
{
QPixmap pixmap(":/level_1/img/block/TransmitBlock.png");
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
pixmapItem->setPos(col*tileSize,row*tileSize);
level_1_Scene->addItem(pixmapItem);
break;
}
case box:
{
QPixmap pixmap(":/level_1/img/block/box.png");
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
pixmapItem->setPos(col*tileSize,row*tileSize);
level_1_Scene->addItem(pixmapItem);
break;
}
}
}
}
m_Steve.setX(300);
m_Steve.setY(950);
level_1_Scene->addItem(&m_Steve);
m_Steve.setMap(this); // 设置Steve的地图指针
m_villager_fake.setX(800);
m_villager_fake.setY(950);
m_villager_fake.setPosition(800,950);
m_villager_fake.setSteve(&m_Steve);
level_1_Scene->addItem(&m_villager_fake);
// 添加血量UI到场景
if (m_Steve.getHealthUI())
{
level_1_Scene->addItem(m_Steve.getHealthUI());
m_Steve.getHealthUI()->setPos(20, 20); // 设置血量UI的位置
}
}
Map_Level_1::~Map_Level_1()
{
delete level_1_Scene;
}
bool Map_Level_1::isRoad(int x, int y) const
{
int col = x / 50; // assuming tileSize is 50
int row = y / 50;
if (row >= 0 && row < map_1.size() && col >= 0 && col < map_1[row].size())
{
return ( map_1[row][col] == 1||map_1[row][col] ==9||map_1[row][col] ==12||map_1[row][col] ==23); // 修改为检查 map_1[row][col] 是否为 路,传送门,梯子
}
return false;
}
bool Map_Level_1::isLadder(int x, int y) const
{
int col = x / 50; // assuming tileSize is 50
int row = y / 50;
if (row >= 0 && row < map_1.size() && col >= 0 && col < map_1[row].size())
{
return map_1[row][col] == 12; // 修改为检查 map_1[row][col] 是否为 梯子
}
return false;
}
bool Map_Level_1::isTransmit(int x, int y) const
{
int col = x / 50; // assuming tileSize is 50
int row = y / 50;
if (row >= 0 && row < map_1.size() && col >= 0 && col < map_1[row].size()) {
return map_1[row][col] == 9; // 修改为检查 map_1[row][col] 是否为 9
}
return false;
}
bool Map_Level_1::isTransmitBack(int x, int y) const
{
int col = x / 50; // assuming tileSize is 50
int row = y / 50;
if (row >= 0 && row < map_1.size() && col >= 0 && col < map_1[row].size()) {
return map_1[row][col] == 15; // 修改为检查 map_1[row][col] 是否为 15
}
return false;
}
bool Map_Level_1::isTrigger(int x, int y) const
{
int col = x / 50;
int row = y / 50;
if (row >= 0 && row < map_1.size() && col >= 0 && col < map_1[row].size()) {
return map_1[row][col] == Trigger;
}
return false;
}
void Map_Level_1::setTrigger(int x, int y)
{
int col = x / 50;
int row = y / 50;
if (row >= 0 && row < map_1.size() && col >= 0 && col < map_1[row].size())
{
// 先检查该位置是否已经有Trigger
if (map_1[row][col] != Trigger) {
map_1[row][col] = Trigger;
// 添加Trigger图片
QPixmap pixmap(":/level_1/img/block/Trigger.png");
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
pixmapItem->setPos(col * 50, row * 50);
pixmapItem->setData(0, "trigger"); // 设置标识符
level_1_Scene->addItem(pixmapItem);
}
}
}
void Map_Level_1::removeTrigger(int x, int y)
{
//空实现
}
bool Map_Level_1::isInteractive(int x, int y) const
{
int col = x / 50; // assuming tileSize is 50
int row = y / 50;
if (row >= 0 && row < map_1.size() && col >= 0 && col < map_1[row].size()) {
// 检查是否为可交互物品(箱子等)
return map_1[row][col] == box;
}
return false;
}
bool Map_Level_1::isBox(int x, int y) const
{
int col = x / 50;
int row = y / 50;
if (row >= 0 && row < map_1.size() && col >= 0 && col < map_1[row].size()) {
return map_1[row][col] == box;
}
return false;
}
bool Map_Level_1::isBoxOpened(int x, int y) const
{
int col = x / 50;
int row = y / 50;
if (row >= 0 && row < map_1.size() && col >= 0 && col < map_1[row].size()) {
return map_1[row][col] == box_open;
}
return false;
}
void Map_Level_1::changeBoxState(int x, int y)
{
int col = x / 50;
int row = y / 50;
if (row >= 0 && row < map_1.size() && col >= 0 && col < map_1[row].size() && map_1[row][col] == box) {
// 将箱子状态改为已打开
map_1[row][col] = box_open;
// 更新箱子图片为打开状态
QPixmap openedBoxPixmap(":/level_1/img/block/box_open.png");
QGraphicsPixmapItem* openedBoxItem = new QGraphicsPixmapItem(openedBoxPixmap);
openedBoxItem->setPos(col * 50, row * 50);
level_1_Scene->addItem(openedBoxItem);
}
}
int Map_Level_1::getBoxId(int x, int y) const
{
int col = x / 50;
int row = y / 50;
QPointF boxPos(col * 50, row * 50);
// 检查是否为箱子3的位置
if (QLineF(boxPos, QPointF(300, 700)).length() < 25) {
return 3;
}
// 检查是否为箱子4的位置
else if (QLineF(boxPos, QPointF(1500, 500)).length() < 25) {
return 4;
}
return 0; // 不是已知的箱子位置
}
void Map_Level_1::handlePlayerInteraction()
{
// 获取玩家位置
QPointF playerPos = m_Steve.pos();
// 检查玩家是否靠近箱子3
QPointF chest1Pos = QPointF(300, 700);
if (QLineF(playerPos, chest1Pos).length() < 50) // 50是交互距离
{
m_Steve.interactWithChest(3);
}
// 检查玩家是否靠近箱子4
QPointF chest2Pos = QPointF(1500, 500);
if (QLineF(playerPos, chest2Pos).length() < 50)
{
m_Steve.interactWithChest(4);
}
}
void Map_Level_1::updateBlock(int x, int y, int newBlockType)
{
// 添加边界检查
if (x < 0 || x >= 40 || y < 0 || y >= 26)
return;
// 检查是否是Cobblestone,如果是则更新为Road
if (map_1[y][x] == Cobblestone && newBlockType == Road)
{
// 更新地图数据
map_1[y][x] = Road;
// 移除原有的Cobblestone图片项
QList<QGraphicsItem*> itemsAtPos = level_1_Scene->items(QPointF(x * 50, y * 50));
for (QGraphicsItem* item : itemsAtPos) {
if (dynamic_cast<QGraphicsPixmapItem*>(item)) {
level_1_Scene->removeItem(item);
delete item;
break;
}
}
// 创建一个新的Road图像项
QGraphicsPixmapItem* roadItem = new QGraphicsPixmapItem(QPixmap(""));
roadItem->setPos(x * 50, y * 50);
// 添加到场景
level_1_Scene->addItem(roadItem);
}
// 检查是否是Cobblestone,如果是则更新为Road
if (map_1[y][x] == Cobblestone && newBlockType == Road)
{
qDebug() << "TNT爆炸更新地图块:" << x << "," << y << "从Cobblestone到Road";
// 更新地图数据
map_1[y][x] = Road;
// 创建一个新的Road图像项
QGraphicsPixmapItem* roadItem = new QGraphicsPixmapItem(QPixmap(" "));
roadItem->setPos(x * 50, y * 50);
// 添加到场景
level_1_Scene->addItem(roadItem);
}
}