Coding > FPC Valkyrie

Inventory

<< < (2/2)

Kornel Kisielewicz:
Okay, so:
1) one item (or stack) per mapsquare
2) inventory handled seperately
3) monster inventory and equipment
4) open size inventory

Yes?

Igor Savin:
Yes.

Kornel Kisielewicz:
Okay, so let's start. Item needs to be a descendant of TNode, as the player. I guess the structure should be:


--- Code: ---TNode -> TThing -> TNPC  -> TPlayer
                -> TItem

--- End code ---

You can also put something like TGameObject between TNode and TThing, I use it so all game objects have a base class.

First, item handling by TLevel. Make TItems children of TLevel. Assign proper x,y coords. And have the "TMapCell" like this:


--- Code: (delphi) ---type TMapCell = record
  NPC : TNPC;
  Item : TItem;
  Cell : Byte; // a reference to a cell array;
end;

--- End code ---

You may ask why is it a record and not an object? Because you will have LOTS of those, and the Allocation and disposing of them would be a hell both speed-wise and memory-fragmentation-wise.

When adding an object to the level you'd use something like this:


--- Code: (delphi) ---procedure TLevel.AddItem(x,y : Word; Item : TItem);
begin
  // just safety measures, these should be checked
  // before, for proper fail handling
  if Item           = nil then Exit;
  if Map[x,y].Item <> nil then Exit;

  Map[x,y].Item := Item; // Register the item to the map
  Add(Item);             // Add it to Self's (TLevel) children
end;

--- End code ---


Then item pickup is simple :


--- Code: (delphi) ---  Level.Map[x,y].Item.Move(Player);
  Level.Map[x,y].Item := nil;

--- End code ---

The Move method detaches the item from the level, and attaches it to the Player. Droping would be similar:


--- Code: (delphi) ---procedure TPlayer.DropItem(Item : TItem);
begin
  // just safety measures, these should be checked
  // before, for proper fail handling
  if Item           = nil then Exit;
  if Map[x,y].Item <> nil then Exit;

  Item.Move(Level);
  Map[x,y].Item := Item;
end;

--- End code ---

Here's a code to list the inventory items:


--- Code: (delphi) ---procedure TPlayer.ListItems(Item : TItem);
var Scan : TNode;
begin
  Scan := Child;
  repeat
    if Scan.inheritsFrom(TItem) then
      Writeln(TItem(Scan).Name);
    Scan := Scan.Next;
  until Scan = Child;
end;

--- End code ---

Yes, you shouldn't use Writeln :-P. But there's a problem here -- you chose unlimited inventory. The UI for unlimited inventory will be hard to code. My advice is to set a limit for inventory now, and then once you feel comfortable with it, redesign it without the limit -- it will be easy, cause engine-wise the limit will be fictional.

The way to handle UI anyway is to put all the items in an array of Items, sort/filter them while doing so, then allow a choice from the filtered list. You have an example in the test code I posted above :).

Kornel Kisielewicz:
Uh wait -- that scan is broken, for it uses the single linked list approach of an older valkyrie :-P

Igor Savin:
Aha... thanks. As soon as I deal with the Latin that arised suddenly, I'll integrate this code into my ouevre...

Navigation

[0] Message Index

[*] Previous page

Go to full version