summaryrefslogtreecommitdiffstats
path: root/src/BlockEntities/BlockEntityWithItems.h
blob: 30a09bc02a4f752f3f8346c17e17914b48932c85 (plain) (blame)
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

// BlockEntityWithItems.h

// Declares the cBlockEntityWithItems class representing a common ancestor for all block entities that have an ItemGrid





#pragma once

#include "BlockEntity.h"
#include "../ItemGrid.h"
#include "../UI/WindowOwner.h"
#include "World.h"





// tolua_begin
class cBlockEntityWithItems :
	public cBlockEntity,
	// tolua_end
	public cItemGrid::cListener,
	// tolua_begin
	public cBlockEntityWindowOwner
{
	typedef cBlockEntity super;

public:
	// tolua_end
	
	BLOCKENTITY_PROTODEF(cBlockEntityWithItems)
	
	cBlockEntityWithItems(
		BLOCKTYPE a_BlockType,                      // Type of the block that the entity represents
		int a_BlockX, int a_BlockY, int a_BlockZ,   // Position of the block entity
		int a_ItemGridWidth, int a_ItemGridHeight,  // Dimensions of the ItemGrid
		cWorld * a_World                            // Optional world to assign to the entity
	) :
		super(a_BlockType, a_BlockX, a_BlockY, a_BlockZ, a_World),
		cBlockEntityWindowOwner(this),
		m_Contents(a_ItemGridWidth, a_ItemGridHeight)
	{
		m_Contents.AddListener(*this);
	}
	
	virtual void Destroy(void) override
	{
		// Drop the contents as pickups:
		ASSERT(m_World != nullptr);
		cItems Pickups;
		m_Contents.CopyToItems(Pickups);
		m_Contents.Clear();
		m_World->SpawnItemPickups(Pickups, m_PosX + 0.5, m_PosY + 0.5, m_PosZ + 0.5);  // Spawn in centre of block
	}
	
	// tolua_begin
	
	const cItem & GetSlot(int a_SlotNum)    const { return m_Contents.GetSlot(a_SlotNum); }
	const cItem & GetSlot(int a_X, int a_Y) const { return m_Contents.GetSlot(a_X, a_Y); }
	
	void SetSlot(int a_SlotNum,    const cItem & a_Item) { m_Contents.SetSlot(a_SlotNum, a_Item); }
	void SetSlot(int a_X, int a_Y, const cItem & a_Item) { m_Contents.SetSlot(a_X, a_Y, a_Item); }

	/// Returns the ItemGrid used for storing the contents
	cItemGrid & GetContents(void) { return m_Contents; }

	// tolua_end
	
	/// Const version of the GetContents() function for C++ type-safety
	const cItemGrid & GetContents(void) const { return m_Contents; }

protected:
	cItemGrid m_Contents;
	
	// cItemGrid::cListener overrides:
	virtual void OnSlotChanged(cItemGrid * a_Grid, int a_SlotNum) override
	{
		UNUSED(a_SlotNum);
		ASSERT(a_Grid == &m_Contents);
		if (m_World != nullptr)
		{
			if (GetWindow() != nullptr)
			{
				GetWindow()->BroadcastWholeWindow();
			}

			m_World->MarkChunkDirty(GetChunkX(), GetChunkZ());
		}
	}
} ;  // tolua_export