Reorganized code: created Guard class
[shapeshift.git] / shapeshift.js
index 4fb57d165692400dfa9ef594b57484e89f5dca20..44d42fda5f84286c9d04b0ce43e8beb141435604 100644 (file)
@@ -1,5 +1,3 @@
-    window.onload = function() {
-
         var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
         var ground;
         var platforms;
             }
         }, false);
 
+        class Guard extends Phaser.Sprite {
+            constructor(x, y, sprite, dialogues) {
+                //var guard = game.add.sprite(x, y, sprite);
+                super(game, x, y, sprite);
+                this.anchor.setTo(.5,.5);
+                this.scale.x = 0.5;
+                this.scale.y = this.scale.x;
+                this.dialogues = dialogues;
+                guards.add(this);
+            }
+
+            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;
+            }
+        }
+
         function sign(n) {
             if (n >= 0) { return 1 } else { return -1 };
         }
             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;
 
         }
 
-        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) {
             var textObject = game.add.text(entity.x, entity.y - (entity.height / 2) - 30, text, { align: 'center', fontSize: 14 });
             textObject.font = 'Ubuntu Mono';
             interaction_handler = interaction;
 
         }
-
-    };