diff --git a/client/entities/local_character.cpp b/client/entities/local_character.cpp index 201ef48..52dcf9f 100644 --- a/client/entities/local_character.cpp +++ b/client/entities/local_character.cpp @@ -21,3 +21,42 @@ */ #include "local_character.hpp" +void LocalCharacter::ProcessCollisions(std::list& boxList) { + if (CheckCollisionSimple(boxList, origin + motion)) { + Vector2 velocity; + velocity.x = CorrectVelocityX(boxList, motion.x); + velocity.y = CorrectVelocityY(boxList, motion.y); + origin += velocity; + } + else { + origin += motion; + } +} + +bool LocalCharacter::CheckCollisionSimple(std::list& boxList, Vector2 newPos) { + for (auto& it : boxList) { + if (it.CheckOverlap(bounds + newPos)) { + return true; + } + } + return false; +} + +double LocalCharacter::CorrectVelocityX(std::list& boxList, double velocityX) { + double ret = velocityX; + for (auto& it : boxList) { + if (it.CheckOverlap(bounds + origin + Vector2(velocityX, 0) )) { + if (velocityX > 0) { + ret = std::min(ret, it.x - origin.x - (bounds.w - bounds.x - 1)); + } + else if (velocityX < 0) { + ret = std::max(ret, (it.x + it.w) - origin.x); + } + } + } + return ret; +} + +double LocalCharacter::CorrectVelocityY(std::list& boxList, double velocityY) { + return velocityY; +} diff --git a/client/entities/local_character.hpp b/client/entities/local_character.hpp index 4493fb4..9726d3e 100644 --- a/client/entities/local_character.hpp +++ b/client/entities/local_character.hpp @@ -23,14 +23,22 @@ #define LOCALCHARACTER_HPP_ #include "base_character.hpp" +#include "bounding_box.hpp" +#include "vector2.hpp" + +#include class LocalCharacter: public BaseCharacter { public: LocalCharacter() = default; virtual ~LocalCharacter() = default; -private: - //NOTE: NO MEMBERS + void ProcessCollisions(std::list& boxList); + +protected: + bool CheckCollisionSimple(std::list& boxList, Vector2 newPos); + double CorrectVelocityX(std::list& boxList, double velocityX); + double CorrectVelocityY(std::list& boxList, double velocityY); }; #endif \ No newline at end of file diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 03ec1e3..49fed1d 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -156,6 +156,10 @@ void InWorld::Update() { //update all entities for (auto& it : characterMap) { + //skip this player's character + if (it.first == characterIndex) { + continue; + } it.second.Update(); } for (auto& it : monsterMap) { @@ -198,7 +202,7 @@ void InWorld::Update() { } //process the collisions - std::cout << "boxList.size(): " << boxList.size() << std::endl; + localCharacter->ProcessCollisions(boxList); //update the camera camera.x = localCharacter->GetOrigin().x - camera.marginX;