Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 01-18-2006, 02:37 AM   #1
xxlm
Meuh? MMeuh!
 
xxlm's Avatar
 
Join Date: Sep 2001
Location: Auckland - New Zealand
Posts: 3,050
Send a message via ICQ to xxlm Send a message via AIM to xxlm Send a message via MSN to xxlm Send a message via Yahoo to xxlm
Default Reusable class ... ?

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...
__________________
I'm a froggy, so excuse me for my poor english
Blog - Dev By MX
For any jobs go on my blog on the contact page...
xxlm is offline   Reply With Quote
Old 01-18-2006, 03:19 AM   #2
astgtciv
Resu Deretsiger
 
astgtciv's Avatar
 
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,329
Default

I think that to do this in AS you would have to change the of the State.execute() method to accept a superclass of Miner and MinerWife (or, ultimately, an Object). Then you would dispatch on the type of passed parameter... (ugly yes ) Manual implementation of method polymorphism...

ActionScript Code:
public function execute(someone:Object):Void { if (someone instanceof Miner) { } else if (someone instanceof MinerWife) { } else { // default } }

This certainly doesn't sound like valuable advice, we'll see what the gurus say about this
astgtciv is offline   Reply With Quote
Old 01-18-2006, 03:22 AM   #3
xxlm
Meuh? MMeuh!
 
xxlm's Avatar
 
Join Date: Sep 2001
Location: Auckland - New Zealand
Posts: 3,050
Send a message via ICQ to xxlm Send a message via AIM to xxlm Send a message via MSN to xxlm Send a message via Yahoo to xxlm
Default

Yeah I thought about this but then a Miner could call a DoCooking class (inheriting from the state one). Have you ever see a miner cooking???
What I mean is then all the derived class of State could be access by all the characters... Not good he...
But I think if there is no real way I will take this one
__________________
I'm a froggy, so excuse me for my poor english
Blog - Dev By MX
For any jobs go on my blog on the contact page...
xxlm is offline   Reply With Quote
Old 01-18-2006, 03:29 AM   #4
astgtciv
Resu Deretsiger
 
astgtciv's Avatar
 
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,329
Default

Well, how would you have used a C++ template for this? E.g., a Miner gets his own parametrized version of the State class, State<Miner>?
astgtciv is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using the mx.util.Delegate class within your classes! madgett ActionScript 2.0 3 09-02-2007 11:11 PM
Database simulated with arrays on a cd-rom lecasn5 Components 61 09-07-2004 11:40 AM
Inherited Properties problem with class hierarchy Johnathon_apple ActionScript 1.0 (and below) 5 09-17-2003 12:38 AM
card game: designing Deck class and good OOP? pigpen Gaming and Game Development 1 11-26-2002 11:41 AM


All times are GMT. The time now is 08:29 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.