Add 'GamePlay' game state
[ld40.git] / ld40.js
diff --git a/ld40.js b/ld40.js
index 6a1eb77ea15a4a09b5d5c371c93c5941f4eb4ced..dffcd503ababccef2f6f680c10315ebcbc47f79e 100644 (file)
--- a/ld40.js
+++ b/ld40.js
@@ -1,4 +1,5 @@
-        var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
+        var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: function() { this.state.add('GamePlay', GamePlay, true); } });
+        var cursors;
         var player;
         var walls;
         var determinations;            // I know it's grammatically incorrect. :P
@@ -32,6 +33,9 @@
             }
         }, false);
 
+        function sign(n) {
+            if (n >= 0) { return 1 } else { return -1 };
+        }
 
         class Player extends Phaser.Text {
             constructor(x, y) {
@@ -44,7 +48,7 @@
                 this.lasttime_shoot = 0;
                 this.lasttime_mode = 0;
                 this.lasttime_damage = 0;
-                this.speed = 150;
+                this.speed = SPEED_PLAYER;
                 this.switchmode(MODE_DETERMINATION);
                 this.weapon = new JusticeBlaster(game, this);
                 this.weapon.bulletSpeed = SPEED_PPROJECTILE;
                 }
             }
 
+            kill() {
+                this.health = 0;
+                this.healthBar.update(this.love, this.maxHealth, this.health);
+                super.kill();
+            }
+
             loveUp() {
                 this.love++
                 this.maxHealth += this.love;
                 this.weapon = new JusticeBlaster(game, this);
                 this.weapon.bulletSpeed = SPEED_MPROJECTILE;
                 this.weapon.trackSprite(this);
+                this.shaketween = game.add.tween(this).to({ x: this.x + (sign(Math.random() - 0.5)) * 5 }, 10, Phaser.Easing.Sinusoidal.InOut, false, 0, 8, true);
             }
 
             enablePhysics() {
                 game.physics.arcade.enable(this);
             }
 
+            damage(amount) {
+                super.damage(amount);
+                if (this.alive) { this.shaketween.start(); }
+            }
+
             kill() {
                 super.kill();
                 player.loveUp();
             }
         }
 
-        function preload () {
-
-            game.load.image('wall', 'wall.png');
-            game.load.image('player', 'player.png');
-            game.load.image('monster', 'monster.png');
-            game.load.image('determination', 'determination.png');
-            game.load.image('projectile', 'projectile.png');
-
-        }
 
