-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveLogik.js
More file actions
354 lines (328 loc) · 14.6 KB
/
MoveLogik.js
File metadata and controls
354 lines (328 loc) · 14.6 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
const ANIMATION_END = {
cloneRemove: function(clone){
document.body.removeChild(clone);
afterAnimationCompleted();
},
cloneRemoveUpdate : function(clone){
document.body.removeChild(clone);
afterAnimationCompleted();
updateTable(model, "tblId_");
}
}
//needs "globalFunctions.js"
function playerMove (verizontale, dierection, model) {
var playerCords = elementDetect(PLAYER, model);
var returnValue = {succes: false, model:model}
for (z = 0; z < playerCords.customLength; z++){
returnValue.succes = true;
returnValue["player_"+ z] = moveElement(playerCords["cellCoord_" + z].x, playerCords["cellCoord_" + z].y , verizontale, dierection, false, true, model)
}
return returnValue;
}
/**
* returns Cords of a element
*/function elementDetect (cell, model) {
var i = 0
var cellCords = {customLength:0,};
for (x = 0; x < model.length; x++){
for (y = 0; y < model[0].length; y++){
if (model[x][y] == cell) {
cellCords.customLength = cellCords.customLength + 1;
cellCords["cellCoord_"+i] = {x:x,y:y};
i++;
}
}
}
return cellCords;
}
/**
* Moves the Element forward
* -x y cordinates
* -dierection -1/1 left/right or up/down
* -verzizontale 0 or 1 horicantale or verticale
* -slide if slide-block true
*
* returns true/false if function worked properly/failed
*/function moveElement(x, y, verizontale, dierection, slide, player, model) {
if (verizontale == 0) {
if (moveCheck(x, y, verizontale, dierection, slide, player, model)) {
//checks if it has to move two or only one Blocks
if (model[x][y + dierection] != AIR) {
//with this code 2 it moves 2 blocks
model[x][y + dierection + dierection] = model[x][y + dierection];
if (!slide) linearAnimation({x:x,y:y+dierection}, {x:x, y:y+dierection+dierection}, "tblId_", 0, 10, 1, ANIMATION_END.cloneRemove,false)
}
model[x][y + dierection] = model[x][y];
model[x][y] = AIR;
if (!slide) linearAnimation({x:x,y:y}, {x:x, y:y+dierection}, "tblId_", 0, 10, 1, ANIMATION_END.cloneRemoveUpdate,true)
//to give the Information where the Block was moved
var returnInf = {succes: true ,newX : x,newY : y + dierection, model: model}
return returnInf;
}
else{
if (!slide) afterAnimationCompleted()
return {succes: false, model: model}
}
}
//same but for verticale "stuff"
else {
if (moveCheck(x, y, verizontale, dierection, slide, player, model)) {
if (model[x + dierection][y] != AIR) {
model[x + dierection + dierection][y] = model[x + dierection][y];
if (!slide) {
linearAnimation({y:y,x:x+dierection}, {y:y, x:x+dierection+dierection}, "tblId_", 1, 10, 1, ANIMATION_END.cloneRemove,false)
}
}
model[x + dierection][y] = model[x][y];
model[x][y] = AIR;
if (!slide) {
linearAnimation({x:x,y:y}, {y:y, x:x+dierection}, "tblId_", 1, 10, 1, ANIMATION_END.cloneRemoveUpdate,true)
}
var returnInf = {succes: true,newX : x + dierection,newY : y, model: model}
return returnInf
}
else {
if (!slide) afterAnimationCompleted()
return {succes: false, model: model}
}
}
}
/**
* checks if move is possible
* -x y cordinates
* -dierection -1 or 1
* -verzizontale 0 or 1
* -second true if it only can "go throug air"
*
* returns: true/false
* @param {number} x
* @param {number} y
* @param {number} verizontale
* @param {number} dierection
*/function moveCheck(x, y, verizontale, dierection, second, player, model) {
if (verizontale == 0) {
//looks if ther's air infront of the objekt
if (model[x][y + dierection] == AIR) {
return true
}
//looks if there's the "first" moveable objekt infront and do a move check for this objekt
else if (MOVEABLE_BLOCKS.includes(model[x][y + dierection]) && !second){
if (moveCheck(x, y+dierection, verizontale, dierection, true, false, model)){
return true
}
}
//for all cells with special Movement
else if (SPECIAL_BLOCKS_MOVE.includes(model[x][y + dierection])) {
if (specialBlocksMovement(x, y + dierection, verizontale, dierection, second, player, model)) {
return true;
}
else {
return false;
}
}
else{
return false;
}
} else {
if (model[x + dierection][y] == AIR) {
return true
}
else if (MOVEABLE_BLOCKS.includes(model[x + dierection][y]) && !second){
if (moveCheck(x + dierection, y, verizontale, dierection, true, false, model)){
return true
}
}
else if (SPECIAL_BLOCKS_MOVE.includes(model[x+dierection][y])) {
if (specialBlocksMovement(x+dierection, y, verizontale, dierection, second, player, model)) {
return true;
}
else {
return false;
}
}
else{
return false;
}
}
}
//all with extra blocks:
function specialBlocksMovement(x, y, verizontale, dierection, second, player, model){
//Slide_block - repetes the move Element function until it crushes something
if (model[x][y] == SLIDE && !second) {
var j = true
var newX = x
var newY = y
var i = 0
while (j) {
var lastCellCords = {newX:newX, newY: newY}
if (!moveCheck(newX, newY, verizontale, dierection, true, false, model)){
j = false
if (i > 0){
model[lastCellCords.newX][lastCellCords.newY] = SLIDE;
moveElement(lastCellCords.newX, lastCellCords.newY, verizontale, dierection, true, false, model);
model[x][y] = AIR;
}
}
if (verizontale== 0) {
newY = newY + dierection
}else{
newX = newX +dierection
}
i++;
}
if (i>1) {
var clone = linearAnimation({x:x, y:y}, {x:lastCellCords.newX, y:lastCellCords.newY}, "tblId_", verizontale, i*7, 1, ANIMATION_END.cloneRemove, false);
return true;
}
}
//Bomb - delets every Bomb destroyable Block around it and activate other bombs (List of Blocks beginging of the algorithm)
if (model[x][y] == BOMB && !player) {
const BOMB_DESTROYABLE_BLOCKS= [
BREAKABLE_BLOCK
]
model[x][y] = AIR;
for (j = -1 ; j <=1; j++){
for (i = -1 ; i <=1; i++) {
if (BOMB_DESTROYABLE_BLOCKS.includes(model[x+j][y+i])) {
model[x+j][y+i] = AIR
}
//other Bombs activation
if (model[x+j][y+i] == BOMB) {
specialBlocksMovement(x+j,y+i, verizontale, dierection, second, player, model)
}
}
}
return true;
}
//Bounce or gravaty fields - they take something what wants to move in them and moves them by move element in the dierection they are facing
var splitBlockName = model[x][y].split ("_",1)
if (splitBlockName == "bounce"){
//backup to save the bounce field data
var backup = model[x][y]
if (verizontale == 0){
//moving the Block on the position of field and deleting the field and the original pos of the block
model[x][y] = model[x][y - dierection]
model[x][y - dierection] = AIR
}
else{
model[x][y] = model[x - dierection][y]
model[x-dierection][y] = AIR
}
//moving the block in the dierection the field was looking
moveElement(x, y, EXTRA_INFORMATION[backup][0], EXTRA_INFORMATION[backup][1], true, player, model)
//re-replacing the field
if (model[x][y] == AIR){
model[x][y] = backup
}
else {
sessionStorage.setItem("bounceBackup_"+ x +"_"+y, JSON.stringify({coordX: x, coordY: y, backup:backup}))
}
//return false to make sure to not move the field (look at move element)
return false;
}
if (model[x][y] == LOCK && !player){
function lockOpened(newX,newY){
model[x][y] = AIR;
model[newX][newY] = AIR;
return true;
}
if (verizontale == 0){
if (model[x][y-dierection] == KEY){
if (lockOpened(x, y- dierection)){
return true;
}
}
}
else{
if (model[x-dierection][y] == KEY){
if(lockOpened(x-dierection, y)){
return true;
}
}
}
}
if (model[x][y] == GOAL && player){
alert ("LevelCompleted")
}
if (model[x][y] == NO_PLAYER && !player){
if (verizontale == 0){
if (model[x][y+dierection]== AIR){
var block = model[x][y - dierection];
model[x][y+ dierection] = block;
model[x][y - dierection] = AIR;
linearAnimation({y:y - dierection,x:x}, {y:y + dierection, x:x}, "tblId_", 0, 20, 1, ANIMATION_END.cloneRemove,false)
return true
}
else{
return false
}
}
else {
if (model[x +dierection][y]== AIR){
var block = model[x - dierection][y];
model[x+ dierection][y] = block;
model[x - dierection][y] = AIR;
linearAnimation({y:y,x:x - dierection}, {y:y, x:x + dierection}, "tblId_", 1, 20, 1, ANIMATION_END.cloneRemove,false)
return true
}
else{
return false
}
}
}
}
/**
* Updates one placing unit
* x-y = cords
*
*/function placingUpdate(x,y) {
//todo - besser gestalten
//for all gravaty simulator variants
var splitBlockName = model[x][y].split ("_",2)
if (splitBlockName[1] == "gravity"){
if(EXTRA_INFORMATION[model[x][y]] == "right"){
//loop it as long as the function returns true
while (bouncePlace(x,y, BOUNCE_ZONE_RIGHT,0,1)){
y++
}
}
if(EXTRA_INFORMATION[model[x][y]] == "left"){
while (bouncePlace(x,y, BOUNCE_ZONE_LEFT,0,-1)){
y = y - 1
}
}
if(EXTRA_INFORMATION[model[x][y]] == "up"){
while (bouncePlace(x,y, BOUNCE_ZONE_UP,1,-1)){
x = x-1
}
}
if(EXTRA_INFORMATION[model[x][y]] == "down"){
while (bouncePlace(x,y, BOUNCE_ZONE_DOWN,1,1)){
x++
}
}
}
}
//have to modify this function to not delet everything in the row/colummn by udating it
function bouncePlace(x,y, cell, verizontale, dierection){
var splitBlockName = model[x][y].split ("_",1)
if (verizontale ==0){
if (model [x][y + dierection + dierection] != UNBREAKABLE_BLOCK && model[x][y + dierection + dierection] != null && model[x][y+dierection].split ("_",1) != "gravity") {
model[x][y+dierection] = cell;
return true
}
}
else{
if (model [x + dierection + dierection][y] != UNBREAKABLE_BLOCK && model[x + dierection + dierection][y] != null && model[x+dierection][y].split ("_",1) != "gravity") {
model[x+dierection][y] = cell;
return true
}
}
}
function placingUpdateAll(){
for (x=0; x < model.length; x++){
for (y=0; y < model[x].length; y++){
placingUpdate(x,y);
}
}
}