107 lines
2.9 KiB
C++
107 lines
2.9 KiB
C++
#include "KPawn.h"
|
|
|
|
#include <memory>
|
|
//#include <typeinfo>
|
|
|
|
#include "Constants.h"
|
|
#include "KCollider.h"
|
|
//#include "KPlayerController.h"
|
|
#include "KSettings.h"
|
|
|
|
namespace KapitanGame
|
|
{
|
|
void KPawn::CollisionDetectionStep(const std::unordered_map<std::pair<int, int>, std::shared_ptr<KSolid>, 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} })
|
|
{
|
|
auto it = objects.find({ xPos + oX, yPos + oY });
|
|
if (it == objects.end()) continue;
|
|
const auto& other = it->second;
|
|
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;
|
|
if (GetPosition().Y - GetCollider()->GetHeight() / 2.f >= other->GetPosition().Y + other->GetCollider()->GetHeight() / 2.f)
|
|
{
|
|
Velocity.Y = 0;
|
|
}
|
|
if (GetPosition().Y + GetCollider()->GetHeight() / 2.f <= other->GetPosition().Y - other->GetCollider()->GetHeight() / 2.f)
|
|
{
|
|
Velocity.Y = 0;
|
|
CanJump = true;
|
|
CanDoubleJump = true;
|
|
HasJumped = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void KPawn::MovementStep(const float& timeStep)
|
|
{
|
|
const float gravityScale = Velocity.Y > 0.f ? 3.f : 1.f;
|
|
Position.X += Velocity.X * timeStep;
|
|
Position.Y += Velocity.Y * timeStep + 1.f / 2.f * gravityScale * Settings->Gravity * timeStep * timeStep;
|
|
Velocity.Y += gravityScale * Settings->Gravity * timeStep;
|
|
}
|
|
|
|
void KPawn::CollisionDetectionWithMapStep(const SDL_FRect& map)
|
|
{
|
|
const auto separationVector = GetCollider()->GetSeparationVector(map);
|
|
Position += separationVector;
|
|
if (separationVector.Y < 0)
|
|
{
|
|
Velocity.Y = 0;
|
|
CanJump = true;
|
|
CanDoubleJump = true;
|
|
HasJumped = false;
|
|
}
|
|
if (separationVector.Y > 0)
|
|
{
|
|
Velocity.Y = 0;
|
|
}
|
|
}
|
|
|
|
void KPawn::AddXMovementInput(const float& input)
|
|
{
|
|
Velocity.X = input * Constants::SPEED * (1 - Constants::SMOOTH) + Velocity.X * Constants::SMOOTH;
|
|
}
|
|
|
|
void KPawn::StopJump() {
|
|
if (!CanJump) {
|
|
if (Velocity.Y < -Settings->ShortJumpVelocity)
|
|
Velocity.Y = -Settings->ShortJumpVelocity;
|
|
}
|
|
}
|
|
|
|
void KPawn::StartJump() {
|
|
if (Velocity.Y != 0.f)
|
|
{
|
|
CanJump = false;
|
|
}
|
|
if (CanJump) {
|
|
CanJump = false;
|
|
HasJumped = true;
|
|
Velocity.Y = -Settings->JumpInitialVelocity;
|
|
}
|
|
else if (HasJumped && CanDoubleJump)
|
|
{
|
|
CanDoubleJump = false;
|
|
Velocity.Y = -Settings->JumpInitialVelocity;
|
|
}
|
|
}
|
|
}
|
|
|