Skip to content

Commit ff1ae47

Browse files
committed
Items.
1 parent ae3ffc1 commit ff1ae47

File tree

4 files changed

+136
-10
lines changed

4 files changed

+136
-10
lines changed

uItem.pas

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,129 @@
22

33
interface
44

5+
uses BearLibItems, uCommon, uMap;
6+
7+
type
8+
TItemBase = record
9+
Symbol: Char;
10+
Name: string;
11+
MaxDurability: Byte;
12+
Color: Cardinal;
13+
Deep: TDeepEnum;
14+
end;
15+
16+
const
17+
ItemCount = 6;
18+
//
19+
itGold = 0;
20+
21+
const
22+
ItemBase: array [0..ItemCount - 1] of TItemBase = (
23+
// All
24+
(Symbol: '$'; Name: 'Gold'; MaxDurability: 0; Color: clYellow; Deep: deDarkWood; ),
25+
// Dark Wood
26+
(Symbol: '/'; Name: 'Item'; MaxDurability: 0; Color: clYellow; Deep: deDarkWood; ),
27+
// Gray Cave
28+
(Symbol: '/'; Name: 'Item'; MaxDurability: 0; Color: clYellow; Deep: deGrayCave; ),
29+
// Deep Cave
30+
(Symbol: '/'; Name: 'Item'; MaxDurability: 0; Color: clYellow; Deep: deDeepCave; ),
31+
// Blood Cave
32+
(Symbol: '/'; Name: 'Item'; MaxDurability: 0; Color: clYellow; Deep: deBloodCave; ),
33+
// Dungeon of Doom
34+
(Symbol: '/'; Name: 'Item'; MaxDurability: 0; Color: clYellow; Deep: deDungeonOfDoom;)
35+
);
36+
37+
type
38+
TItems = class(TObject)
39+
private
40+
41+
public
42+
constructor Create;
43+
destructor Destroy; override;
44+
procedure Render(AX, AY: Byte);
45+
procedure Add(ADeep: TDeepEnum);
46+
end;
47+
48+
var
49+
Items: TItems = nil;
50+
551
implementation
652

53+
uses Math, uTerminal, uPlayer;
54+
55+
{ TItems }
56+
57+
procedure TItems.Add(ADeep: TDeepEnum);
58+
var
59+
ID, FX, FY: Byte;
60+
FItem: Item;
61+
D: Integer;
62+
begin
63+
repeat
64+
ID := Math.RandomRange(0, ItemCount);
65+
FX := Math.RandomRange(0, High(Byte));
66+
FY := Math.RandomRange(0, High(Byte));
67+
until (Map.GetTileEnum(FX, FY, ADeep) in SpawnTiles)
68+
and ((ID <= itGold) or (ItemBase[ID].Deep = ADeep));
69+
FItem.MapID := Ord(ADeep);
70+
FItem.ItemID := ID;
71+
FItem.X := FX;
72+
FItem.Y := FY;
73+
case FItem.ItemID of
74+
itGold: // Gold
75+
begin
76+
FItem.Stack := 1000;
77+
FItem.Amount := Math.RandomRange(0, 25) + 1;
78+
end;
79+
else
80+
begin
81+
FItem.Stack := 1;
82+
FItem.Amount := 1;
83+
end;
84+
end;
85+
if (FItem.Stack = 1) then
86+
begin
87+
D := ItemBase[ID].MaxDurability;
88+
FItem.Durability := Math.RandomRange(D div 5, D) + 1;
89+
end;
90+
Items_Dungeon_AppendItem(FItem);
91+
end;
92+
93+
constructor TItems.Create;
94+
begin
95+
Items_Open;
96+
end;
97+
98+
destructor TItems.Destroy;
99+
begin
100+
Items_Close;
101+
inherited;
102+
end;
103+
104+
procedure TItems.Render(AX, AY: Byte);
105+
var
106+
I, Count: Integer;
107+
FItem: Item;
108+
MapID: Byte;
109+
begin
110+
MapID := Ord(Map.Deep);
111+
Count := Items_Dungeon_GetMapCount(MapID);
112+
for I := Count - 1 downto 0 do
113+
begin
114+
FItem := Items_Dungeon_GetMapItem(MapID, I);
115+
if not Map.InView(FItem.X, FItem.Y)
116+
or (not WizardMode and not Map.GetFOV(FItem.X, FItem.Y)) then Continue;
117+
Terminal.Print(FItem.X - Player.X + AX + View.Left,
118+
FItem.Y - Player.Y + AY + View.Top, ItemBase[FItem.ItemID].Symbol,
119+
ItemBase[FItem.ItemID].Color);
120+
end;
121+
end;
122+
123+
initialization
124+
Items := TItems.Create;
125+
126+
finalization
127+
Items.Free;
128+
Items := nil;
129+
7130
end.

