-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.js
128 lines (111 loc) · 2.82 KB
/
map.js
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
const Dungeon = require('random-dungeon-generator')
const rn = require('random-number');
const Enemy = require('./enemies');
const settings = {
width: 28,
height: 24,
minRoomSize: 2,
maxRoomSize: 6
}
const heightGen = rn.generator({min: 0, max: settings.height - 1, integer: true});
const widthGen = rn.generator({min:0, max: settings.width - 1, integer: true});
const itemGen = rn.generator({min: 8, max: 12, integer: true})
var level = 1;
module.exports = class {
static getLevel() {
return level;
}
static setLevel(lvl){
level = lvl
}
static generateDungeon() {
var dungeon = normaliseDungeon(Dungeon.NewDungeon(settings));
return populateDungeon(dungeon)
}
static parseDungeon(dungeon, client){
console.log("Parsing")
var dungeonMessages = [];
var dungeonMessage = "";
var count = 0;
const subway = client.emojis.find(emoji => emoji.name === "subway")
dungeon.forEach(row => {
row.forEach(cell => {
switch (cell) {
case 9:
dungeonMessage += "🏃";
break;
case 3:
dungeonMessage += "🍎";
break;
case 2:
dungeonMessage += subway;
break;
case 1:
dungeonMessage += "⬛";
break;
case 0:
dungeonMessage += "🔳";
break;
default:
if (cell instanceof Enemy)
dungeonMessage += cell.emoji;
}
});
if (count == 3) {
dungeonMessages.push(dungeonMessage);
dungeonMessage = "";
count = 0;
}
else {
count++;
dungeonMessage += "\n";
}
});
console.log("Finished Parsing")
return dungeonMessages;
}
}
// turn all room numbers into 0s
function normaliseDungeon(dungeon){
console.log("Normalising")
for (y = 0; y < dungeon.length; ++y) {
for (x = 0; x < dungeon[y].length; ++x) {
switch (dungeon[y][x]) {
case 1:
break;
default:
dungeon[y][x] = 0;
break;
}
}
}
console.log("Finished Normalising")
return dungeon
}
function populateDungeon(dungeon){
console.log("Populating")
var numberOfPointBoosts = itemGen()
var numberOfEnemies = itemGen() + 1
// Subway
dungeon = runUntilPopulate(dungeon, 2);
for (var i = 0; i < numberOfPointBoosts; ++i){
dungeon = runUntilPopulate(dungeon, 3);
}
for (var i = 0; i < numberOfEnemies; ++i){
dungeon = runUntilPopulate(dungeon, new Enemy(1 + (level-1)/5));
}
// Player
dungeon = runUntilPopulate(dungeon, 9);
console.log("Finished Populating");
return dungeon;
}
function runUntilPopulate(dungeon, value){
while (true){
var y = heightGen();
var x = widthGen();
if (dungeon[y][x] === 0){
dungeon[y][x] = value;
return dungeon
}
}
}