Mystery Tease 03

cherry_pie_mystery_wip_04.png

It took me longer than expected to implement this. Obviously.

The reason being I wanted to make it somewhat flexible and to test out a few simple ideas without spending too much time overengineering it (I'm aware of the irony). I'm not 100% sure if I managed it, but here's what I came up with:

I have an abstract interface called InventoryProvider. Each Scene (read Level, Room, what have you) has an inventory, and each Player has one. Now, I'm not saying I'm making a game with multiple characters. But if I was, hypothetically, it would be very easy to add new ones with new abilities or change stuff around without disturbing existing ones. The interface looks something like this:

template <typename InvT>
class InventoryProvider {
public:
	virtual ~InventoryProvider() {};

	virtual bool select(Inventory<InvT>&, std::shared_ptr<InvT>&) = 0;
	virtual bool deselect() = 0;
	virtual bool take(Inventory<InvT>&, std::shared_ptr<InvT>) = 0;
	virtual void useSelectedWith(std::shared_ptr<InvT>&) = 0;
};

It outlines the most common tasks you'd want to accomplish in a point-and-click adventure; namely selecting items, picking them up, and using them with other items. The Inventory is essentially a wrapper around a std::array of game objects, with some added info for grid-based inventories:

constexpr uint32_t _slot_size = 10;

template <typename InvT>
class Inventory {
public:
	Inventory();
	void add(std::shared_ptr<InvT>);
	void remove(InvT);
	bool contains(std::shared_ptr<InvT>&);
private:
	struct Slot {
		float posX, posY;
		bool isFilled;
		std::shared_ptr<InvT> item;
	};

	std::array<Slot, _slot_size> slots;
	bool is_grid;
};

The Player or Scene inherits from InventoryProvider, implements the methods, and stores the data in a corresponding Inventory member. I check for clicks in the runloop and react according to the position of the clicked item.

Yes, using std::array this way limits the number of items I can have per scene, but that won't be a problem, since they're very tiny anyway. It make it much easier to work with grids, and that's more important to me than filling a scene with more stuff. Remember, I'm the only person working on this thing. I don't have the capacity to add thousands of items, so I get to make decisions like that.