Item category, script, and all other information relating to items now load properly with error-handling and proper property overriding.

pull/28/head
sigonasr2 1 year ago
parent 877c44c372
commit ba6505b266
  1. 3
      Crawler/Crawler.cpp
  2. 1
      Crawler/DEFINES.h
  3. 90
      Crawler/Item.cpp
  4. 27
      Crawler/Item.h
  5. 5
      Crawler/Player.cpp
  6. 3
      Crawler/Player.h
  7. 2
      Crawler/Version.h
  8. 3
      Crawler/assets/config/configuration.txt
  9. 21
      Crawler/assets/config/items/ItemCategory.txt

@ -19,6 +19,7 @@
#include "Key.h"
#include "Menu.h"
#include "GameState.h"
#include "Item.h"
INCLUDE_EMITTER_LIST
@ -102,6 +103,8 @@ bool Crawler::OnUserCreate(){
camera.SetTarget(player->GetPos());
camera.SetWorldBoundary({0,0},GetCurrentMap().MapSize*GetCurrentMap().TileSize);
camera.EnableWorldBoundary(false);
ItemInfo::InitializeItems();
InitializeGraphics();

@ -14,6 +14,7 @@
#define INCLUDE_TILE_ANIMATION_DATA extern std::map<int,std::vector<std::pair<int,float>>>TILE_ANIMATION_DATA;
#define INCLUDE_GFX extern safemap<std::string,Renderable>GFX;
#define INCLUDE_ITEM_DATA extern safemap<Item::ItemName,ItemInfo>ITEM_DATA;
#define INCLUDE_ITEM_CATEGORIES extern safemap<std::string,std::set<std::string>>ITEM_CATEGORIES;
#define ACCESS_PLAYER Player*p=game->GetPlayer();

@ -6,17 +6,105 @@
INCLUDE_game
INCLUDE_DATA
INCLUDE_GFX
safemap<std::string,ItemInfo>ITEM_DATA;
safemap<std::string,std::function<bool(Crawler*,ItemProps)>>ITEM_SCRIPTS;
safemap<std::string,std::set<std::string>>ITEM_CATEGORIES;
typedef std::string IT;
ItemInfo::ItemInfo()
:customProps({nullptr,nullptr}){}
void ItemInfo::InitializeItems(){
InitializeScripts();
for(auto&key:DATA["ItemCategory"].GetKeys()){
ITEM_CATEGORIES[key.first];
}
for(auto&key:DATA["ItemDatabase"].GetKeys()){
std::cout<<key.first<<std::endl;
std::string imgPath="assets/"+"item_img_directory"_S+key.first+".png";
Renderable&img=GFX["item_img_directory"_S+key.first+".png"];
img.Load(imgPath);
std::string scriptName="",description="",category="";
for(auto&itemKey:DATA["ItemDatabase"][key.first].GetKeys()){
std::string keyName=itemKey.first;
if(keyName=="Description"){
description=DATA["ItemDatabase"][key.first][keyName].GetString();
}else
if(keyName=="ItemCategory"){
category=DATA["ItemDatabase"][key.first][keyName].GetString();
}else
if(keyName=="ItemScript"){
scriptName=DATA["ItemDatabase"][key.first][keyName].GetString();
}else{ //THis is a custom override modifier for a script. NO-OP
}
}
if(scriptName!=""){
if(!ITEM_SCRIPTS.count(scriptName)){
std::cout<<"Could not load script "<<scriptName<<" for Item "<<key.first<<"!"<<std::endl;
throw;
}
}
ItemInfo&it=ITEM_DATA[key.first];
it.name=key.first;
it.description=description;
it.category=category;
if(!ITEM_CATEGORIES.count(it.category)){
std::cout<<"WARNING! Tried to add item "<<it.name<<" to category "<<it.category<<" which does not exist!"<<std::endl;
throw;
}
ITEM_CATEGORIES.at(it.category).insert(it.name);
it.img=img.Decal();
it.script=scriptName;
ItemProps&props=it.customProps;
if(scriptName!=""){
props.scriptProps=&DATA["ItemScript"][scriptName];
props.customProps=&DATA["ItemDatabase"][key.first];
}
it.useFunc=scriptName;
}
ITEM_DATA.SetInitialized();
ITEM_CATEGORIES.SetInitialized();
std::cout<<ITEM_DATA.size()<<" items have been loaded."<<std::endl;
std::cout<<ITEM_CATEGORIES.size()<<" item categories have been loaded."<<std::endl;
}
ItemProps::ItemProps(utils::datafile*scriptProps,utils::datafile*customProps)
:scriptProps(scriptProps),customProps(customProps){}
int ItemProps::GetIntProp(std::string prop){
if(customProps->HasProperty(prop)) return (*customProps)[prop].GetInt();
else return (*scriptProps)[prop].GetInt();
};
float ItemProps::GetFloatProp(std::string prop){
if(customProps->HasProperty(prop)) return (*customProps)[prop].GetReal();
else return (*scriptProps)[prop].GetReal();
};
std::string ItemProps::GetStringProp(std::string prop){
if(customProps->HasProperty(prop)) return (*customProps)[prop].GetString();
else return (*scriptProps)[prop].GetString();
};
void ItemInfo::InitializeScripts(){
ITEM_SCRIPTS["Restore"]=[](Crawler*game,ItemProps props){
game->GetPlayer()->Heal(props.GetIntProp("HP Restore"));
game->GetPlayer()->Heal(props.GetIntProp("HP % Restore"));
game->GetPlayer()->Heal(props.GetIntProp("HP Restore"));
game->GetPlayer()->Heal(props.GetIntProp("HP % Restore"));
return true;
};
ITEM_SCRIPTS.SetInitialized();
std::cout<<ITEM_SCRIPTS.size()<<" item scripts have been loaded."<<std::endl;
}
void Inventory::AddItem(IT it,int amt){

@ -4,6 +4,7 @@
#include <functional>
#include <map>
#include "olcPixelGameEngine.h"
#include "olcUTIL_DataFile.h"
class Crawler;
class ItemInfo;
@ -24,16 +25,36 @@ private:
static std::map<std::string,Item>inventory;
};
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);
};
class ItemInfo{
std::string name;
std::string displayName;
std::string description;
std::string category;
Decal*img;
//Returns true if the item can be used, false otherwise
std::function<bool(Crawler*)>useFunc;
std::string useFunc="";
//Custom properties for this specific item's script.
static utils::datafile NOPROPS;
std::string script;
ItemProps customProps;
private:
static void InitializeScripts();
public:
static void InitializeItems();
ItemInfo();
/*
For the useFunc, return true if the item can be used, false otherwise.
*/
ItemInfo(std::string name,std::string displayName,Decal*img,std::function<bool(Crawler*)>useFunc);
//ItemInfo(std::string name,std::string description,std::string category,Decal*img,utils::datafile&props,std::string useFunc);
};

