[
  {
    "path": ".github/FUNDING.yml",
    "content": "# Support 'GitHub Sponsors' funding.\ngithub: dwmkerr\n"
  },
  {
    "path": ".github/workflows/main.yml",
    "content": "name: Publish to GitHub Pages\non:\n  push:\t\n    branches:\t\n      - master\njobs:\n  build-and-deploy:\n    runs-on: ubuntu-latest\n    steps:\n    - name: Checkout\n      uses: actions/checkout@master\n\n    - name: Build and Deploy\n      uses: JamesIves/github-pages-deploy-action@releases/v2\n      env:\n        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}\n        BASE_BRANCH: master       # Deploy from master...\n        BRANCH: gh-pages          # ...to GitHub pages...\n        BUILD_SCRIPT: make build  # ...run the build action...\n        FOLDER: artifacts/dist    # ...copy the distribution folder.\n"
  },
  {
    "path": ".gitignore",
    "content": "spaceinvaders.sublime-project\nspaceinvaders.sublime-workspace\n"
  },
  {
    "path": ".replit",
    "content": "language = \"html\"\nrun = \"\"\n"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2019 Dave Kerr\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# Space Invaders\n\nThe classic Space Invaders game written in JavaScript as a learning exercise.\n\nNo jQuery or any other third party libraries, just raw JavaScript, CSS and HTML.\n\nSee it Live: [https://dwmkerr.github.io/spaceinvaders/](https://dwmkerr.github.io/spaceinvaders/)\n\n[![Space Invaders Screenshot](./screenshot.jpg \"Space Invaders Screenshot\")](https://dwmkerr.github.io/spaceinvaders/)\n\n## Intro\n\n[![Run on Repl.it](https://repl.it/badge/github/dwmkerr/spaceinvaders)](https://repl.it/github/dwmkerr/spaceinvaders)\n\nWhat's there to say? It's Space Invaders in JavaScript!\n\nCreate the game, give it a `div` to draw to, tell it when the keyboard is mashed and that's all you need to add Space Invaders to a website.\n\nThis is a simple learning exercise, so the JavaScript is deliberate kept all one file. There's no linting, testing, CI, or anything like that. If you want to see such patterns in front-end JavaScript, check out something like [angular-modal-service](https://github.com/dwmkerr/angular-modal-service).\n\n## Adding Space Invaders to a Web Page\n\nFirst, drop the `spaceinvaders.js` file into the website.\n\nNow add a canvas to the page.\n\n```html\n<canvas id=\"gameCanvas\"></canvas>\n```\n\nNext, add the Space Invaders game code. You create the game, initialise it with the canvas, start it and make sure you tell it when a key is pressed or released. That's it!\n\n```html\n<script>\n//  Setup the canvas.\nvar canvas = document.getElementById(\"gameCanvas\");\ncanvas.width = 800;\ncanvas.height = 600;\n\n//  Create the game.\nvar game = new Game();\n\n//  Initialise it with the game canvas.\ngame.initialise(canvas);\n\n//  Start the game.\ngame.start();\n\n//  Listen for keyboard events.\nvar pressedKeys = [];\nwindow.addEventListener(\"keydown\", function keydown(e) {\n  var keycode = window.event.keycode || e.which;\n    if(!pressedKeys[keycode])\n      pressedKeys[keycode] = true;\n    //  Supress further processing of left/right/space (37/29/32)\n    if(keycode == 37 || keycode == 39 || keycode == 32) {\n      e.preventDefault();\n    }\n    game.keyDown(keycode);\n});\nwindow.addEventListener(\"keyup\", function keydown(e) {\n  var keycode = window.event.keycode || e.which;\n    if(pressedKeys[keycode])\n      delete pressedKeys[keycode];\n    game.keyUp(keycode);\n});\n</script>\n```\n\n## References\n\nOther bits and pieces that are useful can be dropped here.\n\n- The sounds came from [http://www.classicgaming.cc/classics/spaceinvaders/sounds.php](http://www.classicgaming.cc/classics/spaceinvaders/sounds.php)\n\n## Publishing\n\nOn changes to the `master` branch, the GitHub Pages site will be automatically updated.\n"
  },
  {
    "path": "css/core.css",
    "content": "/* core.css */\n/* lean and simple css reset, based on Tantek Celik's reset - see comments below. */\n/* (c) 2004-2010 Tantek Çelik. Some Rights Reserved. http://tantek.com */\n/* This style sheet is licensed under a Creative Commons License.      */\n/*             http://creativecommons.org/licenses/by/2.0              */\n\n/* Purpose: undo some of the default styling of common browsers        */\n\n\n:link,:visited,ins { text-decoration:none }\nnav ul,nav ol { list-style:none }\ndl,ul,ol,li,\nh1,h2,h3,h4,h5,h6,\nhtml,body,pre,p,blockquote,\nform,fieldset,input,label\n{ margin:0; padding:0 }\nabbr, img, object,\na img,:link img,:visited img,\na object,:link object,:visited object\n{ border:0 }\naddress,abbr { font-style:normal }\niframe:not(.auto-link) { display:none ! important; visibility:hidden ! important; margin-left: -10000px ! important  }\na {\n  color: #0088cc;\n  text-decoration: none;\n}\n\na:hover,\na:focus {\n  color: #005580;\n  text-decoration: underline;\n}"
  },
  {
    "path": "css/typeography.css",
    "content": "body {\n\tfont-family: \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n\tfont-size: 14px;\n\tline-height: 20px;\n\tcolor: #333333;\n}\n\nh1,\nh2,\nh3,\nh4,\nh5,\nh6 {\n  margin: 10px 0;\n  font-family: inherit;\n  font-weight: bold;\n  line-height: 20px;\n  color: inherit;\n  text-rendering: optimizelegibility;\n}\n\nh1,\nh2,\nh3 {\n  line-height: 40px;\n}\n\nh1 {\n  font-size: 38.5px;\n}\n\nh2 {\n  font-size: 31.5px;\n}\n\nh3 {\n  font-size: 24.5px;\n}\n\nh4 {\n  font-size: 17.5px;\n}\n\nh5 {\n  font-size: 14px;\n}\n\nh6 {\n  font-size: 11.9px;\n}"
  },
  {
    "path": "index.html",
    "content": "<!DOCTYPE html>\n<html>\n    <head>\n        <title>Space Invaders</title>\n        <link rel=\"stylesheet\" type=\"text/css\" href=\"css/core.css\">\n        <link rel=\"stylesheet\" type=\"text/css\" href=\"css/typeography.css\">\n        <style>\n    \n            /* Styling needed for a fullscreen canvas and no scrollbars. */\n            body, html { \n            width: 100%;\n            height: 100%;\n            margin: 0;\n            padding: 0;\n            overflow: hidden;\n            }\n\n            #starfield {\n                width:100%;\n                height:100%;\n                z-index: -1;\n                position: absolute;\n                left: 0px;\n                top: 0px;\n            }\n            #gamecontainer {\n                width: 800px;\n                margin-left: auto;\n                margin-right: auto;\n            }\n            #gamecanvas { \n                width: 800px;\n                height: 600px;\n            }\n            #info {\n                width: 800px;\n                margin-left: auto;\n                margin-right: auto;\n            }\n        </style>\n    </head>\n    <body>\n        <div id=\"starfield\"></div>\n        <div id=\"gamecontainer\">\n        <canvas id=\"gameCanvas\"></canvas>\n        </div>\n        <div id=\"info\">\n            <p>Move with arrow keys or swipe, fire with the space bar or touch. The invaders get faster and drop\n                more bombs as you complete each level!</p>\n            <p><a id=\"muteLink\" href=\"#\" onclick=\"toggleMute()\">mute</a> | \n                <a href=\"http://github.com/dwmkerr/spaceinvaders\">spaceinvaders on github</a> | \n                <a href=\"http://github.com/dwmkerr\">more projects</a> | <a href=\"http://www.dwmkerr.com\">dwmkerr.com</a></p>\n        </div>\n\n        <script src=\"js/starfield.js\"></script>\n        <script src=\"js/spaceinvaders.js\"></script>\n        <script>\n\n            //  Create the starfield.\n            var container = document.getElementById('starfield');\n            var starfield = new Starfield();\n            starfield.initialise(container);\n            starfield.start();\n\n            //  Setup the canvas.\n            var canvas = document.getElementById(\"gameCanvas\");\n            canvas.width = 800;\n            canvas.height = 600;\n\n            //  Create the game.\n            var game = new Game();\n\n            //  Initialise it with the game canvas.\n            game.initialise(canvas);\n\n            //  Start the game.\n            game.start();\n\n            //  Listen for keyboard events.\n            window.addEventListener(\"keydown\", function keydown(e) {\n                var keycode = e.which || window.event.keycode;\n                //  Supress further processing of left/right/space (37/29/32)\n                if(keycode == 37 || keycode == 39 || keycode == 32) {\n                    e.preventDefault();\n                }\n                game.keyDown(keycode);\n\n            });\n            window.addEventListener(\"keyup\", function keydown(e) {\n                var keycode = e.which || window.event.keycode;\n                game.keyUp(keycode);\n            });\n\n            window.addEventListener(\"touchstart\", function (e) {\n                game.touchstart(e);\n            }, false);\n \n            window.addEventListener('touchend', function(e){\n                game.touchend(e);\n            }, false);\n\n            window.addEventListener('touchmove', function(e){\n                game.touchmove(e);\n            }, false);\n\n            function toggleMute() {\n                game.mute();\n                document.getElementById(\"muteLink\").innerText = game.sounds.mute ? \"unmute\" : \"mute\";\n            }\n        </script>\n        <script type=\"text/javascript\">\n\n  var _gaq = _gaq || [];\n  _gaq.push(['_setAccount', 'UA-41728580-1']);\n  _gaq.push(['_trackPageview']);\n\n  (function() {\n    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n  })();\n\n</script>\n    </body>\n</html>\n"
  },
  {
    "path": "js/spaceinvaders.js",
    "content": "/*\n  spaceinvaders.js\n\n  the core logic for the space invaders game.\n\n*/\n\n/*  \n    Game Class\n\n    The Game class represents a Space Invaders game.\n    Create an instance of it, change any of the default values\n    in the settings, and call 'start' to run the game.\n\n    Call 'initialise' before 'start' to set the canvas the game\n    will draw to.\n\n    Call 'moveShip' or 'shipFire' to control the ship.\n\n    Listen for 'gameWon' or 'gameLost' events to handle the game\n    ending.\n*/\n\n//  Constants for the keyboard.\nvar KEY_LEFT = 37;\nvar KEY_RIGHT = 39;\nvar KEY_SPACE = 32;\n\n//  Creates an instance of the Game class.\nfunction Game() {\n\n    //  Set the initial config.\n    this.config = {\n        bombRate: 0.05,\n        bombMinVelocity: 50,\n        bombMaxVelocity: 50,\n        invaderInitialVelocity: 25,\n        invaderAcceleration: 0,\n        invaderDropDistance: 20,\n        rocketVelocity: 120,\n        rocketMaxFireRate: 2,\n        gameWidth: 400,\n        gameHeight: 300,\n        fps: 50,\n        debugMode: false,\n        invaderRanks: 5,\n        invaderFiles: 10,\n        shipSpeed: 120,\n        levelDifficultyMultiplier: 0.2,\n        pointsPerInvader: 5,\n        limitLevelIncrease: 25\n    };\n\n    //  All state is in the variables below.\n    this.lives = 3;\n    this.width = 0;\n    this.height = 0;\n    this.gameBounds = {left: 0, top: 0, right: 0, bottom: 0};\n    this.intervalId = 0;\n    this.score = 0;\n    this.level = 1;\n\n    //  The state stack.\n    this.stateStack = [];\n\n    //  Input/output\n    this.pressedKeys = {};\n    this.gameCanvas =  null;\n\n    //  All sounds.\n    this.sounds = null;\n\n    //  The previous x position, used for touch.\n    this.previousX = 0;\n}\n\n//  Initialis the Game with a canvas.\nGame.prototype.initialise = function(gameCanvas) {\n\n    //  Set the game canvas.\n    this.gameCanvas = gameCanvas;\n\n    //  Set the game width and height.\n    this.width = gameCanvas.width;\n    this.height = gameCanvas.height;\n\n    //  Set the state game bounds.\n    this.gameBounds = {\n        left: gameCanvas.width / 2 - this.config.gameWidth / 2,\n        right: gameCanvas.width / 2 + this.config.gameWidth / 2,\n        top: gameCanvas.height / 2 - this.config.gameHeight / 2,\n        bottom: gameCanvas.height / 2 + this.config.gameHeight / 2,\n    };\n};\n\nGame.prototype.moveToState = function(state) {\n \n   //  If we are in a state, leave it.\n   if(this.currentState() && this.currentState().leave) {\n     this.currentState().leave(game);\n     this.stateStack.pop();\n   }\n   \n   //  If there's an enter function for the new state, call it.\n   if(state.enter) {\n     state.enter(game);\n   }\n \n   //  Set the current state.\n   this.stateStack.pop();\n   this.stateStack.push(state);\n };\n\n//  Start the Game.\nGame.prototype.start = function() {\n\n    //  Move into the 'welcome' state.\n    this.moveToState(new WelcomeState());\n\n    //  Set the game variables.\n    this.lives = 3;\n    this.config.debugMode = /debug=true/.test(window.location.href);\n\n    //  Start the game loop.\n    var game = this;\n    this.intervalId = setInterval(function () { GameLoop(game);}, 1000 / this.config.fps);\n\n};\n\n//  Returns the current state.\nGame.prototype.currentState = function() {\n    return this.stateStack.length > 0 ? this.stateStack[this.stateStack.length - 1] : null;\n};\n\n//  Mutes or unmutes the game.\nGame.prototype.mute = function(mute) {\n\n    //  If we've been told to mute, mute.\n    if(mute === true) {\n        this.sounds.mute = true;\n    } else if (mute === false) {\n        this.sounds.mute = false;\n    } else {\n        // Toggle mute instead...\n        this.sounds.mute = this.sounds.mute ? false : true;\n    }\n};\n\n//  The main loop.\nfunction GameLoop(game) {\n    var currentState = game.currentState();\n    if(currentState) {\n\n        //  Delta t is the time to update/draw.\n        var dt = 1 / game.config.fps;\n\n        //  Get the drawing context.\n        var ctx = this.gameCanvas.getContext(\"2d\");\n        \n        //  Update if we have an update function. Also draw\n        //  if we have a draw function.\n        if(currentState.update) {\n            currentState.update(game, dt);\n        }\n        if(currentState.draw) {\n            currentState.draw(game, dt, ctx);\n        }\n    }\n}\n\nGame.prototype.pushState = function(state) {\n\n    //  If there's an enter function for the new state, call it.\n    if(state.enter) {\n        state.enter(game);\n    }\n    //  Set the current state.\n    this.stateStack.push(state);\n};\n\nGame.prototype.popState = function() {\n\n    //  Leave and pop the state.\n    if(this.currentState()) {\n        if(this.currentState().leave) {\n            this.currentState().leave(game);\n        }\n\n        //  Set the current state.\n        this.stateStack.pop();\n    }\n};\n\n//  The stop function stops the game.\nGame.prototype.stop = function Stop() {\n    clearInterval(this.intervalId);\n};\n\n//  Inform the game a key is down.\nGame.prototype.keyDown = function(keyCode) {\n    this.pressedKeys[keyCode] = true;\n    //  Delegate to the current state too.\n    if(this.currentState() && this.currentState().keyDown) {\n        this.currentState().keyDown(this, keyCode);\n    }\n};\n\nGame.prototype.touchstart = function(s) {\n    if(this.currentState() && this.currentState().keyDown) {\n        this.currentState().keyDown(this, KEY_SPACE);\n    }    \n};\n\nGame.prototype.touchend = function(s) {\n    delete this.pressedKeys[KEY_RIGHT];\n    delete this.pressedKeys[KEY_LEFT];\n};\n\nGame.prototype.touchmove = function(e) {\n\tvar currentX = e.changedTouches[0].pageX;\n    if (this.previousX > 0) {\n        if (currentX > this.previousX) {\n            delete this.pressedKeys[KEY_LEFT];\n            this.pressedKeys[KEY_RIGHT] = true;\n        } else {\n            delete this.pressedKeys[KEY_RIGHT];\n            this.pressedKeys[KEY_LEFT] = true;\n        }\n    }\n    this.previousX = currentX;\n};\n\n//  Inform the game a key is up.\nGame.prototype.keyUp = function(keyCode) {\n    delete this.pressedKeys[keyCode];\n    //  Delegate to the current state too.\n    if(this.currentState() && this.currentState().keyUp) {\n        this.currentState().keyUp(this, keyCode);\n    }\n};\n\nfunction WelcomeState() {\n\n}\n\nWelcomeState.prototype.enter = function(game) {\n\n    // Create and load the sounds.\n    game.sounds = new Sounds();\n    game.sounds.init();\n    game.sounds.loadSound('shoot', 'sounds/shoot.wav');\n    game.sounds.loadSound('bang', 'sounds/bang.wav');\n    game.sounds.loadSound('explosion', 'sounds/explosion.wav');\n};\n\nWelcomeState.prototype.update = function (game, dt) {\n\n\n};\n\nWelcomeState.prototype.draw = function(game, dt, ctx) {\n\n    //  Clear the background.\n    ctx.clearRect(0, 0, game.width, game.height);\n\n    ctx.font=\"30px Arial\";\n    ctx.fillStyle = '#ffffff';\n    ctx.textBaseline=\"middle\"; \n    ctx.textAlign=\"center\"; \n    ctx.fillText(\"Space Invaders\", game.width / 2, game.height/2 - 40); \n    ctx.font=\"16px Arial\";\n\n    ctx.fillText(\"Press 'Space' or touch to start.\", game.width / 2, game.height/2); \n};\n\nWelcomeState.prototype.keyDown = function(game, keyCode) {\n    if(keyCode == KEY_SPACE) {\n        //  Space starts the game.\n        game.level = 1;\n        game.score = 0;\n        game.lives = 3;\n        game.moveToState(new LevelIntroState(game.level));\n    }\n};\n\nfunction GameOverState() {\n\n}\n\nGameOverState.prototype.update = function(game, dt) {\n\n};\n\nGameOverState.prototype.draw = function(game, dt, ctx) {\n\n    //  Clear the background.\n    ctx.clearRect(0, 0, game.width, game.height);\n\n    ctx.font=\"30px Arial\";\n    ctx.fillStyle = '#ffffff';\n    ctx.textBaseline=\"center\"; \n    ctx.textAlign=\"center\"; \n    ctx.fillText(\"Game Over!\", game.width / 2, game.height/2 - 40); \n    ctx.font=\"16px Arial\";\n    ctx.fillText(\"You scored \" + game.score + \" and got to level \" + game.level, game.width / 2, game.height/2);\n    ctx.font=\"16px Arial\";\n    ctx.fillText(\"Press 'Space' to play again.\", game.width / 2, game.height/2 + 40);   \n};\n\nGameOverState.prototype.keyDown = function(game, keyCode) {\n    if(keyCode == KEY_SPACE) {\n        //  Space restarts the game.\n        game.lives = 3;\n        game.score = 0;\n        game.level = 1;\n        game.moveToState(new LevelIntroState(1));\n    }\n};\n\n//  Create a PlayState with the game config and the level you are on.\nfunction PlayState(config, level) {\n    this.config = config;\n    this.level = level;\n\n    //  Game state.\n    this.invaderCurrentVelocity =  10;\n    this.invaderCurrentDropDistance =  0;\n    this.invadersAreDropping =  false;\n    this.lastRocketTime = null;\n\n    //  Game entities.\n    this.ship = null;\n    this.invaders = [];\n    this.rockets = [];\n    this.bombs = [];\n}\n\nPlayState.prototype.enter = function(game) {\n\n    //  Create the ship.\n    this.ship = new Ship(game.width / 2, game.gameBounds.bottom);\n\n    //  Setup initial state.\n    this.invaderCurrentVelocity =  10;\n    this.invaderCurrentDropDistance =  0;\n    this.invadersAreDropping =  false;\n\n    //  Set the ship speed for this level, as well as invader params.\n    var levelMultiplier = this.level * this.config.levelDifficultyMultiplier;\n    var limitLevel = (this.level < this.config.limitLevelIncrease ? this.level : this.config.limitLevelIncrease);\n    this.shipSpeed = this.config.shipSpeed;\n    this.invaderInitialVelocity = this.config.invaderInitialVelocity + 1.5 * (levelMultiplier * this.config.invaderInitialVelocity);\n    this.bombRate = this.config.bombRate + (levelMultiplier * this.config.bombRate);\n    this.bombMinVelocity = this.config.bombMinVelocity + (levelMultiplier * this.config.bombMinVelocity);\n    this.bombMaxVelocity = this.config.bombMaxVelocity + (levelMultiplier * this.config.bombMaxVelocity);\n    this.rocketMaxFireRate = this.config.rocketMaxFireRate + 0.4 * limitLevel;\n\n    //  Create the invaders.\n    var ranks = this.config.invaderRanks + 0.1 * limitLevel;\n    var files = this.config.invaderFiles + 0.2 * limitLevel;\n    var invaders = [];\n    for(var rank = 0; rank < ranks; rank++){\n        for(var file = 0; file < files; file++) {\n            invaders.push(new Invader(\n                (game.width / 2) + ((files/2 - file) * 200 / files),\n                (game.gameBounds.top + rank * 20),\n                rank, file, 'Invader'));\n        }\n    }\n    this.invaders = invaders;\n    this.invaderCurrentVelocity = this.invaderInitialVelocity;\n    this.invaderVelocity = {x: -this.invaderInitialVelocity, y:0};\n    this.invaderNextVelocity = null;\n};\n\nPlayState.prototype.update = function(game, dt) {\n    \n    //  If the left or right arrow keys are pressed, move\n    //  the ship. Check this on ticks rather than via a keydown\n    //  event for smooth movement, otherwise the ship would move\n    //  more like a text editor caret.\n    if(game.pressedKeys[KEY_LEFT]) {\n        this.ship.x -= this.shipSpeed * dt;\n    }\n    if(game.pressedKeys[KEY_RIGHT]) {\n        this.ship.x += this.shipSpeed * dt;\n    }\n    if(game.pressedKeys[KEY_SPACE]) {\n        this.fireRocket();\n    }\n\n    //  Keep the ship in bounds.\n    if(this.ship.x < game.gameBounds.left) {\n        this.ship.x = game.gameBounds.left;\n    }\n    if(this.ship.x > game.gameBounds.right) {\n        this.ship.x = game.gameBounds.right;\n    }\n\n    //  Move each bomb.\n    for(var i=0; i<this.bombs.length; i++) {\n        var bomb = this.bombs[i];\n        bomb.y += dt * bomb.velocity;\n\n        //  If the rocket has gone off the screen remove it.\n        if(bomb.y > this.height) {\n            this.bombs.splice(i--, 1);\n        }\n    }\n\n    //  Move each rocket.\n    for(i=0; i<this.rockets.length; i++) {\n        var rocket = this.rockets[i];\n        rocket.y -= dt * rocket.velocity;\n\n        //  If the rocket has gone off the screen remove it.\n        if(rocket.y < 0) {\n            this.rockets.splice(i--, 1);\n        }\n    }\n\n    //  Move the invaders.\n    var hitLeft = false, hitRight = false, hitBottom = false;\n    for(i=0; i<this.invaders.length; i++) {\n        var invader = this.invaders[i];\n        var newx = invader.x + this.invaderVelocity.x * dt;\n        var newy = invader.y + this.invaderVelocity.y * dt;\n        if(hitLeft == false && newx < game.gameBounds.left) {\n            hitLeft = true;\n        }\n        else if(hitRight == false && newx > game.gameBounds.right) {\n            hitRight = true;\n        }\n        else if(hitBottom == false && newy > game.gameBounds.bottom) {\n            hitBottom = true;\n        }\n\n        if(!hitLeft && !hitRight && !hitBottom) {\n            invader.x = newx;\n            invader.y = newy;\n        }\n    }\n\n    //  Update invader velocities.\n    if(this.invadersAreDropping) {\n        this.invaderCurrentDropDistance += this.invaderVelocity.y * dt;\n        if(this.invaderCurrentDropDistance >= this.config.invaderDropDistance) {\n            this.invadersAreDropping = false;\n            this.invaderVelocity = this.invaderNextVelocity;\n            this.invaderCurrentDropDistance = 0;\n        }\n    }\n    //  If we've hit the left, move down then right.\n    if(hitLeft) {\n        this.invaderCurrentVelocity += this.config.invaderAcceleration;\n        this.invaderVelocity = {x: 0, y:this.invaderCurrentVelocity };\n        this.invadersAreDropping = true;\n        this.invaderNextVelocity = {x: this.invaderCurrentVelocity , y:0};\n    }\n    //  If we've hit the right, move down then left.\n    if(hitRight) {\n        this.invaderCurrentVelocity += this.config.invaderAcceleration;\n        this.invaderVelocity = {x: 0, y:this.invaderCurrentVelocity };\n        this.invadersAreDropping = true;\n        this.invaderNextVelocity = {x: -this.invaderCurrentVelocity , y:0};\n    }\n    //  If we've hit the bottom, it's game over.\n    if(hitBottom) {\n        game.lives = 0;\n    }\n    \n    //  Check for rocket/invader collisions.\n    for(i=0; i<this.invaders.length; i++) {\n        var invader = this.invaders[i];\n        var bang = false;\n\n        for(var j=0; j<this.rockets.length; j++){\n            var rocket = this.rockets[j];\n\n            if(rocket.x >= (invader.x - invader.width/2) && rocket.x <= (invader.x + invader.width/2) &&\n                rocket.y >= (invader.y - invader.height/2) && rocket.y <= (invader.y + invader.height/2)) {\n                \n                //  Remove the rocket, set 'bang' so we don't process\n                //  this rocket again.\n                this.rockets.splice(j--, 1);\n                bang = true;\n                game.score += this.config.pointsPerInvader;\n                break;\n            }\n        }\n        if(bang) {\n            this.invaders.splice(i--, 1);\n            game.sounds.playSound('bang');\n        }\n    }\n\n    //  Find all of the front rank invaders.\n    var frontRankInvaders = {};\n    for(var i=0; i<this.invaders.length; i++) {\n        var invader = this.invaders[i];\n        //  If we have no invader for game file, or the invader\n        //  for game file is futher behind, set the front\n        //  rank invader to game one.\n        if(!frontRankInvaders[invader.file] || frontRankInvaders[invader.file].rank < invader.rank) {\n            frontRankInvaders[invader.file] = invader;\n        }\n    }\n\n    //  Give each front rank invader a chance to drop a bomb.\n    for(var i=0; i<this.config.invaderFiles; i++) {\n        var invader = frontRankInvaders[i];\n        if(!invader) continue;\n        var chance = this.bombRate * dt;\n        if(chance > Math.random()) {\n            //  Fire!\n            this.bombs.push(new Bomb(invader.x, invader.y + invader.height / 2, \n                this.bombMinVelocity + Math.random()*(this.bombMaxVelocity - this.bombMinVelocity)));\n        }\n    }\n\n    //  Check for bomb/ship collisions.\n    for(var i=0; i<this.bombs.length; i++) {\n        var bomb = this.bombs[i];\n        if(bomb.x >= (this.ship.x - this.ship.width/2) && bomb.x <= (this.ship.x + this.ship.width/2) &&\n                bomb.y >= (this.ship.y - this.ship.height/2) && bomb.y <= (this.ship.y + this.ship.height/2)) {\n            this.bombs.splice(i--, 1);\n            game.lives--;\n            game.sounds.playSound('explosion');\n        }\n                \n    }\n\n    //  Check for invader/ship collisions.\n    for(var i=0; i<this.invaders.length; i++) {\n        var invader = this.invaders[i];\n        if((invader.x + invader.width/2) > (this.ship.x - this.ship.width/2) && \n            (invader.x - invader.width/2) < (this.ship.x + this.ship.width/2) &&\n            (invader.y + invader.height/2) > (this.ship.y - this.ship.height/2) &&\n            (invader.y - invader.height/2) < (this.ship.y + this.ship.height/2)) {\n            //  Dead by collision!\n            game.lives = 0;\n            game.sounds.playSound('explosion');\n        }\n    }\n\n    //  Check for failure\n    if(game.lives <= 0) {\n        game.moveToState(new GameOverState());\n    }\n\n    //  Check for victory\n    if(this.invaders.length === 0) {\n        game.score += this.level * 50;\n        game.level += 1;\n        game.moveToState(new LevelIntroState(game.level));\n    }\n};\n\nPlayState.prototype.draw = function(game, dt, ctx) {\n\n    //  Clear the background.\n    ctx.clearRect(0, 0, game.width, game.height);\n    \n    //  Draw ship.\n    ctx.fillStyle = '#999999';\n    ctx.fillRect(this.ship.x - (this.ship.width / 2), this.ship.y - (this.ship.height / 2), this.ship.width, this.ship.height);\n\n    //  Draw invaders.\n    ctx.fillStyle = '#006600';\n    for(var i=0; i<this.invaders.length; i++) {\n        var invader = this.invaders[i];\n        ctx.fillRect(invader.x - invader.width/2, invader.y - invader.height/2, invader.width, invader.height);\n    }\n\n    //  Draw bombs.\n    ctx.fillStyle = '#ff5555';\n    for(var i=0; i<this.bombs.length; i++) {\n        var bomb = this.bombs[i];\n        ctx.fillRect(bomb.x - 2, bomb.y - 2, 4, 4);\n    }\n\n    //  Draw rockets.\n    ctx.fillStyle = '#ff0000';\n    for(var i=0; i<this.rockets.length; i++) {\n        var rocket = this.rockets[i];\n        ctx.fillRect(rocket.x, rocket.y - 2, 1, 4);\n    }\n\n    //  Draw info.\n    var textYpos = game.gameBounds.bottom + ((game.height - game.gameBounds.bottom) / 2) + 14/2;\n    ctx.font=\"14px Arial\";\n    ctx.fillStyle = '#ffffff';\n    var info = \"Lives: \" + game.lives;\n    ctx.textAlign = \"left\";\n    ctx.fillText(info, game.gameBounds.left, textYpos);\n    info = \"Score: \" + game.score + \", Level: \" + game.level;\n    ctx.textAlign = \"right\";\n    ctx.fillText(info, game.gameBounds.right, textYpos);\n\n    //  If we're in debug mode, draw bounds.\n    if(this.config.debugMode) {\n        ctx.strokeStyle = '#ff0000';\n        ctx.strokeRect(0,0,game.width, game.height);\n        ctx.strokeRect(game.gameBounds.left, game.gameBounds.top,\n            game.gameBounds.right - game.gameBounds.left,\n            game.gameBounds.bottom - game.gameBounds.top);\n    }\n\n};\n\nPlayState.prototype.keyDown = function(game, keyCode) {\n\n    if(keyCode == KEY_SPACE) {\n        //  Fire!\n        this.fireRocket();\n    }\n    if(keyCode == 80) {\n        //  Push the pause state.\n        game.pushState(new PauseState());\n    }\n};\n\nPlayState.prototype.keyUp = function(game, keyCode) {\n\n};\n\nPlayState.prototype.fireRocket = function() {\n    //  If we have no last rocket time, or the last rocket time \n    //  is older than the max rocket rate, we can fire.\n    if(this.lastRocketTime === null || ((new Date()).valueOf() - this.lastRocketTime) > (1000 / this.rocketMaxFireRate))\n    {   \n        //  Add a rocket.\n        this.rockets.push(new Rocket(this.ship.x, this.ship.y - 12, this.config.rocketVelocity));\n        this.lastRocketTime = (new Date()).valueOf();\n\n        //  Play the 'shoot' sound.\n        game.sounds.playSound('shoot');\n    }\n};\n\nfunction PauseState() {\n\n}\n\nPauseState.prototype.keyDown = function(game, keyCode) {\n\n    if(keyCode == 80) {\n        //  Pop the pause state.\n        game.popState();\n    }\n};\n\nPauseState.prototype.draw = function(game, dt, ctx) {\n\n    //  Clear the background.\n    ctx.clearRect(0, 0, game.width, game.height);\n\n    ctx.font=\"14px Arial\";\n    ctx.fillStyle = '#ffffff';\n    ctx.textBaseline=\"middle\";\n    ctx.textAlign=\"center\";\n    ctx.fillText(\"Paused\", game.width / 2, game.height/2);\n    return;\n};\n\n/*  \n    Level Intro State\n\n    The Level Intro state shows a 'Level X' message and\n    a countdown for the level.\n*/\nfunction LevelIntroState(level) {\n    this.level = level;\n    this.countdownMessage = \"3\";\n}\n\nLevelIntroState.prototype.update = function(game, dt) {\n\n    //  Update the countdown.\n    if(this.countdown === undefined) {\n        this.countdown = 3; // countdown from 3 secs\n    }\n    this.countdown -= dt;\n\n    if(this.countdown < 2) { \n        this.countdownMessage = \"2\"; \n    }\n    if(this.countdown < 1) { \n        this.countdownMessage = \"1\"; \n    } \n    if(this.countdown <= 0) {\n        //  Move to the next level, popping this state.\n        game.moveToState(new PlayState(game.config, this.level));\n    }\n\n};\n\nLevelIntroState.prototype.draw = function(game, dt, ctx) {\n\n    //  Clear the background.\n    ctx.clearRect(0, 0, game.width, game.height);\n\n    ctx.font=\"36px Arial\";\n    ctx.fillStyle = '#ffffff';\n    ctx.textBaseline=\"middle\"; \n    ctx.textAlign=\"center\"; \n    ctx.fillText(\"Level \" + this.level, game.width / 2, game.height/2);\n    ctx.font=\"24px Arial\";\n    ctx.fillText(\"Ready in \" + this.countdownMessage, game.width / 2, game.height/2 + 36);      \n    return;\n};\n\n\n/*\n \n  Ship\n\n  The ship has a position and that's about it.\n\n*/\nfunction Ship(x, y) {\n    this.x = x;\n    this.y = y;\n    this.width = 20;\n    this.height = 16;\n}\n\n/*\n    Rocket\n\n    Fired by the ship, they've got a position, velocity and state.\n\n    */\nfunction Rocket(x, y, velocity) {\n    this.x = x;\n    this.y = y;\n    this.velocity = velocity;\n}\n\n/*\n    Bomb\n\n    Dropped by invaders, they've got position, velocity.\n\n*/\nfunction Bomb(x, y, velocity) {\n    this.x = x;\n    this.y = y;\n    this.velocity = velocity;\n}\n \n/*\n    Invader \n\n    Invader's have position, type, rank/file and that's about it. \n*/\n\nfunction Invader(x, y, rank, file, type) {\n    this.x = x;\n    this.y = y;\n    this.rank = rank;\n    this.file = file;\n    this.type = type;\n    this.width = 18;\n    this.height = 14;\n}\n\n/*\n    Game State\n\n    A Game State is simply an update and draw proc.\n    When a game is in the state, the update and draw procs are\n    called, with a dt value (dt is delta time, i.e. the number)\n    of seconds to update or draw).\n\n*/\nfunction GameState(updateProc, drawProc, keyDown, keyUp, enter, leave) {\n    this.updateProc = updateProc;\n    this.drawProc = drawProc;\n    this.keyDown = keyDown;\n    this.keyUp = keyUp;\n    this.enter = enter;\n    this.leave = leave;\n}\n\n/*\n\n    Sounds\n\n    The sounds class is used to asynchronously load sounds and allow\n    them to be played.\n\n*/\nfunction Sounds() {\n\n    //  The audio context.\n    this.audioContext = null;\n\n    //  The actual set of loaded sounds.\n    this.sounds = {};\n}\n\nSounds.prototype.init = function() {\n\n    //  Create the audio context, paying attention to webkit browsers.\n    context = window.AudioContext || window.webkitAudioContext;\n    this.audioContext = new context();\n    this.mute = false;\n};\n\nSounds.prototype.loadSound = function(name, url) {\n\n    //  Reference to ourselves for closures.\n    var self = this;\n\n    //  Create an entry in the sounds object.\n    this.sounds[name] = null;\n\n    //  Create an asynchronous request for the sound.\n    var req = new XMLHttpRequest();\n    req.open('GET', url, true);\n    req.responseType = 'arraybuffer';\n    req.onload = function() {\n        self.audioContext.decodeAudioData(req.response, function(buffer) {\n            self.sounds[name] = {buffer: buffer};\n        });\n    };\n    try {\n      req.send();\n    } catch(e) {\n      console.log(\"An exception occured getting sound the sound \" + name + \" this might be \" +\n         \"because the page is running from the file system, not a webserver.\");\n      console.log(e);\n    }\n};\n\nSounds.prototype.playSound = function(name) {\n\n    //  If we've not got the sound, don't bother playing it.\n    if(this.sounds[name] === undefined || this.sounds[name] === null || this.mute === true) {\n        return;\n    }\n\n    //  Create a sound source, set the buffer, connect to the speakers and\n    //  play the sound.\n    var source = this.audioContext.createBufferSource();\n    source.buffer = this.sounds[name].buffer;\n    source.connect(this.audioContext.destination);\n    source.start(0);\n};\n"
  },
  {
    "path": "js/starfield.js",
    "content": "/*\n\tStarfield lets you take a div and turn it into a starfield.\n\n*/\n\n//\tDefine the starfield class.\nfunction Starfield() {\n\tthis.fps = 30;\n\tthis.canvas = null;\n\tthis.width = 0;\n\tthis.width = 0;\n\tthis.minVelocity = 15;\n\tthis.maxVelocity = 30;\n\tthis.stars = 100;\n\tthis.intervalId = 0;\n}\n\n//\tThe main function - initialises the starfield.\nStarfield.prototype.initialise = function(div) {\n\tvar self = this;\n\n\t//\tStore the div.\n\tthis.containerDiv = div;\n\tself.width = window.innerWidth;\n\tself.height = window.innerHeight;\n\n\twindow.onresize = function(event) {\n\t\tself.width = window.innerWidth;\n\t\tself.height = window.innerHeight;\n\t\tself.canvas.width = self.width;\n\t\tself.canvas.height = self.height;\n\t\tself.draw();\n \t}\n\n\t//\tCreate the canvas.\n\tvar canvas = document.createElement('canvas');\n\tdiv.appendChild(canvas);\n\tthis.canvas = canvas;\n\tthis.canvas.width = this.width;\n\tthis.canvas.height = this.height;\n};\n\nStarfield.prototype.start = function() {\n\n\t//\tCreate the stars.\n\tvar stars = [];\n\tfor(var i=0; i<this.stars; i++) {\n\t\tstars[i] = new Star(Math.random()*this.width, Math.random()*this.height, Math.random()*3+1,\n\t\t (Math.random()*(this.maxVelocity - this.minVelocity))+this.minVelocity);\n\t}\n\tthis.stars = stars;\n\n\tvar self = this;\n\t//\tStart the timer.\n\tthis.intervalId = setInterval(function() {\n\t\tself.update();\n\t\tself.draw();\t\n\t}, 1000 / this.fps);\n};\n\nStarfield.prototype.stop = function() {\n\tclearInterval(this.intervalId);\n};\n\nStarfield.prototype.update = function() {\n\tvar dt = 1 / this.fps;\n\n\tfor(var i=0; i<this.stars.length; i++) {\n\t\tvar star = this.stars[i];\n\t\tstar.y += dt * star.velocity;\n\t\t//\tIf the star has moved from the bottom of the screen, spawn it at the top.\n\t\tif(star.y > this.height) {\n\t\t\tthis.stars[i] = new Star(Math.random()*this.width, 0, Math.random()*3+1, \n\t\t \t(Math.random()*(this.maxVelocity - this.minVelocity))+this.minVelocity);\n\t\t}\n\t}\n};\n\nStarfield.prototype.draw = function() {\n\n\t//\tGet the drawing context.\n\tvar ctx = this.canvas.getContext(\"2d\");\n\n\t//\tDraw the background.\n \tctx.fillStyle = '#000000';\n\tctx.fillRect(0, 0, this.width, this.height);\n\n\t//\tDraw stars.\n\tctx.fillStyle = '#ffffff';\n\tfor(var i=0; i<this.stars.length;i++) {\n\t\tvar star = this.stars[i];\n\t\tctx.fillRect(star.x, star.y, star.size, star.size);\n\t}\n};\n\nfunction Star(x, y, size, velocity) {\n\tthis.x = x;\n\tthis.y = y; \n\tthis.size = size;\n\tthis.velocity = velocity;\n}\n"
  },
  {
    "path": "makefile",
    "content": "build:\n\t# Cleanup the artifacts folder, create the distribution location.\n\trm -rf artifacts || true\n\tmkdir artifacts\n\tmkdir artifacts/dist\n\n\t# Copy over the static site files.\n\tcp -r index.html css js sounds artifacts/dist/.\n\n.PHONY: build\n"
  }
]