Seaweed.cpp (1711B)
1 #include "Seaweed.h" 2 #include "../core/Aquarium.h" 3 #include "../utils/Random.h" 4 #include "../utils/defs.h" 5 6 Seaweed::Seaweed() 7 : Entity(), 8 height(Random::intInRange(SEAWEED_MIN_HEIGHT, SEAWEED_MAX_HEIGHT)), 9 speed(Random::floatInRange(0.1f, 0.3f)), 10 lifetime(Random::intInRange(SEAWEED_MIN_LIFETIME, SEAWEED_MAX_LIFETIME)) { 11 12 x = Random::intInRange(0, Aquarium::getInstance().getWidth()); 13 y = Aquarium::getInstance().getHeight() - height; 14 15 current_image.resize(height); 16 current_mask.resize(height); 17 generateCurrentFrame(); 18 } 19 20 void Seaweed::update() noexcept { 21 frame += speed; 22 if (frame >= 1.0f) { 23 pattern_flipped = !pattern_flipped; 24 frame -= 1.0f; 25 frame_dirty = true; // mark frame as needing regeneration 26 } 27 --lifetime; 28 } 29 30 const std::vector<std::string> &Seaweed::getImage() const { 31 if (frame_dirty) { 32 generateCurrentFrame(); 33 frame_dirty = false; 34 } 35 return current_image; 36 } 37 38 const std::vector<std::string> &Seaweed::getMask() const { 39 if (frame_dirty) { 40 generateCurrentFrame(); 41 frame_dirty = false; 42 } 43 return current_mask; 44 } 45 46 void Seaweed::generateCurrentFrame() const { 47 for (size_t i = 0; i < height; ++i) { 48 const bool use_left = (i % 2 == 0) ^ pattern_flipped; 49 const char ch = use_left ? PATTERN_LEFT : PATTERN_RIGHT; 50 const int offset = use_left ? 0 : 1; 51 52 current_image[i] = std::string(offset, ' ') + ch; 53 current_mask[i] = std::string(current_image[i].size(), 'g'); 54 } 55 } 56 57 bool Seaweed::shouldBeRemoved() const noexcept { return lifetime == 0; } 58 59 std::unique_ptr<Entity> Seaweed::createReplacement() const { 60 return std::make_unique<Seaweed>(); 61 } 62 63 int Seaweed::getPreferredLayer() const noexcept { return 1; }