From b4c3742ca914719d5dc49ee37ab23dd68e5707a8 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Wed, 11 Sep 2024 15:20:50 +0200 Subject: [PATCH] Base methods in unit --- src/Junk.cpp | 5 +++++ src/Junk.h | 7 ++++++- src/Robot.cpp | 5 ++--- src/Robot.h | 9 ++++----- src/Unit.cpp | 6 ++++++ src/Unit.h | 7 ++++++- 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/Junk.cpp b/src/Junk.cpp index 3db0369..634b33a 100755 --- a/src/Junk.cpp +++ b/src/Junk.cpp @@ -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 {} diff --git a/src/Junk.h b/src/Junk.h index d866d9e..716acc6 100755 --- a/src/Junk.h +++ b/src/Junk.h @@ -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 diff --git a/src/Robot.cpp b/src/Robot.cpp index f9892a4..0b76830 100755 --- a/src/Robot.cpp +++ b/src/Robot.cpp @@ -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; - } diff --git a/src/Robot.h b/src/Robot.h index d4fed99..2ce8b63 100755 --- a/src/Robot.h +++ b/src/Robot.h @@ -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; }; diff --git a/src/Unit.cpp b/src/Unit.cpp index 61d85dd..8893497 100755 --- a/src/Unit.cpp +++ b/src/Unit.cpp @@ -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 {} diff --git a/src/Unit.h b/src/Unit.h index 49e5bae..ce53e82 100755 --- a/src/Unit.h +++ b/src/Unit.h @@ -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