var EHDI = EHDI || Object.create(null); EHDI.GAME = EHDI.GAME || Object.create(null); EHDI.GAME.utils = (function() { return { center: function(obj) { if(obj.anchor) { obj.anchor.x = 0.5; obj.anchor.y = 0.5; } obj.position.x = EHDI.GAME.sceneManager.getStageWidth()/2; obj.position.y = EHDI.GAME.sceneManager.getStageHeight()/2; }, bringToFront: function(sprite) { if (sprite.parent) { var parent = sprite.parent; parent.removeChild(sprite); parent.addChild(sprite); } }, isInFront: function(sprite) { var parent = sprite.parent; return parent.getChildIndex(sprite) == parent.children.length - 1; }, randomInt: function(min, max) { return Math.floor(EHDI.NumberUtil.randomRange(min, max)); }, permute: function permute(arr, memo, results) { var cur, memo = memo || [], results = results || []; for (var i = 0; i < arr.length; i++) { cur = arr.splice(i, 1); if (arr.length === 0) { results.push(memo.concat(cur)); } permute(arr.slice(), memo.concat(cur), results); arr.splice(i, 0, cur[0]); } return results; }, scaleToFit: function(sprite, width, height) { var width = width || EHDI.GAME.sceneManager.getStageWidth(); var height = height || EHDI.GAME.sceneManager.getStageHeight(); sprite.scale.set(1,1) // sprite.scale.x = 1; // sprite.scale.y = 1; // sprite.texture.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST; sprite.scale.x = width/sprite.width; sprite.scale.y = height/sprite.height; }, shuffle: function(arr) { var newArr = [], index; for(var i = 0, len = arr.length; i < len; i++) { index = EHDI.GAME.utils.randomInt(0, arr.length); newArr.push(arr[index]); arr.splice(index, 1); } return newArr; }, initArray: function(length, val) { var arr = new Array(length); if(val != undefined) { for(var i = 0; i < length; i++) { arr[i] = val; } } return arr; }, setGlowFilter: function(sprite) { sprite.filters = [new EHDI.filters.GlowFilter(sprite.width, sprite.height, 15.0, 2.0, 1.0, 0xFFFFFF, 0.1)]; }, minimax: function minimax(node, maximizingPlayer) { if(node.children.length == 0) { return node; } if(maximizingPlayer) { var bestValue = {score: -1000}; for(var i = 0; i < node.children.length; i++) { var v = minimax(node.children[i], false); if(v.score > bestValue.score) bestValue = v; } return bestValue; } else { var bestValue = {score: 1000}; for(var i = 0; i < node.children.length; i++) { var v = minimax(node.children[i], true); if(v.score < bestValue.score) bestValue = v; } return bestValue; } }, spawnNumberSprite: function(tl, container, x, y, val) { var num = new EHDI.aka.PixiText(val, {fontFamily: "Exo-Bold", fontSize: "44px", fill: 0xFFFFFF}); num.anchor.set(0.5, 0.5); num.position.set(x, y); container.addChild(num); // TweenLite.to(num, 1.5, {y: "-=100", alpha: 0, onComplete: container.removeChild.bind(container, num)}); TweenMax.to(num, 1.5, {y: "-=100", alpha: 0, onComplete: container.removeChild.bind(container, num)}); }, resetTimeline: function(tl) { tl.clear(); tl.pause(0, true); } } })();