Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class structure 1

Status
Not open for further replies.

Fluke026

Programmer
Aug 20, 2002
32
US
Hi,

I am almost sure this has a simple solution, but I can't seem to find it. I have a set of classes TArena and TPlayer with a dec something like this...

Code:
PlayerArr = array[1..MaxPlayer] of TAIPlayer;
TArena = class(TPersistent)
private
   Players: PlayerArr;
   {other goodies}
public
   procedure MovePlayers;
end;

Each TAIPlayer has a limited range of vision forward and to the side. Each AI decides where to move based on what he can see (i.e. are other AIs in the way?, is it close to a wall?). I know that I could send information in from the MovePlayers procedure (like an array), but I wonder if there is any way that I can have the TAIPlayer scan up into the Arena. I think that would be better because I need the AIs to scan each other for speed and direction so they can avoid one another properly.

Can a class look "upward" or do I need to find another approach?

Frank Luke
 
Maybe something like this:

If you make a constructor for your player class:

interface
public
constructor Create(AOwner:TArena);
Owner:TArena;

implementation
constructor TAIPlayer.Create(AOwner:TArena);
begin
inherited create;
Owner := AOwner;
end;

Then whenever you want to know about the arena, you can say
Owner.LeftWall or Owner.PlayerCount etc...
You can then loop through the players owned by the arena using properties of the arena.
 
That looks perfect! I'll have to experiment a little this weekend, but that could solve the biggest hurdle. My main concern will be discovering if that code makes a one time copy of the Arena for each Player of if it points back to it. That is important for when the little players move around--do they see where the others are or where they were at creation? Should be no problem to make it a pointer to the Arena though (or some other solution may become apparent as I work).

Thanks bbegly! How about a star?
 
It points back. Personally I prefer to call such fields "Parent" rather than "Owner"--look at the parent and owner fields in IIRC tWinControl. Owner is the one responsible for cleanup, but parent is what's above it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top