zad5
This commit is contained in:
106
2dkg_zad5/2dgk_zad5/KPawn.cpp
Normal file
106
2dkg_zad5/2dgk_zad5/KPawn.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user