| 1 |
/* |
| 2 |
* Anonymous object test |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "tads.h" |
| 6 |
#include "t3.h" |
| 7 |
|
| 8 |
/* set the 'location' property */ |
| 9 |
+ property location; |
| 10 |
|
| 11 |
PreinitObject |
| 12 |
execute() |
| 13 |
{ |
| 14 |
/* initialize the location for every object */ |
| 15 |
forEachInstance(CObject, { obj: obj.initLocation() }); |
| 16 |
} |
| 17 |
; |
| 18 |
|
| 19 |
main(args) |
| 20 |
{ |
| 21 |
local cnt; |
| 22 |
|
| 23 |
"This is the anonymous object test!\b"; |
| 24 |
|
| 25 |
"List of 'Item' objects:\n"; |
| 26 |
cnt = 0; |
| 27 |
forEachInstance(Item, { obj: "[<<++cnt>>]: <<obj.sdesc>>\n" }); |
| 28 |
"\b"; |
| 29 |
|
| 30 |
"Containment graph:\n"; |
| 31 |
forEachInstance(Room, |
| 32 |
{ obj: "Room: <<obj.sdesc>>\n<<obj.listCont(1)>>\b" }); |
| 33 |
"\b"; |
| 34 |
} |
| 35 |
|
| 36 |
/* |
| 37 |
* root object |
| 38 |
*/ |
| 39 |
class CObject: object |
| 40 |
/* list the contents */ |
| 41 |
listCont(level) |
| 42 |
{ |
| 43 |
for (local i = 1, local cnt = contents.length() ; i <= cnt ; ++i) |
| 44 |
{ |
| 45 |
/* indent */ |
| 46 |
for (local j = 1 ; j <= level ; ++j) |
| 47 |
"\ \ "; |
| 48 |
|
| 49 |
/* show this object and end the line */ |
| 50 |
contents[i].sdesc; |
| 51 |
"\n"; |
| 52 |
|
| 53 |
/* show the contents of this object */ |
| 54 |
contents[i].listCont(level + 1); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/* initialize location - add myself to my location's contents list */ |
| 59 |
initLocation() |
| 60 |
{ |
| 61 |
if (self.propType(&location) == TypeObject) |
| 62 |
location.contents += self; |
| 63 |
} |
| 64 |
|
| 65 |
/* contents list */ |
| 66 |
contents = [] |
| 67 |
; |
| 68 |
|
| 69 |
class Item: CObject |
| 70 |
sdesc = "<Item>" |
| 71 |
; |
| 72 |
|
| 73 |
class Container: Item |
| 74 |
; |
| 75 |
|
| 76 |
class Room: CObject |
| 77 |
sdesc = "<Room>" |
| 78 |
; |
| 79 |
|
| 80 |
Item sdesc = "red ball"; |
| 81 |
Item sdesc = "blue ball"; |
| 82 |
Item sdesc = "green box"; |
| 83 |
|
| 84 |
yellowBox: Item sdesc = "yellow box"; |
| 85 |
|
| 86 |
Cave: Room |
| 87 |
sdesc = "Cave" |
| 88 |
; |
| 89 |
|
| 90 |
+ Item sdesc = "nasty knife" ; |
| 91 |
+ rustyKnife: Item sdesc = "rusty knife" ; |
| 92 |
+ Container sdesc = "box" ; |
| 93 |
|
| 94 |
++ Container sdesc = "envelope" ; |
| 95 |
+++ Item sdesc = "letter" ; |
| 96 |
++ Item sdesc = "fuel cell" ; |
| 97 |
|
| 98 |
+ Item sdesc = "axe" ; |
| 99 |
|
| 100 |
NSPassage: Room |
| 101 |
sdesc = "North-South Passage" |
| 102 |
; |
| 103 |
|
| 104 |
+ Container sdesc = "tank" ; |
| 105 |
++ Item sdesc = "quantity of water" ; |
| 106 |
|