game.js
// battle state, handles player and enemy turns
battle state, handles player and enemy turns
battle state handles player and enemy turns
function battle() {
	targetSelectInstructionText.visible = false;
	specialTexts.visible = false;
	preBattleText.visible = false;
	battleActions.visible = true;
    	playerStatText.setText(player.name + "|Lv" + player.level + "       Wave: " + wave + "\nHP: " + player.health + "/" + player.maxHealth + " SP: " + player.sp + "/" + player.maxSp + "\nEXP: " + player.experience + "/" + player.expNextLevel + "\nSTR: " + player.strength + " DEF: " + player.defense + " CRIT: " + Math.round(player.critChance * 100) + "%");
	if (enemies.length == 0) {
		updateOutput("All enemies defeated! Wave " + wave + " defeated!");
		if(player.health == player.maxHealth) {
			updateOutput("Perfect wave! +3 EXP");
			player.giveExp(3);
		}
		updateOutput("======================")
		wave++;
		state = preBattle;
		return;
	}
	if (player.health <= 0) {
		state = gameOver;
		return;
	}
	if (!player.turn) {
		action = "";
		for(var i=0;i<enemies.length;i++) {
			enemies[i].unDefend();
			var behavior = enemies[i].chooseBehavior();
			switch (behavior) {
				case enemies[i].attack:
					enemies[i].attack(player);
					break;
				case enemies[i].defend:
					enemies[i].defend();
					break;
				case enemies[i].specialAttack:
					enemies[i].specialAttack()
					break;
			}
		}
		player.unDefend();
		updateOutput("----------------------")
		player.turn = true;
	}
	if (player.turn && action) {
		if (target) {
			switch (action) {
				case "ATTACK":
					player.attack(target);
					break;
				case "EXAMINE":
					player.examine(target);
					break;
				case "SPECIAL":
					player.specialAttack(selectedSpecial,target);
					selectedSpecial = null;
					break;
			}
			action = "";
			target = null;
			player.turn = false;
		} else if (selectedSpecial) {
			switch (selectedSpecial.attackType) {
				case "single":
					targetSelect(enemies);
					break;
				case "all":
					target = enemies;
					break;
				case "self":
					target = player;
					break;
				case "sp":
					target = enemies;
					break;
			}
		} else {
			switch (action) {
				case "EXAMINE":
				case "ATTACK":
					targetSelect(enemies);
					break;
				case "DEFEND":
					player.defend();
					player.turn = false;
					break;
				case "SPECIAL":
					selectedSpecial = null;
					if (player.specials.length > 0) {
						if(player.sp >= 1) {
							for(var i=0; i<player.specials.length;i++){
								var txt = new PIXI.Text(player.specials[i].name, {
									font: "14pt monospace",
									fill: 'white'
								});
								txt.position.x = 675;
								txt.position.y = 255 + 40 * i;
								battleActions.addChild(txt);
								txt.interactive = true;
								txt.buttonMode = true;
								txt.click = txt.tap = function(data) {
									var j=0;
									var found = false;
									while(!found && j<player.specials.length) {
										if(this.text == player.specials[j].name) {
											found = true;
											if(player.sp >= player.specials[j].cost){
												selectedSpecial = player.specials[j];
											} else {
												updateOutput(player.name + " doesn't have enough SP for this attack!");
											}
										} else {
											j++;
										}
									}
								}
								specialTexts.addChild(txt)
								state = specialSelectMode;
							}
						} else {
							updateOutput(player.name + " doesn't have enough SP for a special attack!");
							action = "";
						}
					} else {
						updateOutput(player.name + " has no special attacks!");
						action = "";
					}
					break;
				case "RUN":
					player.run();
					break;
			}
		}
		removeDeadEnemies(enemies);
	}	
}
function battle ( ) { targetSelectInstructionText . visible = false ; specialTexts . visible = false ; preBattleText . visible = false ; battleActions . visible = true ; playerStatText . setText ( player . name + "|Lv" + player . level + "       Wave: " + wave + "\nHP: " + player . health + "/" + player . maxHealth + " SP: " + player . sp + "/" + player . maxSp + "\nEXP: " + player . experience + "/" + player . expNextLevel + "\nSTR: " + player . strength + " DEF: " + player . defense + " CRIT: " + Math . round ( player . critChance * 100 ) + "%" ) ; if ( enemies . length == 0 ) { updateOutput ( "All enemies defeated! Wave " + wave + " defeated!" ) ; if ( player . health == player . maxHealth ) { updateOutput ( "Perfect wave! +3 EXP" ) ; player . giveExp ( 3 ) ; } updateOutput ( "======================" ) wave ++ ; state = preBattle ; return ; } if ( player . health <= 0 ) { state = gameOver ; return ; } if ( ! player . turn ) { action = "" ; for ( var i = 0 ; i < enemies . length ; i ++ ) { enemies [ i ] . unDefend ( ) ; var behavior = enemies [ i ] . chooseBehavior ( ) ; switch ( behavior ) { case enemies [ i ] . attack : enemies [ i ] . attack ( player ) ; break ; case enemies [ i ] . defend : enemies [ i ] . defend ( ) ; break ; case enemies [ i ] . specialAttack : enemies [ i ] . specialAttack ( ) break ; } } player . unDefend ( ) ; updateOutput ( "----------------------" ) player . turn = true ; } if ( player . turn && action ) { if ( target ) { switch ( action ) { case "ATTACK" : player . attack ( target ) ; break ; case "EXAMINE" : player . examine ( target ) ; break ; case "SPECIAL" : player . specialAttack ( selectedSpecial , target ) ; selectedSpecial = null ; break ; } action = "" ; target = null ; player . turn = false ; } else if ( selectedSpecial ) { switch ( selectedSpecial . attackType ) { case "single" : targetSelect ( enemies ) ; break ; case "all" : target = enemies ; break ; case "self" : target = player ; break ; case "sp" : target = enemies ; break ; } } else { switch ( action ) { case "EXAMINE" : case "ATTACK" : targetSelect ( enemies ) ; break ; case "DEFEND" : player . defend ( ) ; player . turn = false ; break ; case "SPECIAL" : selectedSpecial = null ; if ( player . specials . length > 0 ) { if ( player . sp >= 1 ) { for ( var i = 0 ; i < player . specials . length ; i ++ ) { var txt = new PIXI . Text ( player . specials [ i ] . name , { font : "14pt monospace" , fill : 'white' } ) ; txt . position . x = 675 ; txt . position . y = 255 + 40 * i ; battleActions . addChild ( txt ) ; txt . interactive = true ; txt . buttonMode = true ; txt . click = txt . tap = function ( data ) { var j = 0 ; var found = false ; while ( ! found && j < player . specials . length ) { if ( this . text == player . specials [ j ] . name ) { found = true ; if ( player . sp >= player . specials [ j ] . cost ) { selectedSpecial = player . specials [ j ] ; } else { updateOutput ( player . name + " doesn't have enough SP for this attack!" ) ; } } else { j ++ ; } } } specialTexts . addChild ( txt ) state = specialSelectMode ; } } else { updateOutput ( player . name + " doesn't have enough SP for a special attack!" ) ; action = "" ; } } else { updateOutput ( player . name + " has no special attacks!" ) ; action = "" ; } break ; case "RUN" : player . run ( ) ; break ; } } removeDeadEnemies ( enemies ) ; } }
battle state, handles player and enemy turns
battle state handles player and enemy turns
// show all battle-relevant graphics // If there are no more enemies, end the wave // If the player finished the wave with full health, give them a small EXP bonus // If the player is dead, switch to game over state // If it is not the player's turn // Each enemy chooses their behavior, then acts // If it is the player's turn // If the player has a target selected // Execute an action // If the player does not have a target, but has selected a special attack, // Handle target selection for special attacks //Otherwise, determine which action the player wants to do // if the player chooses to defend, no target selection is necessary // load up the player's specials with clickable text,  // then send them into the special attack selection mode //console.log(action); // However if the player has no special attacks,  // let them choose something else instead