diff --git a/CharacterEntity.h b/CharacterEntity.h index 6ffd8b2ba4c63f504759433cf42fa26ee0792c71..5af348ac0a1a49ab10a023f95d801614f3aff86c 100644 --- a/CharacterEntity.h +++ b/CharacterEntity.h @@ -14,7 +14,7 @@ namespace entities { /** * Return null if not available */ - std::shared_ptr<weapons::Weapon> getWeapon(); + virtual std::shared_ptr<weapons::Weapon> getWeapon(); render::ITexture& texture; render::ITexture& getTexture() override; diff --git a/Entity.cpp b/Entity.cpp index e3c5da5473212756690521b84823cd89991a3231..c4f509522f36f51495527962f75a3d49825a524e 100644 --- a/Entity.cpp +++ b/Entity.cpp @@ -74,7 +74,7 @@ namespace entities { bool Entity::canBeRemoved() const { - return this->isAlive(); //most times + return !this->isAlive(); //most times } } diff --git a/LivingEntity.cpp b/LivingEntity.cpp index f6a1d95a98a2fd3fce463783440d452f7dc5438f..fd5cbb7ef08059d2508b558e06890ed3f421da3e 100644 --- a/LivingEntity.cpp +++ b/LivingEntity.cpp @@ -15,6 +15,9 @@ namespace entities { { vf2d offset = { 0, 0 }; for (auto& entity : client.getEntities()) { + if(std::dynamic_pointer_cast<LivingEntity>(entity) != nullptr){ + continue; + } vf2d current = this->getCollision(*entity); if (offset == vf2d(0, 0)) { offset = current; diff --git a/PlayerEntity.cpp b/PlayerEntity.cpp index 7aa4d61267363d107e8f28b910e0eff7c2b1765f..c129f00b8731e27327d5fda69755557932c13e75 100644 --- a/PlayerEntity.cpp +++ b/PlayerEntity.cpp @@ -6,7 +6,10 @@ using namespace olc; namespace entities { - + std::shared_ptr<weapons::Weapon> PlayerEntity::getWeapon() + { + return this->weapons[selectedSlot]; + } void PlayerEntity::tick(GameClient& client, float deltaT, std::shared_ptr<Entity>& shared_this) { vf2d newSpeed = { 0, 0 }; @@ -24,9 +27,37 @@ namespace entities { this->getWeapon()->use(shared_this, mouse); } + this->weaponToPickUp = nullptr; + for(auto& entity : client.getEntities()){ if(std::dynamic_pointer_cast<weapons::Weapon>(entity)){ - + weaponToPickUp = std::dynamic_pointer_cast<weapons::Weapon>(entity); + break; + } + } + + if(client.GetKey(TAB).bPressed){ + if(weaponToPickUp != nullptr){ + weaponToPickUp->setPickUp(true); + if(this->getWeapon() != nullptr){ + this->getWeapon()->setPickUp(false); + std::shared_ptr<Entity> tmp = this->getWeapon(); + client.addEntity(tmp); + this->weapons[selectedSlot] = weaponToPickUp; + weaponToPickUp = nullptr; + } + } + } + + int mouseWheel = client.GetMouseWheel(); + if(mouseWheel != 0){ + if(mouseWheel > 0){ + this->selectedSlot = (selectedSlot + 1)%maxWeapons; + } + else{ + if(--this->selectedSlot < 0){ + selectedSlot = maxWeapons - 1; + } } } @@ -38,4 +69,8 @@ namespace entities { { } + const std::shared_ptr<weapons::Weapon>& PlayerEntity::getWeapon() const + { + return this->weapons[selectedSlot]; + } } \ No newline at end of file diff --git a/PlayerEntity.h b/PlayerEntity.h index 8a8e670f0771545fd2dc8ff98b6de7dea257e279..639c128bd371833d993b4a830c2618609b2b3cc9 100644 --- a/PlayerEntity.h +++ b/PlayerEntity.h @@ -1,5 +1,6 @@ #pragma once #include "CharacterEntity.h" +#include <vector> namespace entities { @@ -7,10 +8,18 @@ namespace entities { public CharacterEntity { private: + static const int maxWeapons = 3; std::string name; std::shared_ptr<weapons::Weapon> weaponToPickUp; + std::shared_ptr<weapons::Weapon> weapons[maxWeapons]; + int selectedSlot = 0; + + protected: + std::shared_ptr<weapons::Weapon> getWeapon() override; public: void tick(GameClient& client, float deltaT, std::shared_ptr<Entity>& shared_this) override; PlayerEntity(olc::vf2d pos, render::ITexture& skin, const std::string& name = "Player"); + + const std::shared_ptr<weapons::Weapon>& getWeapon() const; }; } diff --git a/TestGenerator.cpp b/TestGenerator.cpp index e1dd538c5580a455683482fc86dbef79c60ba17d..0284f9a4c4cc24a38fd1096111ee9e9621e2dc83 100644 --- a/TestGenerator.cpp +++ b/TestGenerator.cpp @@ -4,6 +4,8 @@ #include "mainGame.h" #include "PlayerEntity.h" #include "CharacterTexture.h" +#include "DummyEntity.h" +#include "TypicalMeleeWeapon.h" using namespace entities; using namespace std; @@ -19,7 +21,15 @@ shared_ptr<PlayerEntity> TestGenerator::generate(GameClient& client) client += make_shared<WallEntity>(*new WallEntity(olc::vf2d(-1, -1))); client += make_shared<WallEntity>(*asd); - shared_ptr<PlayerEntity> player(new PlayerEntity({ 0, -4 }, render::CharacterTexture::MageTexture)); + client += make_shared<CharacterEntity>(*new DummyEntity(olc::vf2d(5, 6), render::CharacterTexture::MageTexture)); + + auto weapon = make_shared<weapons::TypicalMeleeWeapon>(*new weapons::TypicalMeleeWeapon(weapons::TypicalMeleeWeapon::sword)); + + weapon->setPos({ -3, -4 }); + + client += weapon; + shared_ptr<PlayerEntity> player(new PlayerEntity({ 0, -4 }, render::CharacterTexture::EngineerTexture)); + client += player; return player; diff --git a/Weapon.cpp b/Weapon.cpp index 6721d604b5b20059e918e9e11fa517dfd767cf4b..cb176484a40cf515f0b1c55e4777d1e6d665f6bc 100644 --- a/Weapon.cpp +++ b/Weapon.cpp @@ -21,5 +21,16 @@ namespace weapons { return this->cooldown / this->cooldownTime; } + + bool Weapon::canBeRemoved() const + { + return isPickedUp; + } + + void Weapon::setPickUp(bool bl) + { + isPickedUp = bl; + } + } \ No newline at end of file diff --git a/Weapon.h b/Weapon.h index 061f9b3059e420a76e355bc9a6ea5a6f54c1d9a8..c5dd5e2fb2f911b83bc0fff5ffc0d3afb1d18e7a 100644 --- a/Weapon.h +++ b/Weapon.h @@ -19,6 +19,7 @@ namespace weapons { float cooldownTime; int baseDamage; std::string name; + bool isPickedUp = false; public: Weapon(render::ITexture& texture, float cooldownTime, int damage = 10, const std::string& name = "Weapon", const olc::vf2d& pos = { 0, 0 }); @@ -36,6 +37,10 @@ namespace weapons { */ virtual float getCooldownBar(); + bool canBeRemoved() const override; + + virtual void setPickUp(bool bl); + /** * Secondary charge bar. for some purpose */ diff --git a/mainGame.cpp b/mainGame.cpp index 0526e1c16b4ffaf4f3dbbf007218f1d6d4214552..2bc8a1a64c3207c663d4519c92ef8e042a35a066 100644 --- a/mainGame.cpp +++ b/mainGame.cpp @@ -74,7 +74,7 @@ bool GameClient::OnUserUpdate(float fElapsedTime) //lambda remove condition. because why not? entities.removeIf([](const shared_ptr<Entity>& entity)->bool { - return !entity->canBeRemoved(); + return entity->canBeRemoved(); }); this->updateWorldOffset(fElapsedTime); @@ -103,7 +103,7 @@ void GameClient::setDebugMode(bool bl) this->debug = bl; } -void GameClient::addEntity(std::shared_ptr<entities::Entity>& entity) +void GameClient::addEntity(std::shared_ptr<Entity>& entity) { this->entities.operator+=(entity); } @@ -113,7 +113,7 @@ const olc::TransformedView& GameClient::getScene() return scene; } -GameClient& GameClient::operator+=(std::shared_ptr<entities::Entity> entity) +GameClient& GameClient::operator+=(std::shared_ptr<Entity> entity) { this->addEntity(entity); return *this;