diff --git a/CharacterEntity.cpp b/CharacterEntity.cpp index c144ecaeac3b0c40e8665c758d9bdc4638000eb4..5b9b1d902139782308623a774fbf88258f83178f 100644 --- a/CharacterEntity.cpp +++ b/CharacterEntity.cpp @@ -1,5 +1,7 @@ #include "CharacterEntity.h" +#include "Weapon.h" + namespace entities { std::shared_ptr<weapons::Weapon> CharacterEntity::getWeapon() { @@ -11,4 +13,20 @@ namespace entities { } CharacterEntity::CharacterEntity(render::ITexture& tex, const olc::vf2d& pos) : LivingEntity(pos), texture(tex) {} + void CharacterEntity::tick(GameClient& client, float deltaT, std::shared_ptr<Entity>& shared_this) + { + LivingEntity::tick(client, deltaT, shared_this); + if(this->getWeapon()){ + this->getWeapon()->update(deltaT); + this->getWeapon()->setPos(this->getPos() + this->speed != olc::vf2d(0, 0) ? this->speed.norm()/2 : olc::vf2d(0, 0)); + } + } + + void CharacterEntity::render(olc::TransformedView& scene) + { + LivingEntity::render(scene); + if (this->getWeapon()) { + this->getWeapon()->render(scene); + } + } } diff --git a/CharacterEntity.h b/CharacterEntity.h index 5af348ac0a1a49ab10a023f95d801614f3aff86c..857ee130fd948069ecd5b038698eed9192dde289 100644 --- a/CharacterEntity.h +++ b/CharacterEntity.h @@ -20,5 +20,7 @@ namespace entities { render::ITexture& getTexture() override; public: CharacterEntity(render::ITexture& skin, const olc::vf2d& pos); + void tick(GameClient& client, float deltaT, std::shared_ptr<Entity>& shared_this) override; + void render(olc::TransformedView& scene) override; }; } diff --git a/LivingEntity.cpp b/LivingEntity.cpp index fd5cbb7ef08059d2508b558e06890ed3f421da3e..54159fe0bed7c39c7e0f2f3cabc7885c0ca55e8b 100644 --- a/LivingEntity.cpp +++ b/LivingEntity.cpp @@ -1,6 +1,7 @@ #include "LivingEntity.h" #include "mainGame.h" +#include "WallEntity.h" using namespace olc; @@ -15,7 +16,7 @@ namespace entities { { vf2d offset = { 0, 0 }; for (auto& entity : client.getEntities()) { - if(std::dynamic_pointer_cast<LivingEntity>(entity) != nullptr){ + if(std::dynamic_pointer_cast<WallEntity>(entity) == nullptr){ continue; } vf2d current = this->getCollision(*entity); @@ -37,7 +38,7 @@ namespace entities { } LivingEntity::LivingEntity(olc::vf2d pos, int health) - : Entity(pos), direction(0), health(health), anim_phase(0), timeUntilNextPhase(0) {} + : Entity(pos), health(health), direction(0), anim_phase(0), timeUntilNextPhase(0) {} bool LivingEntity::damage(int damage, Entity& attacker) { diff --git a/LivingEntity.h b/LivingEntity.h index 39e1a9c59a9d35ff67452f11291d3ead35a0e5ba..1780996ffbf6497ee653c63fc6c668bbfc1ee35a 100644 --- a/LivingEntity.h +++ b/LivingEntity.h @@ -5,7 +5,6 @@ namespace entities { public Entity { private: - int health; char direction; char anim_phase; float timeUntilNextPhase; @@ -14,9 +13,9 @@ namespace entities { olc::vf2d getHitBoxSize() const override; olc::vf2d speed; virtual olc::vf2d collisionOffset(GameClient& client); - + int health; public: - LivingEntity(olc::vf2d pos, int health = 512); + LivingEntity(olc::vf2d pos, int health = 100); bool damage(int damage, Entity& attacker) override; char getDirection(); char getAnimPhase() const; diff --git a/MeleeWeapon.cpp b/MeleeWeapon.cpp index 5737f9c0fb15a93848a8de4d113e36348af9fbc5..fd4c4e27f9aab01167c8aae38c166f18cf34b407 100644 --- a/MeleeWeapon.cpp +++ b/MeleeWeapon.cpp @@ -1,7 +1,10 @@ #include "MeleeWeapon.h" + #include "mainGame.h" + namespace weapons { + int MeleeWeapon::getDamage() const { return this->baseDamage; @@ -27,20 +30,18 @@ namespace weapons { bool MeleeWeapon::use(std::shared_ptr<Entity> user, const olc::vf2d& direction) { if (this->cooldown != 0) return false; - this->cooldown = this->cooldownTime; - return damageIf(user, this->getPredicator(direction)); + if (damageIf(user, this->getPredicator(direction))) { + this->cooldown = this->cooldownTime; + return true; + } + return false; } float MeleeWeapon::getHitOffset() const { return 0; } - - - void MeleeWeapon::setPos(const olc::vf2d& newPos) - { - this->pos = newPos; //I should test its pos... - } + bool MeleeWeapon::predicateDistance::operator()(const olc::vf2d& usePos, std::shared_ptr<Entity> other) const { float d = (usePos - other->getPos()).mag(); diff --git a/MeleeWeapon.h b/MeleeWeapon.h index 853011bcddab294e2cb5380719f68a67be120d3f..217e7f0a811d2b4456315b8bcb15b6b52d791ed9 100644 --- a/MeleeWeapon.h +++ b/MeleeWeapon.h @@ -30,6 +30,5 @@ namespace weapons { MeleeWeapon(render::ITexture& texture, const std::string& name, float cooldownTime, int damage = 10, const olc::vf2d& pos = { 0, 0 }); bool use(std::shared_ptr<Entity> user, const olc::vf2d& direction) override; - virtual void setPos(const olc::vf2d& newPos); }; } diff --git a/PlayerEntity.cpp b/PlayerEntity.cpp index c129f00b8731e27327d5fda69755557932c13e75..b2ea294d62d2a9b14aaefe82bbb62bec10173573 100644 --- a/PlayerEntity.cpp +++ b/PlayerEntity.cpp @@ -37,15 +37,15 @@ namespace entities { } if(client.GetKey(TAB).bPressed){ - if(weaponToPickUp != nullptr){ + if (weaponToPickUp != nullptr) { weaponToPickUp->setPickUp(true); - if(this->getWeapon() != nullptr){ + if (this->getWeapon() != nullptr) { this->getWeapon()->setPickUp(false); std::shared_ptr<Entity> tmp = this->getWeapon(); client.addEntity(tmp); - this->weapons[selectedSlot] = weaponToPickUp; - weaponToPickUp = nullptr; } + this->weapons[selectedSlot] = weaponToPickUp; + weaponToPickUp = nullptr; } } @@ -61,16 +61,20 @@ namespace entities { } } - LivingEntity::tick(client, deltaT, shared_this); + CharacterEntity::tick(client, deltaT, shared_this); //super.tick(); } PlayerEntity::PlayerEntity(olc::vf2d pos, render::ITexture& skin, const std::string& name) : CharacterEntity(skin, pos), name(name) { - + this->health = maxHealth; } const std::shared_ptr<weapons::Weapon>& PlayerEntity::getWeapon() const { return this->weapons[selectedSlot]; } + float PlayerEntity::getHealthStatus() + { + return health/maxHealth; + } } \ No newline at end of file diff --git a/PlayerEntity.h b/PlayerEntity.h index 639c128bd371833d993b4a830c2618609b2b3cc9..ca5dc4890cf7d19acc7ef78cd3cb63e456d92709 100644 --- a/PlayerEntity.h +++ b/PlayerEntity.h @@ -13,13 +13,15 @@ namespace entities { std::shared_ptr<weapons::Weapon> weaponToPickUp; std::shared_ptr<weapons::Weapon> weapons[maxWeapons]; int selectedSlot = 0; - - protected: - std::shared_ptr<weapons::Weapon> getWeapon() override; + float maxHealth = 200; public: + std::shared_ptr<weapons::Weapon> getWeapon() override; + 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; + + float getHealthStatus(); }; } diff --git a/Weapon.cpp b/Weapon.cpp index cb176484a40cf515f0b1c55e4777d1e6d665f6bc..fe8f2f97d0eae4ba7c3f530b42eb092984ae2b2e 100644 --- a/Weapon.cpp +++ b/Weapon.cpp @@ -19,7 +19,7 @@ namespace weapons float Weapon::getCooldownBar() { - return this->cooldown / this->cooldownTime; + return 1 - this->cooldown / this->cooldownTime; } bool Weapon::canBeRemoved() const @@ -32,5 +32,10 @@ namespace weapons isPickedUp = bl; } + void Weapon::setPos(const olc::vf2d& newPos) + { + this->pos = newPos; + } + } \ No newline at end of file diff --git a/Weapon.h b/Weapon.h index c5dd5e2fb2f911b83bc0fff5ffc0d3afb1d18e7a..2ee724108de31221a6aed82816e5dc24e4b7c041 100644 --- a/Weapon.h +++ b/Weapon.h @@ -45,5 +45,6 @@ namespace weapons { * Secondary charge bar. for some purpose */ virtual float getSecondaryBar() { return 0; } + virtual void setPos(const olc::vf2d& newPos); }; } \ No newline at end of file diff --git a/mainGame.cpp b/mainGame.cpp index 2bc8a1a64c3207c663d4519c92ef8e042a35a066..027f5e58ccce5b645266e2378f25eec504d6f7f8 100644 --- a/mainGame.cpp +++ b/mainGame.cpp @@ -3,6 +3,7 @@ #include <algorithm> #include "GameException.h" #include "TestGenerator.h" +#include "Weapon.h" const float maxTimeDelta = 0.05f; using namespace std; @@ -55,8 +56,37 @@ bool GameClient::OnUserCreate() return true; } +void GameClient::renderStatusLine(int i, float f, const olc::Pixel& pixel) +{ + DrawLine(vf2d(0, i + 0.f), vf2d( 128.f * f, i ), pixel); + DrawLine(vf2d(0, i + 1.f), vf2d( 128.f * f, i + 1 ), pixel); +} + +void GameClient::renderStatus() +{ + SetDrawTarget(1); + + float health = this->player->getHealthStatus(); + if (this->player->getWeapon()) { + float weapon = 0; + float weapon2 = 0; + shared_ptr<weapons::Weapon> wep = this->player->getWeapon(); + weapon = wep->getCooldownBar(); + weapon2 = wep->getSecondaryBar(); + renderStatusLine(4, weapon, WHITE); + renderStatusLine(6, weapon2, BLUE); + } + + renderStatusLine(0, health, RED); + + + SetDrawTarget(static_cast<uint8_t>(0)); +} + + bool GameClient::OnUserUpdate(float fElapsedTime) { + Clear(BLACK); fElapsedTime = std::min(maxTimeDelta, fElapsedTime); //return false if it want to exit. @@ -90,6 +120,8 @@ bool GameClient::OnUserUpdate(float fElapsedTime) } } + renderStatus(); + //debug section if (debug) { //scene.DrawCircle({ 0, 0 }, 1); diff --git a/mainGame.h b/mainGame.h index fa72dd31972647076f4482deacd0cdae55f0c47b..9e68d747518ef05eac5f7dc8248088f11e6007c8 100644 --- a/mainGame.h +++ b/mainGame.h @@ -20,6 +20,8 @@ private: olc::TransformedView scene; std::shared_ptr<entities::PlayerEntity> player; void updateWorldOffset(float dTick); + void renderStatus(); + void renderStatusLine(int i, float f, const olc::Pixel& pixel); public: static GameClient& getInstance();