game.js
// Define the Actor class, a superclass for both Player and Enemy
Define the Actor class, a superclass for both Player and Enemy
Define the Actor class a superclass for both Player and Enemy
function Actor() {
	this.setSprite = function() {
		var self = this;
		this.spr = new PIXI.Sprite(PIXI.TextureCache[this.name.toLowerCase() + ".png"]);
		this.spr.interactive = true;
		this.spr.buttonMode = true;
		this.spr.click = this.spr.tap = function(data) {
			if(state == targetSelectMode){
				target = self;
				state = battle;
			}
		}
	}
	this.attack = function(target) {
		if (Math.random() > this.accuracy) {
			updateOutput(this.name + "'s attack missed!");
		} else {
			var damage;
			if (Math.random() < this.critChance) {
				updateOutput("Critical hit!");
				damage = this.strength * 3 - target.defense;
			} else {
				damage = this.strength - target.defense;
			}
			if(damage > 0) {
				damage = Math.round(damage);
				updateOutput(this.name + " Attacked " + target.name + ", dealing " + damage + " damage!")
				target.health -= damage;
			} else {
				updateOutput(this.name + "'s attack failed to damage " + target.name);
			}
		}
	}
	this.defend = function() {
		if (!this.defending) {
			this.defense += this.strength;
			this.defending = true;
			updateOutput(this.name + " raised their guard! Defense is now " + this.defense);
		}
	}
	this.unDefend = function() {
		if(this.defending) {
			this.defense -= this.strength;
			this.defending = false;
			updateOutput(this.name + " lowered their guard! Defense is now " + this.defense);
		}
	}
}
function Actor ( ) { this . setSprite = function ( ) { var self = this ; this . spr = new PIXI . Sprite ( PIXI . TextureCache [ this . name . toLowerCase ( ) + ".png" ] ) ; this . spr . interactive = true ; this . spr . buttonMode = true ; this . spr . click = this . spr . tap = function ( data ) { if ( state == targetSelectMode ) { target = self ; state = battle ; } } } this . attack = function ( target ) { if ( Math . random ( ) > this . accuracy ) { updateOutput ( this . name + "'s attack missed!" ) ; } else { var damage ; if ( Math . random ( ) < this . critChance ) { updateOutput ( "Critical hit!" ) ; damage = this . strength * 3 - target . defense ; } else { damage = this . strength - target . defense ; } if ( damage > 0 ) { damage = Math . round ( damage ) ; updateOutput ( this . name + " Attacked " + target . name + ", dealing " + damage + " damage!" ) target . health -= damage ; } else { updateOutput ( this . name + "'s attack failed to damage " + target . name ) ; } } } this . defend = function ( ) { if ( ! this . defending ) { this . defense += this . strength ; this . defending = true ; updateOutput ( this . name + " raised their guard! Defense is now " + this . defense ) ; } } this . unDefend = function ( ) { if ( this . defending ) { this . defense -= this . strength ; this . defending = false ; updateOutput ( this . name + " lowered their guard! Defense is now " + this . defense ) ; } } }
Define the Actor class, a superclass for both Player and Enemy
Define the Actor class a superclass for both Player and Enemy
// Set the object's sprite based on its name // Set up a click event on the sprite that if the player is in the target select mode, clicking on the sprite will return the actor as the target and change the state back to battle mode // How all actors attack // If the attack misses, otherwise... // Roll for a critical hit // Either way, damage is the difference of the caller's strength and the target's defense // If there is any damage at all // How all actors defend themselves // Removes defending state from an actor