var EHDI = EHDI || Object.create(null); EHDI.GAME = EHDI.GAME || Object.create(null); EHDI.GAME.Map = EHDI.GAME.Map || Object.create(null); EHDI.GAME.Map.LevelBuilder = (function() { var instance; function create() { var public = {}; var maxObjects = 6; var maxGuards = 3; var guardPaddingX = 350; var guardPaddingY = 20; var maxGuardWidth = 150; var peakDifficulty = 50; var maxObjectWidth = 250; public.generateLevel = function(container, difficulty) { _setupGuards(container, difficulty); _setupObjects(container, difficulty); } function _setupGuards(container, difficulty) { var guard; var n = Math.floor(Math.sqrt(difficulty)); var nGuards = (n > maxGuards)? maxGuards:n; var player = container.player; var guardPositionsY = [ player.hitBox.position.y + player.hitBox.height/2 - 140, player.hitBox.position.y + player.hitBox.height/2 - 120 ]; var guardPositionsX = [], guardPositions = [], pos, x, y; var pos = (player.hitBox.position.x - player.hitBox.width - guardPaddingX)/3; for(var i = 0, len = 4; i < len; i++) { guardPositionsX.push(i*pos + 250); } var timeLoop = 15000; var sleepTime = timeLoop*((nGuards+1.5)/10); var activeTime = timeLoop - sleepTime; var guards = []; for(var i = 0, len = nGuards; i < len; i++) { guard = EHDI.GAME.GuardManager.removeGuard() || new EHDI.GAME.components.Guard(container); guard.states.sleeping.setTime(sleepTime); guard.states.active.setTime(activeTime); var j = EHDI.GAME.utils.randomInt(0, guardPositionsX.length); guard.getSprite().position.x = guardPositionsX[j]; guardPositionsX.splice(j, 1); guard.getSprite().position.y = guardPositionsY[EHDI.GAME.utils.randomInt(0, guardPositionsY.length)]; _setGuardBehaviour(guard, difficulty); guards.push(guard); } EHDI.GAME.GuardManager.spawn(guards); } function _setupObjects(container, difficulty) { var obj; var n = Math.floor(Math.sqrt(difficulty)); var nObjects; if(n < 5) nObjects = 5; else if (n > maxObjects) nObjects = maxObjects else nObjects = n; var player = container.player; var objPositionsY = [370]; var objPositionsX = []; var slots = EHDI.GAME.sceneManager.getStageWidth()/maxObjectWidth; var objects = []; for(var i = 0, len = slots; i < len; i++) { objPositionsX.push(i*maxObjectWidth + 80); } for(var i = 0, len = nObjects; i < len; i++) { obj = EHDI.GAME.ObjectManager.getActiveObjects().pop() || new EHDI.GAME.components.StationaryObject(); obj.setLighted(false); var j = EHDI.GAME.utils.randomInt(0, objPositionsX.length); obj.position.x = objPositionsX[j]; objPositionsX.splice(j, 1); obj.position.y = objPositionsY[EHDI.GAME.utils.randomInt(0, objPositionsY.length)]; objects.push(obj); container.addChild(obj); } EHDI.GAME.ObjectManager.spawn(objects); } function _setGuardBehaviour(guard, difficulty) { var initial = {}, stateConfigs, behaviours, omitWalking; initial.face = (EHDI.GAME.utils.randomInt(0, 2) == 0)? "left":"right"; stateConfigs = EHDI.GAME.GuardManager.getStateConfigs(); behaviours = stateConfigs[EHDI.GAME.utils.randomInt(0, stateConfigs.length)].slice(0); initial.state = behaviours[0]; guard.initBehaviour(initial, behaviours); } return public; } return { getInstance: function() { if(!instance) { instance = create(); } return instance; } }; })();