-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalFunctions.js
252 lines (227 loc) · 6.64 KB
/
globalFunctions.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
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
const PLAYER = "player"
const UNBREAKABLE_BLOCK = "block-unbreakable_block";
const AIR = "block-air";
const ONE_WAY = "block-one-way";
const MOVE = "block-move";
const GOAL = "block_goal"
const SLIDE = "block-move-slide";
const BOMB = "block-bomb";
const BREAKABLE_BLOCK = "block-breakable-block";
const BOUNCE_ZONE_UP= "bounce_up";
const BOUNCE_ZONE_DOWN= "bounce_down";
const BOUNCE_ZONE_RIGHT= "bounce_right";
const BOUNCE_ZONE_LEFT= "bounce_left";
const GRAVATY_SIM_RIGHT= "block_gravity_simulator_right";
const GRAVATY_SIM_LEFT= "block_gravity_simulator_left";
const GRAVATY_SIM_UP= "block_gravity_simulator_up";
const GRAVATY_SIM_DOWN= "block_gravity_simulator_down";
const LOCK = "block_lock";
const KEY = "block_key";
const NO_PLAYER = "block-no-player";
const POWER = "block_power";
const DUPLICATOR = "duplicator";
var EXTRA_INFORMATION = {
player: {maxElements: 2},
block_goal: {maxElements: 1},
bounce_up: [1, -1],
bounce_down:[1, 1],
bounce_right:[0, 1],
bounce_left: [0, -1],
block_gravity_simulator_right: ["right"],
block_gravity_simulator_left: ["left"],
block_gravity_simulator_up: ["up"],
block_gravity_simulator_down: ["down"],
block_power: 4
}
const MOVEABLE_BLOCKS = [
MOVE,
KEY
]
//special blocks based on their movement
const SPECIAL_BLOCKS_MOVE = [
SLIDE,
BOMB,
BOUNCE_ZONE_DOWN,
BOUNCE_ZONE_LEFT,
BOUNCE_ZONE_RIGHT,
BOUNCE_ZONE_UP,
LOCK,
GOAL,
NO_PLAYER
]
var coordCellX = 0;
var coordCellY = 0;
/**
* Creates a table and model for the game
*
* -to change size change:
* --column
* --row
*
* -returns: null
*
*/function createTable(rows, columns, tableID, cellID) {
var body = document.getElementsByTagName("body")[0];
removeOldTable(tableID);
//creating the table and tbody
var table = document.createElement("table");
var tableBody = document.createElement("tbody");
for (x = 0; x < rows; x++) {
//creating the row
var row = document.createElement("tr")
for (y = 0; y < columns; y++) {
//creating cells and giving them their ID
var cell = document.createElement("td");
cell.id = (cellID + x + "_" + y);
cell.setAttribute("coord_x", x);
cell.setAttribute("coord_y", y);
var cellTxt = document.createTextNode("x");
//asign every Elemnt
cell.appendChild(cellTxt);
row.appendChild(cell);
};
tableBody.appendChild(row);
};
table.appendChild(tableBody);
body.appendChild(table);
table.id = (tableID)
document.getElementById(tableID).classList.add ("playingTable");
return table;
}
function removeOldTable (tableID) {
try {
document.body.removeChild(document.getElementById(tableID));
} catch (e) {
}
}
function modifyModel(modelName, rows, columns) {
modelProt = [[]]
for (y = 0; y < columns; y++) {
modelProt[0][y] = UNBREAKABLE_BLOCK
};
for (x = 0; x < rows; x++) {
modelProt[x] = modelProt[0].slice(0, modelProt[0].length);
};
return modelProt;
}
/**
* You visuelise the model in the table
*
* -input - model data
* -returns: null
*/function updateTable(modelName ,cellID) {
const CONNECTING_TEXTURES = [
"block-unbreakable_block",
]
updateGravatyFields(modelName);
for (x = 0; x < modelName.length; x++) {
for (y = 0; y < modelName[x].length; y++) {
if(CONNECTING_TEXTURES.includes(modelName[x][y])){
connectTextures (modelName ,x, y, cellID)
} else{
insertImages(modelName[x][y], x, y, cellID, modelName)
}
}
}
}
function updateGravatyFields(modelName){
for (x = 0; x < modelName.length; x++) {
for (y = 0; y < modelName[x].length; y++) {
if (sessionStorage.getItem("bounceBackup_"+ x + "_" +y)){
var fieldData = JSON.parse(sessionStorage.getItem("bounceBackup_"+ x +"_"+y));
if (model[fieldData.coordX][fieldData.coordY] == AIR){
var fieldDataBackup = JSON.stringify(fieldData.backup)
model[fieldData.coordX][fieldData.coordY] = fieldData.backup
sessionStorage.removeItem("bounceBackup_"+ x +"_"+y);
}
}
}
}
}
function insertImages (imgData, x, y, cellID, modelName) {
var cell = document.getElementById(cellID+x+ "_" +y);
var img = cell.childNodes[0];
if(img == undefined || img.nodeType != Node.ELEMENT_NODE) {
img = document.createElement("img");
cell.textContent = "";
cell.appendChild(img);
}
var src = "Textures/" +imgData + ".png";
if(img.getAttribute("src") != src) {
img.src = src;
}
}
function connectTextures (modelName ,x, y, cellID) {
var connectData = "";
for (j = 0; j <= 2 ; j = j + 2) {
try {
if (modelName[x -1 + j][y] == modelName[x][y]) {
connectData = connectData + "1"
}
else{
connectData = connectData + "0"
}
} catch {
connectData = connectData + "0"
}
try {
if (modelName[x][y -1 + j] == modelName[x][y]) {
connectData = connectData + "1"
}
else {
connectData = connectData + "0"
}
} catch {
connectData = connectData + "0"
}
}
var imgData = modelName[x][y] + connectData
insertImages(imgData, x, y, cellID);
}
/**
* retunrns
*
*
*/
function tableCellOnclickDet(table) {
var cells = table.querySelectorAll("td");
for (i = 0; i < cells.length; i++) {
cells[i].onclick = cellDetection(cells[i]);
}
}
function cellDetection (cell) {
return function(){
coordCellX = Number(cell.getAttribute("coord_x"));
coordCellY = Number(cell.getAttribute("coord_y"));
};
}
function temporySaveLevelData(model){
const createdLevelData = JSON.stringify({sizeX:model.length, sizeY:model[0].length, model:model, levelName: undefined})
sessionStorage.removeItem("temporyLevelData");
sessionStorage.setItem("temporyLevelData",createdLevelData);
}
function createPlayingTemplate(levelData, tableID, cellID){
createTable(levelData.sizeX, levelData.sizeY, tableID, cellID);
return levelData.model;
}
/**
* loops over the model and calls the function by params row, column
*
**/function loopOverModel(model, func){
for (let row = 0; row < model.length; row++){
for(let column = 0; column < model[row].length; column++){
func(row, column, model)
}
}
}
function generateId(length){
function getRandLetter() {
const LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789012345678901234567890123456789';
return LETTERS.charAt(Math.floor(Math.random() * LETTERS.length));
}
let result = ""
for (let i = 0; i < length; i++){
result += getRandLetter()
}
return result
}