I am feeling generous and decided to share my inventory so far. It is coming along nicely but
nowhere near finished (hence this free source ;D)
You will need Advanced2D to compile.
It probably don't make a lot of sense, but it feels like I wrote a short story after all that commenting so hopefully it will make some sense >.>
Sync On
Sync Rate 0
Rem LW - FIRST OF ALL, let's decide how big our inventory will be
Rem LW - I use global variable and not constant, because in my game
Rem LW - player will eventually be able to upgrade his inventory size
GLOBAL INVENTORY_SLOTS_X : INVENTORY_SLOTS_X = 6
GLOBAL INVENTORY_SLOTS_Y : INVENTORY_SLOTS_Y = 2
Rem LW - Make our Advanced2D font
Global a2FontINV
a2FontINV = a2CreateFont("consolas", 9, a2Size_Point(), a2Style_Normal())
Rem LW - the following is just here to make it media-free
Rem LW - although it should be noted that I am focusing this on 32x32 size tiles/images
Global inventory_image : inventory_image =1
Global item_img_start : item_img_start=100
Rem LW - make inventory image
make_inventory_image()
Rem LW - make items image for eh.. 10 images to increase chance that two items don't have same color
For x = 0 To 9:cls rgb(100+rnd(155),100+rnd(155),100+rnd(155))
Get image item_img_start+x, 0,0, 32,32:Next x
Rem LW - The UDT for all item information
Type tItem
Rem LW - items() and itemdata() shared
Name As String Rem LW - name of item
StackAble As Boolean Rem LW - if item is stackable
StackMax As Integer Rem LW - how many stacks is max
ImageID As Integer
Description As String Rem LW - short(or long) description about the item
Rem LW - items() specific
inInventory As Boolean Rem LW - if the item is in inventory or not
SourceID As Integer Rem LW - it tells us what ID from ITEMDATA() this item is
EndType
Rem LW - This array holds default information about items.
Rem LW - It should be filled automatically by an editor or whatever method you choose.
Dim ITEMDATA() As tItem
Rem LW - ..or fill it out manually like we do here >.>
Array Insert At Bottom ITEMDATA() : pos = Array Count( ITEMDATA() )
ITEMDATA(pos).NAme = "crazy hat"
ITEMDATA(pos).StackAble = 0
ITEMDATA(pos).ImageID = item_img_start+rnd(9)
ITEMDATA(pos).Description = "This crazy hat was once worn by an evil wizard who murdered thousands of ants every year"
Array Insert At Bottom ITEMDATA() : pos = Array Count( ITEMDATA() )
ITEMDATA(pos).Name = "potion"
ITEMDATA(pos).StackAble = 1
ITEMDATA(pos).StackMax = 10
ITEMDATA(pos).ImageID = item_img_start+rnd(9)
ITEMDATA(pos).Description = "You smell a strong fragrance of disgustingness, why would you inspect this potion? WHY?"
Array Insert At Bottom ITEMDATA() : pos = Array Count( ITEMDATA() )
ITEMDATA(pos).Name = "lobster"
ITEMDATA(pos).StackAble = 0
ITEMDATA(pos).ImageID = item_img_start+rnd(9)
ITEMDATA(pos).Description = "This lobster is still alive and struggling, you better put it down soon."
Rem LW - The UDT for inventory information
Type tInventory
SourceID As Integer Rem LW - id of the source item in this slot, as grabbed from ITEMS() array
Stacks As Integer Rem LW - how many stacks of an item in this slot
Busy As Boolean Rem LW - if there is an item in this slot
EndType
Rem LW - Global inventory variable
Dim INVENTORY( (INVENTORY_SLOTS_X*INVENTORY_SLOTS_Y)-1) As tInventory
Rem LW - This array holds information about items that we have loaded into the game
Rem LW - it is the most important because essentially it decides if an object should be hidden or visible-
Rem LW - if, in the game, there existed the possibility of dropping items on the ground.
Dim ITEMS() as tItem
Rem LW - randomly fill this items() array
Rem LW - let's pretend that a boss was killed and it dropped 5 items on the ground
kill_a_boss( 5 )
Global _mouseclick
Do : cls rgb(128,128,128)
Rem LW - if you press SPACE we kill a boss and more items drops
If spacekey() And sl=0 : kill_a_boss( 1+rnd(4) ) : sl =1 : EndIf
If not spacekey() Then sl =0
Rem LW - if you press RETURN add one more row to your inventory
If returnkey() And rl=0
Inc INVENTORY_SLOTS_Y, 1
Dim INVENTORY( (INVENTORY_SLOTS_X*INVENTORY_SLOTS_Y)-1) As tInventory
make_inventory_image()
cls rgb(128,128,128) Rem LW - clear the screen in this color again
rl =1 Rem LW - to avoid flash when creating new inventory image
EndIf
If not returnkey() Then rl =0
Rem LW - latch on mouseclick
_mouseclick =0
If MouseClick()=1 And ml =0 Then ml =1 : _mouseclick =1
If MouseClick()<>1 Then ml =0
Rem LW - I forgot what this function call does
INVENTORY_UPDATE()
Rem LW - this is some useless code here
Set Cursor 0,0 : Print "Items in database: "+Str$( Array Count(ITEMDATA())+1 )
For x = 0 To Array Count( ITEMDATA() ) : Print ITEMDATA(x).Name : NExt x
Print "--press space to drop rnd() amount of items from this database onto the ground below--"
Print : PRint : Print : Print "Items on the ground: "+Str$( _count+1 )
Text 450, 180, "Press return to add one more row in your inventory"
Rem LW - if you click an item from list, pick it up and place in inventory
_count = -1
For x = 0 To Array Count( ITEMS() )
If NOT ITEMS(x).inInventory
inc _count, 1
If MouseX() < 200 And MouseY() > 133+(_count*15) And MouseY() < 15+133+(_count*15) Then add$ = "<" Else add$ = ""
If add$ = "<" And _mouseclick =1 : add$ = "X" `<- that X is just here for no reason, don't worry
Rem LW - here, here is where we pick it up is what I meant
result = INVENTORY_ADD_ITEM( x )
Rem LW - this variable 'result' will be 0=no free slot, or 1=success
EndIf
Print x,") ",ITEMS(x).Name,add$
EndIf
Next x
Print "--click item to pick it up--"
Sync : Loop
Rem LW - this function will add an item to the inventory
Function INVENTORY_ADD_ITEM( id As Integer )
Local _isStack As Boolean
Local _freeSlot As Integer
Rem LW - First of all, we need to see if there is a free slot in the inventory
_isStack = 0
_freeSlot = -1
Rem LW - the first step is to see if the item we want to add is stackable
Rem LW - so that the item we pick up will rest in a stack, and not in a free slot
If ITEMS( id ).StackAble =1
Rem LW - now we need to see if there is a free slot in any of the stacks
For i = 0 To Array Count(INVENTORY())
Rem LW - inside all busy slots,
If Inventory(i).Busy =1
Rem LW - see if the item here is stackable
source = INVENTORY(i).SourceID
If ITEMS(source).StackAble =1
Rem LW - and see if they are from the same item family
If ITEMS(source).SourceID = ITEMS(id).SourceID
Rem LW - now, see if there is a free slot in this stack finally
If INVENTORY(i).Stacks+1 <= ITEMDATA(ITEMS(id).SourceID).StackMax-1
Rem LW - there is a free slot in this stack
Rem LW - we save this position
_freeSlot = i
Rem LW - we also let application know that we want to add to a stack
_isStack =1
Rem LW - now exit
Exit
EndIf
Rem LW - if there was no free slot in this stack, the application
Rem LW - will continue to search through the whole inventory until
Rem LW - it finds a stack that has a free slot, or if it does not
Rem LW - then it will eventually search for a free normal slot below.
EndIf
EndIf
EndIf
Next i
EndIF
Rem LW - now we need to check if we still need to look for a free slot,
Rem LW - because - if the requested item was not stackable then _freeSlot
Rem LW - will still be -1 and as well if there did not exist any free
Rem LW - slot in a stack it will be -1 so even then this requested item
Rem LW - should go to a new slot and open a new stack here
If _freeSlot = -1
For i = 0 To Array Count( INVENTORY() )
If INVENTORY(i).Busy =0
Rem LW - if there exist a free slot, we save this position and exit
_freeSlot = i
Exit
EndIf
Next i
EndIf
Rem LW - Now we need to check one last time what is the value of _freeSlot
Rem LW - if it is still -1 then there is no room for more items
Rem LW - so there is nothing to it.. let's run away!
If _freeSlot = -1 Then ExitFunction 0
Rem LW - if the fuction call made it this far, it means that there is a free slot somewhere in the inventory
Rem LW - and we have the free slot position stored in variable _freeSlot.
Rem LW - but first, let's see if this item should be stored inside a stack
If _isStack =1
Rem LW - and increase the amount of items in this stack
Inc INVENTORY( _freeSlot ).Stacks, 1
Else
Rem LW - Otherwise add this item to the inventory, and set status to busy=1
INVENTORY( _freeSlot ).Busy =1
INVENTORY( _freeSlot ).SourceID = id
EndIf
Rem LW - Finally we need to update items() array on requested id index to
Rem LW - let the game know that this item is now inside the inventory
ITEMS( id ).inInventory = 1
EndFunction 1
Function INVENTORY_UPDATE()
Local _index As Integer
Local _displayInfo As Integer
_displayInfo = -1
Rem LW - temporary variable store for inventory window position
xpos = 450
ypos = 200
Paste Image inventory_image, xpos, ypos
Rem LW - look through whole inventory
Rem LW - this variale _index is here for inventory() array slot position
_index = -1
Rem LW - remember we begin with y first, because we want the items to appear horizontally
For y = 0 To INVENTORY_SLOTS_Y-1
For x = 0 To INVENTORY_SLOTS_X-1
Rem LW - since we start at index -1 we can increase this variable already now
Rem LW - so that we begin this son of a gun loop with an index of 0
Rem LW - which will make our inventory() array very happy
Inc _index, 1
Rem LW - Check if there is anything in this slot
If INVENTORY( _index ).Busy =1
Rem LW - yeah, now paste this image from item
a2DrawImage ITEMS( INVENTORY(_index).SourceID ).ImageID, (xpos+5)+(x*33), (ypos+5)+(y*33), 0, 0, 0, 1, 0, 0xFFFFFFFF
Rem LW - If there are any stacks, let us know!
If INVENTORY( _index ).Stacks >0
text_x = ((xpos+5)+((x+1)*33))-(a2GetTextWidth(a2FontINV,Str$(INVENTORY( _index ).Stacks+1)))-3
a2Text a2FontINV, text_x, (ypos+5)+(y*33), Str$(INVENTORY( _index ).Stacks+1), 0xFF000000
EndIf
Rem LW - check mouse hover over this index
x1 = (xpos+5)+(x*33)
x2 = (xpos+5)+((x+1)*33)
y1 = (ypos+5)+(y*33)
y2 = (ypos+5)+((y+1)*33)
If MouseX() >= x1 And MouseX() <= x2 And MouseY() >= y1 And MouseY() <= y2
Rem LW - store item id in variable so we can draw that information on top of everything else
_displayInfo = INVENTORY(_index).SourceID
EndIf
EndIf
Next x
Next y
Rem LW - if requested, draw some information about this item
If _displayInfo > -1
INVENTORY_DISPLAY_ITEMINFO( _displayInfo )
EndIf
EndFunction
Rem LW - this will draw a little window with some text about the item mouse is currently hovering over
Function INVENTORY_DISPLAY_ITEMINFO( item_id As Integer)
If MouseX() >= Screen Width()/2 Then _direction =1
text$ = ITEMS( item_id ).Name+chr$(10)+ITEMS( item_id ).Description
length = 250 : height = 33 + ((a2GetTextWidth(a2FontINV,text$)/length)*a2GetLineHeight(a2FontINV))
Rem LW - draw towards right side of screen
If _direction =0
x1 = MouseX()+15 : x2 = x1+length
y1 = MouseY() : y2 = y1+height
a2FillBox x1, y1, x2+5, y2+5, 0xFF34332b
a2BoxText a2FontINV, x1+5, y1+5, x2, y2, text$, 0, 0, 1, 0xFFFFFFFF
Else
Rem LW - draw towards left of screen
x1 = MouseX()-15 : x2 = x1-length
y1 = MouseY() : y2 = y1+height
a2FillBox x1, y1, x2+5, y2+5, 0xFF34332b
a2BoxText a2FontINV, x2+5, y1+5, x1, y2, text$, 0, 0, 1, 0xFFFFFFFF
EndIf
EndFunction
Rem LW - this function drops x amount of items on the ground, as available from our itemdata() array
Function kill_a_boss( amount As Integer )
For x = 1 To amount
Array Insert At Bottom ITEMS()
pos = Array Count( ITEMS() )
source_id = rnd(Array Count(ITEMDATA()))
ITEMS( pos ).SourceID = source_id
ITEMS( pos ).ImageID = ITEMDATA( source_id ).ImageID
ITEMS( pos ).Name = ITEMDATA( source_id ).Name
ITEMS( pos ).StackAble = ITEMDATA( source_id ).StackAble
ITEMS( pos ).Description = ITEMDATA( source_id ).Description
Next x
EndFunction
Rem LW - this makes an inventory image for us
Function make_inventory_image()
If image Exist( inventory_image )=1 Then delete image Inventory_Image
CLS RGB(0,0,0):For x = 0 To INVENTORY_SLOTS_X-1:For y = 0 To INVENTORY_SLOTS_Y-1
Ink RGB(128,127,110), 0:Box 5+(x*33), 5+(y*33), 5+(x*33)+32, 5+(y*33)+32
Next y:Next x:Get Image inventory_image, 0,0, 10+(33*INVENTORY_SLOTS_X), 10+(33*INVENTORY_SLOTS_Y)
Ink 0,0
EndFunction
IS that a game, a program, or 3 year old drawing.