Commits

Anonymous committed 21bd621

- have a initGame() function for both game modes (common code)
- first level working

  • Participants
  • Parent commits 2a7d00e

Comments (0)

Files changed (5)

File js/endless_mode.js

 
 function EndlessMode()
 {
-resetStuff();
-
-GameStatistics.start();
-
 GAME_MODE = EndlessMode;
 
-
-GAME_WIDTH = CANVAS.width;
-GAME_HEIGHT = CANVAS.height - 60;
-
-
-MAIN_SHIP = new Ship();
-
-MAIN_SHIP.x = GAME_WIDTH / 2;
-MAIN_SHIP.y = GAME_HEIGHT / 2;
-
-
-STAGE.addChild( MAIN_SHIP );
-
-ZIndex.add( MAIN_SHIP );
-
-    // so that .tick() of EnemyShip/Ship/... is called automatically
-Ticker.addListener( MAIN_SHIP );
-Ticker.addListener( window );
-
-STAGE.enableMouseOver();
-
-    // call update on the stage to make it render the current display list to the canvas
-STAGE.update();
-
-
-    //register key functions
-document.onkeydown = handleKeyDown;
-document.onkeyup = handleKeyUp;
-
-
-STAGE.onMouseMove = function( event ) { MAIN_SHIP.handleMouseMove( event ); };
-STAGE.onMouseDown = function( event ) { MAIN_SHIP.handleClick( event ); };
-
-
-//SoundJS.play("game_music", SoundJS.INTERRUPT_NONE ,0 ,0, -1);
-
-GameMenu();
+initGame();
 }
 
 
 STAGE.update();
 }
     
+    
+    
+/*
+
+ */
+   
+function initGame()
+{
+resetStuff();
+
+GameStatistics.start();
+
+
+GAME_WIDTH = CANVAS.width;
+GAME_HEIGHT = CANVAS.height - 60;
+
+
+MAIN_SHIP = new Ship();
+
+MAIN_SHIP.x = GAME_WIDTH / 2;
+MAIN_SHIP.y = GAME_HEIGHT / 2;
+
+
+STAGE.addChild( MAIN_SHIP );
+
+ZIndex.add( MAIN_SHIP );
+
+    // so that .tick() of EnemyShip/Ship/... is called automatically
+Ticker.addListener( MAIN_SHIP );
+Ticker.addListener( window );
+
+STAGE.enableMouseOver();
+
+    // call update on the stage to make it render the current display list to the canvas
+STAGE.update();
+
+
+    //register key functions
+document.onkeydown = handleKeyDown;
+document.onkeyup = handleKeyUp;
+
+
+STAGE.onMouseMove = function( event ) { MAIN_SHIP.handleMouseMove( event ); };
+STAGE.onMouseDown = function( event ) { MAIN_SHIP.handleClick( event ); };
+
+
+//SoundJS.play("game_music", SoundJS.INTERRUPT_NONE ,0 ,0, -1);
+
+GameMenu();
+}
+   
+    
+    
+    
+/*
+    Resets the configurations (for when restarting the game)
+ */
 
 function resetStuff()
 {
     {
     filePath = 'maps/level' + i + '.json';
    
-    $.ajax(filePath , function(data)
-        {
-        LEVELS.push( JSON.parse( data ) );
+    $.ajax({
+        url: filePath, 
+        dataType: 'json',
+        beforeSend: function(xhr)
+            {
+            if (xhr.overrideMimeType)
+                {
+                xhr.overrideMimeType("application/json");
+                }
+            },
+        success: function(data)
+            {
+            LEVELS.push( data );
+            }
         });
     }
+
+
+
 }
 
 
 var NUMBER_OF_LEVELS = 1;
 
 
+var CURRENT_LEVEL = 0;
+
+
 var COUNT_TICKS = 0;
 
 
 {
 COUNT_TICKS++;
 
-var phase = LEVELS[ LEVEL_PHASE ];
+var currentLevel = LEVELS[ CURRENT_LEVEL ];
+  
+    
+var phase = currentLevel[ LEVEL_PHASE ];
+
+
 
 if ( !ENDING_LEVEL && COUNT_TICKS >= phase.tick)
     {
     var enemyType = window[ phase.enemyType ];
     
-    var enemy = new enemyType();
+    var i;
+    var howMany = parseInt( phase.howMany );
     
-        // random x position
-    if (phase.x < 0)
+    for (i = 0 ; i < howMany ; i++)
         {
-        enemy.x = getRandomInt( 0, GAME_WIDTH );
+        var enemy = new enemyType();
+        
+            // random x position
+        if (phase.x < 0)
+            {
+            enemy.x = getRandomInt( 0, GAME_WIDTH );
+            }
+        
+        else
+            {
+            enemy.x = phase.x;
+            }
+        
+            // random y position
+        if (phase.y < 0)
+            {
+            enemy.y = getRandomInt( 0, GAME_HEIGHT );
+            }
+        
+        else
+            {
+            enemy.y = phase.y;
+            }
+        
+        addNewEnemy( enemy );
         }
     
-    else
-        {
-        enemy.x = phase.x;
-        }
-    
-        // random y position
-    if (phase.y < 0)
-        {
-        enemy.y = getRandomInt( 0, GAME_HEIGHT );
-        }
-    
-    else
-        {
-        enemy.y = phase.y;
-        }
-    
-    addNewEnemy( enemy );
+
     
         // advance to the next phase of the level
     LEVEL_PHASE++;
     
+        // no more phases of the level
         // game ends when there aren't more enemies
-    if (LEVEL_PHASE + 1 >= NUMBER_OF_LEVELS)
+    if (LEVEL_PHASE + 1 >= currentLevel.length)
         {
         ENDING_LEVEL = true;
+        
+            // we're gonna count a bit more, before checking for the ENDING_LEVEL variable, to give time for the Enemies to spawn
+        COUNT_TICKS = 0;
         }
     }
 
     
     // the level ended
-if ( ENDING_LEVEL === true && EnemyShip.all.length === 0 )
+if ( ENDING_LEVEL === true &&  COUNT_TICKS >= 100 && EnemyShip.all.length === 0 )
     {
     MainMenu(); //HERE for now
     }
 };
 
+Maps.LEVELS = LEVELS;
+
+
+window.Maps = Maps;
+
 
 }(window));

File js/start_game.js

 
 function StartGame() 
 {
+GAME_MODE = StartGame;
 
+initGame();
+
+Maps();
+
+Ticker.addListener( Maps.tick );
 }
 
 

File maps/level0.json

 [
     {
-    tick      : 20,
-    enemyType : "EnemyMoveHorizontally",
-    howMany   : 5,
-    x         : -1,
-    y         : -1
+    "tick"      : 20,
+    "enemyType" : "EnemyMoveHorizontally",
+    "howMany"   : 5,
+    "x"         : -1,
+    "y"         : -1
     }
 ]