-        function create () {
+        class GamePlay extends Phaser.State {
 
-            game.world.setBounds(0, 0, 800, 600);
-            game.stage.backgroundColor = '#000000';
-            game.physics.startSystem(Phaser.Physics.ARCADE);
+            preload() {
 
-            walls = game.add.group();
-            walls.classType = Phaser.TileSprite;
-            walls.enableBody = true;
+                game.load.image('wall', 'wall.png');
+                game.load.image('player', 'player.png');
+                game.load.image('monster', 'monster.png');
+                game.load.image('determination', 'determination.png');
+                game.load.image('projectile', 'projectile.png');
 
-            walls.add(game.add.tileSprite(WALL_BORDER, WALL_BORDER, game.world.width - (WALL_BORDER * 2), WALL_THICKNESS, 'wall'));
-            walls.add(game.add.tileSprite(WALL_BORDER, game.world.height - WALL_THICKNESS - WALL_BORDERBOTTOM, game.world.width - (WALL_BORDER * 2), WALL_THICKNESS, 'wall'));
-            walls.add(game.add.tileSprite(WALL_BORDER, WALL_BORDER, WALL_THICKNESS, game.world.height - WALL_BORDER - WALL_BORDERBOTTOM, 'wall'));
-            walls.add(game.add.tileSprite(game.world.width - WALL_THICKNESS - WALL_BORDER, WALL_BORDER, WALL_THICKNESS, game.world.height - WALL_BORDER - WALL_BORDERBOTTOM, 'wall'));
-            walls.children.forEach(function(wall) { wall.body.immovable = true; });
+            }
 
-            determinations = game.add.group();
-            nexttime_determination = Math.random() * WAIT_DETERMINATION;
+            create() {
 
-            monsters = game.add.group();
-            monsters.add(new Monster(400, 500));
-            monsters.children.forEach(function(monster) { monster.enablePhysics(); });
-            nexttime_monsterspawn = WAIT_MONSTERSPAWN;
+                game.world.setBounds(0, 0, 800, 600);
+                game.stage.backgroundColor = '#000000';
+                game.physics.startSystem(Phaser.Physics.ARCADE);
 
-            player = new Player(game.world.width / 2, game.world.height / 2);
-            player.enablePhysics();
-            game.add.existing(player);
-            game.camera.follow(player);
-            cursors = game.input.keyboard.createCursorKeys();
+                walls = game.add.group();
+                walls.classType = Phaser.TileSprite;
+                walls.enableBody = true;
 
-            keyboard_handler = keyPress_default;
+                walls.add(game.add.tileSprite(WALL_BORDER, WALL_BORDER, game.world.width - (WALL_BORDER * 2), WALL_THICKNESS, 'wall'));
+                walls.add(game.add.tileSprite(WALL_BORDER, game.world.height - WALL_THICKNESS - WALL_BORDERBOTTOM, game.world.width - (WALL_BORDER * 2), WALL_THICKNESS, 'wall'));
+                walls.add(game.add.tileSprite(WALL_BORDER, WALL_BORDER, WALL_THICKNESS, game.world.height - WALL_BORDER - WALL_BORDERBOTTOM, 'wall'));
+                walls.add(game.add.tileSprite(game.world.width - WALL_THICKNESS - WALL_BORDER, WALL_BORDER, WALL_THICKNESS, game.world.height - WALL_BORDER - WALL_BORDERBOTTOM, 'wall'));
+                walls.children.forEach(function(wall) { wall.body.immovable = true; });
 
-        }
+                determinations = game.add.group();
+                nexttime_determination = Math.random() * WAIT_DETERMINATION;
 
-        function update () {
+                monsters = game.add.group();
+                nexttime_monsterspawn = WAIT_MONSTERSPAWN / 4;
 
-            game.physics.arcade.collide(player, walls);
-            game.physics.arcade.overlap(player, determinations, function(p,d) { p.overlap_determination(d); });
-            game.physics.arcade.overlap(player, monsters, function(p,m) { m.overlap_player(p); });
+                player = new Player(game.world.width / 2, game.world.height / 2);
+                player.enablePhysics();
+                game.add.existing(player);
+                game.camera.follow(player);
+                cursors = game.input.keyboard.createCursorKeys();
 
-            if (game.time.now > nexttime_determination)
-            {
-                var determination = new Determination((Math.random() * (game.world.width - ((WALL_THICKNESS + WALL_BORDER) * 4))) + ((WALL_THICKNESS + WALL_BORDER) * 2), (Math.random() * (game.world.height - ((WALL_THICKNESS + WALL_BORDER) * 4) - WALL_BORDERBOTTOM)) + ((WALL_THICKNESS + WALL_BORDER) * 2));
-                determination.enablePhysics();
-                determinations.add(determination);
-                nexttime_determination = game.time.now + (WAIT_DETERMINATION / 2) + (Math.random() * WAIT_DETERMINATION);
             }
 
-            if (game.time.now > nexttime_monsterspawn)
-            {
-                var monster = new Monster(Math.random() * game.world.width, Math.random() * game.world.height);
-                monster.enablePhysics();
-                monsters.add(monster);
-                nexttime_monsterspawn = game.time.now + (WAIT_MONSTERSPAWN / player.love) + (Math.random() * WAIT_MONSTERSPAWN);
-            }
+            update() {
 
-            if ((game.time.now - player.lastmovetime) > WAIT_KEY) player.body.velocity.x = player.body.velocity.y = 0;
-            if (keyboard_handler) keyboard_handler();
+                game.physics.arcade.collide(player, walls);
+                game.physics.arcade.overlap(player, determinations, function(p,d) { p.overlap_determination(d); });
+                game.physics.arcade.overlap(player, monsters, function(p,m) { m.overlap_player(p); });
 
-        }
+                if (game.time.now > nexttime_determination)
+                {
+                    var determination = new Determination((Math.random() * (game.world.width - ((WALL_THICKNESS + WALL_BORDER) * 4))) + ((WALL_THICKNESS + WALL_BORDER) * 2), (Math.random() * (game.world.height - ((WALL_THICKNESS + WALL_BORDER) * 4) - WALL_BORDERBOTTOM)) + ((WALL_THICKNESS + WALL_BORDER) * 2));
+                    determination.enablePhysics();
+                    determinations.add(determination);
+                    nexttime_determination = game.time.now + (WAIT_DETERMINATION / 2) + (Math.random() * WAIT_DETERMINATION);
+                }
 
-        function keyPress_default() {
+                if (game.time.now > nexttime_monsterspawn)
+                {
+                    var monster = new Monster(Math.random() * game.world.width, Math.random() * game.world.height);
+                    monster.enablePhysics();
+                    monsters.add(monster);
+                    var waittime = WAIT_MONSTERSPAWN - ((Math.random() * 2000) * player.love);
+                    nexttime_monsterspawn = game.time.now + waittime;
+                    console.log('Next monster in ' + waittime);
+                }
 
-            // Maybe needed later.
+                if ((game.time.now - player.lastmovetime) > WAIT_KEY) player.body.velocity.x = player.body.velocity.y = 0;
 
+            }
         }