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