Reorganized code: created Guard class
authorMegaBrutal <code+git@megabrutal.com>
Thu, 16 Mar 2017 00:05:30 +0000 (01:05 +0100)
committerMegaBrutal <code+git@megabrutal.com>
Thu, 16 Mar 2017 00:05:30 +0000 (01:05 +0100)
Previously guards were not defined as a distinct class.

Since I want to build a conveniently manageable codebase which
would make a nice framework for my future additions, I need to
revise the actual post-LD code heavily.
The goal is to create a clean, well thought-out object-oriented
framework which is extensible and easy to depend on.
This is the first change towards that state, and seeing how
messy the code currently is, there will be many more.

modified:   shapeshift.js

shapeshift.js

index 4fb57d1..44d42fd 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;
 
         }
-
-    };