Add game framework and whitebox level

This commit is contained in:
2026-06-08 23:47:32 +08:00
parent 5525343656
commit e52cf94336
24 changed files with 778 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
#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
+27
View File
@@ -0,0 +1,27 @@
#pragma once
#include <frostbite2D/types/type_math.h>
#include <vector>
namespace ns_game {
struct PlatformWorld {
float levelLeft = 0.0f;
float levelRight = 1280.0f;
std::vector<frostbite2D::Rect> platforms;
};
struct MovementBody {
frostbite2D::Vec2 position;
frostbite2D::Vec2 size;
frostbite2D::Vec2 velocity;
bool grounded = false;
};
class PlatformMover {
public:
MovementBody Step(const MovementBody& body, const PlatformWorld& world,
float gravity, float deltaTime) const;
};
} // namespace ns_game