2023-11-14 18:11:32 -06:00
/*
License ( OLC - 3 )
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2023-11-14 18:12:54 -06:00
Copyright 2018 - 2023 OneLoneCoder . com
2023-11-14 18:11:32 -06:00
Redistribution and use in source and binary forms , with or without modification ,
are permitted provided that the following conditions are met :
1. Redistributions or derivations of source code must retain the above copyright
notice , this list of conditions and the following disclaimer .
2. Redistributions or derivative works in binary form must reproduce the above
copyright notice . This list of conditions and the following disclaimer must be
reproduced in the documentation and / or other materials provided with the distribution .
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission .
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS " AND ANY
EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT ,
INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT LIMITED
TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE , DATA , OR PROFITS ; OR
BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY , WHETHER IN
CONTRACT , STRICT LIABILITY , OR TORT ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE .
*/
2023-10-06 17:19:02 -05:00
# pragma once
# include <string>
# include <functional>
# include <map>
# include "olcPixelGameEngine.h"
2023-10-06 18:46:13 -05:00
# include "olcUTIL_DataFile.h"
2023-10-06 17:19:02 -05:00
class Crawler ;
class ItemInfo ;
2023-10-07 15:47:26 -05:00
class ItemProps ;
2023-10-06 17:19:02 -05:00
2023-10-07 13:33:27 -05:00
typedef std : : string IT ;
typedef std : : string ITCategory ;
2023-10-07 15:47:26 -05:00
typedef std : : function < bool ( Crawler * , ItemProps ) > ItemScript ;
2023-10-06 17:19:02 -05:00
class Item {
2023-10-07 13:33:27 -05:00
friend class Inventory ;
2023-11-14 23:20:13 -06:00
friend class Crawler ;
2023-10-06 17:19:02 -05:00
private :
2023-10-07 13:33:27 -05:00
uint32_t amt ;
2023-10-06 17:19:02 -05:00
ItemInfo * it ;
2023-10-07 13:33:27 -05:00
public :
2023-10-07 15:47:26 -05:00
Item ( ) ;
2023-10-07 13:33:27 -05:00
Item ( uint32_t amt , IT item ) ;
uint32_t Amt ( ) ;
std : : string Name ( ) ;
std : : string Description ( ) ;
ITCategory Category ( ) ;
Decal * Decal ( ) ;
ItemScript & OnUseAction ( ) ;
2023-10-07 15:47:26 -05:00
bool IsBlank ( ) ;
static Item BLANK ;
2023-10-06 17:19:02 -05:00
} ;
class Inventory {
2023-10-07 16:26:03 -05:00
friend class ItemInfo ;
2023-10-06 17:19:02 -05:00
public :
2023-10-07 15:47:26 -05:00
static void AddItem ( IT it , uint32_t amt = 1 ) ;
2023-10-07 13:33:27 -05:00
static uint32_t GetItemCount ( IT it ) ;
2023-10-07 15:47:26 -05:00
static Item GetItem ( IT it ) ;
2023-10-07 13:33:27 -05:00
//Auto-executes its use function and removes the amt specified from the inventory. Multiple amounts will cause the item to execute its useFunc multiple times.
2023-11-11 17:31:53 -06:00
static bool UseItem ( IT it , uint32_t amt = 1 ) ;
static bool RemoveItem ( IT it , uint32_t amt = 1 ) ;
2023-10-07 13:33:27 -05:00
static std : : vector < IT > & get ( ITCategory itemCategory ) ;
2023-10-07 13:49:32 -05:00
2023-10-07 18:28:19 -05:00
static bool SwapItems ( ITCategory itemCategory , uint32_t slot1 , uint32_t slot2 ) ;
2023-11-14 23:20:13 -06:00
//Makes sure this is a valid category. Will error out if it doesn't exist! Use for ERROR HANDLING!
static bool ValidateItemCategory ( ITCategory itemCategory ) {
get ( itemCategory ) ;
return true ;
}
2023-10-06 17:19:02 -05:00
private :
2023-10-07 13:33:27 -05:00
static void InsertIntoSortedInv ( IT item ) ;
static bool ExecuteAction ( IT item ) ;
static std : : map < IT , Item > _inventory ;
static std : : map < ITCategory , std : : vector < IT > > sortedInv ;
2023-10-06 17:19:02 -05:00
} ;
2023-10-06 18:46:13 -05:00
class ItemProps {
friend class ItemInfo ;
utils : : datafile * scriptProps ;
utils : : datafile * customProps ;
public :
ItemProps ( utils : : datafile * scriptProps , utils : : datafile * customProps ) ;
int GetIntProp ( std : : string prop ) ;
float GetFloatProp ( std : : string prop ) ;
std : : string GetStringProp ( std : : string prop ) ;
} ;
2023-10-06 17:19:02 -05:00
class ItemInfo {
2023-10-07 13:33:27 -05:00
friend class Inventory ;
2023-10-06 17:19:02 -05:00
std : : string name ;
2023-10-06 18:46:13 -05:00
std : : string description ;
std : : string category ;
2023-10-06 17:19:02 -05:00
Decal * img ;
//Returns true if the item can be used, false otherwise
2023-10-06 18:46:13 -05:00
std : : string useFunc = " " ;
//Custom properties for this specific item's script.
static utils : : datafile NOPROPS ;
ItemProps customProps ;
private :
static void InitializeScripts ( ) ;
2023-10-06 17:19:02 -05:00
public :
static void InitializeItems ( ) ;
2023-10-06 18:46:13 -05:00
ItemInfo ( ) ;
2023-10-07 15:47:26 -05:00
std : : string Name ( ) ;
std : : string Description ( ) ;
ITCategory Category ( ) ;
Decal * Decal ( ) ;
ItemScript & OnUseAction ( ) ;
2023-10-06 17:19:02 -05:00
/*
For the useFunc , return true if the item can be used , false otherwise .
*/
} ;