1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

#include "gfCGameInventoryComponent.h"

#include <algorithm>

#include <fsAppCore/src/components/fsCNameComponent.h>
#include <fsAppCore/src/fsCAppMessageDispatcher.h>
#include <fsAppCore/src/fsCProfileManager.h>
#include <fsRenderer/src/components/fsCSpriteComponent.h>
#include <fsRenderer/src/fsCBrushManager.h>

#include <fsCore/fsIFile.h>
#include <fsCore/src/fsCUserSavedData.h>

#include <gfComponents/components/gfCVisibilityStateComponent.h>
#include <gfComponents/components/gfICollectableComponent.h>
#include <gfComponents/components/gfCDraggableKeyComponent.h>

#include <gfGameCore/components/gfCInventoryComponent.h>

const fsCUniqueId gfCGameInventoryComponent::mTypeId("gfCInventory");
const fsStr gfCGameInventoryComponent::mInventorySavedDataKey("inventoryData");
const fsCUniqueId gfCGameInventoryComponent::mInventoryCollectedMsg("inventoryCollected");


fsStr gfCGameInventoryComponent::inventoryDataGet() const
{
	fsStr invData;
	for (const fsStr& itemName : mInventory)
	{
		invData += itemName + ",";<--- Consider using std::accumulate algorithm instead of a raw loop.
	}
	return invData;
}

void gfCGameInventoryComponent::serialiseState()
{	//Using save data store not normal level data as inventory needs to exist across levels
	fsCProfileManager::instanceGet()->userSavedDataGet()->strSet(mInventorySavedDataKey, inventoryDataGet());
}

void gfCGameInventoryComponent::deserialiseState()
{
	fsStr loadStr = fsCProfileManager::instanceGet()->userSavedDataGet()->strGet(mInventorySavedDataKey);
	if (loadStr.emptyGet())
	{
		return;
	}
	selectionsRecreate(loadStr);
}

void gfCGameInventoryComponent::selectionsRecreate(fsStr& pLoadStr)
{
	while (!pLoadStr.emptyGet())
	{
		fsStr inventoryEntityName = pLoadStr.chomp(",");
		inventoryItemCreate(inventoryEntityName);
	}
}

void gfCGameInventoryComponent::listenersDeregisterDo()
{
	fsCAppMessageDispatcher::instanceGet()->listenerDeregister(gfCVisibilityStateComponent::mLockBrokenMsg, this);
	fsCAppMessageDispatcher::instanceGet()->listenerDeregister(gfICollectableComponent::mCollectionEndMsg, this);
	fsIMessageListenerComponent::listenersDeregisterDo();
}

void gfCGameInventoryComponent::listenersRegisterDo()
{
	fsCAppMessageDispatcher::instanceGet()->listenerRegister(gfCVisibilityStateComponent::mLockBrokenMsg, this);
	fsCAppMessageDispatcher::instanceGet()->listenerRegister(gfICollectableComponent::mCollectionEndMsg, this);
	fsIMessageListenerComponent::listenersRegisterDo();
}

fsBool gfCGameInventoryComponent::inventoryItemCreate(const fsStr& pEntityName)
{
	const fsStr strippedName(fsCNameComponent::nameSansPrefixCalculate(pEntityName));
	const fsCResourceName invIconName(gfCDraggableKeyComponent::useInventoryIconNameGet(strippedName),
	                                  fsCResourceName::eRES_LOCATION_ASSETS);
	if (fsIFile::fileExistsGet(invIconName))
	{
		mInventory.push_back(pEntityName);
		return true;
	}
	return false;
}

fsBool gfCGameInventoryComponent::messageProcessDo(const fsCUniqueId& pMessageType,
                                         const fsIMessageDispatcher::sMessageParameters& pParam)
{
	if (gfICollectableComponent::mCollectionEndMsg == pMessageType)
	{
		const gfICollectableComponent::sEntityCollectedParam& msgParams = static_cast<const
			gfICollectableComponent::sEntityCollectedParam&>(pParam);
		if (msgParams.mEnt->componentGet<gfCInventoryComponent>())
		{
			if (inventoryItemCreate(fsCNameComponent::get(msgParams.mEnt)->nameSansSuffixGet()))
			{
				serialiseState();
				fsCProfileManager::instanceGet()->allDataSave(true);

				fsIMessageDispatcher::sMessageParameters p;
				fsCAppMessageDispatcher::instanceGet()->messageDispatch(gfCGameInventoryComponent::mInventoryCollectedMsg, p);
			}
		} // allow the message to propagate
	}
	else if (gfCVisibilityStateComponent::mLockBrokenMsg == pMessageType)
	{
		consumedItemRemove(static_cast<const fsIMessageDispatcher::sStrMessageParam&>(pParam).mStr);
	} // allow the message to propagate
	return false;
}

void gfCGameInventoryComponent::consumedItemRemove(const fsStr& pKeyName)
{
	auto const consumed = std::find_if(mInventory.begin(), mInventory.end(),
	                                   [&pKeyName](const fsStr& itemName)
	                                   {
		                                   return 0 != itemName.find(gfCInventoryComponent::mInventoryToolEntityPrefix) &&
			                                   fsCNameComponent::nameSansPrefixCalculate(itemName) == pKeyName;
	                                   });
	if (mInventory.end() != consumed)
	{
		mInventory.erase(consumed);
		serialiseState();
	}
}


fsIComponent* gfCGameInventoryComponent::create(fsCEntity* pParent)
{
	auto inst = new gfCGameInventoryComponent(pParent);
	inst->deserialiseState();
	return inst;
}

gfCGameInventoryComponent::gfCGameInventoryComponent(fsCEntity* const pParent):
	fsIMessageListenerComponent(pParent)
{
}

gfCGameInventoryComponent::~gfCGameInventoryComponent()
{
	serialiseState();
}