package com.platypotamus.samplecode{ /** *Now we're going to start to look at classes as intended: classes are objects! *We are writing object oriented code here, which means that we will define objects *and then turn them loose to do their thing. Naturally, we get to determine what "their thing" *is. All objects need: * *1. Data *2. A constuctor *3. Behaviours. */ public class Fighter{ /** *Any variables defined within the class, but outside of all functions is what's called a class *variable. Or member variable. This makes the variable a part of the definition of an object *It means that every instance of the object we create has a copy of this variable (potentially *with its own distinct value). Fighters have a number of bits of data: */ private var offense:uint; private var defense:uint; private var health:uint; private var damage:uint; private var lastWords:String; private var victorySpeech:String; private var name:String; /** *Remember, the function with the same name as the class acts as a constructor. *In here, we are actually creating our Fighter object. Among other things, we'll initialize *a bunch of the values for our Fighter (through random generation). */ public function Fighter(){ //"this" means "the fighter that we are constructing" //so set "the fighter that we are constructing"'s offense variable to a random number 1-10 this.offense = Math.ceil(Math.random() * 10); this.defense = Math.ceil(Math.random() * 10); this.health = Math.ceil(Math.random() * 50) + 50; this.damage = Math.ceil(Math.random() * 10); } /** *The third thing our fighter needs is a set of behviors. These are functions attached to *the fighter. Many of these make sense in the outside world. Some, only in code */ /** *The first section of behaviors we'll want are getters and (sometimes) setters. It's a good *practice to make all of the member data private, like we did above. This allows you to *control access to it. getters grant read access, setters grant write access. */ public function getOffense():uint{ return this.offense; } public function getDefense():uint{ return this.defense; } public function getHealth():uint{ return this.health; } public function getDamage():uint{ return this.damage; } //name is included for free! public function getName():String{ return this.name; } //Boolean "getter" type functions use the word "is" public function isDead():Boolean{ return (this.health <= 0); } //setters provide write access to a private variable public function setLastWords(s:String):void{ this.lastWords = s; } public function setVictorySpeech(s:String):void{ this.victorySpeech = s; } public function setName(s:String):void{ this.name = s; } /** *This function allows one fighter to attack another. Stats will be modified by *random die rolls, using a FightSimulator static function. These will be compared *and health will be updated. While it is not optimal, we'll directly output the results *as we do this. * *target - the fighter that "this" fighter is attacking */ public function attack(target:Fighter):void{ var modifiedOffense:uint = this.offense + FightSimulator.roll(); var modifiedDefense:uint = target.getDefense() + FightSimulator.roll(); var modifiedDamage:uint = this.damage + FightSimulator.roll(); if(modifiedOffense > modifiedDefense){ trace(this.name + " hits " + target.getName() + "!"); target.takeDamage(modifiedDamage); trace(target.getName() + " takes " + modifiedDamage + " damage!"); }else{ trace(this.name + " misses " + target.getName() + "!"); } } /** *This function subtracts from this fighter's health. * *amount - the amount of health to deduct */ public function takeDamage(amount:uint):void{ if(amount > this.health){ //lets cap the amount of damage at their current health //so it doesn't roll over to a really big number amount = this.health; } this.health -= amount; } /** *This function outputs the fighter's last words to the output */ public function speakLastWords():void{ trace(this.name + " says, \"" + this.lastWords + "\""); } /** *This function outputs the fighter's victory speech */ public function claimVictory():void{ trace(this.name + " says, \"" + this.victorySpeech + "\""); } } }