add ship, improve inheritance
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include "defs.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <ncurses.h>
|
||||
|
||||
int g_maxCells = 0;
|
||||
@@ -72,6 +73,7 @@ void Aquarium::resize() {
|
||||
|
||||
addWaterline();
|
||||
addCastle();
|
||||
addShip();
|
||||
for (int i = 0; i < width / 15; i++)
|
||||
addSeaweed();
|
||||
for (int i = 0; i < width * (height - 9) / 350; i++)
|
||||
@@ -112,10 +114,13 @@ void Aquarium::redraw() {
|
||||
}
|
||||
|
||||
int baseFishLayer = 10;
|
||||
for (auto it = fishes.begin(); it != fishes.end();) { // use an iterator
|
||||
for (auto it = fishes.begin(); it != fishes.end();) {
|
||||
auto &fish = *it;
|
||||
fish->draw(baseFishLayer +
|
||||
|
||||
static_cast<const Entity *>(fish.get())
|
||||
->draw(baseFishLayer +
|
||||
static_cast<int>(std::distance(fishes.begin(), it)));
|
||||
|
||||
fish->update();
|
||||
|
||||
float fx = fish->getX();
|
||||
@@ -123,17 +128,22 @@ void Aquarium::redraw() {
|
||||
addBubble(fx, fish->getY());
|
||||
}
|
||||
|
||||
if (fx > width || fx < -30) {
|
||||
it = fishes.erase(it); // erase and update iterator
|
||||
if (fish->isOffScreen()) {
|
||||
it = fishes.erase(it);
|
||||
addFish();
|
||||
} else {
|
||||
++it; // only increment if not erasing
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
waterline->draw();
|
||||
waterline->update();
|
||||
|
||||
static_cast<const Entity *>(ship.get())->draw(9);
|
||||
if (ship->isOffScreen())
|
||||
addShip();
|
||||
ship->update();
|
||||
|
||||
applyBackBuffer();
|
||||
}
|
||||
|
||||
@@ -151,6 +161,8 @@ void Aquarium::addCastle() { castle = std::make_unique<Castle>(); }
|
||||
|
||||
void Aquarium::addFish() { fishes.emplace_back(std::make_unique<Fish>()); }
|
||||
|
||||
void Aquarium::addShip() { ship = std::make_unique<Ship>(); }
|
||||
|
||||
void Aquarium::clearBackBuffer() {
|
||||
for (auto &row : backBuffer)
|
||||
std::fill(row.begin(), row.end(), Cell());
|
||||
|
||||
Reference in New Issue
Block a user