59 lines
1.6 KiB
C++
59 lines
1.6 KiB
C++
#include "KPawn.h"
|
|
|
|
#include <memory>
|
|
#include <typeinfo>
|
|
#include <unordered_map>
|
|
|
|
#include "Constants.h"
|
|
#include "KPlayerController.h"
|
|
#include "KExit.h"
|
|
|
|
namespace KapitanGame
|
|
{
|
|
void KPawn::CollisionDetectionStep(const std::unordered_map<std::pair<int, int>, std::shared_ptr<KObject>, Utils::PairHash>& objects)
|
|
{
|
|
if (GetCollider() == nullptr) return;
|
|
|
|
const auto xPos = static_cast<int>(GetPosition().X / Constants::TILE_WIDTH);
|
|
const auto yPos = static_cast<int>(GetPosition().Y / Constants::TILE_HEIGHT);
|
|
|
|
for (const auto& [oX, oY] : std::initializer_list<std::pair<int, int>>{ {0, 0}, {-1,0}, {1,0}, {0,-1}, {0,1}, {-1,-1}, {1,1}, {1,-1}, {-1,1} })
|
|
{
|
|
try {
|
|
const auto& other = objects.at({ xPos + oX, yPos + oY });
|
|
if (other->GetCollider() == nullptr) continue;
|
|
if (other->Id == Id) continue;
|
|
if (GetCollider()->IsCollision(other->GetCollider())) {
|
|
if (typeid(*other) == typeid(KExit)) {
|
|
if (const auto pc = PlayerController.lock()) {
|
|
pc->NotifyWin();
|
|
}
|
|
return;
|
|
}
|
|
const auto separationVector = GetCollider()->GetSeparationVector(other->GetCollider());
|
|
Position += separationVector;
|
|
}
|
|
}
|
|
catch (std::out_of_range&)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
|
|
void KPawn::MovementStep(const float& timeStep)
|
|
{
|
|
Position += Velocity * timeStep;
|
|
}
|
|
|
|
void KPawn::CollisionDetectionWithMapStep(const SDL_FRect& map)
|
|
{
|
|
Position += GetCollider()->GetSeparationVector(map);
|
|
}
|
|
|
|
void KPawn::AddMovementInput(const KVector2D& input)
|
|
{
|
|
Velocity = input * Constants::SPEED * (1 - Constants::SMOOTH) + Velocity * Constants::SMOOTH;
|
|
}
|
|
}
|
|
|