@ -701,6 +701,7 @@ void Player::SetIframes(float duration){
iframe_time=duration;
}
void Player::SetMana(int mana){
this->mana=std::clamp(mana,0,maxmana);
bool Player::Heal(int damage){
hp=std::clamp(hp+damage,0,maxhp);
return true;
}

@ -134,7 +134,6 @@ public:
bool CanAct(Ability&ability);
void Knockback(vf2d vel);
void SetIframes(float duration);
void SetMana(int mana);
void AddBuff(BuffType type,float duration,float intensity);
std::vector<Buff>GetBuffs(BuffType buff);
@ -143,6 +142,8 @@ public:
void RemoveAllBuffs(); //Remove every buff.
bool Hurt(int damage,bool onUpperLevel,float z);
//Return false if healing was not possible.
bool Heal(int damage);
//specificClass is a bitwise-combination of classes from the Class enum. It makes sure certain animations only play if you are a certain class.
void UpdateAnimation(std::string animState,int specificClass=ANY);
Animate2D::Frame GetFrame();

@ -2,7 +2,7 @@
#define VERSION_MAJOR 0
#define VERSION_MINOR 2
#define VERSION_PATCH 0
#define VERSION_BUILD 1796
#define VERSION_BUILD 1811
#define stringify(a) stringify_(a)
#define stringify_(a) #a

@ -45,6 +45,9 @@ item_config = items/items.txt
# Path to items configuration
item_directory = items/
# Path to items configuration
item_img_directory = items/
# Whether or not to show individual data accesses from config data structure.
debug_access_options = 0

@ -1,19 +1,8 @@
ItemCategory
{
Consumables
{
Description = Items that will be consumed after a single use.
}
Equipment
{
Description = Gear that can be placed onto a player.
}
Accesories
{
Description = Items worn as extra items on the player.
}
Materials
{
Description = Items used as crafting materials for the forge.
}
# Random side note, these descriptions aren't used anywhere in-game atm.
Consumables = Items that will be consumed after a single use.
Equipment = Gear that can be placed onto a player.
Accesories = Items worn as extra items on the player.
Materials = Items used as crafting materials for the forge.
}
Loading…
Cancel
Save