├── .gitignore ├── HealthBar.js ├── HealthBar.standalone.js ├── README.md ├── example ├── .bowerrc ├── .editorconfig ├── .jshintrc ├── Gruntfile.js ├── assets │ └── plus_minus.png ├── bower.json ├── config.json ├── dist │ ├── assets │ │ └── plus_minus.png │ ├── css │ │ └── styles.css │ ├── index.html │ └── js │ │ ├── game.js │ │ ├── phaser.js │ │ └── phaser.min.js ├── game │ ├── main.js │ ├── prefabs │ │ └── HealthBar.js │ └── states │ │ └── play.js ├── index.html ├── package.json └── templates │ └── _main.js.tpl ├── license.txt ├── package.json └── phaser.healthbar.config.png /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | bower_components 4 | .idea 5 | -------------------------------------------------------------------------------- /HealthBar.js: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2015 Belahcen Marwane (b.marwane@gmail.com) 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | SOFTWARE. 21 | */ 22 | 23 | var HealthBar = function(game, providedConfig) { 24 | this.game = game; 25 | this.group = null; 26 | 27 | this.setupConfiguration(providedConfig); 28 | this.setPosition(this.config.x, this.config.y); 29 | this.drawBackground(); 30 | this.drawBorder(); 31 | this.drawHealthBar(); 32 | this.setFixedToCamera(this.config.isFixedToCamera); 33 | }; 34 | HealthBar.prototype.constructor = HealthBar; 35 | 36 | HealthBar.prototype.setupConfiguration = function (providedConfig) { 37 | this.config = this.mergeWithDefaultConfiguration(providedConfig); 38 | this.flipped = this.config.flipped; 39 | }; 40 | 41 | HealthBar.prototype.mergeWithDefaultConfiguration = function(newConfig) { 42 | var defaultConfig= { 43 | width: 250, 44 | height: 40, 45 | x: 0, 46 | y: 0, 47 | bg: { 48 | color: '#651828' 49 | }, 50 | bar: { 51 | color: '#FEFF03' 52 | }, 53 | border: { 54 | color: "#000000", 55 | width: 1 56 | }, 57 | animationDuration: 200, 58 | flipped: false, 59 | isFixedToCamera: false 60 | }; 61 | 62 | return mergeObjetcs(defaultConfig, newConfig); 63 | }; 64 | 65 | function mergeObjetcs(targetObj, newObj) { 66 | for (var p in newObj) { 67 | try { 68 | targetObj[p] = newObj[p].constructor==Object ? mergeObjetcs(targetObj[p], newObj[p]) : newObj[p]; 69 | } catch(e) { 70 | targetObj[p] = newObj[p]; 71 | } 72 | } 73 | return targetObj; 74 | } 75 | 76 | HealthBar.prototype.drawBorder = function() { 77 | var border = this.config.border.width * 2; 78 | 79 | var bmd = this.game.add.bitmapData(this.config.width + border, this.config.height + border); 80 | bmd.ctx.fillStyle = this.config.border.color; 81 | bmd.ctx.beginPath(); 82 | bmd.ctx.rect(0, 0, this.config.width + border, this.config.height + border); 83 | bmd.ctx.stroke(); 84 | bmd.update(); 85 | 86 | this.borderSprite = this.game.add.sprite(this.x, this.y, bmd); 87 | this.borderSprite.anchor.set(0.5); 88 | }; 89 | 90 | HealthBar.prototype.drawBackground = function() { 91 | 92 | var bmd = this.game.add.bitmapData(this.config.width, this.config.height); 93 | bmd.ctx.fillStyle = this.config.bg.color; 94 | bmd.ctx.beginPath(); 95 | bmd.ctx.rect(0, 0, this.config.width, this.config.height); 96 | bmd.ctx.fill(); 97 | bmd.update(); 98 | 99 | this.bgSprite = this.game.add.sprite(this.x, this.y, bmd); 100 | this.bgSprite.anchor.set(0.5); 101 | 102 | if(this.flipped){ 103 | this.bgSprite.scale.x = -1; 104 | } 105 | }; 106 | 107 | HealthBar.prototype.drawHealthBar = function() { 108 | var bmd = this.game.add.bitmapData(this.config.width, this.config.height); 109 | bmd.ctx.fillStyle = this.config.bar.color; 110 | bmd.ctx.beginPath(); 111 | bmd.ctx.rect(0, 0, this.config.width, this.config.height); 112 | bmd.ctx.fill(); 113 | bmd.update(); 114 | 115 | this.barSprite = this.game.add.sprite(this.x - this.bgSprite.width/2, this.y, bmd); 116 | this.barSprite.anchor.y = 0.5; 117 | 118 | if(this.flipped){ 119 | this.barSprite.scale.x = -1; 120 | } 121 | }; 122 | 123 | HealthBar.prototype.setPosition = function (x, y) { 124 | this.x = x; 125 | this.y = y; 126 | 127 | if(this.bgSprite !== undefined && this.barSprite !== undefined && this.borderSprite !== undefined){ 128 | this.bgSprite.position.x = x; 129 | this.bgSprite.position.y = y; 130 | 131 | this.barSprite.position.x = x - this.config.width/2; 132 | this.barSprite.position.y = y; 133 | 134 | this.borderSprite.position.x = x; 135 | this.borderSprite.position.y = y; 136 | } 137 | }; 138 | 139 | 140 | HealthBar.prototype.setPercent = function(newValue){ 141 | if(newValue < 0) newValue = 0; 142 | if(newValue > 100) newValue = 100; 143 | 144 | var newWidth = (newValue * this.config.width) / 100; 145 | 146 | this.setWidth(newWidth); 147 | }; 148 | 149 | /* 150 | Hex format, example #ad3aa3 151 | */ 152 | HealthBar.prototype.setBarColor = function(newColor) { 153 | var bmd = this.barSprite.key; 154 | bmd.update(); 155 | 156 | var currentRGBColor = bmd.getPixelRGB(0, 0); 157 | var newRGBColor = hexToRgb(newColor); 158 | bmd.replaceRGB(currentRGBColor.r, 159 | currentRGBColor.g, 160 | currentRGBColor.b, 161 | 255 , 162 | 163 | newRGBColor.r, 164 | newRGBColor.g, 165 | newRGBColor.b, 166 | 255); 167 | 168 | }; 169 | 170 | HealthBar.prototype.setWidth = function(newWidth){ 171 | if(this.flipped) { 172 | newWidth = -1 * newWidth; 173 | } 174 | this.game.add.tween(this.barSprite).to( { width: newWidth }, this.config.animationDuration, Phaser.Easing.Linear.None, true); 175 | }; 176 | 177 | HealthBar.prototype.setFixedToCamera = function(fixedToCamera) { 178 | this.bgSprite.fixedToCamera = fixedToCamera; 179 | this.barSprite.fixedToCamera = fixedToCamera; 180 | this.borderSprite.fixedToCamera = fixedToCamera; 181 | }; 182 | 183 | HealthBar.prototype.setAnchor = function(xAnchor, yAnchor) { 184 | this.bgSprite.anchor.set(xAnchor, yAnchor); 185 | this.barSprite.position.x = this.bgSprite.position.x - this.config.width * this.bgSprite.anchor.x; 186 | this.borderSprite.anchor.set(xAnchor, yAnchor); 187 | this.barSprite.anchor.y = yAnchor; 188 | if (this.flipped){ 189 | this.barSprite.anchor.x = 1; 190 | this.barSprite.position.x = this.bgSprite.position.x; 191 | } 192 | }; 193 | 194 | 195 | HealthBar.prototype.setToGroup = function(group) { 196 | group.add(this.bgSprite); 197 | group.add(this.barSprite); 198 | 199 | this.group = group; 200 | }; 201 | 202 | HealthBar.prototype.removeFromGroup = function() { 203 | this.game.world.add(this.bgSprite); 204 | this.game.world.add(this.barSprite); 205 | 206 | this.group = null; 207 | }; 208 | 209 | HealthBar.prototype.kill = function() { 210 | this.bgSprite.kill(); 211 | this.barSprite.kill(); 212 | this.borderSprite.kill(); 213 | }; 214 | 215 | module.exports = HealthBar; 216 | 217 | 218 | 219 | /** 220 | Utils 221 | */ 222 | 223 | function hexToRgb(hex) { 224 | // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF") 225 | var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; 226 | hex = hex.replace(shorthandRegex, function(m, r, g, b) { 227 | return r + r + g + g + b + b; 228 | }); 229 | 230 | var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); 231 | return result ? { 232 | r: parseInt(result[1], 16), 233 | g: parseInt(result[2], 16), 234 | b: parseInt(result[3], 16) 235 | } : null; 236 | } -------------------------------------------------------------------------------- /HealthBar.standalone.js: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright (c) 2015 Belahcen Marwane (b.marwane@gmail.com) 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | SOFTWARE. 21 | */ 22 | 23 | var HealthBar = function(game, providedConfig) { 24 | this.game = game; 25 | this.group = null; 26 | 27 | this.setupConfiguration(providedConfig); 28 | this.setPosition(this.config.x, this.config.y); 29 | this.drawBackground(); 30 | this.drawBorder(); 31 | this.drawHealthBar(); 32 | this.setFixedToCamera(this.config.isFixedToCamera); 33 | }; 34 | HealthBar.prototype.constructor = HealthBar; 35 | 36 | HealthBar.prototype.setupConfiguration = function (providedConfig) { 37 | this.config = this.mergeWithDefaultConfiguration(providedConfig); 38 | this.flipped = this.config.flipped; 39 | }; 40 | 41 | HealthBar.prototype.mergeWithDefaultConfiguration = function(newConfig) { 42 | var defaultConfig= { 43 | width: 250, 44 | height: 40, 45 | x: 0, 46 | y: 0, 47 | bg: { 48 | color: '#651828' 49 | }, 50 | bar: { 51 | color: '#FEFF03' 52 | }, 53 | border: { 54 | color: "#000000", 55 | width: 1 56 | }, 57 | animationDuration: 200, 58 | flipped: false, 59 | isFixedToCamera: false 60 | }; 61 | 62 | return mergeObjetcs(defaultConfig, newConfig); 63 | }; 64 | 65 | function mergeObjetcs(targetObj, newObj) { 66 | for (var p in newObj) { 67 | try { 68 | targetObj[p] = newObj[p].constructor==Object ? mergeObjetcs(targetObj[p], newObj[p]) : newObj[p]; 69 | } catch(e) { 70 | targetObj[p] = newObj[p]; 71 | } 72 | } 73 | return targetObj; 74 | } 75 | 76 | HealthBar.prototype.drawBorder = function() { 77 | var border = this.config.border.width * 2; 78 | 79 | var bmd = this.game.add.bitmapData(this.config.width + border, this.config.height + border); 80 | bmd.ctx.fillStyle = this.config.border.color; 81 | bmd.ctx.beginPath(); 82 | bmd.ctx.rect(0, 0, this.config.width + border, this.config.height + border); 83 | bmd.ctx.stroke(); 84 | bmd.update(); 85 | 86 | this.borderSprite = this.game.add.sprite(this.x, this.y, bmd); 87 | this.borderSprite.anchor.set(0.5); 88 | }; 89 | 90 | HealthBar.prototype.drawBackground = function() { 91 | 92 | var bmd = this.game.add.bitmapData(this.config.width, this.config.height); 93 | bmd.ctx.fillStyle = this.config.bg.color; 94 | bmd.ctx.beginPath(); 95 | bmd.ctx.rect(0, 0, this.config.width, this.config.height); 96 | bmd.ctx.fill(); 97 | bmd.update(); 98 | 99 | this.bgSprite = this.game.add.sprite(this.x, this.y, bmd); 100 | this.bgSprite.anchor.set(0.5); 101 | 102 | if(this.flipped){ 103 | this.bgSprite.scale.x = -1; 104 | } 105 | }; 106 | 107 | HealthBar.prototype.drawHealthBar = function() { 108 | var bmd = this.game.add.bitmapData(this.config.width, this.config.height); 109 | bmd.ctx.fillStyle = this.config.bar.color; 110 | bmd.ctx.beginPath(); 111 | bmd.ctx.rect(0, 0, this.config.width, this.config.height); 112 | bmd.ctx.fill(); 113 | bmd.update(); 114 | 115 | this.barSprite = this.game.add.sprite(this.x - this.bgSprite.width/2, this.y, bmd); 116 | this.barSprite.anchor.y = 0.5; 117 | 118 | if(this.flipped){ 119 | this.barSprite.scale.x = -1; 120 | } 121 | }; 122 | 123 | HealthBar.prototype.setPosition = function (x, y) { 124 | this.x = x; 125 | this.y = y; 126 | 127 | if(this.bgSprite !== undefined && this.barSprite !== undefined && this.borderSprite !== undefined){ 128 | this.bgSprite.position.x = x; 129 | this.bgSprite.position.y = y; 130 | 131 | this.barSprite.position.x = x - this.config.width/2; 132 | this.barSprite.position.y = y; 133 | 134 | this.borderSprite.position.x = x; 135 | this.borderSprite.position.y = y; 136 | } 137 | }; 138 | 139 | 140 | HealthBar.prototype.setPercent = function(newValue){ 141 | if(newValue < 0) newValue = 0; 142 | if(newValue > 100) newValue = 100; 143 | 144 | var newWidth = (newValue * this.config.width) / 100; 145 | 146 | this.setWidth(newWidth); 147 | }; 148 | 149 | /* 150 | Hex format, example #ad3aa3 151 | */ 152 | HealthBar.prototype.setBarColor = function(newColor) { 153 | var bmd = this.barSprite.key; 154 | bmd.update(); 155 | 156 | var currentRGBColor = bmd.getPixelRGB(0, 0); 157 | var newRGBColor = hexToRgb(newColor); 158 | bmd.replaceRGB(currentRGBColor.r, 159 | currentRGBColor.g, 160 | currentRGBColor.b, 161 | 255 , 162 | 163 | newRGBColor.r, 164 | newRGBColor.g, 165 | newRGBColor.b, 166 | 255); 167 | 168 | }; 169 | 170 | HealthBar.prototype.setWidth = function(newWidth){ 171 | if(this.flipped) { 172 | newWidth = -1 * newWidth; 173 | } 174 | this.game.add.tween(this.barSprite).to( { width: newWidth }, this.config.animationDuration, Phaser.Easing.Linear.None, true); 175 | }; 176 | 177 | HealthBar.prototype.setFixedToCamera = function(fixedToCamera) { 178 | this.bgSprite.fixedToCamera = fixedToCamera; 179 | this.barSprite.fixedToCamera = fixedToCamera; 180 | this.borderSprite.fixedToCamera = fixedToCamera; 181 | }; 182 | 183 | HealthBar.prototype.setAnchor = function(xAnchor, yAnchor) { 184 | this.bgSprite.anchor.set(xAnchor, yAnchor); 185 | this.barSprite.position.x = this.bgSprite.position.x - this.config.width * this.bgSprite.anchor.x; 186 | this.borderSprite.anchor.set(xAnchor, yAnchor); 187 | this.barSprite.anchor.y = yAnchor; 188 | if (this.flipped){ 189 | this.barSprite.anchor.x = 1; 190 | this.barSprite.position.x = this.bgSprite.position.x; 191 | } 192 | }; 193 | 194 | 195 | HealthBar.prototype.setToGroup = function(group) { 196 | group.add(this.bgSprite); 197 | group.add(this.barSprite); 198 | 199 | this.group = group; 200 | }; 201 | 202 | HealthBar.prototype.removeFromGroup = function() { 203 | this.game.world.add(this.bgSprite); 204 | this.game.world.add(this.barSprite); 205 | 206 | this.group = null; 207 | }; 208 | 209 | HealthBar.prototype.kill = function() { 210 | this.bgSprite.kill(); 211 | this.barSprite.kill(); 212 | this.borderSprite.kill(); 213 | }; 214 | 215 | /** 216 | Utils 217 | */ 218 | 219 | function hexToRgb(hex) { 220 | // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF") 221 | var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; 222 | hex = hex.replace(shorthandRegex, function(m, r, g, b) { 223 | return r + r + g + g + b + b; 224 | }); 225 | 226 | var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); 227 | return result ? { 228 | r: parseInt(result[1], 16), 229 | g: parseInt(result[2], 16), 230 | b: parseInt(result[3], 16) 231 | } : null; 232 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Phaser.HealthBar 2 | 3 | An attempt to create a simple and customizable health bar for Phaser.js games. 4 | 5 | I made a tutorial in French that explain how to create this health bar from scratch, you can check it out [here](http://apprendre-le-js.com/phaser-js-healthbar-tutorial/ "apprendre-le-js.com healthbar tutorial"). 6 | 7 | ## Demo 8 | 9 | [Demo](http://apprendre-le-js.com/tuto_examples/healthbar/4/) 10 | 11 | ## Usage 12 | 13 | ### 1 - Import HealthBar file 14 | 15 | If you are using a CommonJS implementation (Browserify) : 16 | 17 | ```javascript 18 | var HealthBar = require('path/to/HealthBar.js'); 19 | ``` 20 | 21 | if not, just include the HealthBar.standalone.js in the html file. 22 | example : 23 | ``` html 24 | 25 | ``` 26 | 27 | ### 2 - create a HealthBar : 28 | 29 | in the game/state create function instantiate a HealthBar like this: 30 | 31 | ```javascript 32 | create: function() { 33 | var barConfig = {x: 200, y: 100}; 34 | this.myHealthBar = new HealthBar(this.game, barConfig); 35 | } 36 | ``` 37 | ## Configuration 38 | 39 |  40 | 41 | - **width** 42 | - **height** 43 | - **x:** initial x position 44 | - **y:** initial y position 45 | - **bg.color:** background color 46 | - **bar.color:** color of the actual bar 47 | - **animationDuration:** control the animation when the bar value is changed 48 | - **flipped:** if true the bar will change size from left to right 49 | 50 | this is the default configuration : 51 | ```javascript 52 | { 53 | width: 250, 54 | height: 40, 55 | x: 0, 56 | y: 0, 57 | bg: { 58 | color: '#651828' 59 | }, 60 | bar: { 61 | color: '#FEFF03' 62 | }, 63 | animationDuration: 200, 64 | flipped: false 65 | }; 66 | ``` 67 | 68 | ## Methods 69 | 70 | ### setPercent(value): 71 | 72 | set the width of the bar to the passed percentage value. 73 | 74 | **example:** 75 | 76 | ```javascript 77 | this.myHealthBar = new HealthBar(this.game, {x: 200, y: 200, width: 120}); 78 | 79 | // the width will be set to 50% of the actual size so the new value will be 60 80 | this.myHealthBar.setPercent(50); 81 | ``` 82 | 83 | ### setPosition(x, y): 84 | change the position of the bar to the provided coordinates. 85 | 86 | ### setBarColor(newColor) 87 | 88 | change the bar color, use the hex color format. 89 | example : 90 | ```javascript 91 | this.myHealthBar.setBarColor('#fc9802'); 92 | ``` 93 | 94 | ### setFixedToCamera(fixedToCamera); 95 | fixedToCamera must be true or false value (boolean type). 96 | method allows fixed to camera. 97 | 98 | ### setToGroup(group); 99 | add bar to some group 100 | 101 | ### removeFromGroup(); 102 | remove bar from current group and add back to game.world group 103 | 104 | ### kill(); 105 | will kill the HealthBar. 106 | 107 | # License 108 | 109 | Phaser.HealthBar is released under the [MIT License](https://opensource.org/licenses/MIT). 110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /example/.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "bower_components" 3 | } -------------------------------------------------------------------------------- /example/.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /example/.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "camelcase": true, 6 | "curly": true, 7 | "eqeqeq": true, 8 | "immed": true, 9 | "indent": 2, 10 | "latedef": true, 11 | "newcap": true, 12 | "noarg": true, 13 | "quotmark": "single", 14 | "regexp": true, 15 | "undef": true, 16 | "unused": true, 17 | "strict": true, 18 | "trailing": true, 19 | "smarttabs": true, 20 | "white": true 21 | } 22 | -------------------------------------------------------------------------------- /example/Gruntfile.js: -------------------------------------------------------------------------------- 1 | // Generated on 2014-03-28 using generator-phaser-official 0.0.8-rc-2 2 | 'use strict'; 3 | var config = require('./config.json'); 4 | var _ = require('underscore'); 5 | _.str = require('underscore.string'); 6 | 7 | // Mix in non-conflict functions to Underscore namespace if you want 8 | _.mixin(_.str.exports()); 9 | 10 | var LIVERELOAD_PORT = 35729; 11 | var lrSnippet = require('connect-livereload')({port: LIVERELOAD_PORT}); 12 | var mountFolder = function (connect, dir) { 13 | return connect.static(require('path').resolve(dir)); 14 | }; 15 | 16 | module.exports = function (grunt) { 17 | // load all grunt tasks 18 | require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks); 19 | 20 | grunt.initConfig({ 21 | watch: { 22 | scripts: { 23 | files: [ 24 | 'game/**/*.js', 25 | '!game/main.js' 26 | ], 27 | options: { 28 | spawn: false, 29 | livereload: LIVERELOAD_PORT 30 | }, 31 | tasks: ['build'] 32 | } 33 | }, 34 | connect: { 35 | options: { 36 | port: 9000, 37 | // change this to '0.0.0.0' to access the server from outside 38 | hostname: 'localhost' 39 | }, 40 | livereload: { 41 | options: { 42 | middleware: function (connect) { 43 | return [ 44 | lrSnippet, 45 | mountFolder(connect, 'dist') 46 | ]; 47 | } 48 | } 49 | } 50 | }, 51 | open: { 52 | server: { 53 | path: 'http://localhost:9000' 54 | } 55 | }, 56 | copy: { 57 | dist: { 58 | files: [ 59 | // includes files within path and its sub-directories 60 | { expand: true, src: ['assets/**'], dest: 'dist/' }, 61 | { expand: true, flatten: true, src: ['game/plugins/*.js'], dest: 'dist/js/plugins/' }, 62 | { expand: true, flatten: true, src: ['bower_components/**/build/*.js'], dest: 'dist/js/' }, 63 | { expand: true, src: ['css/**'], dest: 'dist/' }, 64 | { expand: true, src: ['index.html'], dest: 'dist/' } 65 | ] 66 | } 67 | }, 68 | browserify: { 69 | build: { 70 | src: ['game/main.js'], 71 | dest: 'dist/js/game.js' 72 | } 73 | } 74 | }); 75 | 76 | grunt.registerTask('build', ['buildBootstrapper', 'browserify','copy']); 77 | grunt.registerTask('serve', ['build', 'connect:livereload', 'open', 'watch']); 78 | grunt.registerTask('default', ['serve']); 79 | grunt.registerTask('prod', ['build', 'copy']); 80 | 81 | grunt.registerTask('buildBootstrapper', 'builds the bootstrapper file correctly', function() { 82 | var stateFiles = grunt.file.expand('game/states/*.js'); 83 | var gameStates = []; 84 | var statePattern = new RegExp(/(\w+).js$/); 85 | stateFiles.forEach(function(file) { 86 | var state = file.match(statePattern)[1]; 87 | if (!!state) { 88 | gameStates.push({shortName: state, stateName: _.capitalize(state) + 'State'}); 89 | } 90 | }); 91 | config.gameStates = gameStates; 92 | console.log(config); 93 | var bootstrapper = grunt.file.read('templates/_main.js.tpl'); 94 | bootstrapper = grunt.template.process(bootstrapper,{data: config}); 95 | grunt.file.write('game/main.js', bootstrapper); 96 | }); 97 | }; -------------------------------------------------------------------------------- /example/assets/plus_minus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bmarwane/phaser.healthbar/645f22aff1e3ce541eb69aa040e4d3541069d7c9/example/assets/plus_minus.png -------------------------------------------------------------------------------- /example/bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "demo", 3 | "version": "0.0.0", 4 | "dependencies": { 5 | "phaser-official": "2.4.3" 6 | } 7 | } -------------------------------------------------------------------------------- /example/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "projectName": "demo", 3 | "gameWidth": "600", 4 | "gameHeight": "280" 5 | } 6 | -------------------------------------------------------------------------------- /example/dist/assets/plus_minus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bmarwane/phaser.healthbar/645f22aff1e3ce541eb69aa040e4d3541069d7c9/example/dist/assets/plus_minus.png -------------------------------------------------------------------------------- /example/dist/css/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #333; 3 | margin:0; 4 | } 5 | -------------------------------------------------------------------------------- /example/dist/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
7 |