Skip to content

Commit

Permalink
fix: bottom corner collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadecraft committed Apr 3, 2024
1 parent d18e6ff commit 6eb86ab
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// Entry point
int main() {
auto window = sf::RenderWindow{ { 1920u, 1080u }, "PlatScifi" };
// TODO: better framerate limit ?
window.setFramerateLimit(144);

// TODO: build not working on Linux?
Expand Down Expand Up @@ -84,22 +85,24 @@ int main() {
keysPressed.erase(sf::Keyboard::P);
}

// Update the game and world
if (LEVEL_DESIGN_MODE) {
// Level Design Mode: keep reloading the world without updating it
worldSpawner.spawnWorld(gameState, gameState.getLevelName());
}
// Update the game and world
UpdateResult updateResult = gameState.update();
if (updateResult == UpdateResult::NextLevel) {
// Load the next level, if possible
// TODO: impl better
worldSpawner.spawnWorld(gameState, gameState.getNextLevelName());
}

// Render
window.clear(sf::Color(0, 0, 0));
// Render the game and world
window.clear(sf::Color(111, 201, 252));
renderer.setCamera(player->getLocx(), player->getLocy(), 0.8);
renderer.renderWorldObjects(gameState);
renderer.renderWorld(gameState);
// Update
window.display();
}
}
8 changes: 6 additions & 2 deletions src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ void Renderer::setCamera(double newWorldCameraCenterX, double newWorldCameraCent
worldCameraCenterY = worldCameraCenterY * percentEasing + newWorldCameraCenterY * (1 - percentEasing);
}

void Renderer::renderWorldObjects(GameState& gameState) {
// todo: fix on mac/linux
void Renderer::renderWorld(GameState& gameState) {
// Clear/render sky
window.clear(sf::Color(111, 201, 252));
// Render world objects
for (std::vector<WorldObject*>::iterator it = gameState.objectsBegin(); it != gameState.objectsEnd(); it++) {
renderFromData((*it)->getRenderData());
}
// TODO: Render UI

}
2 changes: 1 addition & 1 deletion src/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ class Renderer {
void setCamera(double newWorldCameraCenterX, double newWorldCameraCenterY, double percentEasing = 0.0);

// Render the entire world
void renderWorldObjects(GameState& worldState);
void renderWorld(GameState& worldState);
};
29 changes: 21 additions & 8 deletions src/worldobjects/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Player : public WorldObject {
bool overlapped = false;
if (object->hasAttribute(ObjectAttribute::Collision)) {
// X movement pushout
// todo: better way of doing the 0.1 thing
// todo: better way of doing the 0.1 thing (subtract velocity instead?)
if (locy + height - 0.1 >= object->getLocy() && locy + 0.1 < object->getLocy() + object->getHeight()) {
// Within the y
if (locx + width > object->getLocx() && locx < object->getLocx() + object->getWidth()) {
Expand All @@ -81,13 +81,26 @@ class Player : public WorldObject {
if (locx + width >= object->getLocx() && locx < object->getLocx() + object->getWidth()) {
// Within the x
if (locy + height > object->getLocy() && locy < object->getLocy() + object->getHeight()) {
// Floor/ceiling
locy -= vely;
vely = 0;
collided = true;
overlapped = true;
// todo: should not be on ground on a bottom corner
onGround = true;
if (locy > object->getLocy() + object->getHeight() / 2.0) {
// Ceiling
if (vely >= 0) {
// Already traveling down: do nothing
} else {
// Traveling up: stop
locy -= vely;
vely = 0;
}
collided = true;
overlapped = true;
} else {
// Floor
locy -= vely;
vely = 0;
collided = true;
overlapped = true;
// todo: should not be on ground on a bottom corner
onGround = true;
}
}
}
}
Expand Down

0 comments on commit 6eb86ab

Please sign in to comment.