Whale.cpp (1321B)
1 #include "Whale.h" 2 #include "../assets/WhaleAssets.h" 3 #include "../core/Aquarium.h" 4 #include "../utils/Random.h" 5 6 Whale::Whale() : Whale(getRandomDirection()) {} 7 8 Whale::Whale(int asset_index) 9 : Entity((asset_index == 0)), frames(getWhaleAssets()[asset_index].frames), 10 mask(getWhaleAssets()[asset_index].mask), speed(WHALE_SPEED) { 11 12 const auto &aquarium = Aquarium::getInstance(); 13 y = 0; 14 15 // Use first frame for positioning calculations 16 const auto &first_frame = frames[0]; 17 if (moving_right) { 18 x = -static_cast<float>(first_frame[6].length()); 19 } else { 20 x = static_cast<float>(aquarium.getWidth()); 21 } 22 23 current_image = first_frame; 24 } 25 26 int Whale::getRandomDirection() { return Random::intInRange(0, 1); } 27 28 void Whale::update() noexcept { 29 x += moving_right ? speed : -speed; 30 31 ++animation_counter; 32 33 // Use longer delay for first frame (no water spout) 34 int current_delay = 35 (current_frame_index == 0) ? FIRST_FRAME_PAUSE : ANIMATION_DELAY; 36 37 if (animation_counter >= current_delay) { 38 current_frame_index = (current_frame_index + 1) % frames.size(); 39 animation_counter = 0; 40 } 41 } 42 43 const std::vector<std::string> &Whale::getImage() const { 44 current_image = frames[current_frame_index]; 45 return current_image; 46 } 47 48 int Whale::getPreferredLayer() const noexcept { return 8; }