fissh

termios terminal aquarium. demo at ssh://fish@kloet.net
Download | Log | Files | Refs

SeaMonster.cpp (1265B)


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