Skip to content

Commit

Permalink
feat: ui improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadecraft committed Apr 7, 2024
1 parent 2d2649f commit e277e84
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 24 deletions.
19 changes: 12 additions & 7 deletions src/assets/level_1.csv
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ StaticCollider, 19, 25, 6, 2
// Vertical colliders
StaticCollider, 0, 0, 2, 25
StaticCollider, 25, 0, 2, 19
// Extra blocks
StaticCollider, 23, 29, 44, 19
// Main large bottom block
StaticCollider, 23, 29, 80, 19
// Rightmost barrier
StaticCollider, 103, 26, 2, 3
// Ending teleporter
LevelTp, 100, 28
// Spike
Spike, 28, 28
// Static deadly section
StaticDeadly, 33, 28, 5, 2
// Ending teleporter
LevelTp, 55, 28
// Moving Platform
// Moving Platforms
MovingCollider, 8, 32, 5, 2, 300, 20, 32
// Animal
MovingCollider, 40, 24, 5, 2, 300, 40, 15
MovingCollider, 47, 15, 5, 2, 300, 45, 15
// Animals
Animal, bird, 50, 15
Animal, fipa, 55, 15
Animal, fipa, 55, 15
Animal, replicator, 58, 15
2 changes: 1 addition & 1 deletion src/gamestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void GameState::updateLevelNames(std::string newLevelName, std::string newNextLe
nextLevelName = newNextLevelName;
// Add the message
// TODO: use level display names
addUIMessage("Teleported to " + newLevelName, { 255, 255, 255 });
addUIMessage("Teleported to " + newLevelName, { 0, 160, 0 });
}

