Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "crt.bi"
- 'base item enum
- 'used to determine exactly what type of item to process
- enum ITEM_ENUM
- FOOD = 1
- POTION
- end enum
- 'food item enum
- 'used to determine exactly what type of food to process
- enum FOOD_ENUM
- BREAD = 1
- CHEESE
- APPLE
- end enum
- 'potion item enum
- enum POTION_ENUM
- WATER = 1
- BEER
- end enum
- 'the base type
- 'all items will extend from this base type
- type base_item
- as item_enum iType
- as string*24 name
- as String*128 description
- 'as vec3f position
- 'as fb.image ptr sprite
- end type
- 'the food type...
- 'since it extends base_item, it inherits iType, name, etc...
- type food_item extends base_item
- as FOOD_ENUM fType
- as integer hp
- end type
- 'the potion type...
- 'since it extends base_item, it inherits the same things as food_item,
- 'but notice that it has totally different variables...
- 'we'll still be able to store them both in the same container
- type potion_item extends base_item
- as POTION_ENUM pType
- as integer hpEffect
- as integer strEffect
- end type
- 'the bag_struct
- 'using an "any ptr" allows us to allocate any size memory blocks we wish
- 'we just have to get a little creative with the casting
- type bag_struct
- as any ptr items(any)
- 'as fb.image ptr sprite
- end type
- 'the main inventory_struct, which houses the bags,
- 'and facilitates adding/removing items from them...
- type inventory_struct
- as bag_struct bags(any)
- declare function add_item( byref item as any ptr ) as integer
- declare function remove_item( byref bagId as integer, byref itemID as integer ) as integer
- end type
- 'the player struct...
- type player_struct
- 'as vec3f position
- as inventory_struct inventory
- end type
- 'create a player
- dim as player_struct player
- 'give him one bag
- redim player.inventory.bags(0)
- 'let that bag hold 1000 items
- with player.inventory.bags(0)
- redim .items(999)
- end with
- 'create a bread food item and add it to the inventory
- dim as food_item food
- food.iType = ITEM_ENUM.FOOD
- food.fType = FOOD_ENUM.BREAD
- food.name = "Bread"
- food.description = "Moldy ass bread... yum."
- food.hp = 10
- player.inventory.add_item( @food )
- 'create a water potion item and add it to the inventory
- dim as potion_item potion
- potion.iType = ITEM_ENUM.POTION
- potion.pType = POTION_ENUM.WATER
- potion.name = "Water"
- potion.hpEffect = 1
- potion.description = "Smells like orc piss... kinda makes me horny?"
- player.inventory.add_item( @potion )
- 'create a beer potion item and add it to the inventory
- dim as potion_item potion2
- potion2.iType = ITEM_ENUM.POTION
- potion2.pType = POTION_ENUM.BEER
- potion2.name = "Beer"
- potion2.hpEffect = -5
- potion2.strEffect = 7
- potion2.description = "Smells of university?"
- player.inventory.add_item( @potion2 )
- 'now that those items have been added,
- 'we'll just print out some stuff to make sure they're actually in there
- print ""
- print "Checking the player's inventory..."
- print ""
- for i as integer = 0 to ubound(player.inventory.bags(0).items)
- 'if the ptr is valid, then there is an item here...
- if player.inventory.bags(0).items(i)<>0 then
- 'cast the ptr to the base type to get the base information
- dim as base_item ptr item = cast(base_item ptr, player.inventory.bags(0).items(i) )
- 'now we can figure out which type of item this actually is...
- select case as const item->iType
- case ITEM_ENUM.FOOD
- 'since we know this is a food item,
- 'we can safely cast the original ptr to the food_item type
- 'doing this allows us to extract the data exclusive to this particular type
- dim as food_item ptr food = cast(food_item ptr, player.inventory.bags(0).items(i))
- print "This item is... " + food->name
- print rtrim(food->description)
- print "It yields " & food->hp & " hit points"
- 'now we can refine the processing a little more,
- 'to figure out exactly what type of food this is
- 'perhaps you want to play a special sound fx or something...
- select case as const food->fType
- case FOOD_ENUM.BREAD
- print "Hard as a rock!"
- case FOOD_ENUM.CHEESE
- print "nyuk, nyuk, nyuk..."
- end select
- case ITEM_ENUM.POTION
- 'process potions the same way...
- dim as potion_item ptr potion = cast(potion_item ptr, player.inventory.bags(0).items(i))
- print "This item is... " + potion->name
- print rtrim(potion->description)
- print "It yields " & potion->hpEffect & " hit points"
- print "It yields " & potion->strEffect & " str points"
- 'and check the refined potion type...
- select case as const potion->pType
- case POTION_ENUM.WATER
- print "Sip, sip... "
- case POTION_ENUM.BEER
- print "GLUG, GLUG, GLUG!!!"
- end select
- end select
- print " "
- end if
- next
- sleep
- 'this function adds an item to the inventory
- 'it fills the first slot in the first bag...
- 'returns true on success
- 'returns false if no more space available
- 'the casting stuff works exactly the same as above...
- function inventory_struct.add_item( byref item as any ptr ) as integer
- if item = 0 then return false
- for b as integer = 0 to ubound(bags)
- for i as integer = 0 to ubound(bags(b).items)
- if bags(b).items(i) = 0 then
- dim as base_item ptr aPtr = cast( base_item ptr, item )
- select case as const aPtr->iType
- case ITEM_ENUM.FOOD
- dim as food_item ptr pPtr = cast( food_item ptr, item )
- if pPtr = 0 then
- return false
- end if
- print pPtr->name + " food added"
- bags(b).items(i) = callocate( 1, sizeof(food_item) )
- memcpy( bags(b).items(i), item, sizeof(food_item) )
- return true
- case ITEM_ENUM.POTION
- dim as potion_item ptr pPtr = cast( potion_item ptr, item )
- if pPtr = 0 then
- return false
- end if
- print pPtr->name + " potion added"
- bags(b).items(i) = callocate( 1, sizeof(potion_item) )
- memcpy( bags(b).items(i), item, sizeof(potion_item) )
- return true
- end select
- end if
- next
- next
- return false
- end function
- function inventory_struct.remove_item( byref bagId as integer, byref itemID as integer ) as integer
- if bagId>ubound(bags) then return false
- if itemId<lbound(bags(bagId).items) then return false
- if itemId>ubound(bags(bagId).items) then return false
- 'if there is an item here, get rid of it...
- if bags(bagId).items(itemId)<>0 then
- deallocate( bags(bagId).items(itemId) )
- bags(bagId).items(itemId) = 0
- return true
- end if
- return false
- end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement