-
Notifications
You must be signed in to change notification settings - Fork 1
/
setupPage.js
335 lines (295 loc) · 9.67 KB
/
setupPage.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
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
/*****************************************************************************
*****************************************************************************
SetupPage:
For handling the setup page, for the user to place units before the game starts
*****************************************************************************
*****************************************************************************/
/*********************************************
Variables
******************************************/
var selectedItem;// stores the snippet of the item currently selected
var isWaiting = false; //for multiplayer. when waiting after setup, user cannot do anything
//used to store a label that is displayed when the user clicks on "Play" prematurely
//var isPlayWarned = false;
//var playWarning1;
//var playWarning2;
/*******************************************************************8
Setup functions
***********************************************************************/
/*
Draws the page for the user to set up his units
*/
function drawSetupPage(){
//alert("drawing setup");
isUnitSelected = false;
boardArea.removeAllContentItems();
boardArea.contentFlags = gddContentFlagHaveDetails | gddContentFlagManualLayout;
//lblScore.innerText = strSetup+"\n";
itemsGlobal = new Array();
for (var i = 0; i < 6; i++) {
for (var j = 0; j < 10; j++) {
//squares[items.length] = "";
var item = new ContentItem();
item.image = getSpotImageSetup(j,i);
item.flags = gddContentItemFlagNoRemove;
item.snippet = itemsGlobal.length;
item.onDetailsView = OnSetupClicked;
item.setRect(j*blocksize, i*blocksize+textheight, blocksize, blocksize);
itemsGlobal.push(item);
board[j][i] = -1;
}
}
for (var i = 6; i < 10; i++) {
for (var j = 0; j < 10; j++) {
//squares[items.length] = "";
var item = new ContentItem();
item.image = imgBlueList[getSoldierRank(itemsGlobal.length - 60)];
item.flags = gddContentItemFlagNoRemove;
item.snippet = itemsGlobal.length;
item.onDetailsView = OnSetupClicked;
item.setRect(j*blocksize, i*blocksize+textheight, blocksize, blocksize);
itemsGlobal.push(item);
board[j][i] = item.snippet-60;
}
}
// add playbutton
itemsGlobal.push(GetButtonManual(0, 10*blocksize+textheight, 30, strPlay, OnPlayNotify));
// add cancel button
itemsGlobal.push(GetButtonManual(90, 10*blocksize+textheight, 40, strCancel, OnSetupCancel));
//add button to use a random setup
itemsGlobal.push(GetButtonManual(23, 11*blocksize+textheight-3, 68, strRandomSet, OnSetRandomNotify));
// add setup Label
itemsGlobal.push(GetLabelManual(47, 0, 40, strSetupLabel));
boardArea.contentItems = itemsGlobal;
//alert("done drawing setup");
EnableFlip();
};
/*************************************************************
Event handlers
*************************************************************/
/*
Called when the "Play" button is clicked
*/
function OnPlayNotify(item){
//alert("play clicked");
if (!isValidSetup()){
lblWarning.innerText = strBadSetup;
setTimeout("RemovePlayWarning()", 5000);
}
else if(isSinglePlay){ //if single player mode, go right into game
DisableFlip(); //user cannot flip board items at this point
RemovePlayWarning();
fillPlayerList();
loadUnits(); //prepare a list for the setup of the computer unit
setupComputerPlayer(getUnits()); //load into computer
DrawBoard();
printScore();
TestStartPosition();
}
else{ //a bit trickier, as both players have to be ready
DisableFlip(); //user cannot flip board items at this point
RemovePlayWarning();
PrepareHisUnits();
fillPlayerList(); //have your units ready before your opponents
isReady = true;
try {
//let friend know that i am ready
googleTalk.SendTalkData(friend_user_id, msgReady);
} catch (e) {
alert(strConnectionError);
OnMainMenu();
}
//if i knew that my friend was already ready, time to send my units
if (isFriendReady){
SendMyUnits();
}
else{
DisplayFriendWait();
}
}
};
/*
Called when a box on the setup board is clicked
*/
function OnSetupClicked(item){
if (isCancelState()) return; //everything should be disabled at cancel prompt
var items = boardArea.contentItems.toArray();
if (isUnitSelected){
if (hasSetupItem(item.snippet)){
items[selectedItem].image = imgBlueList[getSoldierRank(getBoardItem(selectedItem))];
items[item.snippet].image = imgBlueListS[getSoldierRank(getBoardItem(item.snippet))];
selectedItem = item.snippet;
}
else if(item.snippet > 19){ //if selected a valid spot to move unit
var xOld = selectedItem%10;
var yOld = Math.floor(selectedItem / 10);
var xNew = item.snippet%10;
var yNew = Math.floor(item.snippet / 10);
items[item.snippet].image = imgBlueList[getSoldierRank(getBoardItem(selectedItem))];
items[selectedItem].image = getSpotImageSetup(xOld, yOld);
board[xNew][yNew] = getBoardItem(selectedItem);
board[xOld][yOld] = -1;
selectedItem = item.snippet;
isUnitSelected = false;
}
else{ //should not move unit there, reset previous image
items[selectedItem].image = imgBlueList[getSoldierRank(getBoardItem(selectedItem))];
isUnitSelected = false;
}
}
else {
//alert(item.snippet);
if (hasSetupItem(item.snippet)){
isUnitSelected = true;
items[item.snippet].image = imgBlueListS[getSoldierRank(getBoardItem(item.snippet))];
selectedItem = item.snippet;
}
}
};
/*
Called when user clicks on setup random button, to try a random piece setup
*/
function OnSetRandomNotify(item){
//if (isCancelState()) return; //everything should be disabled at cancel prompt
isUnitSelected = false;
RandomSetup(); //just call this in SetupSelect.js, and the operation is performed
};
/**********************************************************************
board change handlers
***********************************************************************/
/*
called when ready to play. the user's list of units are to be filled with his setup, as well as the play board
*/
function fillPlayerList(){
Units[0] = new Array(40);
for (var i = 2; i < 6; i++){
for (var j = 0; j < 10; j++){
Units[0][board[j][i]] = new soldier(board[j][i]);
Units[0][board[j][i]].setSoldierLocation(j,i);
board[j][i-2] = board[j][i]+1;
board[j][i] = 0;
}
}
//units must be flipped for proper placement
var temp;
for (var i = 0; i < 2; i++){
for (var j = 0; j < 10; j++){
temp = board[j][i];
Units[0][board[j][i]-1].setSoldierLocation(j,3-i);
board[j][i] = board[j][3-i];
Units[0][board[j][3-i]-1].setSoldierLocation(j,i);
board[j][3-i] = temp;
}
}
for (var i = 0; i < 4; i++){
for (var j = 0; j < 5; j++){
temp = board[j][i];
Units[0][board[j][i]-1].setSoldierLocation(9-j,i);
board[j][i] = board[9-j][i];
Units[0][board[9-j][i]-1].setSoldierLocation(j,i);
board[9-j][i] = temp;
board[j][i];
board[9-j][i];
}
}
//if this is player2, the units have to be flipped to the other side of the board
if(!isPlayer1){
FlipMyUnits();
}
//Goes to AIDifficulty.js
SetMyVisibility();
};
function FlipMyUnits(){
for(var i = 0; i < 40; i++){
board[9-Units[0][i].x][9-Units[0][i].y] = board[Units[0][i].x][Units[0][i].y];
//board[10-Units[0][i].x][10-Units[0][i].y] = -board[Units[0][i].x][Units[0][i].y];
board[Units[0][i].x][Units[0][i].y] = 0;
Units[0][i].x = 9-Units[0][i].x;
Units[0][i].y = 9-Units[0][i].y;
}
};
function PrepareHisUnits(){
Units[1] = new Array(40);
for (var i = 0; i < 40; i++){
Units[1][i] = new soldier(i);
}
};
function DisplayFriendWait(){
// add note that list is being loaded
boardArea.removeAllContentItems();
boardArea.contentFlags = gddContentFlagHaveDetails;
var waitSign = new ContentItem();
waitSign.heading = strWaitFriend;
waitSign.flags = gddContentItemFlagStatic;
waitSign.SetRect(0, 0, 200, textheight);
boardArea.addContentItem(waitSign, gddItemDisplayInSidebar);
// add cancel button
var cancel = new ContentItem();
cancel.heading = strCancel;
cancel.onDetailsView = OnWaitCancel; //Here, we ask the userr to confirm cancel
cancel.flags = gddContentItemFlagNoRemove;
cancel.SetRect(0, 11 * blocksize, 80, textheight);
boardArea.addContentItem(cancel, gddItemDisplayInSidebar);
lblScore.innerText = "";
};
function TestStartPosition(){
if(!CanMove()){
setGameOver(1); //The game is over, no more turns
//alert(strYouCantMove);
DisplayWarning(strYouCantMove);
if (!isSinglePlay){
try{
googleTalk.SendTalkData(friend_user_id, msgNoMoves);
} catch (e){
}
}
setPlayerTurn(3);
}
};
/*******************************************************
Utilities
*******************************************************/
/*
gets the item displayed on the board, based on an item snippet
*/
function getBoardItem(itemNum){
var x = itemNum%10;
var y = Math.floor(itemNum / 10);
return board[x][y];
};
/*
determines whether the selected item has a soldier on it
*/
function hasSetupItem(itemNum){
if (getBoardItem(itemNum) > -1) return true;
return false;
};
/*
for initial display of the setup board.
gets the basic image, either blank, or invalid, etc.
*/
function getSpotImageSetup(x, y){
if ( ( (x==2)||(x==3)||(x==6)||(x==7) ) && ( (y==0)||(y==1) ) ) return imgForbidden;
if ((y > 1)&&(y < 6)) return imgGreen;
return imgBlank;
};
/*
When the user has clicked on "Play", this is called to make sure that the user has properly set up his units
*/
function isValidSetup(){
//Go through the 4 setup rows, make sure each cell is filled.
for(var i = 2; i < 6; i++){
for (var j = 0; j < 10; j++){
if (board[j][i] < 0) return false;
}
}
return true;
};
/*
Removes the play warning
*/
function RemovePlayWarning(){
if (!isCancelState()){
lblWarning.innerText = "";
}
}