Diablo inventory

BlitzMax Forums/BlitzMax Programming/Diablo inventory

Here's a working sample for ya Verfum. Just use any 32x32 PNG image you have where it loads the .png

'Diablo Style Inventory System
'by Chroma
'Dec 17, 2007

Graphics 800,600

Local mainInventory:TInventory = New TInventory
mainInventory.SetParam(4,2,32)	'Set the inventory to be 4 across and 2 high and 32x32 in size
mainInventory.SetPos(300,200)

Local mouseSlot:TSlot = New TSlot
mouseSlot.item = New TItem
mouseSlot.item.image = LoadImage("sword001.png")	'Load any 32x32 PNG image here

Local mx:Int, my:Int
Local LMH:Int

MoveMouse 400,300

While KeyDown(KEY_ESCAPE)=False And AppTerminate()=False
Cls

	'Get Mouse Info
	mx = MouseX()
	my = MouseY()
	LMH = MouseHit(1)

	'Hide the cursor if player is moving an item
	If mouseSlot.item <> Null
		HideMouse 
	Else
		ShowMouse
	EndIf
	
	If LMH = True
		Local slot:TSlot
		For slot = EachIn mainInventory.SlotList
			If MouseOver(mainInventory.x+slot.x, mainInventory.y+slot.y, slot.w, slot.h) = True
				'Transfering from Mouse Slot to Inventory Slot
				If mouseSlot.item <> Null
					If slot.item = Null
						slot.item = mouseSlot.item
						mouseSlot.item = Null
					EndIf
				Else
				'Transferring from Inventory Slot to Mouse Slot
					If slot.item <> Null
						mouseSlot.item = slot.item
						slot.item = Null
					EndIf
				EndIf
			EndIf
		Next
	EndIf

	mainInventory.Render()
	mouseSlot.Render(mx,my)

Flip
Wend
End


Type TInventory
	Field x:Int, y:Int, size:Int
	Field SlotList:TList = New TList
	
	Method SetPos(x%,y%)
		self.x = x
		self.y = y
	End Method
	
	Method SetParam(x%,y%,s%)
		Local a%, b%
		For b = 0 To y-1
			For a = 0 To x-1
				Local slot:TSlot = New TSlot
				slot.x = a*s
				slot.y = b*s
				slot.w = s
				slot.h = s
				slot.border = True	'so you can see the slot boundary temporary
				self.SlotList.AddLast(slot)
			Next	
		Next
	
	End Method
	
	Method Logic()
	
	End Method
	
	Method Render()
		Local slot:TSlot
		For slot = EachIn self.SlotList
			slot.Render(self.x, self.y)
		Next
	End Method
	
End Type

Type TSlot
	Field x:Int, y:Int, w:Int, h:Int
	Field item:TItem
	Field border:Int
	
	Method Render(xorigin%, yorigin%)
		If self.border = True
			Local xp% = xorigin + self.x
			Local yp% = yorigin + self.y
			DrawLine xp, yp, xp+self.w, yp
			DrawLine xp+self.w, yp, xp+self.w, yp+self.h
			DrawLine xp+self.w, yp+self.h, xp, yp+self.h
			DrawLine xp, yp+self.h, xp, yp		
		EndIf
		If self.item <> Null
			DrawImage self.item.image, xorigin + self.x, yorigin + self.y
		EndIf
	End Method
End Type

Type TItem
	Field image:TImage
End Type


Function MouseOver:Int(x%, y%, w%, h%)
	If MouseX() > x And MouseX() < x + w
		If MouseY() > y And MouseY() < y + h
			Return True
		EndIf
	EndIf
End Function