1. Pedro Ferreira
  2. space_ship_game

Commits

drk  committed 3e4923e

- continue working on having two separate modes
- have a .tick() for each game mode
- fix being able to fire bullets when returning to the MainMenu (calling from the GameMenu) -- had to unbind the .onMouseDown event

  • Participants
  • Parent commits 00f2fb5
  • Branches default

Comments (0)

Files changed (7)

File home.html

View file
  • Ignore whitespace
         <script type="text/javascript" src="js/z_index.js"></script>
         <script type="text/javascript" src="js/maps.js"></script>
         
+        <script type="text/javascript" src="js/start_game.js"></script>
+        <script type="text/javascript" src="js/endless_mode.js"></script>
+        
         <script type="text/javascript" src="js/main.js"></script>
         <script type="text/javascript" src="js/main_menu.js"></script>
                 

File js/endless_mode.js

View file
  • Ignore whitespace
+
+(function(window)
+{
+
+    // from how many ticks, until next enemy
+var NEXT_ENEMY_TICKS = 50;
+
+var COUNT_TICKS_NEXT_ENEMY = 0;
+
+
+    // number of ticks until we increase the difficulty 
+var INCREASE_DIFFICULTY_TICKS = 200;
+var COUNT_INCREASE_DIFFICULTY_TICKS = 0;
+
+
+
+
+/*
+    Doesn't have levels/maps
+    
+    Difficulty increases with time
+ */
+
+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();
+}
+
+
+
+
+
+/*
+    Gets called after the main tick()
+ */
+
+EndlessMode.tick = function()
+{
+COUNT_TICKS_NEXT_ENEMY--;
+
+if (COUNT_TICKS_NEXT_ENEMY < 0)
+    {
+    COUNT_TICKS_NEXT_ENEMY = NEXT_ENEMY_TICKS;
+    
+  
+    var enemy = new ENEMY_TYPES[ getRandomInt(0, ENEMY_TYPES.length - 1 ) ]();
+    
+    var x = getRandomInt( 0, GAME_WIDTH );
+    var y = getRandomInt( 0, GAME_HEIGHT );
+    
+   
+    //var enemy = new EnemyMoveHorizontally();
+    //var enemy = new EnemyRocks();
+    
+    enemy.x = x;
+    enemy.y = y;
+    
+    addNewEnemy( enemy );
+    }
+
+    
+
+    // deal with increasing the difficulty of the game
+COUNT_INCREASE_DIFFICULTY_TICKS--;
+
+if (COUNT_INCREASE_DIFFICULTY_TICKS < 0)
+    {
+    COUNT_INCREASE_DIFFICULTY_TICKS = INCREASE_DIFFICULTY_TICKS;
+    
+        // increase the difficulty of the game
+    $( ENEMY_TYPES ).each(function(index, enemyType)
+        {
+        enemyType.increaseDifficulty();
+        });
+    
+        // reduce the time it takes until a new enemy is added
+    NEXT_ENEMY_TICKS--;
+    }    
+};
+
+
+window.EndlessMode = EndlessMode;
+
+}(window));

File js/game_menu.js

View file
  • Ignore whitespace
     
     IS_OPENED = false;
     
-    startGame();
+    GAME_MODE();
     };
 
 restart.x = centeredX;

File js/main.js

View file
  • Ignore whitespace
     ];
     
 
-
-    // from how many ticks, until next enemy
-var NEXT_ENEMY_TICKS = 50;
-
-var COUNT_TICKS_NEXT_ENEMY = 0;
-
-
-    // number of ticks until we increase the difficulty 
-var INCREASE_DIFFICULTY_TICKS = 200;
-var COUNT_INCREASE_DIFFICULTY_TICKS = 0;
-
+    
+var GAME_MODE = null;
+    
     
 function initialLoad()
 {
 {
 STAGE.removeAllChildren();
 
+    // unbind the event
+STAGE.onMouseDown = null;
+
 Ticker.removeAllListeners();
 
 ZIndex.clear();
     
     
     
-function startGame() 
-{
-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();
-}
-
-
-
-/*
-    Doesn't have levels/maps
-    
-    Difficulty increases with time
- */
-
-function startEndlessMode()
-{
-
-}
-
-
-
-
 
 
 function checkIfBulletsHitAnything( ships, bullets )
 
 function tick()
 {
-COUNT_TICKS_NEXT_ENEMY--;
-
-if (COUNT_TICKS_NEXT_ENEMY < 0)
-    {
-    COUNT_TICKS_NEXT_ENEMY = NEXT_ENEMY_TICKS;
-    
-  
-    var enemy = new ENEMY_TYPES[ getRandomInt(0, ENEMY_TYPES.length - 1 ) ]();
-    
-    var x = getRandomInt( 0, GAME_WIDTH );
-    var y = getRandomInt( 0, GAME_HEIGHT );
-    
-   
-    //var enemy = new EnemyMoveHorizontally();
-    //var enemy = new EnemyRocks();
-    
-    enemy.x = x;
-    enemy.y = y;
-    
-    addNewEnemy( enemy );
-    }
-    
-
     // check if enemy bullets hit our ship
 checkIfBulletsHitAnything( [ MAIN_SHIP ], Weapons.enemyBullets );
 
     // check if our bullets hit the enemy
 checkIfBulletsHitAnything( EnemyShip.all, Weapons.allyBullets );
 
-
-    // deal with increasing the difficulty of the game
-COUNT_INCREASE_DIFFICULTY_TICKS--;
-
-if (COUNT_INCREASE_DIFFICULTY_TICKS < 0)
-    {
-    COUNT_INCREASE_DIFFICULTY_TICKS = INCREASE_DIFFICULTY_TICKS;
-    
-        // increase the difficulty of the game
-    $( ENEMY_TYPES ).each(function(index, enemyType)
-        {
-        enemyType.increaseDifficulty();
-        });
-    
-        // reduce the time it takes until a new enemy is added
-    NEXT_ENEMY_TICKS--;
-    }
+    // call the tick() of the current game mode
+GAME_MODE.tick();
 
 STAGE.update();
 }

File js/main_menu.js

View file
  • Ignore whitespace
     along with space_ship_game.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-/*global LOADING_INTERVAL, STAGE, Text, CANVAS, $, EVENT_KEY, startGame*/
+/*global LOADING_INTERVAL, STAGE, Text, CANVAS, $, EVENT_KEY, GAME_MODE*/
 /*jslint vars: true, white: true*/
 
 "use strict";
     // holds references to functions that get called when an entry is selected
 var ENTRIES = [ 
 
-    startGame,
-    startEndlessMode
+    StartGame,
+    EndlessMode
 
     ];
 

File js/ship.js

View file
  • Ignore whitespace
         {
         if (event.keyCode === EVENT_KEY.enter) 
             {
-            startGame();
+            GAME_MODE();
             }
         });
     
     
     
     
+    
 Ship.prototype.handleClick = function( event )  
 {
 if ( !event )
 var position = Ship.all.indexOf( this );
 
 Ship.all.splice( position, 1 );
+
+$( this ).unbind();
 };
 
 

File js/start_game.js

View file
  • Ignore whitespace
+
+
+
+(function(window)
+{
+
+
+
+
+function StartGame() 
+{
+
+}
+
+
+
+
+StartGame.tick = function()
+{
+
+};
+
+
+
+window.StartGame = StartGame;
+
+}(window));