uMap.pas

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ TMap = class(TObject)
133133

134134
implementation
135135

136-
uses Math, uPlayer, uMob;
136+
uses Math, uPlayer, uMob, uItem;
137137

138138
{ TMap }
139139

@@ -331,7 +331,8 @@ procedure TMap.Gen;
331331
for FDeep := Low(TDeepEnum) to High(TDeepEnum) do
332332
begin
333333
for I := 0 to 255 do Mobs.Add(FDeep);
334-
end;
334+
for I := 0 to 255 do Items.Add(FDeep);
335+
end;
335336
end;
336337

337338
function TMap.GetTile(ATileEnum: TTileEnum): TTile;

uMob.pas

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ TMobs = class(TObject)
6262
function GetIndex(AX, AY: Byte): Integer;
6363
end;
6464

65-
type
65+
type
6666
TGetXYVal = function(X, Y: Integer): Boolean; stdcall;
6767

6868
var
@@ -148,10 +148,11 @@ procedure TMob.Process;
148148

149149
procedure TMob.Render(AX, AY: Byte);
150150
begin
151-
if not Map.InView(X, Y) or (not WizardMode and not Map.GetFOV(X, Y)) then Exit;
152-
Terminal.ForegroundColor(MobBase[ID].Color);
151+
if not Map.InView(X, Y) or (not WizardMode
152+
and not Map.GetFOV(X, Y)) then Exit;
153153
Terminal.Print(X - Player.X + AX + View.Left,
154-
Y - Player.Y + AY + View.Top, MobBase[ID].Symbol);
154+
Y - Player.Y + AY + View.Top, MobBase[ID].Symbol,
155+
MobBase[ID].Color);
155156
end;
156157

157158
{ TMobs }
@@ -166,7 +167,7 @@ procedure TMobs.Add(ADeep: TDeepEnum);
166167
FMob[I].AddRandom(ADeep);
167168
Exit;
168169
end;
169-
SetLength(FMob, Length(FMob) + 1);
170+
SetLength(FMob, Length(FMob) + 1);
170171
I := Length(FMob) - 1;
171172
FMob[I] := TMob.Create;
172173
FMob[I].AddRandom(ADeep);

uScenes.pas

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ implementation
120120

121121
uses
122122
SysUtils, Types, Dialogs, Math, uCommon, uTerminal, uPlayer, BearLibTerminal,
123-
uMap, uMob, uMsgLog;
123+
uMap, uMob, uMsgLog, uItem;
124124

125125
{ TScene }
126126

@@ -389,10 +389,11 @@ procedure TSceneGame.Render;
389389
if WizardMode or not Map.GetFog(X, Y) then
390390
Terminal.Print(DX + View.Left, DY + View.Top, T.Symbol);
391391
end;
392-
// Player, mobs, items
392+
// Items, player, mobs
393+
Items.Render(PX, PY);
393394
Player.Render(PX, PY);
394395
Mobs.Render(PX, PY);
395-
// Player info
396+
// Player info
396397
Terminal.BackgroundColor(0);
397398
Terminal.ForegroundColor(clYellow);
398399
Terminal.Print(Status.Left, Status.Top, 'Trollhunter');

0 commit comments

Comments
 (0)