From 7e7aa3e423a6b6f201c6e06afe412e44aa461283 Mon Sep 17 00:00:00 2001 From: KosmX <kosmx.mc@gmail.com> Date: Mon, 10 May 2021 11:25:40 +0200 Subject: [PATCH] working collision and player animation --- DynamicArray.hpp | 10 ++++++++-- Entity.cpp | 8 +++++--- LazySprite.cpp | 4 ++-- LazySprite.h | 4 ++-- LivingEntity.cpp | 21 +++++++++++---------- LivingEntity.h | 2 +- PlayerEntity.cpp | 10 +++++----- mainGame.cpp | 9 ++++----- 8 files changed, 38 insertions(+), 30 deletions(-) diff --git a/DynamicArray.hpp b/DynamicArray.hpp index af15525..3193f6d 100644 --- a/DynamicArray.hpp +++ b/DynamicArray.hpp @@ -24,11 +24,17 @@ public: return *this; } - void finalizeAdd() + /** + * @return an iterator pointing to the first, newly added element. + */ + auto finalizeAdd() { //this moves every entry form newEntries to entries. //makes the newEntries empty. - entries.merge(newEntries); + //entries.merge(newEntries); merge needs two ordered lists, i have only sets. + auto oldEnd = newEntries.begin() != newEntries.end() ? newEntries.begin() : entries.end(); + entries.splice(entries.end(), newEntries); + return oldEnd; } void removeIf(bool (*l)(const T&)) diff --git a/Entity.cpp b/Entity.cpp index 531a448..ff0c886 100644 --- a/Entity.cpp +++ b/Entity.cpp @@ -5,7 +5,7 @@ using namespace std; using namespace olc; // fine tuning the collision engine, the edges of a box won't collide -const float ignoreDistance = 0.01f; +const float ignoreDistance = 0.08f; namespace entities { /* @@ -15,6 +15,8 @@ namespace entities { */ vf2d Entity::getCollision(const Entity& other) { + if (&other == this)return { 0, 0 }; //don't do collision with itself + const vf2d delta = this->getPos() - other.getPos(); //pair<bool, bool> dir(delta.x < 0, delta.y < 0); vf2d dir(delta.x < 0 ? 1 : -1, delta.y < 0 ? 1 : -1); @@ -24,10 +26,10 @@ namespace entities { offset = vf2d(offset.x < 0 ? offset.x : 0, offset.y < 0 ? offset.y : 0); offset *= dir; hitSize -= vf2d(ignoreDistance, ignoreDistance); - if (delta.x > hitSize.x) { + if (abs(delta.x) > hitSize.x) { offset.y = 0; } - if (delta.y > hitSize.y) { + if (abs(delta.y) > hitSize.y) { offset.x = 0; } return offset; diff --git a/LazySprite.cpp b/LazySprite.cpp index 9ed1caa..c131c5b 100644 --- a/LazySprite.cpp +++ b/LazySprite.cpp @@ -16,13 +16,13 @@ namespace render { : sprite(nullptr), resourceName(resName) {} - void LazySprite::render(olc::TransformedView& scene, const olc::vi2d& pos, const olc::vf2d& uv, const olc::vf2d& size, olc::vf2d scale) + void LazySprite::render(olc::TransformedView& scene, const olc::vf2d& pos, const olc::vf2d& uv, const olc::vf2d& size, olc::vf2d scale) { scene.DrawPartialDecal(pos, this->getDecay(), uv, size, scale); } - void LazySprite::renderCentered(olc::TransformedView& scene, const olc::vi2d& pos, const olc::vf2d& uv, const olc::vf2d& size, olc::vf2d scale) + void LazySprite::renderCentered(olc::TransformedView& scene, const olc::vf2d& pos, const olc::vf2d& uv, const olc::vf2d& size, olc::vf2d scale) { this->render(scene, pos + size / 2.0f, uv, size, scale); } diff --git a/LazySprite.h b/LazySprite.h index f38867c..b72ea26 100644 --- a/LazySprite.h +++ b/LazySprite.h @@ -30,9 +30,9 @@ namespace render { /** * Render the sprite in world-space */ - void render(olc::TransformedView& scene, const olc::vi2d& pos, const olc::vf2d& uv, const olc::vf2d& size, olc::vf2d scale = olc::vf2d(1, 1)); + void render(olc::TransformedView& scene, const olc::vf2d& pos, const olc::vf2d& uv, const olc::vf2d& size, olc::vf2d scale = olc::vf2d(1, 1)); - void renderCentered(olc::TransformedView& scene, const olc::vi2d& pos, const olc::vf2d& uv, const olc::vf2d& size, olc::vf2d scale = olc::vf2d(1, 1)); + void renderCentered(olc::TransformedView& scene, const olc::vf2d& pos, const olc::vf2d& uv, const olc::vf2d& size, olc::vf2d scale = olc::vf2d(1, 1)); //decal reference is not my stuff, I don't have to delete it. }; diff --git a/LivingEntity.cpp b/LivingEntity.cpp index f6e10ca..7d1425c 100644 --- a/LivingEntity.cpp +++ b/LivingEntity.cpp @@ -8,7 +8,7 @@ namespace entities { vf2d LivingEntity::getHitBoxSize() const { - return olc::vf2d(0.8, 0.8); + return olc::vf2d(0.8f, 0.8f)/2; } vf2d LivingEntity::collisionOffset(GameClient& client) @@ -33,8 +33,8 @@ namespace entities { return offset; } - LivingEntity::LivingEntity(olc::vf2d pos) - : Entity(pos), timeUntilNextPhase(0), direction(0), anim_phase(0) {} + LivingEntity::LivingEntity(olc::vf2d pos, int health) + : Entity(pos), direction(0), health(health), anim_phase(0), timeUntilNextPhase(0) {} bool LivingEntity::damage(int damage, Entity& attacker) { @@ -55,17 +55,17 @@ namespace entities { if(speed.mag2() < 0.1 ){ return direction; } - else if(speed.x > std::abs(speed.y)){ + else if(speed.x >= std::abs(speed.y)){ direction = 2; } - else if(speed.x < -std::abs(speed.y)){ + else if(speed.x <= -std::abs(speed.y)){ direction = 1; } - else if(speed.y > std::abs(speed.x)){ - direction = 3; + else if(speed.y >= std::abs(speed.x)){ + direction = 0; } else{ - direction = 0; + direction = 3; } return direction; } @@ -77,7 +77,8 @@ namespace entities { void LivingEntity::tick(GameClient& client, float deltaT, std::shared_ptr<Entity>& shared_this) { - timeUntilNextPhase += deltaT; + timeUntilNextPhase += deltaT * speed.mag(); + if (speed == vf2d(0, 0))anim_phase = 0; if(timeUntilNextPhase > phaseLength){ anim_phase = (anim_phase + 1) % 4; timeUntilNextPhase -= phaseLength; @@ -94,5 +95,5 @@ namespace entities { return this; } - const float LivingEntity::phaseLength = 0.5; + const float LivingEntity::phaseLength = 1; } \ No newline at end of file diff --git a/LivingEntity.h b/LivingEntity.h index 2e7999e..fd9434b 100644 --- a/LivingEntity.h +++ b/LivingEntity.h @@ -16,7 +16,7 @@ namespace entities { virtual olc::vf2d collisionOffset(GameClient& client); public: - LivingEntity(olc::vf2d pos); + LivingEntity(olc::vf2d pos, int health = 512); bool damage(int damage, Entity& attacker) override; char getDirection(); char getAnimPhase() const; diff --git a/PlayerEntity.cpp b/PlayerEntity.cpp index dca29ac..2d6e826 100644 --- a/PlayerEntity.cpp +++ b/PlayerEntity.cpp @@ -14,11 +14,11 @@ namespace entities { void PlayerEntity::tick(GameClient& client, float deltaT, std::shared_ptr<Entity>& shared_this) { vf2d newSpeed = { 0, 0 }; - if (client.GetKey(olc::Key::A).bHeld) newSpeed -= {-1, 0}; - if (client.GetKey(olc::Key::S).bHeld) newSpeed -= {0, 1}; - if (client.GetKey(olc::Key::D).bHeld) newSpeed -= {1, 0}; - if (client.GetKey(olc::Key::W).bHeld) newSpeed -= {0, -1}; - this->speed = newSpeed; + if (client.GetKey(olc::Key::A).bHeld) newSpeed += {-1, 0}; + if (client.GetKey(olc::Key::S).bHeld) newSpeed += {0, 1}; + if (client.GetKey(olc::Key::D).bHeld) newSpeed += {1, 0}; + if (client.GetKey(olc::Key::W).bHeld) newSpeed += {0, -1}; + this->speed = newSpeed == vf2d(0, 0) ? newSpeed : newSpeed.norm() * 6; LivingEntity::tick(client, deltaT, shared_this); } diff --git a/mainGame.cpp b/mainGame.cpp index 6e9b59a..7f8c3a1 100644 --- a/mainGame.cpp +++ b/mainGame.cpp @@ -57,11 +57,10 @@ bool GameClient::OnUserUpdate(float fElapsedTime) entity->tick(*this, fElapsedTime, entity); } - entities.finalizeAdd(); - for(auto &entity : entities){ - if(!entity->isInitialized()){ - entity->init(*this); - } + auto iterator = entities.finalizeAdd(); + + while (iterator != entities.end()){ + iterator++->get()->init(*this); } //lambda remove condition. because why not? -- GitLab