Hi there...
I'm reading a book about AI "Programming Game by Example" by Mat buckland.
This book has C++ example (no flash

)
So as I'm going through the book I'm writting the example for flash.
The thing is I come on something I dunno if it's possible to do in flash.
I will try to simplify at max the explaination.
Let's say you have a miner which is an instance of the Miner class.
ActionScript Code:
import classes.EnterMineAndDigForNugget;
import classes.Location
import classes.State
class classes.Miner extends classes.Character {
// actual State
private var _pCurrentState:State;
// the place where the miner is currently situated
private var _sLocation:Location;
// how many nuggets the miner has in this pockets
private var _nGoldCarried:Number = 0;
// how much money the miner has deposited in the bank
private var _nMoneyBank:Number = 0;
// the higher the value the thirstier the miner
private var _nThirst:Number = 0;
// the higher the value, the more tired the miner
private var _nFatigue:Number = 0;
// constant
// the amount of gold a miner must have before he feels comfortable
private var _nComfortLevel:Number = 5;
// the amount of nuggets a miner can carry
private var _nMaxNugget:Number = 3;
// above this value a miner is thirsty
private var _nThirstLevel:Number = 5;
// above this value a miner is sleepy
private var _nTirednessThreshold:Number = 5;
// constructor
public function Miner() {
_pCurrentState = EnterMineAndDigForNugget.instance;
}
// overwritting
public function update():Void {
_nThirst += 1;
if (_pCurrentState != undefined) _pCurrentState.execute(this);
}
// this function change the current state to the new state
public function changeState(s:State):Void {
// call the exit methods of the current state
if (_pCurrentState != undefined) _pCurrentState.exit(this);
_pCurrentState = s;
// call the enter methods of the current state
if (_pCurrentState != undefined) _pCurrentState.enter(this);
}
/*
* Miner action and state
*/
public function BuyAndDrinkAWhiskey(Void):Void {
_nThirst = 0;
_nMoneyBank -= 2;
}
public function increaseFatigue(Void):Void {
_nFatigue++;
}
public function decreaseFatigue(Void):Void {
_nFatigue--;
}
public function fatigued():Boolean {
if (_nFatigue > _nTirednessThreshold) return true;
return false;
}
public function pocketsFull(Void):Boolean {
if (_nGoldCarried >= _nMaxNugget) return true;
return false;
}
public function thirsty(Void):Boolean {
if (_nThirst >= _nThirstLevel) return true;
return false;
}
/******************************************************************************
* GET AND SET
******************************************************************************/
public function set location(s:String) {
_sLocation.change(s);
}
public function get location():String {
return _sLocation.find();
}
public function set goldCarrier (n:Number) {
_nGoldCarried = n;
}
public function get goldCarrier ():Number {
return _nGoldCarried;
}
public function set moneyBank (n:Number) {
_nMoneyBank = n;
}
public function get moneyBank ():Number {
return _nMoneyBank;
}
public function get comfortLevel ():Number {
return _nComfortLevel
}
public function set fatigue (n:Number) {
_nFatigue = n;
}
public function get fatigue ():Number {
return _nFatigue;
}
}
The miner can have different state (thirsty, working, sleep)
Each state is a singleton class holding the code for this state and to change to another state (ie, sleep: if (!tired) miner.changeState(working))
Those 3 classes are inheriting the State class
ActionScript Code:
import classes.Miner
class classes.State {
private function State(){}
public function enter(miner:Miner):Void {}
public function execute(miner:Miner):Void {}
public function exit(miner:Miner):Void {}
}
and implementing the IState interface (thus I'm doing like an abstract class)
ActionScript Code:
import classes.Miner
interface classes.IState {
public function enter(miner:Miner):Void;
public function execute(miner:Miner):Void;
public function exit(miner:Miner):Void;
}
As you can see the state class has for this function a typed argument.
What I would like to do is beeing able to reuse this class for other character than miner.
Let's say I have minerWife. I would like beeing able to use the same State class in my definition, like
ActionScript Code:
class classes.minerWife {
// actual State
private var _pCurrentState:State;
....
// overwritting
public function update():Void {
_nThirst += 1;
// if I can't change the type inside the State class I will get an error here
// because the state will wait a Miner object and not a minerWife one
if (_pCurrentState != undefined) _pCurrentState.execute(this);
}
}
I don't want to rewrite the state class as often as I need a new class to use it...
I know that it can be achive in C++ with the class template.
Thx mates...