From: MegaBrutal Date: Thu, 16 Mar 2017 00:05:30 +0000 (+0100) Subject: Reorganized code: created Guard class X-Git-Url: http://git.megabrutal.com/?p=shapeshift.git;a=commitdiff_plain;h=9646274677048d57e16a58389ae8f14e29459bb0 Reorganized code: created Guard class 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 --- diff --git a/shapeshift.js b/shapeshift.js index 4fb57d1..44d42fd 100644 --- a/shapeshift.js +++ b/shapeshift.js @@ -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; @@ -65,6 +63,26 @@ } }, 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 }; } @@ -111,16 +129,17 @@ 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; @@ -225,23 +244,6 @@ } - 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'; @@ -320,5 +322,3 @@ interaction_handler = interaction; } - - };