Ideas for Managing A Character Inventory (Typed Arrays)

Started by kevin, December 23, 2009, 06:57:30 AM

Previous topic - Next topic

kevin

  Ideas for Managing A Character Inventory (Typed Arrays)

  At times you might want to store typed arrays within a type, however the parser doesn't support this.  But it's relatively easily overcome.  The following uses a second 2D array to house each characters items in..


PlayBASIC Code: [Select]
   MaxItemsInVentory =50

MaxCharacters =10

Type tCharacter
Status
Name$
ItemsInInventory
EndType

Type tInventory
Name$
EndType


Dim Char(MaxCharacters) as tCharacter

; parallel array, so each character has space for a max number of items
Dim Char_Inventory(MaxCharacters,MaxItemsInVentory) as tInventory



; -----------------------------------
; init this characters structure
; -----------------------------------
Char(1).status =true
Char(1).name$= "Bill"

; add some items to this character
Char_Inventory(1,0).name$= "A Golf Ball"
Char_Inventory(1,1).name$= "Tickets to see Bonnie Ratt"
Char_Inventory(1,2).name$= "The gold key"

Char(1).ItemsInInventory=3


; -----------------------------------
; init this characters structure
; -----------------------------------
Char(2).status =true
Char(2).name$= "Dookie"

; add some items to this character
Char_Inventory(2,0).name$= "Wombat"
Char_Inventory(2,1).name$= "The Meaning of life"

Char(2).ItemsInInventory=2



For lp=0 to MaxCharacters
if Char(lp).status=true
print "=================================================="
print "Name:"+Char(lp).Name$
ItemCount=Char(lp).ItemsInInventory
print "Items:"+str$(ItemCount)

s$=""
for Item=0 to ItemCount-1
s$=s$+Char_Inventory(lp,Item).name$+","
next
print s$
endif
next

Sync
Waitkey





kevin

 This one uses the ability to return typed arrays from the functions.  


requires PlayBasic V1.64k (revision 2) (or higher)

PlayBASIC Code: [Select]
; PROJECT : Inventory
; AUTHOR : Kevin Picone
; CREATED : 11/7/2009
; EDITED : 11/7/2009
; ---------------------------------------------------------------------

SetFps 60

type tInventory
itemname$
EndType


// Declare a game object called of SHIP
type tShip
x#,y#
speedx#,speedy#
radius
colour
InventoryArray
EndType


Dim Ship as tship list

// throw some ships randomly into the list so we're someting to look at
for lp =1 to 20
ship = new tship
ship.x=rndrange(100,700)
ship.y=rndrange(100,500)
angle#=rnd#(360)
ship.speedx=cos(angle#)
ship.speedy=sin(angle#)
ship.radius =rndrange(10,20)
ship.colour =rndrgb()

// Store the inventory arrays bank index, for later use
Ship.InventoryArray =CreateShipsInventory()
next



// This the items that characters can pick up.
type tItem
x#,y#
Starttime,DeathTime
Name$ ; name of the pick up item characters can pick up
BoundingBoxShape
endtype

Dim Item as tItem list


// create an array to hold the names of the inventory items that game character can pick up
Dim ItemNames$(0)
s$="Gold Bar, Silver Bar, Cricket Bat,20 cents,Commodore 64"
NumberOfItems=SplitToArray(s$,",",ItemNames$(),0)



; ---------------------------------------------------------
Do // game LOOP
; ---------------------------------------------------------

Cls 0

; ---------------------------------------------------------
; Show Items
; ---------------------------------------------------------
CurrentTime=timer()
if CurrentTime>SpawnNewItem
SpawnNewItem=CurrentTime+rndrange(50,250)

Item = new tItem
Item.x = Rnd(GetScreenWidth())
Item.y = Rnd(GetScreenHeight())
Item.StartTime=CurrentTime
Item.DeathTime=CurrentTime+2500

Item.name =ItemNames$(int(rnd#(NumberOfItems-1)))

width=gettextwidth(item.name)
height=gettextheight(item.name)

x1=-(Width/2)
x2=(Width/2)
y1=0
y2=(Height)
Item.BoundingBoxShape =NewBoundingBoxShape(x1,y1,x2,y2)


endif

for each Item()
timepast#=currenttime-item.StartTime
scaler#=(TimePast#/(item.DeathTime-item.StartTime))
Scaler#=1-cliprange#(scaler#,0,1)
ink rgbfade($ffffff, scaler#*100)
centertext item.x, item.y, Item.name
if CurrentTime>Item.DeathTime
item=Null
endif
next
ink $ffffff

; ---------------------------------------------------------
; Move Ships
; ---------------------------------------------------------
for each Ship()
x#=ship.x#+ship.speedx#
y#=ship.y#+ship.speedy#
if x#<10 then x#=10 : Ship.SpeedX*=-1
if x#>790 then x#=790 : Ship.SpeedX*=-1
if y#<10 then y#=10 : Ship.Speedy*=-1
if y#>590 then y#=590 : Ship.Speedy*=-1

ship.x=x#
ship.y=y#

; chekc if ship hit an inventory item
ShipHitItem(Ship())

circlec x#,y#,ship.radius,true,ship.colour

ShowShipInventory(ship())

next

Sync
loop



Function CreateShipsInventory()
Dim MyInventory as tInventory list
EndFunction MyInventory.tInventory



Function ShowShipInventory(ThisShip.tship)
Makearray Inventory.tInventory

Inventory() =ThisShip.InventoryArray

x#=thisShip.x#
y#=thisShip.y#
th=gettextheight("Y")
ink $ff0000
for each Inventory()
Login required to view complete source code



 Originally Posted in the PlayBasic V1.64k WIP blog