Item category, script, and all other information relating to items now load properly with error-handling and proper property overriding.
This commit is contained in:
parent
877c44c372
commit
ba6505b266
@ -19,6 +19,7 @@
|
|||||||
#include "Key.h"
|
#include "Key.h"
|
||||||
#include "Menu.h"
|
#include "Menu.h"
|
||||||
#include "GameState.h"
|
#include "GameState.h"
|
||||||
|
#include "Item.h"
|
||||||
|
|
||||||
INCLUDE_EMITTER_LIST
|
INCLUDE_EMITTER_LIST
|
||||||
|
|
||||||
@ -102,6 +103,8 @@ bool Crawler::OnUserCreate(){
|
|||||||
camera.SetTarget(player->GetPos());
|
camera.SetTarget(player->GetPos());
|
||||||
camera.SetWorldBoundary({0,0},GetCurrentMap().MapSize*GetCurrentMap().TileSize);
|
camera.SetWorldBoundary({0,0},GetCurrentMap().MapSize*GetCurrentMap().TileSize);
|
||||||
camera.EnableWorldBoundary(false);
|
camera.EnableWorldBoundary(false);
|
||||||
|
|
||||||
|
ItemInfo::InitializeItems();
|
||||||
|
|
||||||
InitializeGraphics();
|
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_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_GFX extern safemap<std::string,Renderable>GFX;
|
||||||
#define INCLUDE_ITEM_DATA extern safemap<Item::ItemName,ItemInfo>ITEM_DATA;
|
#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();
|
#define ACCESS_PLAYER Player*p=game->GetPlayer();
|
||||||
|
|
||||||
|
@ -6,17 +6,105 @@
|
|||||||
|
|
||||||
INCLUDE_game
|
INCLUDE_game
|
||||||
INCLUDE_DATA
|
INCLUDE_DATA
|
||||||
|
INCLUDE_GFX
|
||||||
|
|
||||||
safemap<std::string,ItemInfo>ITEM_DATA;
|
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;
|
typedef std::string IT;
|
||||||
|
|
||||||
|
ItemInfo::ItemInfo()
|
||||||
|
:customProps({nullptr,nullptr}){}
|
||||||
|
|
||||||
void ItemInfo::InitializeItems(){
|
void ItemInfo::InitializeItems(){
|
||||||
|
|
||||||
|
InitializeScripts();
|
||||||
|
|
||||||
|
for(auto&key:DATA["ItemCategory"].GetKeys()){
|
||||||
|
ITEM_CATEGORIES[key.first];
|
||||||
|
}
|
||||||
|
|
||||||
for(auto&key:DATA["ItemDatabase"].GetKeys()){
|
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_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){
|
void Inventory::AddItem(IT it,int amt){
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include "olcPixelGameEngine.h"
|
#include "olcPixelGameEngine.h"
|
||||||
|
#include "olcUTIL_DataFile.h"
|
||||||
|
|
||||||
class Crawler;
|
class Crawler;
|
||||||
class ItemInfo;
|
class ItemInfo;
|
||||||
@ -24,16 +25,36 @@ private:
|
|||||||
static std::map<std::string,Item>inventory;
|
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{
|
class ItemInfo{
|
||||||
std::string name;
|
std::string name;
|
||||||
std::string displayName;
|
std::string description;
|
||||||
|
std::string category;
|
||||||
Decal*img;
|
Decal*img;
|
||||||
//Returns true if the item can be used, false otherwise
|
//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:
|
public:
|
||||||
static void InitializeItems();
|
static void InitializeItems();
|
||||||
|
ItemInfo();
|
||||||
/*
|
/*
|
||||||
For the useFunc, return true if the item can be used, false otherwise.
|
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;
|
iframe_time=duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::SetMana(int mana){
|
bool Player::Heal(int damage){
|
||||||
this->mana=std::clamp(mana,0,maxmana);
|
hp=std::clamp(hp+damage,0,maxhp);
|
||||||
|
return true;
|
||||||
}
|
}
|
@ -134,7 +134,6 @@ public:
|
|||||||
bool CanAct(Ability&ability);
|
bool CanAct(Ability&ability);
|
||||||
void Knockback(vf2d vel);
|
void Knockback(vf2d vel);
|
||||||
void SetIframes(float duration);
|
void SetIframes(float duration);
|
||||||
void SetMana(int mana);
|
|
||||||
|
|
||||||
void AddBuff(BuffType type,float duration,float intensity);
|
void AddBuff(BuffType type,float duration,float intensity);
|
||||||
std::vector<Buff>GetBuffs(BuffType buff);
|
std::vector<Buff>GetBuffs(BuffType buff);
|
||||||
@ -143,6 +142,8 @@ public:
|
|||||||
void RemoveAllBuffs(); //Remove every buff.
|
void RemoveAllBuffs(); //Remove every buff.
|
||||||
|
|
||||||
bool Hurt(int damage,bool onUpperLevel,float z);
|
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.
|
//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);
|
void UpdateAnimation(std::string animState,int specificClass=ANY);
|
||||||
Animate2D::Frame GetFrame();
|
Animate2D::Frame GetFrame();
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#define VERSION_MAJOR 0
|
#define VERSION_MAJOR 0
|
||||||
#define VERSION_MINOR 2
|
#define VERSION_MINOR 2
|
||||||
#define VERSION_PATCH 0
|
#define VERSION_PATCH 0
|
||||||
#define VERSION_BUILD 1796
|
#define VERSION_BUILD 1811
|
||||||
|
|
||||||
#define stringify(a) stringify_(a)
|
#define stringify(a) stringify_(a)
|
||||||
#define stringify_(a) #a
|
#define stringify_(a) #a
|
||||||
|
@ -45,6 +45,9 @@ item_config = items/items.txt
|
|||||||
# Path to items configuration
|
# Path to items configuration
|
||||||
item_directory = items/
|
item_directory = items/
|
||||||
|
|
||||||
|
# Path to items configuration
|
||||||
|
item_img_directory = items/
|
||||||
|
|
||||||
# Whether or not to show individual data accesses from config data structure.
|
# Whether or not to show individual data accesses from config data structure.
|
||||||
debug_access_options = 0
|
debug_access_options = 0
|
||||||
|
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
ItemCategory
|
ItemCategory
|
||||||
{
|
{
|
||||||
Consumables
|
# Random side note, these descriptions aren't used anywhere in-game atm.
|
||||||
{
|
Consumables = Items that will be consumed after a single use.
|
||||||
Description = 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.
|
||||||
Equipment
|
Materials = Items used as crafting materials for the forge.
|
||||||
{
|
|
||||||
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.
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user