Skip to content

Commit

Permalink
Items.
Browse files Browse the repository at this point in the history
  • Loading branch information
devapromix committed Mar 12, 2017
1 parent ae3ffc1 commit ff1ae47
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 10 deletions.
123 changes: 123 additions & 0 deletions uItem.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,129 @@

interface

uses BearLibItems, uCommon, uMap;

type
TItemBase = record
Symbol: Char;
Name: string;
MaxDurability: Byte;
Color: Cardinal;
Deep: TDeepEnum;
end;

const
ItemCount = 6;
//
itGold = 0;

const
ItemBase: array [0..ItemCount - 1] of TItemBase = (
// All
(Symbol: '$'; Name: 'Gold'; MaxDurability: 0; Color: clYellow; Deep: deDarkWood; ),
// Dark Wood
(Symbol: '/'; Name: 'Item'; MaxDurability: 0; Color: clYellow; Deep: deDarkWood; ),
// Gray Cave
(Symbol: '/'; Name: 'Item'; MaxDurability: 0; Color: clYellow; Deep: deGrayCave; ),
// Deep Cave
(Symbol: '/'; Name: 'Item'; MaxDurability: 0; Color: clYellow; Deep: deDeepCave; ),
// Blood Cave
(Symbol: '/'; Name: 'Item'; MaxDurability: 0; Color: clYellow; Deep: deBloodCave; ),
// Dungeon of Doom
(Symbol: '/'; Name: 'Item'; MaxDurability: 0; Color: clYellow; Deep: deDungeonOfDoom;)
);

type
TItems = class(TObject)
private

public
constructor Create;
destructor Destroy; override;
procedure Render(AX, AY: Byte);
procedure Add(ADeep: TDeepEnum);
end;

var
Items: TItems = nil;

implementation

uses Math, uTerminal, uPlayer;

{ TItems }

procedure TItems.Add(ADeep: TDeepEnum);
var
ID, FX, FY: Byte;
FItem: Item;
D: Integer;
begin
repeat
ID := Math.RandomRange(0, ItemCount);
FX := Math.RandomRange(0, High(Byte));
FY := Math.RandomRange(0, High(Byte));
until (Map.GetTileEnum(FX, FY, ADeep) in SpawnTiles)
and ((ID <= itGold) or (ItemBase[ID].Deep = ADeep));
FItem.MapID := Ord(ADeep);
FItem.ItemID := ID;
FItem.X := FX;
FItem.Y := FY;
case FItem.ItemID of
itGold: // Gold
begin
FItem.Stack := 1000;
FItem.Amount := Math.RandomRange(0, 25) + 1;
end;
else
begin
FItem.Stack := 1;
FItem.Amount := 1;
end;
end;
if (FItem.Stack = 1) then
begin
D := ItemBase[ID].MaxDurability;
FItem.Durability := Math.RandomRange(D div 5, D) + 1;
end;
Items_Dungeon_AppendItem(FItem);
end;

constructor TItems.Create;
begin
Items_Open;
end;

destructor TItems.Destroy;
begin
Items_Close;
inherited;
end;

procedure TItems.Render(AX, AY: Byte);
var
I, Count: Integer;
FItem: Item;
MapID: Byte;
begin
MapID := Ord(Map.Deep);
Count := Items_Dungeon_GetMapCount(MapID);
for I := Count - 1 downto 0 do
begin
FItem := Items_Dungeon_GetMapItem(MapID, I);
if not Map.InView(FItem.X, FItem.Y)
or (not WizardMode and not Map.GetFOV(FItem.X, FItem.Y)) then Continue;
Terminal.Print(FItem.X - Player.X + AX + View.Left,
FItem.Y - Player.Y + AY + View.Top, ItemBase[FItem.ItemID].Symbol,
ItemBase[FItem.ItemID].Color);
end;
end;

initialization
Items := TItems.Create;

finalization
Items.Free;
Items := nil;

end.
5 changes: 3 additions & 2 deletions uMap.pas
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ TMap = class(TObject)

implementation

uses Math, uPlayer, uMob;
uses Math, uPlayer, uMob, uItem;

{ TMap }

Expand Down Expand Up @@ -331,7 +331,8 @@ procedure TMap.Gen;
for FDeep := Low(TDeepEnum) to High(TDeepEnum) do
begin
for I := 0 to 255 do Mobs.Add(FDeep);
end;
for I := 0 to 255 do Items.Add(FDeep);
end;
end;

function TMap.GetTile(ATileEnum: TTileEnum): TTile;
Expand Down
11 changes: 6 additions & 5 deletions uMob.pas
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ TMobs = class(TObject)
function GetIndex(AX, AY: Byte): Integer;
end;

type
type
TGetXYVal = function(X, Y: Integer): Boolean; stdcall;

var
Expand Down Expand Up @@ -148,10 +148,11 @@ procedure TMob.Process;

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

{ TMobs }
Expand All @@ -166,7 +167,7 @@ procedure TMobs.Add(ADeep: TDeepEnum);
FMob[I].AddRandom(ADeep);
Exit;
end;
SetLength(FMob, Length(FMob) + 1);
SetLength(FMob, Length(FMob) + 1);
I := Length(FMob) - 1;
FMob[I] := TMob.Create;
FMob[I].AddRandom(ADeep);
Expand Down
7 changes: 4 additions & 3 deletions uScenes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ implementation

uses
SysUtils, Types, Dialogs, Math, uCommon, uTerminal, uPlayer, BearLibTerminal,
uMap, uMob, uMsgLog;
uMap, uMob, uMsgLog, uItem;

{ TScene }

Expand Down Expand Up @@ -389,10 +389,11 @@ procedure TSceneGame.Render;
if WizardMode or not Map.GetFog(X, Y) then
Terminal.Print(DX + View.Left, DY + View.Top, T.Symbol);
end;
// Player, mobs, items
// Items, player, mobs
Items.Render(PX, PY);
Player.Render(PX, PY);
Mobs.Render(PX, PY);
// Player info
// Player info
Terminal.BackgroundColor(0);
Terminal.ForegroundColor(clYellow);
Terminal.Print(Status.Left, Status.Top, 'Trollhunter');
Expand Down

0 comments on commit ff1ae47

Please sign in to comment.