From 8713d03831226dcd559c6a1e2b1c7b0d7c660bac Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Wed, 16 Mar 2022 11:53:46 -0400 Subject: added signs (which can't be read yet) --- src/animation.h | 2 ++ src/direction.h | 12 ++++++++++ src/game.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/game.h | 14 +++++++++++- src/map.h | 5 ++-- src/renderer.cpp | 22 ++++++++++++++---- src/renderer.h | 1 + 7 files changed, 116 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/animation.h b/src/animation.h index 0108c12..f1eddb6 100644 --- a/src/animation.h +++ b/src/animation.h @@ -20,6 +20,8 @@ public: return frames_.at(animations_.at(animationId_).at(animationFrame_)); } + Direction getDirection() const { return dir_; } + void update(int dt); private: diff --git a/src/direction.h b/src/direction.h index 0fe3e5a..14ea0a5 100644 --- a/src/direction.h +++ b/src/direction.h @@ -3,6 +3,9 @@ #include #include +#include + +using coord = std::tuple; enum class Direction { up, @@ -19,4 +22,13 @@ inline Direction directionFromString(std::string_view str) { throw std::invalid_argument("Invalid direction: " + std::string(str)); } +inline coord coordInDirection(int x, int y, Direction dir) { + switch (dir) { + case Direction::up: return {x, y-1}; + case Direction::down: return {x, y+1}; + case Direction::left: return {x-1, y}; + case Direction::right: return {x+1, y}; + } +} + #endif /* end of include guard: DIRECTION_H_42BDAFB9 */ diff --git a/src/game.cpp b/src/game.cpp index 170f584..301447f 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "util.h" #include "renderer.h" #include "consts.h" @@ -23,6 +24,12 @@ Game::Game(std::mt19937& rng, Muxer& muxer) : } tick(); + + std::ifstream textFile("../res/childoflight.txt"); + std::string line; + while (std::getline(textFile, line)) { + signTexts.push_back(line); + } } inline bool isTileSetOrNotLit(const Map& map, int x, int y) @@ -119,7 +126,7 @@ void Game::tick(bool onlyDark) bool Game::movePlayer(int x, int y) { - if (map.at(x,y).tile == Tile::Floor) + if (map.at(x,y).tile == Tile::Floor && !map.at(x,y).sign) { if (map.at(player_x, player_y).tile == Tile::Floor) { @@ -258,16 +265,36 @@ void Game::recalculateRender() { map.at(x,y).dirtyRender = false; if (map.at(x,y).tile == Tile::Floor) { - if (std::bernoulli_distribution(0.05)(rng)) { + int renderDesc = 0; + if (isTileSetOrNotLit(map, x-1, y-1)) renderDesc |= (1 << 7); + if (isTileSetOrNotLit(map, x , y-1)) renderDesc |= (1 << 6); + if (isTileSetOrNotLit(map, x+1, y-1)) renderDesc |= (1 << 5); + if (isTileSetOrNotLit(map, x+1, y )) renderDesc |= (1 << 4); + if (isTileSetOrNotLit(map, x+1, y+1)) renderDesc |= (1 << 3); + if (isTileSetOrNotLit(map, x , y+1)) renderDesc |= (1 << 2); + if (isTileSetOrNotLit(map, x-1, y+1)) renderDesc |= (1 << 1); + if (isTileSetOrNotLit(map, x-1, y )) renderDesc |= (1 << 0); + + if (renderDesc == 0 && map.at(x,y).sign) { + map.at(x,y).renderId = TilesetIndex(24, 13); + } else if (std::bernoulli_distribution(0.05)(rng)) { static const std::vector furnishings { TilesetIndex(20, 16), TilesetIndex(21, 2), TilesetIndex(22, 2), TilesetIndex(21, 3), TilesetIndex(22, 3)}; - map.at(x,y).renderId = furnishings.at(std::uniform_int_distribution(0, furnishings.size()-1)(rng)); + + if (renderDesc == 0 && !(x == player_x && y == player_y) && std::bernoulli_distribution(0.2)(rng)) { + map.at(x,y).renderId = TilesetIndex(24, 13); + map.at(x,y).sign = true; + } else { + map.at(x,y).renderId = furnishings.at(std::uniform_int_distribution(0, furnishings.size()-1)(rng)); + map.at(x,y).sign = false; + } } else { map.at(x,y).renderId = -1; + map.at(x,y).sign = false; } } else if (map.at(x,y).tile == Tile::Wall) { static bool initWalls = false; @@ -785,6 +812,43 @@ void Game::update(size_t frameTime) { } } + switch (signInstructionState) { + case SignInstructionState::Hidden: { + auto [lookX, lookY] = coordInDirection(player_x, player_y, playerAnim.getDirection()); + if (map.at(lookX, lookY).sign) { + signInstructionState = SignInstructionState::FadingIn; + signFade.start(1000); + } + + break; + } + case SignInstructionState::FadingIn: { + signFade.tick(frameTime); + if (signFade.isComplete()) { + signInstructionState = SignInstructionState::Visible; + } + + break; + } + case SignInstructionState::Visible: { + auto [lookX, lookY] = coordInDirection(player_x, player_y, playerAnim.getDirection()); + if (!map.at(lookX, lookY).sign || losing != LoseState::None) { + signInstructionState = SignInstructionState::FadingOut; + signFade.start(1000); + } + + break; + } + case SignInstructionState::FadingOut: { + signFade.tick(frameTime); + if (signFade.isComplete()) { + signInstructionState = SignInstructionState::Hidden; + } + + break; + } + } + if (dirtyLighting) { recalculateLighting(); diff --git a/src/game.h b/src/game.h index f307156..71685e6 100644 --- a/src/game.h +++ b/src/game.h @@ -13,7 +13,7 @@ #include "consts.h" constexpr int TilesetIndex(int x, int y) { - return x + y * 24; + return x + y * 25; } enum class LoseState { @@ -23,6 +23,13 @@ enum class LoseState { Outro }; +enum class SignInstructionState { + Hidden, + FadingIn, + Visible, + FadingOut +}; + struct Input { bool left = false; bool right = false; @@ -100,6 +107,11 @@ public: Timer losePopPlayerTimer = {3000}; //Timer zoomTimer = {62}; + std::vector signTexts; + int nextSignIndex = 0; + SignInstructionState signInstructionState = SignInstructionState::Hidden; + Interpolation signFade; + private: void tick( diff --git a/src/map.h b/src/map.h index 2faac85..de74b14 100644 --- a/src/map.h +++ b/src/map.h @@ -8,8 +8,7 @@ #include #include #include "consts.h" - -using coord = std::tuple; +#include "direction.h" enum class Tile { Floor, @@ -35,6 +34,8 @@ struct MapData { std::set litTiles; int renderId = -1; bool dirtyRender = true; + bool sign = false; + std::string text; }; struct Chunk { diff --git a/src/renderer.cpp b/src/renderer.cpp index befad8a..2be36ae 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -109,6 +109,7 @@ Renderer::Renderer() loadTextureFromFile("../res/player.png", playerSheet_); loadTextureFromFile("../res/runninbloods.png", tileset_); loadTextureFromFile("../res/lamp.png", lamp_); + loadTextureFromFile("../res/read_instruction.png", readInstruction_); loadTextureFromFile("../res/title0.png", titles_[0]); SDL_QueryTexture(titles_[0].get(), nullptr, nullptr, &titleWidths_[0], &titleHeights_[0]); @@ -171,14 +172,14 @@ void Renderer::renderGame( SDL_RenderCopy(ren_.get(), tileset_.get(), &tileRect, &rect); if (game.map.at(x,y).renderId != -1) { - tileRect.x = game.map.at(x,y).renderId % 24 * 16; - tileRect.y = game.map.at(x,y).renderId / 24 * 16; + tileRect.x = game.map.at(x,y).renderId % 25 * 16; + tileRect.y = game.map.at(x,y).renderId / 25 * 16; SDL_RenderCopy(ren_.get(), tileset_.get(), &tileRect, &rect); } } else { SDL_Rect tileRect { - game.map.at(x,y).renderId % 24 * 16, - game.map.at(x,y).renderId / 24 * 16, + game.map.at(x,y).renderId % 25 * 16, + game.map.at(x,y).renderId / 25 * 16, 16, 16}; @@ -375,6 +376,19 @@ void Renderer::renderGame( } SDL_RenderCopy(ren_.get(), canvas.get(), &zoomRect, nullptr); + + if (game.signInstructionState != SignInstructionState::Hidden) { + int instOpacity = 255; + if (game.signInstructionState == SignInstructionState::FadingIn) { + instOpacity = game.signFade.getProgress(0, 255); + } else if (game.signInstructionState == SignInstructionState::FadingOut) { + instOpacity = game.signFade.getProgress(255, 0); + } + + SDL_SetTextureAlphaMod(readInstruction_.get(), instOpacity); + SDL_RenderCopy(ren_.get(), readInstruction_.get(), nullptr, nullptr); + } + SDL_RenderPresent(ren_.get()); } diff --git a/src/renderer.h b/src/renderer.h index 1364550..de1e125 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -137,6 +137,7 @@ private: texture_ptr playerSheet_; texture_ptr tileset_; texture_ptr lamp_; + texture_ptr readInstruction_; std::array titles_; std::array titleWidths_; -- cgit 1.4.1