Fishtank
fish.cpp
Go to the documentation of this file.
1 #include "fish.h"
2 #include "screen.h"
3 
4 #include <stdexcept>
5 
6 using std::string;
7 using std::vector;
8 
9 Fish::Fish(Shape shape, int col, int row, int horizontal_speed,
10  int vertical_speed)
11  : shape_(shape), col_(col), row_(row), horizontal_speed_(horizontal_speed),
12  vertical_speed_(vertical_speed) {
13 }
14 
15 int Fish::col() const { return col_; }
16 
17 int Fish::row() const { return row_; }
18 
19 int Fish::width() const { return shape_.width(); }
20 
21 int Fish::height() const { return shape_.height(); }
22 
23 void Fish::tick() {
24  // TODO(max): This will eventually overflow
25  col_ += horizontal_speed_;
26  row_ += vertical_speed_;
27 }
28 
29 static int wrap(int value, int max) {
30  // Can still be negative
31  int clamped = value % max;
32 
33  // Shift into positive range if needed.
34  return clamped >= 0 ? clamped : clamped + max;
35 }
36 
37 // TODO(max): Add some notion of transparency?
38 void Fish::draw(Screen *screen) const {
39  for (int row = 0; row < height(); row++) {
40  for (int col = 0; col < width(); col++) {
41  screen->charAtPut(wrap(this->col() + col, screen->width()),
42  wrap(this->row() + row, screen->height()),
43  shape_.charAt(col, row));
44  }
45  }
46 }
Fish(Shape shape, int col, int row, int horizontal_speed, int vertical_speed)
Definition: fish.cpp:9
int height() const
Definition: fish.cpp:21
int col() const
Definition: fish.cpp:15
void draw(Screen *screen) const
Render to the given Screen. Draws over whatever is already there with no concept of transparency.
Definition: fish.cpp:38
void tick()
Update col using horizontal_speed. Update row using vertical_speed.
Definition: fish.cpp:23
int row() const
Definition: fish.cpp:17
int width() const
Definition: fish.cpp:19
Represents the abstract notion of a character-addressable writable screen.
Definition: screen.h:4
int width() const
Definition: screen.cpp:31
virtual void charAtPut(int col, int row, char value)=0
int height() const
Definition: screen.cpp:33
Represents an ASCII-art drawing and its corresponding bounding box.
Definition: shape.h:7
char charAt(int col, int row) const
Fetch the character at the given coordinate pair.
Definition: shape.cpp:33
int width() const
Definition: shape.cpp:29
int height() const
Definition: shape.cpp:31