From 3e741134279e02d204a8d4638f2d46dfbf814d0c Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 12 Sep 2014 23:18:02 +0100 Subject: Implemented Chest Minecarts --- src/Entities/Minecart.cpp | 41 +++++++++++++++++++++++++++++++++-------- src/Entities/Minecart.h | 40 ++++++++++++++++++++++++++++++---------- 2 files changed, 63 insertions(+), 18 deletions(-) (limited to 'src/Entities') diff --git a/src/Entities/Minecart.cpp b/src/Entities/Minecart.cpp index 1501eea84..a3927298e 100644 --- a/src/Entities/Minecart.cpp +++ b/src/Entities/Minecart.cpp @@ -1103,29 +1103,54 @@ void cRideableMinecart::OnRightClicked(cPlayer & a_Player) // cMinecartWithChest: cMinecartWithChest::cMinecartWithChest(double a_X, double a_Y, double a_Z) : - super(mpChest, a_X, a_Y, a_Z) + super(mpChest, a_X, a_Y, a_Z), + m_Contents(ContentsWidth, ContentsHeight) { + m_Contents.AddListener(*this); } -void cMinecartWithChest::SetSlot(size_t a_Idx, const cItem & a_Item) +void cMinecartWithChest::OnRightClicked(cPlayer & a_Player) { - ASSERT(a_Idx < ARRAYCOUNT(m_Items)); - - m_Items[a_Idx] = a_Item; + // If the window is not created, open it anew: + cWindow * Window = GetWindow(); + if (Window == NULL) + { + OpenNewWindow(); + Window = GetWindow(); + } + + // Open the window for the player: + if (Window != NULL) + { + if (a_Player.GetWindow() != Window) + { + a_Player.OpenWindow(Window); + } + } } -void cMinecartWithChest::OnRightClicked(cPlayer & a_Player) +void cMinecartWithChest::OpenNewWindow() +{ + OpenWindow(new cMinecartWithChestWindow(this)); +} + + + + + +void cMinecartWithChest::Destroyed() { - // Show the chest UI window to the player - // TODO + cItems Pickups; + m_Contents.CopyToItems(Pickups); + GetWorld()->SpawnItemPickups(Pickups, GetPosX(), GetPosY() + 1, GetPosZ(), 4); } diff --git a/src/Entities/Minecart.h b/src/Entities/Minecart.h index 410d3c77d..f77a171ba 100644 --- a/src/Entities/Minecart.h +++ b/src/Entities/Minecart.h @@ -10,6 +10,7 @@ #pragma once #include "Entity.h" +#include "../UI/WindowOwner.h" @@ -108,27 +109,46 @@ protected: class cMinecartWithChest : - public cMinecart + public cMinecart, + public cItemGrid::cListener, + public cWindowOwner { typedef cMinecart super; public: CLASS_PROTODEF(cMinecartWithChest) - /// Number of item slots in the chest - static const int NumSlots = 9 * 3; - cMinecartWithChest(double a_X, double a_Y, double a_Z); + + enum + { + ContentsHeight = 3, + ContentsWidth = 9, + }; - const cItem & GetSlot(int a_Idx) const { return m_Items[a_Idx]; } - cItem & GetSlot(int a_Idx) { return m_Items[a_Idx]; } - - void SetSlot(size_t a_Idx, const cItem & a_Item); + const cItem & GetSlot(int a_Idx) const { return m_Contents.GetSlot(a_Idx); } + void SetSlot(size_t a_Idx, const cItem & a_Item) { m_Contents.SetSlot(a_Idx, a_Item); } protected: + cItemGrid m_Contents; + void OpenNewWindow(void); + virtual void Destroyed() override; - /// The chest contents: - cItem m_Items[NumSlots]; + // cItemGrid::cListener overrides: + virtual void OnSlotChanged(cItemGrid * a_Grid, int a_SlotNum) + { + UNUSED(a_SlotNum); + ASSERT(a_Grid == &m_Contents); + if (m_World != NULL) + { + if (GetWindow() != NULL) + { + GetWindow()->BroadcastWholeWindow(); + } + + m_World->MarkChunkDirty(GetChunkX(), GetChunkZ()); + } + } // cEntity overrides: virtual void OnRightClicked(cPlayer & a_Player) override; -- cgit v1.2.3 From 7ce09a9113d693b85fdda13b3c04a8b8eb900153 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sat, 27 Sep 2014 19:19:28 +0100 Subject: Suggestions --- src/Entities/Minecart.cpp | 4 ++-- src/Entities/Minecart.h | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'src/Entities') diff --git a/src/Entities/Minecart.cpp b/src/Entities/Minecart.cpp index 42ac57a07..c5e1c07d5 100644 --- a/src/Entities/Minecart.cpp +++ b/src/Entities/Minecart.cpp @@ -7,7 +7,6 @@ #include "Globals.h" #include "Minecart.h" -#include "../World.h" #include "../ClientHandle.h" #include "../Chunk.h" #include "Player.h" @@ -1107,7 +1106,8 @@ void cRideableMinecart::OnRightClicked(cPlayer & a_Player) cMinecartWithChest::cMinecartWithChest(double a_X, double a_Y, double a_Z) : super(mpChest, a_X, a_Y, a_Z), - m_Contents(ContentsWidth, ContentsHeight) + m_Contents(ContentsWidth, ContentsHeight), + cEntityWindowOwner(this) { m_Contents.AddListener(*this); } diff --git a/src/Entities/Minecart.h b/src/Entities/Minecart.h index f77a171ba..6b6ad36b5 100644 --- a/src/Entities/Minecart.h +++ b/src/Entities/Minecart.h @@ -10,6 +10,7 @@ #pragma once #include "Entity.h" +#include "World.h" #include "../UI/WindowOwner.h" @@ -111,7 +112,7 @@ protected: class cMinecartWithChest : public cMinecart, public cItemGrid::cListener, - public cWindowOwner + public cEntityWindowOwner { typedef cMinecart super; -- cgit v1.2.3 From a9243257e583ae22d7a27f6439f6eb973f7e11ce Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 28 Sep 2014 13:11:41 +0100 Subject: Compilation fix --- src/Entities/Minecart.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Entities') diff --git a/src/Entities/Minecart.cpp b/src/Entities/Minecart.cpp index c5e1c07d5..f45e7bb69 100644 --- a/src/Entities/Minecart.cpp +++ b/src/Entities/Minecart.cpp @@ -1106,8 +1106,8 @@ void cRideableMinecart::OnRightClicked(cPlayer & a_Player) cMinecartWithChest::cMinecartWithChest(double a_X, double a_Y, double a_Z) : super(mpChest, a_X, a_Y, a_Z), - m_Contents(ContentsWidth, ContentsHeight), - cEntityWindowOwner(this) + cEntityWindowOwner(this), + m_Contents(ContentsWidth, ContentsHeight) { m_Contents.AddListener(*this); } -- cgit v1.2.3