/** \brief Handle updates in time of our fish * * This is called before we draw and allows us to * move our fish. We add our speed times the amount * of time that has elapsed. * \param elapsed Time elapsed since the class call */ void CFish::Update(double elapsed) { SetLocation(GetX() + mSpeedX * elapsed, GetY() + mSpeedY * elapsed); // X movement if (mSpeedX > 0 && GetX() + GetWidth()/2 + flipOffset >= GetAquarium()->GetWidth()) { mSpeedX = -mSpeedX; SetMirror(mSpeedX < 0); } else if (mSpeedX < 0 && GetX() - GetWidth()/2 - flipOffset <= 0){ mSpeedX = -mSpeedX; SetMirror(mSpeedX < 0); } // Y movement if (mSpeedY > 0 && GetY() + GetHeight() / 2 + heightOffset >= GetAquarium()->GetHeight()) { mSpeedY = -mSpeedY; } else if (mSpeedY < 0 && GetY() - GetHeight() / 2 - heightOffset <= 0){ mSpeedY = -mSpeedY; } }
void CFish::BreedingUpdate(double elapsed) { CAquarium *aquarium = GetAquarium(); //fish logic for breeding if (mCanBreed == true && mAge >= MaxAge) { if (mInterestTime >= InterestTimePreBreed) { mIsInterested = true; } else if (mIsGestating == false) mInterestTime++; if (mIsGestating == true) mGestatingTime++; if (mIsInterested == true) { CBreedVisitor visitor(mIsMale, GetType()); aquarium->Accept(&visitor); if (visitor.IsInterested()) { // Compute a vector from (x1,y1) to (x2,y2) double dx = visitor.GetX() - GetX(); double dy = visitor.GetY() - GetY(); // How long is that vector? double len = sqrt(dx * dx + dy * dy); if (len > 0) { // Normalize the vector dx /= len; dy /= len; } // Update the location if (GetType() == "beta") { mSpeedX = dx * (MaxSpeedX - BetaSpeed) * BreedMultuplier * elapsed; mSpeedY = dy * (MaxSpeedY - BetaSpeed) * BreedMultuplier * elapsed; } if (GetType() == "bubbles") { mSpeedX = dx * (MaxSpeedX - BubblesSpeed) * BreedMultuplier * elapsed; mSpeedY = dy * (MaxSpeedY - BubblesSpeed) * BreedMultuplier * elapsed; } if (GetType() == "dory") { mSpeedX = dx * (DorySpeed - MaxSpeedX) * BreedMultuplier * elapsed; mSpeedY = dy * (DorySpeed - MaxSpeedY) * BreedMultuplier * elapsed; } // check if the visited fish overlaps this one if (OverlapTest(visitor.GetX(), visitor.GetY())) { if (mIsMale == 0) { mIsGestating = true; } else { visitor.SetGestating(); } mIsInterested = false; mInterestTime = 0; visitor.SetUninterested(); if (GetType() == "beta") { mSpeedX = ((double)rand() / RAND_MAX) * (MaxSpeedX - BetaSpeed); mSpeedY = ((double)rand() / RAND_MAX) * (MaxSpeedY - BetaSpeed); } else if (GetType() == "bubbles") { mSpeedX = ((double)rand() / RAND_MAX) * (MaxSpeedX - BubblesSpeed); mSpeedY = ((double)rand() / RAND_MAX) * (MaxSpeedY - BubblesSpeed); } else if (GetType() == "dory") { mSpeedX = ((double)rand() / RAND_MAX) * (DorySpeed- MaxSpeedX); mSpeedY = ((double)rand() / RAND_MAX) * (DorySpeed- MaxSpeedY); } } } } if (mGestatingTime >= GestatingTime) { mIsGestating = false; mGestatingTime = 0; if (GetType() == "beta") { auto fish = make_shared<CFishBeta>(aquarium); fish->SetLocation(GetX(), GetY()); aquarium->AddTempItem(fish); } if (GetType() == "bubbles") { auto fish = make_shared<CFishBubbles>(aquarium); fish->SetLocation(GetX(), GetY()); aquarium->AddTempItem(fish); } if (GetType() == "dory") { auto fish = make_shared<CFishDory>(aquarium); fish->SetLocation(GetX(), GetY()); aquarium->AddTempItem(fish); } } if (mSpeedX < 0) SetMirror(true); if (mSpeedX > 0) SetMirror(false); } }