Code coverage report for ./src/client/app/level/level.controller.js

Statements: 10.32% (26 / 252)      Branches: 0% (0 / 88)      Functions: 3.23% (1 / 31)      Lines: 10.36% (26 / 251)      Ignored: none     

All files » ./src/client/app/level/ » level.controller.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 4131     1       1     1                                                                     1                                                       1                           1             1                               1         1                                               1                 1                                               1                   1                                 1             1                                                               1                 1                                         1                         1               1                         1           1             1                                     1                                                                                                                                                   1        
(function() {
	'use strict';
 
	angular
		.module('app.level')
		.controller('LevelController', LevelController);
 
	LevelController.$inject = ['levelService', 'playerService', 'enemyService', '$scope', '$timeout', 'messageService', 'progressService', 'inventoryService'];
 
	/* @ngInject */
	function LevelController(levelService, playerService, enemyService, $scope, $timeout, messageService, progressService, inventoryService) {
		var vm = this;
		vm.title = 'LevelController';
 
		vm.currentLevel = levelService.treeOne;
		vm.currentLevel.checkLength();
		vm.player = playerService.player;
		vm.enemySpawn = false;
		vm.mainMessage = messageService.mainMessage;
		vm.abilities = vm.player.abilities;
		vm.itemDictionary = inventoryService.itemDictionary;
 
 
		//ability functions
		vm.activateAbility = function(ability) {
			var message = '';
			if (!vm.abilities[ability].active && vm.abilities[ability].cd === 0) {
				vm.abilities[ability].special();
				message = vm.abilities[ability].name + ' has been used.';
				messageService.updateMainMessage(message);
				var id = '#' + vm.abilities[ability].slug;
				var elem = id + ' .abilitybg';
				angular.element(elem).css('width', '100%');
				abilityTimer(ability, elem);
			}
			else if (vm.abilities[ability].cd > 0) {
				message = vm.abilities[ability].name + ' is on cooldown!';
				messageService.updateMainMessage(message, true);
			}
			else {
				message = vm.abilities[ability].name + ' already active!';
				messageService.updateMainMessage(message, true);
			}
		};
 
		function abilityTimer(ability, elem) {
			if (vm.abilities[ability].active) {
				vm.player.damage = vm.player.calculateTotalDamage();
				vm.player.armorValue = vm.player.calculateTotalArmor();
				if (vm.abilities[ability].timer > 0) {
					vm.abilities[ability].timer = vm.abilities[ability].timer - 1;
					var percent = (vm.abilities[ability].timer / vm.abilities[ability].max)*100;
					angular.element(elem).css('width', percent + '%');
					$timeout(function() {
						abilityTimer(ability, elem);
					}, 125);
				}
				else {
					angular.element(elem).css('width', '0%');
					vm.abilities[ability].timer = 0;
					vm.abilities[ability].active = false;
					abilityCooldown(ability, elem);
				}
			}
		}
 
		vm.initPotion = function() {
			var potionUsed = vm.player.usePotion();
			if (potionUsed) {
				potionCooldown();
			}
		};
 
		function potionCooldown() {
			if (vm.player.potionCD > 0) {
				vm.player.potionCD = vm.player.potionCD - 1;
				var percent = (vm.player.potionCD / 300)*100 - 1;
				angular.element('.potioncd .abilitybg').css('width', percent + '%');
				$timeout(function() {
					potionCooldown();
				}, 125);
			}
			else {
				vm.player.potionCD = 0;
			}
		}
 
		function abilityCooldown(ability, elem) {
			var cdMax = vm.abilities[ability].cdMax;
			vm.abilities[ability].cd = cdMax;
			angular.element(elem).addClass('abilitycooldown');
			abilityCooldownLoop(ability, elem);
		}
 
		function abilityCooldownLoop(ability, elem) {
			if (vm.abilities[ability].cd > 0) {
				vm.abilities[ability].cd = vm.abilities[ability].cd - 1;
				var percent = (vm.abilities[ability].cd / vm.abilities[ability].cdMax)*100;
				angular.element(elem).css('width', percent + '%');
				$timeout(function() {
					abilityCooldownLoop(ability, elem);
				}, 125);
			}
			else {
				vm.abilities[ability].cd = 0;
				angular.element(elem).removeClass('abilitycooldown');
				angular.element(elem).css('width', '0%');
			}
		}
 
		function resetAbilities() {
			vm.abilities.resetAbilities();
			vm.player.potionCD = 0;
		}
 
		function prevReset() {
			vm.player.prev = false;
			vm.player.prevCheck = false;
		}
 
		//sets everything to default when called
		vm.resetLevel = function() {
			enemyService.currentEnemy = undefined;
			vm.currentLevel = levelService.currentLevel;
			prevReset();
			resetAbilities();
			specialEnd();
			vm.unitArray = [playerService.player];
			vm.player.active = true;
			createAscii();
			initLevel();
			var spawn = [];
			spawn[0] = vm.currentLevel.playerSpawn[0];
			spawn[1] = vm.currentLevel.playerSpawn[1];
			vm.player.position = spawn;
		};
		vm.resetLevel();
 
		//used for levels that end early
		function specialEnd() {
			if (vm.currentLevel.specialEnd) {
				vm.player.specialEnd = vm.currentLevel.specialEnd;
			} else {
				vm.player.specialEnd = undefined;
			}
		}
 
		//init level
		function initLevel() {
			messageService.emptyLog();
			resetRenderArea();
			//runs if enemies are immobile and spawn at start, like the trees lvl 1
			if (typeof vm.currentLevel.spawnAtStart !== 'undefined') {
				for (var i = 0; i < vm.currentLevel.spawnAtStart.length; i++) {
					if (i + 1 === vm.currentLevel.spawnAtStart.length) {
						if (vm.currentLevel.enemyArray.length > 1) {
							spawnEnemyAtStart(vm.currentLevel.spawnAtStart[i], true);
						}
						else {
							spawnEnemyAtStart(vm.currentLevel.spawnAtStart[i]);
						}
					} else {
						spawnEnemyAtStart(vm.currentLevel.spawnAtStart[i]);
					}
				}
			}
			//otherwise default spawn is on
			else {
				vm.enemySpawn = true;
			}
		}
 
		function createAscii() {
			var ascii = [];
			for (var i = 0; i < vm.currentLevel.defaultAscii.length; i++) {
				ascii[i] = vm.currentLevel.defaultAscii[i];
			}
			vm.currentLevel.ascii = ascii;
			levelRenderArea();
		}
 
		//moves level to left after certain distance has been travelled
		function levelRenderArea() {
			var length = vm.currentLevel.ascii[0].length;
			var width = window.innerWidth;
			var value = 50;
			if (width < 500) {
				value = 10;
			}
			else if (width < 1000) {
				value = 30;
			}
			if (vm.player.position[0] > value && vm.player.position[0] < length - value) {
				var left = (vm.player.position[0] - value)*8;
				var elem = document.getElementById('levelwrap');
				elem.style.left = '-' + left + 'px';
			}
		}
 
		function resetRenderArea() {
			var elem = document.getElementById('levelwrap');
			elem.style.left = 'auto';
		}
 
		//function for default spawning, runs if rng works
		var enemyCount = 0;
		function createEnemy() {
			var random = Math.round(Math.random()*100);
			if ((random > vm.currentLevel.spawnChance) && (enemyCount < vm.currentLevel.enemyMax)) {
				enemyCount = enemyCount + 1;
				var random = Math.floor(Math.random()*100);
				for (var i = 0; i < vm.currentLevel.unitSpawnChance.length; i++) {
					if (random < vm.currentLevel.unitSpawnChance[i]) {
						var unit = new vm.currentLevel.enemyArray[i]();
						if (typeof vm.currentLevel.specialSpawn !== 'undefined') {
							if (vm.player.position[0] < vm.currentLevel.specialSpawn[0]) {
								var specialCheck = Math.floor(Math.random()*100);
								if (specialCheck < vm.currentLevel.specialSpawnChance) {
									regularSpawn(unit, 'specialSpawn');
								}
								else {
									regularSpawn(unit, 'enemySpawn');
								}
							}
							else {
								regularSpawn(unit, 'enemySpawn');
							}
						}
						else {
							regularSpawn(unit, 'enemySpawn');
						}
						return;
					}
				}
			}
		}
 
		//spawn
		function regularSpawn(unit, type) {
			var spawn = [];
			spawn[0] = vm.currentLevel[type][0];
			spawn[1] = vm.currentLevel[type][1];
			unit.position = spawn;
			vm.unitArray.push(unit);
		}
 
		//function for spawning immobile enemies at start of level
		function spawnEnemyAtStart(position, extra) {
			var entity;
			if (extra) {
				entity = new vm.currentLevel.enemyArray[1]();
			}
			else {
				entity = new vm.currentLevel.enemyArray[0]();
			}
			var spawn = [];
			var currentTile = vm.currentLevel.ascii[position[1]][position[0]];
			if (currentTile === '_') {
				entity.prev = true;
				entity.prevCheck = true;
			}
			spawn[0] = position[0];
			spawn[1] = position[1];
			entity.position = spawn;
			vm.unitArray.push(entity);
		}
 
		//updates map when units move/die
		function updateMap(unit, unitOld, map, unitSymbol, prevCheck) {
			//checks if previous title unit was on is replaceable so it can replace with correct char
			if (!prevCheck) {
				map[unitOld[1]] = setCharAt(map[unitOld[1]], unitOld[0], ' ');
				map[unit[1]] = setCharAt(map[unit[1]], unit[0], unitSymbol);
			}
			else {
				map[unitOld[1]] = setCharAt(map[unitOld[1]], unitOld[0], '_');
				map[unit[1]] = setCharAt(map[unit[1]], unit[0], unitSymbol);
			}
		}
 
		//sets character based on function arguments
		function setCharAt(str,index,chr) {
			if (typeof str !== 'undefined') {
				if(index > str.length-1) return str;
				return str.substr(0,index) + chr + str.substr(index+1);
			}
		}
 
		//if it has an extra collission box for large ascii art, delete the extra on death as well
		function checkBig(unit) {
			if (typeof unit.colBox !== 'undefined') {
				for (var j = 0; j < unit.colBox[1]; j++) {
					for (var i = 0; i < unit.colBox[0]; i++) {
						setCharAt();
						vm.currentLevel.ascii[unit.position[1] - j] = setCharAt(vm.currentLevel.ascii[unit.position[1] - j], unit.position[0] + i, ' ');
						vm.currentLevel.ascii[unit.position[1] - j] = setCharAt(vm.currentLevel.ascii[unit.position[1] - j], unit.position[0] - i, ' ');
					}
				}
			}
		}
 
		//kills unit that reach end
		function autoKill(unit) {
			if (unit.position[0] === 0) {
				unit.alive = false;
			}
		}
 
		function teleportPlayer() {
			vm.player.position = [0, 24];
			vm.currentLevel.ascii[24] = setCharAt(vm.currentLevel.ascii[24], 110, ' ');
			spawnBuddiesLoop();
			resetRenderArea();
		}
 
		function spawnBuddiesLoop() {
			var random = [];
			for (var i = 0; i < 3; i++) {
				(function spawnBuddies(i, random) {
					var unit = new vm.currentLevel.enemyArray[0]();
					random[i] = Math.floor(Math.random()*60) + 40;
					var spawn = [];
					spawn[0] = random[i];
					spawn[1] = 24;
					unit.position = spawn;
					vm.unitArray.push(unit);
				})(i, random);
			}
			console.log(random);
		}
 
		
 
		//master loop for levels
		function levelLoop() {
			var dead = false;
			vm.messageLog = messageService.messageLog;
			vm.player.attackSpeed  = vm.player.weapon.attackSpeed;
			vm.player.damage = vm.player.calculateTotalDamage();
			vm.player.armorValue = vm.player.calculateTotalArmor();
 
			if (vm.player.active) {
				if (vm.currentLevel.slug === 'theEnd') {
					if (typeof vm.currentEnemy !== 'undefined') {
						if (vm.currentEnemy.playerWarp) {
							vm.currentEnemy.playerWarp = false;
							teleportPlayer();
						}
					}
				}
				if (!vm.player.alive) {
					vm.player.alive = true;
					messageService.updateMainMessage('You have been slain.', true);
					return;
				}
				for (var i = 0; i < vm.unitArray.length; i++) {
					vm.unitArray[i].collisionCheck(vm.currentLevel.ascii, vm.unitArray);
					if (i > 0) {
						autoKill(vm.unitArray[i]);
					}
					if (!vm.unitArray[i].alive) {
						if (vm.unitArray[i].slug === 'Lich') {
							vm.player.lichKilled = true;
						}
						checkBig(vm.unitArray[i]);
						var lootMessage = '';
						if (vm.unitArray[i].foundLoot) {
							lootMessage = vm.unitArray[i].deathMessage + ' ' + vm.unitArray[i].lootMessage;
						}
						else {
							lootMessage = vm.unitArray[i].deathMessage;
						}
						messageService.addMessage(lootMessage);
						enemyCount = enemyCount - 1;
						var newArray = [];
						for (var j = 0; j < vm.unitArray.length; j++) {
							if (j !== i) {
								newArray.push(vm.unitArray[j]);
							}
							else {
								updateMap(vm.unitArray[i].position, vm.unitArray[i].positionOld, vm.currentLevel.ascii, ' ', vm.unitArray[i].prevCheck);
								dead = true;
							}
						}
						vm.unitArray = newArray;
					}
					if (typeof vm.unitArray[i] !== 'undefined') {
						updateMap(vm.unitArray[i].position, vm.unitArray[i].positionOld, vm.currentLevel.ascii, vm.unitArray[i].symbol, vm.unitArray[i].prevCheck);
					}
					vm.currentEnemy = enemyService.currentEnemy;
				}
				if (vm.enemySpawn) {
					createEnemy();
				}
				levelRenderArea();
				$timeout(levelLoop, 125);
			}
			else if (vm.player.levelComplete && vm.player.alive) {
				if (vm.currentLevel.unlock) {
					progressService.progress.levels[vm.currentLevel.unlock] = true;
				}
			}
		}
 
		activate();
 
		////////////////
 
		function activate() {
			levelLoop();
		}
	}
})();