Coding > FPC Valkyrie
Some Pascal Object help
konijn:
I could find it on Google I guess,
but my questions here could illicit some design advice as well.
I am planning to have door tiles with a door Entity, so that when the player bumps into the door , the door knows it should change the tile it is on to 'open door' and remove itself from the map ( destroy itself ? ).
Now ( starting from the roguelike tutorial ):
- Is it possible to assign 1 door entity to several different cells to save memory ( in that case I would not destroy the entity )
- How do I know if object x is an instance of class y ? What is the keyword in other words ?
- How do I pass the bumper to the entity ( do I use self or this or still some other keyword ? )
if Cells[tx,ty].Entity <> nil then
Cells[tx,ty].Entity.bump( this or self or myself or what ? )
exit(False);
end;
-How does one do casting of an object ?
Cheers,
T.
Kornel Kisielewicz:
--- Quote from: konijn on March 15, 2007, 07:15 ---I could find it on Google I guess,
but my questions here could illicit some design advice as well.
--- End quote ---
--- Quote from: konijn on March 15, 2007, 07:15 ---I am planning to have door tiles with a door Entity, so that when the player bumps into the door , the door knows it should change the tile it is on to 'open door' and remove itself from the map ( destroy itself ? ).
--- End quote ---
First of all -- the biggest error people make with real-world OOP usage is overusing OOP. Why do you need an object for a door at all? Make it a normal tile, add it an "OPENABLE" flag and OpensTo : {TileID} and check it when you check bumping into objects.
Now ( starting from the roguelike tutorial ):
--- Quote from: konijn on March 15, 2007, 07:15 ---- Is it possible to assign 1 door entity to several different cells to save memory ( in that case I would not destroy the entity )
--- End quote ---
I keep all cell data in a flat record... Example :
--- Code: (delphi) ---// Terrain data -- collection of tile definitions. Tile 0 is special -- it's
// not supposed to be used, but is there to prevent and identify errors.
const TerraData : array[0..MAXTILES] of TTerrainData = (
( Name : 'void'; Picture : Ord(' ')+256*LightGray;
DarkPic : Ord(' ')+256*DarkGray; DR: 0; DestroyID : 0; ActID : 0; Flags : [TF_NOMOVE,TF_NOSIGHT]),
( Name : 'floor'; Picture : Ord('.')+256*LightGray;
DarkPic : Ord('.')+256*DarkGray; DR: 0; DestroyID : 0; ActID : 0; Flags : []),
( Name : 'wall'; Picture : Ord('#')+256*LightGray;
DarkPic : Ord('#')+256*DarkGray; DR: 20;DestroyID : 9; ActID : 0; Flags : [TF_NOMOVE,TF_NOSIGHT]),
( Name : 'closed door'; Picture : Ord('-')+256*LightGray;
DarkPic : Ord('-')+256*DarkGray; DR: 0; DestroyID : 0; ActID : 4; Flags : [TF_NOMOVE,TF_NOSIGHT,TF_OPENABLE]),
( Name : 'open door'; Picture : Ord('/')+256*LightGray;
DarkPic : Ord('-')+256*DarkGray; DR: 10;DestroyID : 9; ActID : 3; Flags : []),
...
--- End code ---
--- Quote from: konijn on March 15, 2007, 07:15 ---- How do I know if object x is an instance of class y ? What is the keyword in other words ?
--- End quote ---
--- Code: (delphi) ---if Node.inheritsFrom(TBeing) then ... ; // True if Node inherits from TBeing
--- End code ---
This has the added benefit that if TPlayer descends from TBeing this will also be true.
inheritsFrom is a built-in FreePascal Class function.
--- Quote from: konijn on March 15, 2007, 07:15 ---- How do I pass the bumper to the entity ( do I use self or this or still some other keyword ? )
--- End quote ---
Self.
--- Quote from: konijn on March 15, 2007, 07:15 ---if Cells[tx,ty].Entity <> nil then
Cells[tx,ty].Entity.bump( this or self or myself or what ? )
exit(False);
end;
--- End quote ---
I don't know what you want to achieve here :|
--- Quote from: konijn on March 15, 2007, 07:15 ----How does one do casting of an object ?
--- End quote ---
What do you mean by casting? This?:
--- Code: (delphi) ---procedure TLevel.Tick;
var Node : TNode;
begin
Node := Child;
if Node <> nil then
repeat
TBeing(Node).Tick; // <-- this is casting?
Node := Node.Next;
until Node = Child;
end;
--- End code ---
konijn:
Thanks Kornel!
Wow,
responding with a quote that quotes a post is tough ;)
Anyway,
- Doors in HaH are in a flat structure, the entity just changes closed door to open door, since I didnt want too many flags in my flat structure ( I am now tempted to undo that )
- Flags : [TF_NOMOVE,TF_NOSIGHT] is really interesting, what would be the type of Flags ? a set ?
- Casting , if you get an object x of class TNode, but you know that really it is also of class TPlayer, how do I force it into a variable y of class TPlayer ? just y := TPlayer(x)?
My RL ( I undid my decision for WesnothRL ) is advancing slowly, but it feels good to do Pascal again. The habits are slowly returning. Where are the days I did Pascal homeworks for money ;)
He Who Sheds Light,
T.
Kornel Kisielewicz:
--- Quote from: konijn on March 15, 2007, 13:06 ---- Flags : [TF_NOMOVE,TF_NOSIGHT] is really interesting, what would be the type of Flags ? a set ?
--- End quote ---
Yeah, there should be even two types in the vutil.pas:
--- Code: (delphi) ---TFlags = set of Byte; // This one can hold 256 flags and takes 8 bytes memory
TFlags32 = set of [0..31];// This one can hold 32 flags but takes only 4 bytes
--- End code ---
The cool thing about it is that you can write things like :
--- Code: (delphi) ---if FLAG_NOMOVE in Cell.Flags then ...
Include(Flags,FLAG_NOMOVE);
--- End code ---
And use other set operators on it (sets are something that C++ people can envy us). See the FP documentation on sets.
--- Quote from: konijn on March 15, 2007, 13:06 ---- Casting , if you get an object x of class TNode, but you know that really it is also of class TPlayer, how do I force it into a variable y of class TPlayer ? just y := TPlayer(x)?
--- End quote ---
Yes :).
--- Quote from: konijn on March 15, 2007, 13:06 ---My RL ( I undid my decision for WesnothRL ) is advancing slowly, but it feels good to do Pascal again. The habits are slowly returning. Where are the days I did Pascal homeworks for money ;)
--- End quote ---
I'm sorry I didn't update Valkyrie before the 7DRL :(. I feel kinda' guilty :(.
konijn:
--- Quote from: Kornel Kisielewicz on March 15, 2007, 13:52 ---
--- Quote from: konijn on March 15, 2007, 13:06 ---My RL ( I undid my decision for WesnothRL ) is advancing slowly, but it feels good to do Pascal again. The habits are slowly returning. Where are the days I did Pascal homeworks for money ;)
--- End quote ---
I'm sorry I didn't update Valkyrie before the 7DRL :(. I feel kinda' guilty :(.
--- End quote ---
Eh, no worries, I would most likely still not have made it ;)
Also, I was compiling from my USB stick so that I could work on several laptops,
once I started compiling on my hd, it flies ;)
I have a fixed, map, @ walking around, things to pick up, doors, so all I need now is some monsters and los / AI ;)
For TNode I was going to propose the method Has( child: TNode ) but really one could also say
if child.Parent = whoeverIthinkMyParent is. It is up to you ;)
Cheers,
T.
Navigation
[0] Message Index
[#] Next page
Go to full version