Files
NS_unknown_game/game/src/movement/platform_world.cpp
T

40 lines
1.2 KiB
C++

#include "platform_world.h"
#include <algorithm>
namespace ns_game {
MovementBody PlatformMover::Step(const MovementBody& body,
const PlatformWorld& world, float gravity,
float deltaTime) const {
MovementBody next = body;
const frostbite2D::Vec2 previousPosition = next.position;
next.velocity.y += gravity * deltaTime;
next.position += next.velocity * deltaTime;
next.grounded = false;
const float previousBottom = previousPosition.y + next.size.y;
const float newBottom = next.position.y + next.size.y;
for (const auto& platform : world.platforms) {
const bool horizontallyOverlaps =
next.position.x + next.size.x > platform.left() &&
next.position.x < platform.right();
const bool wasAbove = previousBottom <= platform.top() + 4.0f;
const bool crossedTop = newBottom >= platform.top();
if (next.velocity.y >= 0.0f && horizontallyOverlaps && wasAbove &&
crossedTop) {
next.position.y = platform.top() - next.size.y;
next.velocity.y = 0.0f;
next.grounded = true;
}
}
next.position.x =
std::clamp(next.position.x, world.levelLeft, world.levelRight - next.size.x);
return next;
}
} // namespace ns_game