Skip to content

Commit

Permalink
fix: local velocity imparting on platform
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadecraft committed Apr 7, 2024
1 parent ac0d8c1 commit 2d2649f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/assethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ sf::Font& AssetHandler::getMainFont() {
std::cout << "ERR: Failed to load 'assets/LiberationSans-Regular.ttf'" << std::endl;
// TODO: handle
}
std::cout << "Main font loaded" << std::endl;
mainFontLoaded = true;
return mainFont;
}
10 changes: 10 additions & 0 deletions src/worldobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ WorldObject::WorldObject() {
locy = 0.0;
width = 1.0;
height = 1.0;
velx = 0.0;
vely = 0.0;
}

UpdateResult WorldObject::update(WorldState& worldState, std::vector<WorldObject*>& objects) {
Expand Down Expand Up @@ -33,6 +35,14 @@ double WorldObject::getHeight() {
return height;
}

double WorldObject::getVelx() {
return velx;
}

double WorldObject::getVely() {
return vely;
}

bool WorldObject::hasAttribute(ObjectAttribute attribute) {
return objectAttributes.find(attribute) != objectAttributes.end();
}
Expand Down
4 changes: 4 additions & 0 deletions src/worldobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class WorldObject {
double locy;
double width;
double height;
double velx;
double vely;
std::set<ObjectAttribute> objectAttributes;

public:
Expand All @@ -47,6 +49,8 @@ class WorldObject {
double getLocy();
double getWidth();
double getHeight();
double getVelx();
double getVely();

bool hasAttribute(ObjectAttribute attribute);

Expand Down
8 changes: 6 additions & 2 deletions src/worldobjects/movingcollider.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ class MovingCollider : public WorldObject {
UpdateResult update(WorldState& worldState, std::vector<WorldObject*>& objects) {
double alpha = sin(2 * 3.14159 * frameCount / periodFrames) * 0.5 + 0.5;
// Interpolate between positions
this->locx = spawnx * (1.0 - alpha) + targetx * (alpha);
this->locy = spawny * (1.0 - alpha) + targety * (alpha);
double locxnew = spawnx * (1.0 - alpha) + targetx * (alpha);
double locynew = spawny * (1.0 - alpha) + targety * (alpha);
this->velx = locxnew - this->locx;
this->vely = locynew - this->locy;
this->locx = locxnew;
this->locy = locynew;
frameCount++;
return UpdateResult::None;
}
Expand Down
13 changes: 12 additions & 1 deletion src/worldobjects/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,18 @@ class Player : public WorldObject {
} else {
// Floor
locy -= vely;
vely = 0;
if (object->getVely() < 0) {
vely = object->getVely(); // Impart the others' velocity
locy += vely;
} else {
vely = 0;
}
if (
object->getVelx() > 0 && object->getVelx() > velx
|| object->getVelx() < 0 && object->getVelx() < velx
) {
locx += object->getVelx();
}
collided = true;
overlapped = true;
// todo: should not be on ground on a bottom corner
Expand Down

0 comments on commit 2d2649f

Please sign in to comment.