Compare commits
3 Commits
polymorphi
...
4e7fcd8705
Author | SHA1 | Date | |
---|---|---|---|
4e7fcd8705 | |||
dd61f06a18 | |||
c9be0f2d2f |
@ -22,11 +22,48 @@ GameState::GameState(int numberOfRobots) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
GameState::~GameState(){
|
GameState::~GameState(){
|
||||||
|
clearUnits();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameState::clearUnits(){
|
||||||
for (Unit *unit: units)
|
for (Unit *unit: units)
|
||||||
delete unit;
|
delete unit;
|
||||||
units.clear();
|
units.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GameState &GameState::operator=(const GameState &other) {
|
||||||
|
if (this == &other)
|
||||||
|
return *this;
|
||||||
|
|
||||||
|
clearUnits();
|
||||||
|
for (const Unit *const unit : other.units)
|
||||||
|
units.push_back(unit->clone());
|
||||||
|
hero = other.hero;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
GameState::GameState(const GameState &other) : hero(other.hero) {
|
||||||
|
clearUnits();
|
||||||
|
for (const Unit *const unit : other.units)
|
||||||
|
units.push_back(unit->clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
GameState &GameState::operator=(GameState &&other) noexcept {
|
||||||
|
if (this == &other)
|
||||||
|
return *this;
|
||||||
|
clearUnits();
|
||||||
|
|
||||||
|
hero = std::move(other.hero);
|
||||||
|
units = std::move(other.units);
|
||||||
|
other.units.clear();
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
GameState::GameState(GameState &&other) noexcept : units(std::move(other.units)), hero(std::move(other.hero)) {
|
||||||
|
other.units.clear();
|
||||||
|
}
|
||||||
|
|
||||||
void GameState::draw(QGraphicsScene *scene) const {
|
void GameState::draw(QGraphicsScene *scene) const {
|
||||||
scene->clear();
|
scene->clear();
|
||||||
hero.draw(scene);
|
hero.draw(scene);
|
||||||
|
@ -27,6 +27,13 @@ public:
|
|||||||
|
|
||||||
~GameState();
|
~GameState();
|
||||||
|
|
||||||
|
// Copy
|
||||||
|
GameState &operator=(const GameState &other);
|
||||||
|
GameState(const GameState &other);
|
||||||
|
|
||||||
|
// Move
|
||||||
|
GameState &operator=(GameState &&other) noexcept;
|
||||||
|
GameState(GameState &&other) noexcept;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Clear and redraw entire playing field
|
* Clear and redraw entire playing field
|
||||||
@ -84,6 +91,7 @@ private:
|
|||||||
|
|
||||||
// private helpers
|
// private helpers
|
||||||
bool isEmpty(const Unit& unit) const;
|
bool isEmpty(const Unit& unit) const;
|
||||||
|
void clearUnits();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -14,3 +14,10 @@ void Hero::draw(QGraphicsScene *scene) const {
|
|||||||
UNIT_WIDTH, UNIT_HEIGHT), Qt::NoPen, QBrush(HERO_COLOR));
|
UNIT_WIDTH, UNIT_HEIGHT), Qt::NoPen, QBrush(HERO_COLOR));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Unit *Hero::clone() const {
|
||||||
|
return new Hero(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Hero::isAlive() const { return true; }
|
||||||
|
bool Hero::isToBeJunked() const { return false; }
|
||||||
|
void Hero::doCrash() {}
|
||||||
|
11
src/Hero.h
11
src/Hero.h
@ -12,12 +12,15 @@
|
|||||||
class Hero : public Unit {
|
class Hero : public Unit {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Unit *clone() const override;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Draws this hero onto the given QGraphicsScene.
|
* Draws this hero 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;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // HERO_H
|
#endif // HERO_H
|
||||||
|
@ -19,3 +19,7 @@ bool Junk::isAlive() const { return false; }
|
|||||||
bool Junk::isToBeJunked() const { return false; }
|
bool Junk::isToBeJunked() const { return false; }
|
||||||
void Junk::doCrash() {}
|
void Junk::doCrash() {}
|
||||||
void Junk::moveTowards(const Point &) {}
|
void Junk::moveTowards(const Point &) {}
|
||||||
|
|
||||||
|
Unit *Junk::clone() const {
|
||||||
|
return new Junk(*this);
|
||||||
|
}
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
class Junk : public Unit {
|
class Junk : public Unit {
|
||||||
public:
|
public:
|
||||||
Junk(const Point& p): Unit(p){}
|
Junk(const Point& p): Unit(p){}
|
||||||
|
Unit *clone() const override;
|
||||||
/*
|
/*
|
||||||
* Draws this junk onto the given QGraphicsScene.
|
* Draws this junk onto the given QGraphicsScene.
|
||||||
*/
|
*/
|
||||||
|
@ -25,4 +25,6 @@ void Robot::draw(QGraphicsScene *scene) const {
|
|||||||
JUNK_RADIUS, JUNK_RADIUS), QPen(), QBrush(ROBOT_COLOR));
|
JUNK_RADIUS, JUNK_RADIUS), QPen(), QBrush(ROBOT_COLOR));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Unit *Robot::clone() const{
|
||||||
|
return new Robot(*this);
|
||||||
|
}
|
||||||
|
@ -13,7 +13,7 @@ class Robot : public Unit {
|
|||||||
bool toBeJunked = false;
|
bool toBeJunked = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Unit* clone() const override;
|
||||||
/*
|
/*
|
||||||
* did not crash yet
|
* did not crash yet
|
||||||
*/
|
*/
|
||||||
|
@ -59,7 +59,4 @@ void Unit::checkBounds() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Default implementation
|
// Default implementation
|
||||||
bool Unit::isAlive() const { return true; }
|
|
||||||
bool Unit::isToBeJunked() const { return false; }
|
|
||||||
void Unit::doCrash() {}
|
|
||||||
void Unit::draw(QGraphicsScene *) const {}
|
|
||||||
|
10
src/Unit.h
10
src/Unit.h
@ -28,6 +28,8 @@ public:
|
|||||||
|
|
||||||
virtual ~Unit(){}
|
virtual ~Unit(){}
|
||||||
|
|
||||||
|
virtual Unit *clone() const = 0;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return Point representation of Unit
|
* Return Point representation of Unit
|
||||||
*/
|
*/
|
||||||
@ -55,10 +57,10 @@ public:
|
|||||||
*/
|
*/
|
||||||
double distanceTo(const Unit& u) const;
|
double distanceTo(const Unit& u) const;
|
||||||
|
|
||||||
virtual bool isAlive() const;
|
virtual bool isAlive() const = 0;
|
||||||
virtual bool isToBeJunked() const;
|
virtual bool isToBeJunked() const = 0;
|
||||||
virtual void doCrash();
|
virtual void doCrash() = 0;
|
||||||
virtual void draw(QGraphicsScene *scene) const;
|
virtual void draw(QGraphicsScene *scene) const = 0;
|
||||||
private:
|
private:
|
||||||
int x; // x position of this unit
|
int x; // x position of this unit
|
||||||
int y; // y position of this unit
|
int y; // y position of this unit
|
||||||
|
Reference in New Issue
Block a user