void GameState::addUIMessage(std::string message, Color color, int fontSize, long durationFrames) {
Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ int main() {
// Render
window.clear(sf::Color(0, 0, 0));
// Game and world
renderer.setCamera(player->getLocx(), player->getLocy(), 0.8);
double cameraZoom = 1.0 - 0.1 * sqrt(pow(player->getVelx(), 2) + 0.3 * pow(player->getVely(), 2));
renderer.setCamera(player->getLocx(), player->getLocy(), 0.8, cameraZoom);
renderer.renderWorld(gameState);
// Update
window.display();
Expand Down
19 changes: 11 additions & 8 deletions src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Renderer::Renderer(sf::RenderWindow& window, AssetHandler& assetHandler)

void Renderer::renderFromData(RenderData data) {
// TODO: implement other types (like images)
double worldScale = zoomFactor * screenPixPerWorld;
if (data.type == RenderType::Rectangle) {
// Rectangle
sf::RectangleShape toRender;
Expand All @@ -21,8 +22,8 @@ void Renderer::renderFromData(RenderData data) {
// Convert from world to screen coordinates
// TODO: render from the center position?
// TODO: do not render if out of screen range
toRender.setSize(sf::Vector2f(data.width * screenPixPerWorld, data.height * screenPixPerWorld));
toRender.setPosition(sf::Vector2f((data.locx - worldCameraCenterX) * screenPixPerWorld + window.getSize().x / 2.0, (data.locy - worldCameraCenterY) * screenPixPerWorld + window.getSize().y / 2.0));
toRender.setSize(sf::Vector2f(data.width * worldScale, data.height * worldScale));
toRender.setPosition(sf::Vector2f((data.locx - worldCameraCenterX) * worldScale + window.getSize().x / 2.0, (data.locy - worldCameraCenterY) * worldScale + window.getSize().y / 2.0));
}
toRender.setFillColor(sf::Color(data.color.r, data.color.g, data.color.b));
window.draw(toRender);
Expand All @@ -36,8 +37,8 @@ void Renderer::renderFromData(RenderData data) {
} else {
// Convert from world to screen coordinates
// TODO: render from the center position?
toRender.setRadius((data.width + data.height) / 4 * screenPixPerWorld);
toRender.setPosition(sf::Vector2f((data.locx - worldCameraCenterX) * screenPixPerWorld + window.getSize().x / 2.0, (data.locy - worldCameraCenterY) * screenPixPerWorld + window.getSize().y / 2.0));
toRender.setRadius((data.width + data.height) / 4 * worldScale);
toRender.setPosition(sf::Vector2f((data.locx - worldCameraCenterX) * worldScale + window.getSize().x / 2.0, (data.locy - worldCameraCenterY) * worldScale + window.getSize().y / 2.0));
}
toRender.setFillColor(sf::Color(data.color.r, data.color.g, data.color.b));
window.draw(toRender);
Expand All @@ -61,8 +62,8 @@ void Renderer::renderFromData(RenderData data) {
tex.create(img.getSize().x, img.getSize().y);
tex.update(img);
toRender.setTexture(tex);
toRender.setPosition(sf::Vector2f((data.locx - worldCameraCenterX) * screenPixPerWorld + window.getSize().x / 2.0, (data.locy - worldCameraCenterY) * screenPixPerWorld + window.getSize().y / 2.0));
toRender.setScale(data.width * screenPixPerWorld / img.getSize().x, data.height * screenPixPerWorld / img.getSize().y);
toRender.setPosition(sf::Vector2f((data.locx - worldCameraCenterX) * worldScale + window.getSize().x / 2.0, (data.locy - worldCameraCenterY) * worldScale + window.getSize().y / 2.0));
toRender.setScale(data.width * worldScale / img.getSize().x, data.height * worldScale / img.getSize().y);
}
window.draw(toRender);
}
Expand All @@ -73,14 +74,16 @@ void Renderer::renderMessage(UIMessage& uiMessage, int locx, int locy) {
text.setFont(assetHandler.getMainFont());
text.setString(uiMessage.message);
text.setCharacterSize(uiMessage.fontSize);
text.setFillColor(sf::Color(uiMessage.color.r, uiMessage.color.g, uiMessage.color.b));
double alpha = std::min(sqrt(uiMessage.durationFramesRemaining) / 12.0, 1.0);
text.setFillColor(sf::Color(uiMessage.color.r, uiMessage.color.g, uiMessage.color.b, alpha * 255));
text.setPosition(locx, locy);
window.draw(text);
}

void Renderer::setCamera(double newWorldCameraCenterX, double newWorldCameraCenterY, double percentEasing) {
void Renderer::setCamera(double newWorldCameraCenterX, double newWorldCameraCenterY, double percentEasing, double zoomFactor) {
worldCameraCenterX = worldCameraCenterX * percentEasing + newWorldCameraCenterX * (1 - percentEasing);
worldCameraCenterY = worldCameraCenterY * percentEasing + newWorldCameraCenterY * (1 - percentEasing);
this->zoomFactor = this->zoomFactor * percentEasing + zoomFactor * (1 - percentEasing);
}

void Renderer::renderWorld(GameState& gameState) {
Expand Down
3 changes: 2 additions & 1 deletion src/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Renderer {
double worldCameraCenterX = 0;
double worldCameraCenterY = 0;
int screenPixPerWorld = 32;
double zoomFactor = 1.0;

public:
// Create the renderer
Expand All @@ -26,7 +27,7 @@ class Renderer {
void renderMessage(UIMessage& message, int locx, int locy);

// Set the camera
void setCamera(double newWorldCameraCenterX, double newWorldCameraCenterY, double percentEasing = 0.0);
void setCamera(double newWorldCameraCenterX, double newWorldCameraCenterY, double percentEasing = 0.0, double zoomFactor = 1.0);

// Render the entire world
void renderWorld(GameState& worldState);
Expand Down
2 changes: 0 additions & 2 deletions src/worldobjects/animal.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ struct AnimalData {
// The data for an animal in the world
class Animal : public WorldObject {
private:
double velx = 0;
double vely = 0;
bool onGround = false;
long frameCount = 0;
std::string texturePrefix;
Expand Down
6 changes: 2 additions & 4 deletions src/worldobjects/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// The data for the player
class Player : public WorldObject {
private:
double velx = 0;
double vely = 0;
bool onGround = false;
int deathCounter = 0;

Expand Down Expand Up @@ -63,7 +61,7 @@ class Player : public WorldObject {
if (!LEVEL_DESIGN_MODE) {
vely += worldState.getGravityStrength();
}
velx *= 0.88;
velx *= 0.90; // Friction (0.88)
vely *= 0.96;
locx += velx;
locy += vely;
Expand All @@ -82,7 +80,7 @@ class Player : public WorldObject {
if (object->hasAttribute(ObjectAttribute::Collision)) {
// X movement pushout
// 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()) {
if (locy + height - 0.2 >= object->getLocy() && locy + 0.2 < object->getLocy() + object->getHeight()) {
// Within the y
if (locx + width > object->getLocx() && locx < object->getLocx() + object->getWidth()) {
// Side wall
Expand Down

0 comments on commit e277e84

Please sign in to comment.