From e277e840ed7df8aab0e3efea49d12d0aee7d4a8d Mon Sep 17 00:00:00 2001 From: Cadecraft Date: Sun, 7 Apr 2024 14:46:15 -0700 Subject: [PATCH] feat: ui improve --- src/assets/level_1.csv | 19 ++++++++++++------- src/gamestate.cpp | 2 +- src/main.cpp | 3 ++- src/renderer.cpp | 19 +++++++++++-------- src/renderer.h | 3 ++- src/worldobjects/animal.h | 2 -- src/worldobjects/player.h | 6 ++---- 7 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/assets/level_1.csv b/src/assets/level_1.csv index 64c3688..6551ae0 100644 --- a/src/assets/level_1.csv +++ b/src/assets/level_1.csv @@ -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 \ No newline at end of file +Animal, fipa, 55, 15 +Animal, replicator, 58, 15 \ No newline at end of file diff --git a/src/gamestate.cpp b/src/gamestate.cpp index f875550..3ef0594 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -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) { diff --git a/src/main.cpp b/src/main.cpp index 3946175..be2b970 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); diff --git a/src/renderer.cpp b/src/renderer.cpp index 4331e52..27e4f77 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -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; @@ -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); @@ -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); @@ -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); } @@ -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) { diff --git a/src/renderer.h b/src/renderer.h index c92e315..5e79238 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -14,6 +14,7 @@ class Renderer { double worldCameraCenterX = 0; double worldCameraCenterY = 0; int screenPixPerWorld = 32; + double zoomFactor = 1.0; public: // Create the renderer @@ -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); diff --git a/src/worldobjects/animal.h b/src/worldobjects/animal.h index ecfce65..b40fab4 100644 --- a/src/worldobjects/animal.h +++ b/src/worldobjects/animal.h @@ -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; diff --git a/src/worldobjects/player.h b/src/worldobjects/player.h index 57673e8..4f39a6c 100644 --- a/src/worldobjects/player.h +++ b/src/worldobjects/player.h @@ -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; @@ -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; @@ -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