Reorganized code: created Entity base class
[shapeshift.git] / shapeshift.js
index ed05a37577947edb0c4405782e0e28c27e2f9077..14b4fda470cdcb3ee4ed6b72c01fde2a890ac306 100644 (file)
@@ -1,6 +1,5 @@
-    window.onload = function() {
-
         var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
+        var ground;
         var platforms;
         var guards;
         var guard1, guard2;
             [ { actor: 'guard',  text: "(By the way, this is the end of the game.\nYou really can't do anything more.\nThanks for playing!)" } ];
 
 
+        window.addEventListener("keydown", function(e) {
+            // Prevent default browser action for arrows and spacebar
+            if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
+            e.preventDefault();
+            }
+        }, false);
+
+        class Entity extends Phaser.Sprite {
+            constructor(x, y, sprite) {
+                super(game, x, y, sprite);
+                this.anchor.setTo(.5,.5);
+                this.scale.x = 0.5;
+                this.scale.y = this.scale.x;
+            }
+
+            enablePhysics() {
+                game.physics.arcade.enable(this);
+                this.body.bounce.y = 0.2;
+                this.body.bounce.x = 0.2;
+                this.body.gravity.y = 300;
+                this.body.collideWorldBounds = true;
+            }
+        }
+
+        class Player extends Entity {
+            constructor(x, y, sprite) {
+                super(x, y, sprite);
+                this.anchor.setTo(.4,.5);
+                this.pushed = 0;
+                this.shape = SHAPE_GIRL;
+                this.learnedBird = false;
+            }
+        }
+
+        class Guard extends Entity {
+            constructor(x, y, sprite, dialogues) {
+                //var guard = game.add.sprite(x, y, sprite);
+                super(x, y, sprite);
+                this.dialogues = dialogues;
+                guards.add(this);
+            }
+        }
+
         function sign(n) {
             if (n >= 0) { return 1 } else { return -1 };
         }
 
         function preload () {
 
-            game.load.image('ground', 'ground.svg');
-            game.load.image('player_girl', 'csaj.svg');
-            game.load.image('player_goat', 'kecskecsaj.svg');
-            game.load.image('player_bird', 'bird.svg');
-            game.load.image('guard', 'guard.svg');
-            game.load.image('birdcage', 'birdcage.svg');
+            game.load.image('ground', 'ground.png');
+            game.load.image('player_girl', 'csaj.png');
+            game.load.image('player_goat', 'kecskecsaj.png');
+            game.load.image('player_bird', 'bird.png');
+            game.load.image('guard', 'guard.png');
+            game.load.image('birdcage', 'birdcage.png');
 
         }
 
             platforms.enableBody = true;
 
 
-            var ground = game.add.tileSprite(0, game.world.height - 80, game.world.width, 200, 'ground');
+            ground = game.add.tileSprite(0, game.world.height - 80, game.world.width, 200, 'ground');
             platforms.add(ground);
             ground.body.immovable = true;
 
 
-            player = game.add.sprite(64, game.world.height - 200, 'player_girl');
-            player.anchor.setTo(.4,.5);
-            player.scale.x = 0.5;
-            player.scale.y = player.scale.x;
-            game.physics.arcade.enable(player);
-            player.body.bounce.y = 0.2;
-            player.body.bounce.x = 0.2;
-            player.body.gravity.y = 300;
-            player.body.collideWorldBounds = true;
-            player.pushed = 0;
-            player.shape = SHAPE_GIRL;
-            player.learnedBird = false;
+            //player = game.add.sprite(64, game.world.height - 200, 'player_girl');
+            player = new Player(64, game.world.height - 200, 'player_girl');
+            player.enablePhysics();
+            game.add.existing(player);
             game.camera.follow(player);
 
             guards = game.add.group();
-            guard1 = createGuard(600, game.world.height - 200, 'guard',
+            guard1 = new Guard(600, game.world.height - 200, 'guard',
                 { girl: dialogue_guard1_girl, goat: dialogue_guard1_goat });
-            guard2 = createGuard(1000, game.world.height - 200, 'player_girl',
+            guard2 = new Guard(1000, game.world.height - 200, 'player_girl',
                 { girl: dialogue_guard2_girl, goat: dialogue_guard2_goat });
             guard2.scale.x = -guard2.scale.x;
-            guard3 = createGuard(1400, game.world.height - 200, 'guard',
+            guard3 = new Guard(1400, game.world.height - 200, 'guard',
                 { girl: dialogue_guard3_girl, goat: dialogue_guard3_goat, bird: dialogue_guard3_bird });
-            guard4 = createGuard(2400, game.world.height - 200, 'player_goat',
+            guard4 = new Guard(2400, game.world.height - 200, 'player_goat',
                 { girl: dialogue_guard4_girl, goat: dialogue_guard4_goat, bird: dialogue_guard4_girl });
             guard4.scale.x = -guard4.scale.x;
+            guards.children.forEach(function(guard) { guard.enablePhysics(); });
 
             birdcage = game.add.sprite(1100, game.world.height - 300, 'birdcage');
             birdcage.scale.x = 0.5;
                     player.shape = SHAPE_GIRL;
                     player.loadTexture('player_girl');
                 }
+
+                player.body.setSize(player.texture.width, player.texture.height);
+                if ((ground.y - player.y) < player.body.halfHeight)
+                    player.y = ground.y - player.body.halfHeight;
+
                 lastkeytime = game.time.now;
             }
 
 
         }
 
-        function createGuard(x, y, sprite, dialogues) {
-
-            var guard = game.add.sprite(x, y, sprite);
-            guard.anchor.setTo(.5,.5);
-            guard.scale.x = 0.5;
-            guard.scale.y = guard.scale.x;
-            game.physics.arcade.enable(guard);
-            guard.body.bounce.y = 0.2;
-            guard.body.bounce.x = 0.2;
-            guard.body.gravity.y = 300;
-            guard.body.collideWorldBounds = true;
-            guard.dialogues = dialogues;
-            guards.add(guard);
-            return guard;
-
-        }
-
-        function putText(entity, text, color = '#000000') {
-            var textObject = game.add.text(entity.x, entity.y - (entity.height / 2) - 30, text, { align: 'center', fill: color, fontSize: 14 });
+        function putText(entity, text) {
+            var textObject = game.add.text(entity.x, entity.y - (entity.height / 2) - 30, text, { align: 'center', fontSize: 14 });
             textObject.font = 'Ubuntu Mono';
             textObject.anchor.setTo(.5,.5);
             return textObject;
             interaction_handler = interaction;
 
         }
-
-    };