From 4a3951938aeed3b6a5bc2e33f0e6bd546c0b42b3 Mon Sep 17 00:00:00 2001 From: Andrew nuark G Date: Wed, 23 Dec 2020 01:23:36 +0700 Subject: [PATCH] Vessel interaction update --- sea_transport/entities/vessel_entity.cpp | 23 ++++++++++++++++++++++- sea_transport/entities/vessel_entity.h | 6 +++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/sea_transport/entities/vessel_entity.cpp b/sea_transport/entities/vessel_entity.cpp index 34f1b2b..e3bd829 100644 --- a/sea_transport/entities/vessel_entity.cpp +++ b/sea_transport/entities/vessel_entity.cpp @@ -3,7 +3,7 @@ entity_id vessel_entity::__global_id = 0; -vessel_entity::vessel_entity(entity_id harbor_id, unsigned int capacity) : _harbor_id(harbor_id), _capacity(capacity) { +vessel_entity::vessel_entity(entity_id skipper_id, entity_id harbor_id, unsigned int capacity) : _skipper_id(skipper_id), _harbor_id(harbor_id), _capacity(capacity) { this->_id = ++vessel_entity::__global_id; } @@ -23,6 +23,27 @@ const QVector vessel_entity::cargo() { return this->_cargo; } +void vessel_entity::add_cargo(cargo_entity object, bool &success) { + success = ((int)this->_capacity - (int)object.volume()) > 0; + if (success) { + this->_cargo.push_back(object); + this->_capacity -= object.volume(); + } +} + +void vessel_entity::withdraw_cargo(entity_id oid, bool &success) { + success = false; + auto vit = this->_cargo.begin(); + for (; vit != this->_cargo.end(); vit++) { + if ((*vit).id() == oid) { + this->_capacity += (*vit).volume(); + this->_cargo.erase(vit); + success = true; + break; + } + } +} + void vessel_entity::serialize(QDataStream &output) { output << this->_id << this->_harbor_id; output << this->_capacity << this->_cargo.size(); diff --git a/sea_transport/entities/vessel_entity.h b/sea_transport/entities/vessel_entity.h index e96ecfe..22a1c7c 100644 --- a/sea_transport/entities/vessel_entity.h +++ b/sea_transport/entities/vessel_entity.h @@ -11,19 +11,23 @@ private: static entity_id __global_id; entity_id _id; + entity_id _skipper_id; entity_id _harbor_id; unsigned int _capacity; QVector _cargo; public: vessel_entity() = default; - vessel_entity(entity_id harbor_id, unsigned int capacity); + vessel_entity(entity_id skipper_id, entity_id harbor_id, unsigned int capacity); entity_id id() const; entity_id harbor() const; unsigned int capacity() const; const QVector cargo(); + void add_cargo(cargo_entity object, bool &success); + void withdraw_cargo(entity_id oid, bool &success); + void serialize(QDataStream &output); void deserialize(QDataStream &input); static void preloadGlobalId(entity_id gid);