-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
401 lines (356 loc) · 13.1 KB
/
index.html
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<!DOCTYPE html>
<html lang="zh_cn">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文字游戏</title>
<link href="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js"></script>
<script src="lib/jquery/js/jquery-3.6.3.min.js"></script>
</head>
<body>
<link rel="stylesheet" href="./css/common.css">
<!-- 游戏暂停的模态框 -->
<div class="modal" id="game-stopped-modal">
<div class="modal-dialog">
<div class="modal-content">
<!-- 模态框内容 -->
<div class="modal-body text-center">
<h1>游戏已暂停</h1>
<p class="turn-device">请旋转你的设备,使用横屏进行游戏</p>
<div class="d-grid">
<button type="button" id="game-continue" class="btn btn-primary btn-block"
data-bs-dismiss="modal">继续游戏</button>
</div>
</div>
</div>
</div>
</div>
<!-- 游戏结束的模态框 -->
<div class="modal fade" id="gameover-modal">
<div class="modal-dialog">
<div class="modal-content">
<!-- 模态框内容 -->
<div class="modal-body text-center">
<h1>游戏结束!</h1>
<p id="gaveover-reason"></p>
<p id="game-score"></p>
<div class="d-grid">
<button type="button" class="btn btn-primary btn-block" onclick="window.respawn();"
data-bs-dismiss="modal">原地复活</button>
<button type="button" class="btn btn-danger btn-block mt-4"
onclick="location.reload();">重新开始</button>
</div>
</div>
</div>
</div>
</div>
<div id="player-control-container">
<div class="player-container-center"></div>
<ul id="game-Inventory" class="d-inline-flex"></ul>
<div id="game-healthbar"></div>
<div id="direction-buttons" class="control-btns">
<button id="upBtn" class="contron-btn direction-btn"></button>
<button id="downBtn" class="contron-btn direction-btn"></button>
<button id="leftBtn" class="contron-btn direction-btn"></button>
<button id="rightBtn" class="contron-btn direction-btn"></button>
</div>
<button id="doBtn" class="contron-btn">💣</button><br>
</div>
<!-- 画布 -->
<div id="map" class="text-center"></div>
<!-- 操作 -->
<!--
----------------→y
|
|
|
|
|
|
↓x -->
<!-- <script src="js/tick.js"></script>
<script src="js/map.js"></script>
<script src="js/boom.js"></script>
<script src="js/DrawMap.js"></script>
<script src="js/mob.js"></script>
<script src="js/ecosystem.js"></script>
<script src="js/player.js"></script> -->
<script src="./js/perlin.js"></script>
<script type="module">
import { Ticker } from "./js/tick.js";
import { ChunkLoadManager } from "./js/chunkLoadManager.js";
import { EntityLive } from "./js/entity.js";
import { Map } from "./js/map.js";
import { Boom } from "./js/boom.js";
import { MapDrawer } from "./js/DrawMap.js";
import { SummonMob } from "./js/mob.js";
import { BlockUpdateDetector } from "./js/BUD.js";
import { Player, PlayerController } from "./js/player.js";
// !注意!这里必须闭包,因为Map类和BootStrap5中的Map类有冲突!
//定义地图
const colorDict = {
"草": "green",
"水": "blue",
"沙": "khaki",
"桥": "orange",
"人": "red",
"怪": "red",
"土": "Sienna",
"炸": "black"
};
window.Boom = Boom;
const inventory = [
{
name: "💣",
do: function () {
if (this.game.map.getEntitiesAt(this.game.centerX, this.game.centerY)
.some(entity => entity.x === this.x && entity.y === this.y && entity.type === "boom")) {
return;
}
const boom = new Boom({
x: this.x,
y: this.y,
power: 2,
game: this.game,
});
boom.setup();
this.game.drawMap();
}
},
{
name: "草",
do: function () {
playerSetBlock(this.x, this.y, "草");
}
},
{
name: "桥",
do: function () {
playerSetBlock(this.x, this.y, "桥");
}
},
{
name: "土",
do: function () {
playerSetBlock(this.x, this.y, "土");
}
},
{
name: "水",
do: function () {
playerSetBlock(this.x, this.y, "水");
}
}
];
function playerSetBlock(x, y, name) {
game.map.setBlock(x, y, { name });
game.drawMap();
game.bud.queue.push(
{ x: x, y: y, delay: 0, name: game.map.getBlock(x, y).name },
{ x: x + 1, y: y, delay: 0, name: game.map.getBlock(x + 1, y).name },
{ x: x - 1, y: y, delay: 0, name: game.map.getBlock(x - 1, y).name },
{ x: x, y: y + 1, delay: 0, name: game.map.getBlock(x, y + 1).name },
{ x: x, y: y - 1, delay: 0, name: game.map.getBlock(x, y - 1).name },
);
game.drawMap();
}
const passable = ["草", "水", "沙", "桥", "土", "怪", "人"];
function gameOver() {
ticker.stop();
// 取消暂停游戏事件
window.onblur = null;
ticker.stop();
// 写入游戏结束的原因
switch (player.hurtedBy.type) {
case "boom":
$('#gaveover-reason').html(`你 爆炸了!`);
break;
case "mob":
$('#gaveover-reason').html(`被怪物殴打致死 <del>欧拉欧拉欧拉</del>`);
break;
}
// 屏幕渐变成黑白,动画时间为1s
$('#gameover-modal').modal({
keyboard: false,
backdrop: 'static'
}).modal("show");
}
/**
* 计算屏幕字符容纳量
* @returns {Object} {width: Number, height: Number}
*/
function calcCharCapacity() {
// 方法为:向#map内添加不可见字符直到换行,然后计算换行后的字符数
// 然后再不断添加<p>,直到超出屏幕,计算<p>的个数
// 最后返回换行前的字符数*<p>的个数
const $map = $("#map");
const $p = $("<p></p>");
const $span = $("<span>水</span>");
$map.empty();
$map.append($p.append($span));
let height = 0;
while ($map.height() < document.documentElement.clientHeight) {
$map.append($p.clone());
height++;
}
height = height % 2 === 0 ? height - 1 : height;
// 取span.getBoundingClientRect();的width属性
const spanWidth = $span[0].getBoundingClientRect().width;
let width = Math.floor($p.text("a".repeat(100)).width() / spanWidth);
width = width % 2 === 0 ? width - 1 : width;
$map.empty();
return {
width,
height
}
}
window.onresize = function () {
const { width, height } = calcCharCapacity();
game.mapDrawer.outputXLength = height;
game.mapDrawer.outputYLength = width;
game.drawMap();
}
// 申请全屏横屏
$(document).ready(() => {
// 生成一个按钮,点击后全屏
const $fullscreenBtn = $("#game-continue");
$fullscreenBtn.click(() => {
const element = document.documentElement;
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen();
}
});
// 如果现在在竖屏,暂停游戏
gameStop();
})
function respawn() {
// 重生
player.health = 20;
player.map.drawMap();
}
window.respawn = respawn;
var game = {
config: {
forceLoadRadius: 3, // 强加载区块半径
weakLoadRadius: 4, // 弱加载区块半径
},
centerX: 0,
centerY: 0,
stopped: false,
// map: map,
// ticker: ticker,
chunkLoaders: [], // 区块加载器所在的区块坐标{chunkX: Number, chunkY: Number}
activeEntities: [], // 活跃实体列表
players: [], // 玩家列表
gameOver: gameOver,
respawn: respawn,
};
var map = new Map({
seed: "random seed",
passable: passable,
});
game.map = map;
var ticker = new Ticker({
updateMapFunc: game.map.drawMap,
})
ticker.start();
game.ticker = ticker;
new ChunkLoadManager({
game: game,
}).setup();
const mapDrawer = new MapDrawer({
mapElement: "#map",
colorDict: colorDict,
game: game,
outputXLength: calcCharCapacity().height,
outputYLength: calcCharCapacity().width,
});
game.mapDrawer = mapDrawer;
game.drawMap = () => game.mapDrawer.drawMap(game.centerX, game.centerY);
window.game = game;
game.drawMap();
ticker.updateMapFunc = game.drawMap;
new EntityLive({
game: game,
}).setup();
var bud = new BlockUpdateDetector({
game: game,
});
bud.setup();
game.bud = bud;
new SummonMob({
game: game,
}).setup();
// new Eco({
// map: map,
// ticker: ticker,
// }).setup();
var player = new Player({
x: 0,
y: 0,
game: game,
speed: 5,
});
player.setup();
window.player = player;
game.players.push(player);
var playerController = new PlayerController({
player: player,
game: game,
});
player.health = 20;
// 画地图
// mapDrawer.drawMap(map);
// 向#game-inventory中添加物品
inventory.forEach((item) => {
const li = document.createElement("li");
li.innerText = item.name;
// 按照map.colorDict中的颜色,给每个方块上色
li.style.color = colorDict[item.name];
li.onclick = function () {
const active = document.querySelector("#game-Inventory li.active");
if (active) {
active.classList.remove("active");
}
this.classList.add("active");
player.inventory = item;
// 替换player的do方法
player.do = function () {
this.inventory.do.call(this);
}
// 设置doBtn的文字
document.querySelector("#doBtn").innerText = item.name;
}
document.querySelector("#game-Inventory").appendChild(li);
});
// 点击第一个物品
document.querySelector("#game-Inventory li").click();
// 游戏暂停的function
function gameStop() {
ticker.stop();
game.stopped = true;
// 唤起模态框
$('#game-stopped-modal').modal({
keyboard: false,
backdrop: 'static'
}).modal('show');
$('#game-stopped-modal').on('hidden.bs.modal', function (e) {
if (game.stopped) {
ticker.start();
game.stopped = false;
}
})
}
// 在当前标签页失焦时,暂停游戏
window.onblur = gameStop;
</script>
</body>
</html>