From f07b22a6bf7942c34d5879e4cbdbe0a3192957b1 Mon Sep 17 00:00:00 2001 From: KosmX <kosmx.mc@gmail.com> Date: Wed, 12 May 2021 22:27:30 +0200 Subject: [PATCH] Weapons and logic fixes --- CharacterEntity.h | 2 +- Entity.cpp | 2 +- LivingEntity.cpp | 3 +++ PlayerEntity.cpp | 39 +++++++++++++++++++++++++++++++++++++-- PlayerEntity.h | 9 +++++++++ TestGenerator.cpp | 12 +++++++++++- Weapon.cpp | 11 +++++++++++ Weapon.h | 5 +++++ mainGame.cpp | 6 +++--- 9 files changed, 81 insertions(+), 8 deletions(-) diff --git a/CharacterEntity.h b/CharacterEntity.h index 6ffd8b2..5af348a 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 e3c5da5..c4f5095 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 f6a1d95..fd5cbb7 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 7aa4d61..c129f00 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 8a8e670..639c128 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 e1dd538..0284f9a 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 6721d60..cb17648 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 061f9b3..c5dd5e2 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 0526e1c..2bc8a1a 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; -- GitLab