-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from dev-osrose/componentUpdate
Component update and implementation of inventory
- Loading branch information
Showing
41 changed files
with
659 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
|
||
#include "system.h" | ||
|
||
namespace Systems { | ||
|
||
class InventorySystem : public System { | ||
public: | ||
InventorySystem(SystemManager &manager); | ||
virtual ~InventorySystem() = default; | ||
|
||
virtual void update(EntityManager &es, double dt); | ||
|
||
static uint8_t findNextEmptySlot(Entity entity); | ||
static bool swapItems(Entity entity, uint8_t &a, uint8_t &b); | ||
|
||
void processEquip(CMapClient *client, Entity entity, const RoseCommon::CliEquipItem &packet); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "systems/inventorysystem.h" | ||
#include "cmapserver.h" | ||
#include "cmapclient.h" | ||
|
||
using namespace Systems; | ||
using namespace RoseCommon; | ||
|
||
InventorySystem::InventorySystem(SystemManager &manager) : System(manager) { | ||
manager.registerDispatcher(ePacketType::PAKCS_EQUIP_ITEM, &InventorySystem::processEquip); | ||
} | ||
|
||
void InventorySystem::update(EntityManager&, double) {} | ||
|
||
uint8_t InventorySystem::findNextEmptySlot(Entity entity) { | ||
if (!entity || !entity.component<Inventory>()) | ||
return 0; | ||
const auto &inventory = entity.component<Inventory>()->items_; | ||
for (size_t i = Inventory::MAX_EQUIP_ITEMS; i < Inventory::maxItems; ++i) | ||
if (!inventory[i].id_) | ||
return i; | ||
return 0; | ||
} | ||
|
||
bool InventorySystem::swapItems(Entity entity, uint8_t &a, uint8_t &b) { | ||
if (!entity || !entity.component<Inventory>()) | ||
return false; | ||
if (a >= Inventory::maxItems || b >= Inventory::maxItems) | ||
return false; | ||
if (a == b || (!a && !b)) | ||
return false; | ||
if (!a) | ||
a = findNextEmptySlot(entity); | ||
if (!b) | ||
b = findNextEmptySlot(entity); | ||
auto inventory = entity.component<Inventory>(); | ||
std::swap(inventory->items_[a], inventory->items_[b]); | ||
return true; | ||
} | ||
|
||
void InventorySystem::processEquip(CMapClient *client, Entity entity, const RoseCommon::CliEquipItem &packet) { | ||
logger_->trace("InventorySystem::processEquip"); | ||
if (packet.slotTo() < 1 || packet.slotTo() >= Inventory::MAX_EQUIP_ITEMS) { | ||
logger_->warn("When requesting to change equipped item, the destination wasn't good"); | ||
return; | ||
} | ||
uint8_t to = packet.slotTo(); | ||
uint8_t from = packet.slotFrom(); | ||
if (!swapItems(entity, to, from)) { | ||
logger_->warn("There was an error while swapping items for client {}", entity.component<BasicInfo>()->id_); | ||
return; | ||
} | ||
CMapServer::SendPacket(client, CMapServer::eSendType::EVERYONE, | ||
*makePacket<ePacketType::PAKWC_EQUIP_ITEM>(entity, packet.slotTo())); | ||
auto list = {to, from}; | ||
client->Send(*makePacket<ePacketType::PAKWC_SET_ITEM>(entity, list)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.