]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Home/Dashlets/InvadersDashlet/InvadersDashletScript.tpl
Release 6.5.0
[Github/sugarcrm.git] / modules / Home / Dashlets / InvadersDashlet / InvadersDashletScript.tpl
1 {*
2
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39
40 *}
41
42
43 <script>
44 {literal}
45 if(typeof InvadersGame == 'undefined') { 
46         InvadersGame = function() {
47                 var playerX, move, timer, rate, shotX, shotY, started, shotFired, alienX, alienY, aMove;
48                 var rightRowsCleared, leftRowsCleared, bottomRow;
49             return {
50                 init: function() {
51                         //alert("initilized");
52                         InvadersGame.rate = 30;
53                         document.onkeydown = InvadersGame.keyDown;
54                                 document.onkeyup = InvadersGame.keyUp;
55                                 clearTimeout(InvadersGame.timer);
56                                 InvadersGame.timer = setTimeout("InvadersGame.update()",InvadersGame.rate);
57                                 InvadersGame.started = false;
58                                 InvadersGame.shotFired = false;
59                                 InvadersGame.level = 1;
60                 },
61                 moveLeft: function(frameTime) {
62                         InvadersGame.playerX = InvadersGame.playerX + (5 * frameTime);
63                 },
64                 moveRight: function(frameTime) {
65                         InvadersGame.playerX = InvadersGame.playerX - (5 * frameTime);
66                 },
67                 checkLoss: function() {
68                     if (InvadersGame.alienY + (InvadersGame.bottomRow * 16) > 250) {
69                         InvadersGame.init();
70                         var startScreen = document.getElementById("startScreen");
71                     startScreen.style.display = "block";
72                     var messageText = document.getElementById("messageText");
73                     messageText.innerHTML = {/literal}"{$dashletStrings.LBL_GAME_OVER}"{literal};
74                     }
75                 },
76                 keyDown: function(keyEvents) {
77                         var key = (window.event) ? event.keyCode : keyEvents.keyCode;
78                         if (InvadersGame.started) {
79                                 if (key == 65){ //'a' key
80                                         InvadersGame.move = -1;
81                                 }
82                                 if (key == 68){ //'d' key
83                                         InvadersGame.move = 1;
84                                 }
85                                 if (key == 83){ //'s' key
86                                         InvadersGame.shoot();
87                                 }
88                                 if (key == 82){ //'s' key
89                                         InvadersGame.reset();
90                                 }
91                         }
92                 },
93                 keyUp: function(keyEvents) {
94                         var key = (window.event) ? event.keyCode : keyEvents.keyCode;
95                         if (key == 65 || key == 68){
96                                 InvadersGame.move = 0;
97                         }
98                 },
99                 reset: function() {
100                         InvadersGame.started = true;
101                         InvadersGame.lastFrame = new Date().getTime();
102                         var startScreen = document.getElementById("startScreen");
103                         startScreen.style.display = "none";
104                         var aliensTable = document.getElementById("aliens");
105                                 var aliens = aliensTable.getElementsByTagName("td");
106                                 var shot = document.getElementById("shot");
107                                 shot.style.display = "none";
108                                 var i=0;
109                                 for(i=0; i < aliens.length; i++) {
110                                         aliens[i].style.backgroundImage = "url('modules/Home/Dashlets/InvadersDashlet/sprites/alien.png')"
111                                 }
112                                 InvadersGame.playerX = 134;
113                         InvadersGame.move = 0;
114                         InvadersGame.aMove = (3 + InvadersGame.level) / 4;
115                         InvadersGame.alienX = 0;
116                         InvadersGame.alienY = 0;
117                         InvadersGame.shoty = 250;
118                         InvadersGame.shotFired = false;
119                         InvadersGame.rightRowsCleared = 0;
120                         InvadersGame.leftRowsCleared = 0;
121                         InvadersGame.bottomRow = 3;
122                         aliensTable.style.left = InvadersGame.alienX;
123                         aliensTable.style.top  = InvadersGame.alienY;
124                         clearTimeout(InvadersGame.timer);
125                         InvadersGame.timer = setTimeout("InvadersGame.update()",InvadersGame.rate);
126                 },
127                 shoot: function() {
128                         if (!InvadersGame.shotFired) {
129                                 InvadersGame.shotX = InvadersGame.playerX;
130                                 InvadersGame.shotY = 250;
131                                 InvadersGame.shotFired = true;
132                                 var shot = document.getElementById("shot");
133                                 shot.style.left = InvadersGame.playerX + 8 + "px";
134                                 shot.style.top = 250 + "px";
135                                 shot.style.display = "inline";
136                         }
137                 },
138                 update: function() {
139                   var time = new Date().getTime();
140               var frameTime = (time - InvadersGame.lastFrame) / InvadersGame.rate;
141               if (frameTime == 0) frameTime = 1;
142               InvadersGame.lastFrame = time;
143                         if (InvadersGame.started) {
144                                 if (InvadersGame.move < 0 && InvadersGame.playerX > 0) {
145                                         InvadersGame.moveRight(frameTime);
146                                 }
147                                 if (InvadersGame.move > 0 && InvadersGame.playerX < 368) {
148                                         InvadersGame.moveLeft(frameTime);
149                                 }
150                                 InvadersGame.alienX += InvadersGame.aMove * frameTime;
151                                 if (InvadersGame.aMove > 0 && InvadersGame.alienX > (225 + InvadersGame.rightRowsCleared)) {
152                                         InvadersGame.aMove = -(3 + InvadersGame.level) / (4);
153                                         InvadersGame.alienY += 16;
154                                         InvadersGame.checkLoss();
155                                 }
156                                 else if (InvadersGame.alienX < (0 - InvadersGame.leftRowsCleared) && InvadersGame.aMove < 0) {
157                                 InvadersGame.aMove = (3 + InvadersGame.level) / (4);
158                                         InvadersGame.alienY += 16;
159                                         InvadersGame.checkLoss();
160                                 }
161
162                                 var player = document.getElementById("player");
163                                 var shot = document.getElementById("shot");
164                                 var aliens = document.getElementById("aliens");
165                                 
166                                 if (InvadersGame.shotY < 8){
167                                         InvadersGame.shotY = 235;
168                                         InvadersGame.shotFired = false;
169                                         shot.style.display = "none";
170                                 }
171                                 if (InvadersGame.shotFired) {
172                                         InvadersGame.shotY -= 5;
173                                         shot.style.top = InvadersGame.shotY + "px";
174                                 }
175                                 player.style.left = InvadersGame.playerX + "px";
176                                 aliens.style.left = InvadersGame.alienX + "px";
177                                 aliens.style.top = InvadersGame.alienY + "px";
178                                 if(InvadersGame.shotFired) {
179                                         InvadersGame.checkCollide(shot, aliens);
180                                 }
181                         }
182                         InvadersGame.timer = setTimeout("InvadersGame.update()", InvadersGame.rate);
183                 },
184                 nextLevel: function() {
185                   InvadersGame.started = false;
186                   var startScreen = document.getElementById("startScreen");
187               startScreen.style.display = "block";
188               InvadersGame.level++;
189               var messageText = document.getElementById("messageText");
190               messageText.innerHTML = "Level " + InvadersGame.level;
191               clearTimeout(InvadersGame.timer);
192               InvadersGame.timer = setTimeout("InvadersGame.reset()",5000);
193                 },
194                 checkCollide: function(shot, aliens) {
195                         var sx = InvadersGame.shotX + 8;
196                         var sy = InvadersGame.shotY;
197                         var ax = InvadersGame.alienX;
198                         var ay = InvadersGame.alienY;
199                         var cx = Math.floor((sx - ax) / 34);
200                         var cy = Math.floor((sy - ay) / 16);
201                         if (cx > -1 && cx < 5 && cy > -1 && cy < 4) {
202                                 var dAlien = document.getElementById("a" + cx + cy);
203                                 if (!(dAlien.style.backgroundImage == "none")) {
204                                     //Collision occured
205                                         dAlien.style.backgroundImage = "none";
206                                         InvadersGame.shotFired = false;
207                                         shot.style.display = "none";
208                                         var aliens = document.getElementById("aliens").getElementsByTagName("td");
209                                         var i = 0;
210                                         var col = 0;
211                                         var row = 0;
212                                         var count = new Array(5);
213                                         var rows = new Array(5);
214                                         for (i = 0; i < count.length; i++) {
215                                                 count[i] = 0;
216                                                 rows[i] = 0;
217                                         }
218                                         for (i = 0; i < aliens.length; i++) {
219                                                 col = i % 5;
220                                                 row = Math.floor(i / 5);
221                                                 if (aliens[i].style.backgroundImage != "none") {
222                                                         count[col]++;
223                                                     rows[row]++;
224                                                 }
225                                         }
226                                         if (count[0] == 0) {
227                                           if (count[1] == 0) {
228                                             if (count[2] == 0) {
229                                               if (count[3] == 0) {
230                                                 if (count[4] == 0) { InvadersGame.nextLevel();} 
231                                                     else {InvadersGame.leftRowsCleared = 32 * 4;}       
232                                                   } else {InvadersGame.leftRowsCleared = 32 * 3;}
233                                             } else {InvadersGame.leftRowsCleared = 32 * 2;}
234                                           } else {InvadersGame.leftRowsCleared = 32;}
235                                         } else {InvadersGame.leftRowsCleared = 0;}
236                                         
237                                         if (count[4] == 0) {
238                                           if (count[3] == 0) {
239                                             if (count[2] == 0) {
240                                               if (count[1] == 0) {
241                                                 if (count[0] == 0) {InvadersGame.rightRowsCleared = 32 * 5;
242                                                 } else {InvadersGame.rightRowsCleared = 32 * 4;}        
243                                                   } else {InvadersGame.rightRowsCleared = 32 * 3; }
244                                             } else {InvadersGame.rightRowsCleared = 32 * 2;}
245                                           } else { InvadersGame.rightRowsCleared = 32;}
246                                         } else {InvadersGame.rightRowsCleared = 0;}
247                                 
248                                         for (i = 0; i < 4 ; i++) {
249                                            if (rows[i] > 0) {
250                                                InvadersGame.bottomRow = i;
251                                            }
252                                         }
253                                         
254                                 }
255                         }
256                 }       
257                 };
258         }();
259 }
260 InvadersGame.init();
261
262 </script>{/literal}