-
Notifications
You must be signed in to change notification settings - Fork 1
/
maze.js
162 lines (124 loc) · 5.01 KB
/
maze.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
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
const tileSize = 80;
const cobbleImage = new Image();
cobbleImage.src = "walls2.png";
const floorImage = new Image();
floorImage.src = "Floor.png";
const A = new Image();
A.src = "A.png";
const B = new Image();
B.src = "B.png";
const C = new Image();
C.src = "C.png";
const D = new Image();
D.src = "D.png";
const translatoeer = new Image();
translatoeer.src = "Translator.png";
function tileComponent(type, x, y, key) {
this.width = tileSize;
this.height = tileSize;
this.type = type;
this.image = new Image();
this.x = x;
this.y = y;
this.rand = 0;
this.key = key || null;
this.draw = function(camera) {
let cameraPositionX = camera.width / 2 + (this.x - camera.x);
let cameraPositionY = camera.height / 2 + (this.y - camera.y);
ctx = world.context;
if (this.type == "Floor.png") {
ctx.imageSmoothingQuality = "high";
ctx.drawImage(floorImage, cameraPositionX, cameraPositionY, this.width, this.height);
} else if (this.type == "walls2.png") {
ctx.imageSmoothingQuality = "high";
ctx.drawImage(cobbleImage, cameraPositionX, cameraPositionY, this.width, this.height);
} else if (this.type == "symbols") {
if (this.rand == 0) {
let r = Math.floor(Math.random() * 4) + 1;
this.rand = r;
}
if (!this.key.collected) {
if (this.rand == 1) {
ctx.drawImage(floorImage, cameraPositionX, cameraPositionY, this.width, this.height);
ctx.drawImage(A, cameraPositionX, cameraPositionY, this.width, this.height);
}
if (this.rand == 2) {
ctx.drawImage(floorImage, cameraPositionX, cameraPositionY, this.width, this.height);
ctx.drawImage(B, cameraPositionX, cameraPositionY, this.width, this.height);
}
if (this.rand == 3) {
ctx.drawImage(floorImage, cameraPositionX, cameraPositionY, this.width, this.height);
ctx.drawImage(C, cameraPositionX, cameraPositionY, this.width, this.height);
}
if (this.rand == 4) {
ctx.drawImage(floorImage, cameraPositionX, cameraPositionY, this.width, this.height);
ctx.drawImage(D, cameraPositionX, cameraPositionY, this.width, this.height);
}
}else {
ctx.drawImage(floorImage, cameraPositionX, cameraPositionY, this.width, this.height);
}
} else if (this.type == "Translator.png") {
ctx.drawImage(floorImage, cameraPositionX, cameraPositionY, this.width, this.height);
ctx.drawImage(translatoeer, cameraPositionX, cameraPositionY, this.width, this.height);
}
else {
ctx.drawImage(floorImage, cameraPositionX, cameraPositionY, this.width, this.height);
}
}
}
const tType = { "f": "Floor.png", "w": "walls2.png", "t": "Translator.png", "e": "Enemy.png", "s": "#FF00FF", "k": "symbols", "x": "#000000" };
function Maze(mazeData, rows, enemies, keys) {
this.data = mazeData;
this.rows = rows;
this.enemiesNum = enemies;
this.keys = keys;
this.first = true;
this.startPosX = 0;
this.startPosY = 0;
this.tileNum = rows * rows;
this.tileSet = new Array();
this.enemySet = new Array();
this.enemiesI = 0;
this.translator = null;
this.keyList = new Array();
this.keyIter = 0;
this.exit = null;
for (i = 0; i < this.tileNum; i++) {
let type = tType[this.data[i]];
var xPos = i % this.rows;
var yPos = Math.floor(i / this.rows);
if (tType[this.data[i]] == "#FF00FF") {
this.startPosX = xPos * tileSize + 5;
this.startPosY = yPos * tileSize + 5;
}
else if (this.data[i] == "e") {
this.enemySet[this.enemiesI] = new enemyComponent(xPos * tileSize, yPos * tileSize, 100, 100);
this.enemiesI++;
console.log(this.enemiesI)
}
else if (tType[this.data[i]] == "Translator.png") {
this.translator = new translator(xPos * tileSize, yPos * tileSize, this.keys);
}
else if (tType[this.data[i]] == "symbols") {
this.keyList[this.keyIter] = new key(xPos * tileSize, yPos * tileSize);
this.keyIter++;
}
else if (tType[this.data[i]] == "#000000") {
this.exit = new Exit(xPos * tileSize, yPos * tileSize);
}
this.tileSet[i] = new tileComponent(type, xPos * (tileSize - 1), yPos * tileSize, this.keyList[this.keyIter - 1]);
}
this.update = function(target) {
for (let i = 0; i < this.enemiesNum; i++) {
this.enemySet[i].update(target, this);
}
}
this.draw = function(camera) {
for (i = 0; i < this.tileNum; i++) {
this.tileSet[i].draw(camera);
}
for (let i = 0; i < this.enemiesNum; i++) {
this.enemySet[i].draw(camera);
}
}
}