Base methods in unit

This commit is contained in:
Love 2024-09-11 15:20:50 +02:00
parent de44ead6db
commit 7af25040f9
6 changed files with 29 additions and 10 deletions

View File

@ -14,3 +14,8 @@ void Junk::draw(QGraphicsScene *scene) const {
scene->addEllipse(QRectF(corner.x * UNIT_WIDTH, corner.y * UNIT_HEIGHT,
JUNK_RADIUS, JUNK_RADIUS), QPen(), QBrush(JUNK_COLOR));
}
bool Junk::isAlive() const { return false; }
bool Junk::isToBeJunked() const { return false; }
void Junk::doCrash() {}
void Junk::moveTowards(const Point&) const {}

View File

@ -16,7 +16,12 @@ public:
/*
* Draws this junk onto the given QGraphicsScene.
*/
void draw(QGraphicsScene* scene) const;
void draw(QGraphicsScene *scene) const override;
bool isAlive() const override;
bool isToBeJunked() const override;
void doCrash() override;
void moveTowards(const Point&) const;
};
#endif // JUNK_H

View File

@ -10,13 +10,12 @@ bool Robot::isAlive() const{
return !toBeJunked;
}
void Robot::doCrash(){
void Robot::doCrash() {
toBeJunked = true;
}
bool Robot::isToBeJunked() const{
bool Robot::isToBeJunked() const {
return toBeJunked;
}

View File

@ -17,23 +17,22 @@ public:
/*
* did not crash yet
*/
bool isAlive() const;
bool isAlive() const override;
/*
* Crashes and remembers it
*/
void doCrash();
void doCrash() override;
/*
* Return whether the robot crashed
*/
bool isToBeJunked() const;
bool isToBeJunked() const override;
/*
* Draws this robot onto the given QGraphicsScene.
*/
void draw(QGraphicsScene* scene) const;
void draw(QGraphicsScene *scene) const override;
};

View File

@ -57,3 +57,9 @@ void Unit::checkBounds() {
if (y < MIN_Y) y = MIN_Y;
if (y > MAX_Y) y = MAX_Y;
}
// Default implementation
bool Unit::isAlive() const { return true; }
bool Unit::isToBeJunked() const { return false; }
void Unit::doCrash() {}
void Unit::draw(QGraphicsScene *) const {}

View File

@ -42,7 +42,7 @@ public:
/*
* Take one step closer to point
*/
void moveTowards(const Point&);
virtual void moveTowards(const Point&);
/*
@ -54,6 +54,11 @@ public:
* Euclidean distance to u
*/
double distanceTo(const Unit& u) const;
virtual bool isAlive() const;
virtual bool isToBeJunked() const;
virtual void doCrash();
virtual void draw(QGraphicsScene *scene) const;
private:
int x; // x position of this unit
int y; // y position of this unit