3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
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;
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;
61 moveLeft: function(frameTime) {
62 InvadersGame.playerX = InvadersGame.playerX + (5 * frameTime);
64 moveRight: function(frameTime) {
65 InvadersGame.playerX = InvadersGame.playerX - (5 * frameTime);
67 checkLoss: function() {
68 if (InvadersGame.alienY + (InvadersGame.bottomRow * 16) > 250) {
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};
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;
82 if (key == 68){ //'d' key
83 InvadersGame.move = 1;
85 if (key == 83){ //'s' key
88 if (key == 82){ //'s' key
93 keyUp: function(keyEvents) {
94 var key = (window.event) ? event.keyCode : keyEvents.keyCode;
95 if (key == 65 || key == 68){
96 InvadersGame.move = 0;
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";
109 for(i=0; i < aliens.length; i++) {
110 aliens[i].style.backgroundImage = "url('modules/Home/Dashlets/InvadersDashlet/sprites/alien.png')"
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);
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";
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);
147 if (InvadersGame.move > 0 && InvadersGame.playerX < 368) {
148 InvadersGame.moveLeft(frameTime);
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();
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();
162 var player = document.getElementById("player");
163 var shot = document.getElementById("shot");
164 var aliens = document.getElementById("aliens");
166 if (InvadersGame.shotY < 8){
167 InvadersGame.shotY = 235;
168 InvadersGame.shotFired = false;
169 shot.style.display = "none";
171 if (InvadersGame.shotFired) {
172 InvadersGame.shotY -= 5;
173 shot.style.top = InvadersGame.shotY + "px";
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);
182 InvadersGame.timer = setTimeout("InvadersGame.update()", InvadersGame.rate);
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);
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")) {
205 dAlien.style.backgroundImage = "none";
206 InvadersGame.shotFired = false;
207 shot.style.display = "none";
208 var aliens = document.getElementById("aliens").getElementsByTagName("td");
212 var count = new Array(5);
213 var rows = new Array(5);
214 for (i = 0; i < count.length; i++) {
218 for (i = 0; i < aliens.length; i++) {
220 row = Math.floor(i / 5);
221 if (aliens[i].style.backgroundImage != "none") {
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;}
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;}
248 for (i = 0; i < 4 ; i++) {
250 InvadersGame.bottomRow = i;