From 03df2e208f439f1607e7e69c4a39220e070b2539 Mon Sep 17 00:00:00 2001 From: cystom Date: Sun, 13 Feb 2022 20:25:30 +0800 Subject: [PATCH] Formatting with Google C++ Style --- README.md | 6 +- include/bullet/bullet.h | 38 ++-- include/bullet/gunbullet.h | 24 ++- include/bullet/laser.h | 30 +-- include/bullet/missile.h | 42 ++-- include/common.h | 81 ++++--- include/enemy/blackwarrior.h | 17 +- include/enemy/cowardplane.h | 19 +- include/enemy/dragon.h | 21 +- include/enemy/enemy.h | 91 ++++---- include/enemy/fallenwarrior.h | 22 +- include/enemy/ghost.h | 18 +- include/enemy/robotsoldier.h | 26 +-- include/enemy/shaman.h | 22 +- include/enemy/spirit.h | 17 +- include/game.h | 86 ++++---- include/gameitem.h | 90 ++++---- include/gamemap.h | 62 ++++++ include/mainwindow.h | 39 ++-- include/map.h | 56 ----- include/shop.h | 38 ++-- include/statistic.h | 40 ++-- include/tower/bomb.h | 26 +-- include/tower/campfire.h | 27 +-- include/tower/guntower.h | 21 +- include/tower/infopanel.h | 28 +-- include/tower/lasertower.h | 34 +-- include/tower/missiletower.h | 20 +- include/tower/repeller.h | 17 +- include/tower/sawtooth.h | 22 +- include/tower/shield.h | 20 +- include/tower/tower.h | 72 +++---- src/bullet/bullet.cpp | 42 ++-- src/bullet/gunbullet.cpp | 74 +++---- src/bullet/laser.cpp | 76 +++---- src/bullet/missile.cpp | 156 +++++++------- src/enemy/blackwarrior.cpp | 33 ++- src/enemy/cowardplane.cpp | 106 +++++---- src/enemy/dragon.cpp | 50 ++--- src/enemy/enemy.cpp | 293 +++++++++++++------------ src/enemy/fallenwarrior.cpp | 187 ++++++++-------- src/enemy/ghost.cpp | 33 ++- src/enemy/robotsoldier.cpp | 155 +++++++------ src/enemy/shaman.cpp | 123 +++++------ src/enemy/spirit.cpp | 46 ++-- src/game.cpp | 338 +++++++++++++++-------------- src/gameitem.cpp | 106 ++++----- src/gamemap.cpp | 376 ++++++++++++++++++++++++++++++++ src/main.cpp | 20 +- src/mainwindow.cpp | 211 +++++++++--------- src/map.cpp | 396 ---------------------------------- src/shop.cpp | 165 +++++++------- src/statistic.cpp | 220 +++++++++---------- src/tower/bomb.cpp | 92 ++++---- src/tower/campfire.cpp | 95 ++++---- src/tower/guntower.cpp | 57 +++-- src/tower/infopanel.cpp | 82 ++++--- src/tower/lasertower.cpp | 96 ++++----- src/tower/missiletower.cpp | 57 +++-- src/tower/repeller.cpp | 67 +++--- src/tower/sawtooth.cpp | 67 +++--- src/tower/shield.cpp | 72 +++---- src/tower/tower.cpp | 151 ++++++------- 63 files changed, 2535 insertions(+), 2679 deletions(-) create mode 100644 include/gamemap.h delete mode 100644 include/map.h create mode 100644 src/gamemap.cpp delete mode 100644 src/map.cpp diff --git a/README.md b/README.md index fcca04e..4a63b37 100644 --- a/README.md +++ b/README.md @@ -58,8 +58,10 @@ Qt >= 5.12 Ubuntu -- `qt5-default` -- `qtmultimedia5` +``` +apt update +apt install cmake make g++ qt5-default qtmultimedia5-dev +``` Windows diff --git a/include/bullet/bullet.h b/include/bullet/bullet.h index c296324..71e816a 100644 --- a/include/bullet/bullet.h +++ b/include/bullet/bullet.h @@ -1,26 +1,28 @@ -#ifndef BULLET_H -#define BULLET_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_BULLET_BULLET_H_ +#define INCLUDE_BULLET_BULLET_H_ #include #include -#include +#include class Bullet : public QObject, public QGraphicsItem { - Q_OBJECT - Q_INTERFACES(QGraphicsItem) -public: - Bullet(); - QRectF boundingRect() const override; - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - void advance(int phase) override; - enum { Type = UserType + 4 }; - int type() const override; - virtual void moveForward(); + Q_OBJECT + Q_INTERFACES(QGraphicsItem) + public: + Bullet(); + QRectF boundingRect() const override; + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + void advance(int phase) override; + enum { Type = UserType + 4 }; + int type() const override; + virtual void moveForward(); -protected: - QImage image; - qreal atk; - qreal speed; + protected: + QImage image; + qreal atk; + qreal speed; }; -#endif // BULLET_H +#endif // INCLUDE_BULLET_BULLET_H_ diff --git a/include/bullet/gunbullet.h b/include/bullet/gunbullet.h index fdf979d..62a6bec 100644 --- a/include/bullet/gunbullet.h +++ b/include/bullet/gunbullet.h @@ -1,16 +1,18 @@ -#ifndef GUNBULLET_H -#define GUNBULLET_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_BULLET_GUNBULLET_H_ +#define INCLUDE_BULLET_GUNBULLET_H_ -#include "bullet.h" -#include "enemy/enemy.h" +#include +#include class GunBullet : public Bullet { -public: - GunBullet(qreal atk); - QRectF boundingRect() const override; - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - void advance(int phase) override; - void moveForward() override; + public: + explicit GunBullet(qreal atk); + QRectF boundingRect() const override; + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + void advance(int phase) override; + void moveForward() override; }; -#endif // GUNBULLET_H +#endif // INCLUDE_BULLET_GUNBULLET_H_ diff --git a/include/bullet/laser.h b/include/bullet/laser.h index efc2eaf..3a42e49 100644 --- a/include/bullet/laser.h +++ b/include/bullet/laser.h @@ -1,20 +1,22 @@ -#ifndef LASER_H -#define LASER_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_BULLET_LASER_H_ +#define INCLUDE_BULLET_LASER_H_ -#include "bullet.h" -#include "enemy/enemy.h" +#include +#include class Laser : public Bullet { -public: - Laser(GameItem* parent, qreal atk); - ~Laser(); - QRectF boundingRect() const override; - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - void advance(int phase) override; + public: + Laser(GameItem* parent, qreal atk); + ~Laser(); + QRectF boundingRect() const override; + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + void advance(int phase) override; -protected: - GameItem* parent; - int counter; + protected: + GameItem* parent; + int counter; }; -#endif // LASER_H +#endif // INCLUDE_BULLET_LASER_H_ diff --git a/include/bullet/missile.h b/include/bullet/missile.h index 6b8d6ee..b1adb59 100644 --- a/include/bullet/missile.h +++ b/include/bullet/missile.h @@ -1,27 +1,29 @@ -#ifndef MISSILE_H -#define MISSILE_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_BULLET_MISSILE_H_ +#define INCLUDE_BULLET_MISSILE_H_ -#include "bullet.h" +#include class Missile : public Bullet { -public: - Missile(QPointer atkTarget, qreal atk); - ~Missile(); - QRectF boundingRect() const override; - QPainterPath shape() const override; - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - void advance(int phase) override; - void moveForward() override; - void bombing(); + public: + Missile(QPointer atkTarget, qreal atk); + ~Missile(); + QRectF boundingRect() const override; + QPainterPath shape() const override; + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + void advance(int phase) override; + void moveForward() override; + void bombing(); -private: - QPointer atkTarget; - qreal atkAngle; + private: + QPointer atkTarget; + qreal atkAngle; - QGraphicsEllipseItem* bombArea; - bool isBombing; - qreal bombRadius; - QMovie movie; + QGraphicsEllipseItem* bombArea; + bool isBombing; + qreal bombRadius; + QMovie movie; }; -#endif // MISSILE_H +#endif // INCLUDE_BULLET_MISSILE_H_ diff --git a/include/common.h b/include/common.h index cc70e18..41e3716 100644 --- a/include/common.h +++ b/include/common.h @@ -1,14 +1,18 @@ -#ifndef COMMON_H -#define COMMON_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_COMMON_H_ +#define INCLUDE_COMMON_H_ class GameItem; class Enemy; class Tower; -class Map; +class GameMap; class InfoPanel; class Game; class MainWindow; +#include +#include + #include #include #include @@ -40,53 +44,42 @@ class MainWindow; #include #include #include -#include -#include -#include -#include template class GameValue { -private: - Val curValue; - Val maxValue; + private: + Val curValue; + Val maxValue; -public: - GameValue() - { - curValue = Val(); - maxValue = Val(); - }; - GameValue(Val cur, Val max) - { - curValue = cur; - maxValue = max; - } - ~GameValue() { } - inline const Val& getCurValue() const { return curValue; }; - inline void setCurValue(const Val& newCurValue) - { - curValue = newCurValue; - if (curValue < Val()) - curValue = Val(); - if (curValue > maxValue) - curValue = maxValue; - }; - inline void changeCurValue(Val delta) { setCurValue(curValue + delta); }; - inline const Val& getMaxValue() const { return maxValue; }; - inline void setMaxValue(const Val& newMaxValue) { maxValue = newMaxValue; }; + public: + GameValue() { + curValue = Val(); + maxValue = Val(); + } + GameValue(Val cur, Val max) { + curValue = cur; + maxValue = max; + } + ~GameValue() {} + inline const Val& getCurValue() const { return curValue; } + inline void setCurValue(const Val& newCurValue) { + curValue = newCurValue; + if (curValue < Val()) curValue = Val(); + if (curValue > maxValue) curValue = maxValue; + } + inline void changeCurValue(Val delta) { setCurValue(curValue + delta); } + inline const Val& getMaxValue() const { return maxValue; } + inline void setMaxValue(const Val& newMaxValue) { maxValue = newMaxValue; } }; class InfoMsg : public QWidget { -public: - InfoMsg(QWidget* parent) - : QWidget(parent) {}; - void paintEvent(QPaintEvent* e) override - { - Q_UNUSED(e) - QPainter p(this); - p.drawImage(QRectF(-10, -10, 358, 242), QImage(":/images/messagebox.png")); - }; + public: + explicit InfoMsg(QWidget* parent) : QWidget(parent) {} + void paintEvent(QPaintEvent* e) override { + Q_UNUSED(e) + QPainter p(this); + p.drawImage(QRectF(-10, -10, 358, 242), QImage(":/images/messagebox.png")); + }; }; -#endif // COMMON_H +#endif // INCLUDE_COMMON_H_ diff --git a/include/enemy/blackwarrior.h b/include/enemy/blackwarrior.h index 55c3864..bbddc2a 100644 --- a/include/enemy/blackwarrior.h +++ b/include/enemy/blackwarrior.h @@ -1,13 +1,14 @@ -#ifndef BLACKWARRIOR_H -#define BLACKWARRIOR_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_ENEMY_BLACKWARRIOR_H_ +#define INCLUDE_ENEMY_BLACKWARRIOR_H_ -#include "enemy.h" -#include "game.h" +#include +#include class BlackWarrior : public Enemy { -public: - BlackWarrior(Game* game, QList& path); - QPainterPath shape() const override; + public: + BlackWarrior(Game* game, QList* path); + QPainterPath shape() const override; }; -#endif // BLACKWARRIOR_H +#endif // INCLUDE_ENEMY_BLACKWARRIOR_H_ diff --git a/include/enemy/cowardplane.h b/include/enemy/cowardplane.h index dac2065..02a6119 100644 --- a/include/enemy/cowardplane.h +++ b/include/enemy/cowardplane.h @@ -1,14 +1,15 @@ -#ifndef COWARDPLANE_H -#define COWARDPLANE_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_ENEMY_COWARDPLANE_H_ +#define INCLUDE_ENEMY_COWARDPLANE_H_ -#include "enemy.h" -#include "game.h" +#include +#include class CowardPlane : public Enemy { -public: - CowardPlane(Game* game, QList& path); - QPainterPath shape() const override; - void advance(int phase) override; + public: + CowardPlane(Game* game, QList* path); + QPainterPath shape() const override; + void advance(int phase) override; }; -#endif // COWARDPLANE_H +#endif // INCLUDE_ENEMY_COWARDPLANE_H_ diff --git a/include/enemy/dragon.h b/include/enemy/dragon.h index 97ade96..72f8b23 100644 --- a/include/enemy/dragon.h +++ b/include/enemy/dragon.h @@ -1,15 +1,16 @@ -#ifndef DRAGON_H -#define DRAGON_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_ENEMY_DRAGON_H_ +#define INCLUDE_ENEMY_DRAGON_H_ -#include "enemy.h" -#include "game.h" +#include +#include class Dragon : public Enemy { -public: - Dragon(Game* game, QList& path); - ~Dragon(); - QRectF boundingRect() const override; - QPainterPath shape() const override; + public: + Dragon(Game* game, QList* path); + ~Dragon(); + QRectF boundingRect() const override; + QPainterPath shape() const override; }; -#endif // DRAGON_H +#endif // INCLUDE_ENEMY_DRAGON_H_ diff --git a/include/enemy/enemy.h b/include/enemy/enemy.h index dcb3b0d..8756036 100644 --- a/include/enemy/enemy.h +++ b/include/enemy/enemy.h @@ -1,5 +1,6 @@ -#ifndef ENEMY_H -#define ENEMY_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_ENEMY_ENEMY_H_ +#define INCLUDE_ENEMY_ENEMY_H_ #include #include @@ -7,62 +8,60 @@ #include class Enemy : public GameItem { - Q_INTERFACES(QGraphicsItem) + Q_INTERFACES(QGraphicsItem) -public: - enum enemyMoveType { - WALKING = 0, - FLYING = 1, - }; - Enemy(Game* game, - QList& path, - int money = 0, + public: + enum enemyMoveType { + WALKING = 0, + FLYING = 1, + }; + Enemy(Game* game, QList* path, int money = 0, GameValue HP = GameValue(), GameValue atk = GameValue(), - GameValue speed = GameValue(), - int moveType = WALKING, + GameValue speed = GameValue(), int moveType = WALKING, qreal atkRadius = 0); - ~Enemy(); - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - void advance(int phase) override; - enum { Type = UserType + 2 }; - int type() const override; + ~Enemy(); + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + void advance(int phase) override; + enum { Type = UserType + 2 }; + int type() const override; - /* enemy actions*/ - void aquireTarget() override; - void attack() override; - void moveForward(); + /* enemy actions*/ + void aquireTarget() override; + void attack() override; + void moveForward(); - /*setter and getter*/ + /*setter and getter*/ - QPointF getPoint(int index) const; - QPointF getDest() const; - int getpointIndex(); - int getMoveType() const; - bool getIsStopped() const; - void setIsStopped(bool newIsStopped); + QPointF getPoint(int index) const; + QPointF getDest() const; + int getpointIndex(); + int getMoveType() const; + bool getIsStopped() const; + void setIsStopped(bool newIsStopped); -protected: - Game* game; - QList points; - QPointF dest; - int pointIndex; + protected: + Game* game; + QList points; + QPointF dest; + int pointIndex; - int moveType; - bool isStopped; + int moveType; + bool isStopped; - bool canAttackMelee; - bool canAttackRange; + bool canAttackMelee; + bool canAttackRange; - QMovie movie; - QMovie atkMovie; - QRectF atkMovieRect; + QMovie movie; + QMovie atkMovie; + QRectF atkMovieRect; - // int deathCounter; - GameValue deathCounter; + // int deathCounter; + GameValue deathCounter; -public: - static int enemyCount; + public: + static int enemyCount; }; -#endif // ENEMY_H +#endif // INCLUDE_ENEMY_ENEMY_H_ diff --git a/include/enemy/fallenwarrior.h b/include/enemy/fallenwarrior.h index a38fc21..ce236ec 100644 --- a/include/enemy/fallenwarrior.h +++ b/include/enemy/fallenwarrior.h @@ -1,17 +1,19 @@ -#ifndef FALLENWARRIOR_H -#define FALLENWARRIOR_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_ENEMY_FALLENWARRIOR_H_ +#define INCLUDE_ENEMY_FALLENWARRIOR_H_ #include class FallenWarrior : public Enemy { -public: - FallenWarrior(Game* game, QList& path); - QRectF boundingRect() const override; - QPainterPath shape() const override; - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - void advance(int phase) override; + public: + FallenWarrior(Game* game, QList* path); + QRectF boundingRect() const override; + QPainterPath shape() const override; + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + void advance(int phase) override; - void attack() override; + void attack() override; }; -#endif // FALLENWARRIOR_H +#endif // INCLUDE_ENEMY_FALLENWARRIOR_H_ diff --git a/include/enemy/ghost.h b/include/enemy/ghost.h index d4118ad..9954f89 100644 --- a/include/enemy/ghost.h +++ b/include/enemy/ghost.h @@ -1,13 +1,15 @@ -#ifndef GHOST_H -#define GHOST_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_ENEMY_GHOST_H_ +#define INCLUDE_ENEMY_GHOST_H_ -#include "enemy.h" +#include class Ghost : public Enemy { -public: - Ghost(Game* game, QList& path); - // void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; - QPainterPath shape() const override; + public: + Ghost(Game* game, QList* path); + // void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + // QWidget *widget) override; + QPainterPath shape() const override; }; -#endif // GHOST_H +#endif // INCLUDE_ENEMY_GHOST_H_ diff --git a/include/enemy/robotsoldier.h b/include/enemy/robotsoldier.h index 9c507c5..1f96831 100644 --- a/include/enemy/robotsoldier.h +++ b/include/enemy/robotsoldier.h @@ -1,19 +1,21 @@ -#ifndef ROBOTSOLDIER_H -#define ROBOTSOLDIER_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_ENEMY_ROBOTSOLDIER_H_ +#define INCLUDE_ENEMY_ROBOTSOLDIER_H_ -#include "enemy.h" +#include class RobotSoldier : public Enemy { -public: - RobotSoldier(Game* game, QList& path); - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - QRectF boundingRect() const override; - QPainterPath shape() const override; + public: + RobotSoldier(Game* game, QList* path); + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + QRectF boundingRect() const override; + QPainterPath shape() const override; - void attack() override; + void attack() override; -protected: - qreal atkSpeed; + protected: + qreal atkSpeed; }; -#endif // ROBOTSOLDIER_H +#endif // INCLUDE_ENEMY_ROBOTSOLDIER_H_ diff --git a/include/enemy/shaman.h b/include/enemy/shaman.h index 9ab64fc..6be5414 100644 --- a/include/enemy/shaman.h +++ b/include/enemy/shaman.h @@ -1,15 +1,17 @@ -#ifndef SHAMAN_H -#define SHAMAN_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_ENEMY_SHAMAN_H_ +#define INCLUDE_ENEMY_SHAMAN_H_ -#include "enemy.h" +#include class Shaman : public Enemy { -public: - Shaman(Game* game, QList& path); - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - QPainterPath shape() const override; - void aquireTarget() override; - void attack() override; + public: + Shaman(Game* game, QList* path); + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + QPainterPath shape() const override; + void aquireTarget() override; + void attack() override; }; -#endif // SHAMAN_H +#endif // INCLUDE_ENEMY_SHAMAN_H_ diff --git a/include/enemy/spirit.h b/include/enemy/spirit.h index a36d156..a8a5b80 100644 --- a/include/enemy/spirit.h +++ b/include/enemy/spirit.h @@ -1,13 +1,14 @@ -#ifndef SPIRIT_H -#define SPIRIT_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_ENEMY_SPIRIT_H_ +#define INCLUDE_ENEMY_SPIRIT_H_ -#include "enemy.h" +#include class Spirit : public Enemy { -public: - Spirit(Game* game, QList& path); - QRectF boundingRect() const override; - QPainterPath shape() const override; + public: + Spirit(Game* game, QList* path); + QRectF boundingRect() const override; + QPainterPath shape() const override; }; -#endif // SPIRIT_H +#endif // INCLUDE_ENEMY_SPIRIT_H_ diff --git a/include/game.h b/include/game.h index 5711912..4e34df6 100644 --- a/include/game.h +++ b/include/game.h @@ -1,49 +1,53 @@ -#ifndef GAME_H -#define GAME_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_GAME_H_ +#define INCLUDE_GAME_H_ #include +#include #include -#include #include +#include + class Game : public QGraphicsView { - Q_OBJECT -public: - Game(MainWindow* parent = 0, QString mapConfig = QString(":maps/map1.txt")); - ~Game(); - -public: - MainWindow* parent; - QGraphicsScene scene; - Map map; - Statistic statistic; - QString mapConfig; - - int enemyNum; - int enemyMaxnum; - - void createEnemy(int num); - void updateGameSpeed(); - void keyPressEvent(QKeyEvent* event) override; -signals: - void GameOver(); -public slots: - void spawnEnemy(); - void endThisGame(QString); - -public: - int gameSpeed; - QTimer spawnTimer; - QTimer advanceTimer; - QElapsedTimer FPSCounterTimer; - - QMediaPlayer BGMplayer; - - std::vector probEnemy; - std::vector probRec; - std::vector> enemyLambda; - int probSum; - int RandEnemyIndex(); + Q_OBJECT + + public: + explicit Game(MainWindow* parent = 0, QString* mapConfig = 0); + ~Game(); + + public: + MainWindow* parent; + QGraphicsScene scene; + GameMap map; + Statistic statistic; + QString* mapConfig; + + int enemyNum; + int enemyMaxnum; + + void createEnemy(int num); + void updateGameSpeed(); + void keyPressEvent(QKeyEvent* event) override; + signals: + void GameOver(); + public slots: + void spawnEnemy(); + void endThisGame(QString); + + public: + int gameSpeed; + QTimer spawnTimer; + QTimer advanceTimer; + QElapsedTimer FPSCounterTimer; + + QMediaPlayer BGMplayer; + + std::vector probEnemy; + std::vector probRec; + std::vector> enemyLambda; + int probSum; + int RandEnemyIndex(); }; -#endif // GAME_H +#endif // INCLUDE_GAME_H_ diff --git a/include/gameitem.h b/include/gameitem.h index bd75550..f1227bc 100644 --- a/include/gameitem.h +++ b/include/gameitem.h @@ -1,50 +1,52 @@ -#ifndef GAMEITEM_H -#define GAMEITEM_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_GAMEITEM_H_ +#define INCLUDE_GAMEITEM_H_ #include class GameItem : public QGraphicsObject { -public: - GameItem(); - virtual ~GameItem(); - -public: - QRectF boundingRect() const override; - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - virtual void aquireTarget() = 0; - virtual void attack() = 0; - - virtual bool getIsDead() const; - virtual void setIsDead(bool newIsDead); - - QPointer getAtkTarget() const; - void setAtkTarget(QPointer newAtkTarget); - - const QString& getName() const; - void setName(const QString& newName); - - void mousePressEvent(QGraphicsSceneMouseEvent* event) override; - void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override; - -protected: - QString name; - - int money; - qreal atkRadius; - bool isDead; - - QPointer atkTarget; - -public: - GameValue HP; - GameValue atk; - GameValue speed; - GameValue blockNumber; - GameValue aquireCounter; - QGraphicsEllipseItem* atkArea; - QGraphicsRectItem* HPMeter; - bool underAtk; - GameValue blinkCounter; + public: + GameItem(); + virtual ~GameItem(); + + public: + QRectF boundingRect() const override; + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + virtual void aquireTarget() = 0; + virtual void attack() = 0; + + virtual bool getIsDead() const; + virtual void setIsDead(bool newIsDead); + + QPointer getAtkTarget() const; + void setAtkTarget(QPointer newAtkTarget); + + const QString& getName() const; + void setName(const QString& newName); + + void mousePressEvent(QGraphicsSceneMouseEvent* event) override; + void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override; + + protected: + QString name; + + int money; + qreal atkRadius; + bool isDead; + + QPointer atkTarget; + + public: + GameValue HP; + GameValue atk; + GameValue speed; + GameValue blockNumber; + GameValue aquireCounter; + QGraphicsEllipseItem* atkArea; + QGraphicsRectItem* HPMeter; + bool underAtk; + GameValue blinkCounter; }; -#endif // GAMEITEM_H +#endif // INCLUDE_GAMEITEM_H_ diff --git a/include/gamemap.h b/include/gamemap.h new file mode 100644 index 0000000..3c3c9dc --- /dev/null +++ b/include/gamemap.h @@ -0,0 +1,62 @@ +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_GAMEMAP_H_ +#define INCLUDE_GAMEMAP_H_ + +#include + +#include +#include + +class GameMap : public QGraphicsObject { + Q_OBJECT + Q_INTERFACES(QGraphicsItem) + + public: + GameMap(Game* game, QString* mapConfig); + ~GameMap(); + QRectF boundingRect() const override; + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + void advance(int phase) override; + + void mousePressEvent(QGraphicsSceneMouseEvent* event) override; + void dragEnterEvent(QGraphicsSceneDragDropEvent* event) override; + void dragMoveEvent(QGraphicsSceneDragDropEvent* event) override; + void dropEvent(QGraphicsSceneDragDropEvent* event) override; + + public: + Game* game; + QString* mapConfig; + QImage mapImage; + QList road; + QList walkingPath; + QList flyingPath; + void loadConfig(); + void addTower(QString s, QPointF pos); + void addTowerShadow(QString s, QPointF pos); + void removeTower(QPointF pos); + + bool IsRoad(QPoint p); + bool IsOccupied(QPoint p); + void Occupy(QGraphicsItem* item, QPointF pos); + QGraphicsItem* ItemOccupied(QPoint p); + void Destory(QPointF pos); + + private: + QVector> isroad; + QVector> isoccupied; + QVector> itemoccupied; + QMovie destinationMovie; + Tower* towerShadow; + QRectF towerShadowRect; + std::set> pointCount; + + public: + static QPoint CoordinateToBlock(QPointF p); + static QPointF BlockToCoordinate(QPoint p); + static const int width = 1280; + static const int height = 960; + static bool outofScreen(QGraphicsItem* p); +}; + +#endif // INCLUDE_GAMEMAP_H_ diff --git a/include/mainwindow.h b/include/mainwindow.h index 7305639..68de880 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -1,30 +1,31 @@ -#ifndef MAINWINDOW_H -#define MAINWINDOW_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_MAINWINDOW_H_ +#define INCLUDE_MAINWINDOW_H_ #include #include class MainWindow : public QMainWindow { - Q_OBJECT -public: - explicit MainWindow(QWidget* parent = nullptr); - void keyPressEvent(QKeyEvent* event) override; - void paintEvent(QPaintEvent* e) override; + Q_OBJECT + public: + explicit MainWindow(QWidget* parent = nullptr); + void keyPressEvent(QKeyEvent* event) override; + void paintEvent(QPaintEvent* e) override; - Game* getGame() const; - void setGame(Game* newGame); - QString mapConfig; + Game* getGame() const; + void setGame(Game* newGame); + QString mapConfig; - // void loadStyleSheet(const QString &styleSheetFile); + // void loadStyleSheet(const QString &styleSheetFile); -public slots: - void startGame(); - void chooseMap(); - void exportMap(); + public slots: + void startGame(); + void chooseMap(); + void exportMap(); -private: - Game* game; - int starticonangle; + private: + Game* game; + int starticonangle; }; -#endif // MAINWINDOW_H +#endif // INCLUDE_MAINWINDOW_H_ diff --git a/include/map.h b/include/map.h deleted file mode 100644 index ec275b1..0000000 --- a/include/map.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef MAP_H -#define MAP_H - -#include - -class Map : public QGraphicsObject { - Q_OBJECT - Q_INTERFACES(QGraphicsItem) -public: - Map(Game* game, QString& mapConfig); - ~Map(); - QRectF boundingRect() const override; - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - void advance(int phase) override; - - void mousePressEvent(QGraphicsSceneMouseEvent* event) override; - void dragEnterEvent(QGraphicsSceneDragDropEvent* event) override; - void dragMoveEvent(QGraphicsSceneDragDropEvent* event) override; - void dropEvent(QGraphicsSceneDragDropEvent* event) override; - -public: - Game* game; - QString mapConfig; - QImage mapImage; - QList road; - QList walkingPath; - QList flyingPath; - void loadConfig(); - void addTower(QString s, QPointF pos); - void addTowerShadow(QString s, QPointF pos); - void removeTower(QPointF pos); - - bool IsRoad(QPoint p); - bool IsOccupied(QPoint p); - void Occupy(QGraphicsItem* item, QPointF pos); - QGraphicsItem* ItemOccupied(QPoint p); - void Destory(QPointF pos); - -private: - QVector> isroad; - QVector> isoccupied; - QVector> itemoccupied; - QMovie destinationMovie; - Tower* towerShadow; - QRectF towerShadowRect; - std::set> pointCount; - -public: - static QPoint CoordinateToBlock(QPointF p); - static QPointF BlockToCoordinate(QPoint p); - static const int width = 1280; - static const int height = 960; - static bool outofScreen(QGraphicsItem* p); -}; - -#endif // MAP_H diff --git a/include/shop.h b/include/shop.h index 08f87e9..c7dab10 100644 --- a/include/shop.h +++ b/include/shop.h @@ -1,26 +1,28 @@ -#ifndef SHOP_H -#define SHOP_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_SHOP_H_ +#define INCLUDE_SHOP_H_ #include class Shop : public QGraphicsItem { -public: - int counter; + public: + int counter; - qreal aleft, atop, awidth, aheight; + qreal aleft, atop, awidth, aheight; - QString towername; - Shop(QString s, qreal left, qreal top, qreal width, qreal height); - const static QMap map; - const static QVector name; - const static QVector cost; - const static QVector cdtime; - QRectF boundingRect() const override; - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - void advance(int phase) override; - void mousePressEvent(QGraphicsSceneMouseEvent* event) override; - void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override; - void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override; + QString towername; + Shop(QString s, qreal left, qreal top, qreal width, qreal height); + static const QMap map; + static const QVector name; + static const QVector cost; + static const QVector cdtime; + QRectF boundingRect() const override; + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + void advance(int phase) override; + void mousePressEvent(QGraphicsSceneMouseEvent* event) override; + void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override; + void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override; }; -#endif // SHOP_H +#endif // INCLUDE_SHOP_H_ diff --git a/include/statistic.h b/include/statistic.h index c1eb707..a5c1e4e 100644 --- a/include/statistic.h +++ b/include/statistic.h @@ -1,26 +1,28 @@ -#ifndef STATISTIC_H -#define STATISTIC_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_STATISTIC_H_ +#define INCLUDE_STATISTIC_H_ #include class Statistic : public QGraphicsObject { - Q_OBJECT -public: - Statistic(Game* game); - QRectF boundingRect() const override; - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - void advance(int phase) override; + Q_OBJECT + public: + explicit Statistic(Game* game); + QRectF boundingRect() const override; + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + void advance(int phase) override; -public: - Game* game; - GameValue life; - GameValue money; - GameValue moneyAddCounter; - int FPS; - static int frameCount; - QImage shopBackground; - QImage lifeIcon; - QImage coinIcon; + public: + Game* game; + GameValue life; + GameValue money; + GameValue moneyAddCounter; + int FPS; + static int frameCount; + QImage shopBackground; + QImage lifeIcon; + QImage coinIcon; }; -#endif // STATISTIC_H +#endif // INCLUDE_STATISTIC_H_ diff --git a/include/tower/bomb.h b/include/tower/bomb.h index 2f74e00..2071bf5 100644 --- a/include/tower/bomb.h +++ b/include/tower/bomb.h @@ -1,18 +1,20 @@ -#ifndef BOMB_H -#define BOMB_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_TOWER_BOMB_H_ +#define INCLUDE_TOWER_BOMB_H_ -#include "tower.h" +#include class Bomb : public Tower { -public: - Bomb(Map* map); - ~Bomb(); - void advance(int phase) override; - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - void bombing(); + public: + explicit Bomb(GameMap* map); + ~Bomb(); + void advance(int phase) override; + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + void bombing(); -protected: - QMovie movie; + protected: + QMovie movie; }; -#endif // BOMB_H +#endif // INCLUDE_TOWER_BOMB_H_ diff --git a/include/tower/campfire.h b/include/tower/campfire.h index b38a200..5d60fa6 100644 --- a/include/tower/campfire.h +++ b/include/tower/campfire.h @@ -1,17 +1,20 @@ -#ifndef CAMPFIRE_H -#define CAMPFIRE_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_TOWER_CAMPFIRE_H_ +#define INCLUDE_TOWER_CAMPFIRE_H_ + +#include -#include "tower.h" class CampFire : public Tower { -public: - CampFire(Map* map); - ~CampFire(); - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - int angle; - void aquireTarget() override; - void advance(int phase) override; + public: + explicit CampFire(GameMap* map); + ~CampFire(); + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + int angle; + void aquireTarget() override; + void advance(int phase) override; - QMovie movie; + QMovie movie; }; -#endif // CAMPFIRE_H +#endif // INCLUDE_TOWER_CAMPFIRE_H_ diff --git a/include/tower/guntower.h b/include/tower/guntower.h index 9a00549..0f75df4 100644 --- a/include/tower/guntower.h +++ b/include/tower/guntower.h @@ -1,17 +1,18 @@ -#ifndef GUNTOWER_H -#define GUNTOWER_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_TOWER_GUNTOWER_H_ +#define INCLUDE_TOWER_GUNTOWER_H_ -#include "tower.h" #include +#include class GunTower : public Tower { -public: - GunTower(Map* map); - ~GunTower(); - QRectF boundingRect() const override; + public: + explicit GunTower(GameMap* map); + ~GunTower(); + QRectF boundingRect() const override; -public: - void attack() override; + public: + void attack() override; }; -#endif // GUNTOWER_H +#endif // INCLUDE_TOWER_GUNTOWER_H_ diff --git a/include/tower/infopanel.h b/include/tower/infopanel.h index 16a03fd..6ed69d1 100644 --- a/include/tower/infopanel.h +++ b/include/tower/infopanel.h @@ -1,21 +1,23 @@ -#ifndef INFOPANEL_H -#define INFOPANEL_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_TOWER_INFOPANEL_H_ +#define INCLUDE_TOWER_INFOPANEL_H_ #include #include class InfoPanel : public QGraphicsItem { - Q_INTERFACES(QGraphicsItem) -public: - InfoPanel(GameItem* parent = 0); - ~InfoPanel(); - QRectF boundingRect() const override; - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - void advance(int phase) override; - void mousePressEvent(QGraphicsSceneMouseEvent* event) override; + Q_INTERFACES(QGraphicsItem) + public: + explicit InfoPanel(GameItem* parent = 0); + ~InfoPanel(); + QRectF boundingRect() const override; + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + void advance(int phase) override; + void mousePressEvent(QGraphicsSceneMouseEvent* event) override; -private: - GameItem* parent; + private: + GameItem* parent; }; -#endif // INFOPANEL_H +#endif // INCLUDE_TOWER_INFOPANEL_H_ diff --git a/include/tower/lasertower.h b/include/tower/lasertower.h index 2651eff..4b7da3a 100644 --- a/include/tower/lasertower.h +++ b/include/tower/lasertower.h @@ -1,26 +1,28 @@ -#ifndef LASERTOWER_H -#define LASERTOWER_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_TOWER_LASERTOWER_H_ +#define INCLUDE_TOWER_LASERTOWER_H_ -#include "tower.h" #include +#include + class LaserTower : public Tower { -public: - LaserTower(Map* map); - ~LaserTower(); - QRectF boundingRect() const override; + public: + explicit LaserTower(GameMap* map); + ~LaserTower(); + QRectF boundingRect() const override; - void attack() override; + void attack() override; - Laser* getTrajectory() const; - void setTrajectory(Laser* newTrajectory); + Laser* getTrajectory() const; + void setTrajectory(Laser* newTrajectory); -protected: - int prevAngle; - Laser* trajectory; + protected: + int prevAngle; + Laser* trajectory; -public: - void aquireTarget() override; + public: + void aquireTarget() override; }; -#endif // LASERTOWER_H +#endif // INCLUDE_TOWER_LASERTOWER_H_ diff --git a/include/tower/missiletower.h b/include/tower/missiletower.h index 92708a1..ecf4396 100644 --- a/include/tower/missiletower.h +++ b/include/tower/missiletower.h @@ -1,16 +1,18 @@ -#ifndef MISSILETOWER_H -#define MISSILETOWER_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_TOWER_MISSILETOWER_H_ +#define INCLUDE_TOWER_MISSILETOWER_H_ -#include "tower.h" #include +#include + class MissileTower : public Tower { -public: - MissileTower(Map* map); - ~MissileTower(); + public: + explicit MissileTower(GameMap* map); + ~MissileTower(); - void aquireTarget() override; - void attack() override; + void aquireTarget() override; + void attack() override; }; -#endif // MISSILETOWER_H +#endif // INCLUDE_TOWER_MISSILETOWER_H_ diff --git a/include/tower/repeller.h b/include/tower/repeller.h index 65587f1..c3a49fa 100644 --- a/include/tower/repeller.h +++ b/include/tower/repeller.h @@ -1,13 +1,14 @@ -#ifndef REPELLER_H -#define REPELLER_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_TOWER_REPELLER_H_ +#define INCLUDE_TOWER_REPELLER_H_ -#include "tower.h" +#include class Repeller : public Tower { -public: - Repeller(Map* map); - ~Repeller(); - void advance(int phase) override; + public: + explicit Repeller(GameMap* map); + ~Repeller(); + void advance(int phase) override; }; -#endif // REPELLER_H +#endif // INCLUDE_TOWER_REPELLER_H_ diff --git a/include/tower/sawtooth.h b/include/tower/sawtooth.h index 68917a4..3f32aa5 100644 --- a/include/tower/sawtooth.h +++ b/include/tower/sawtooth.h @@ -1,15 +1,17 @@ -#ifndef SAWTOOTH_H -#define SAWTOOTH_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_TOWER_SAWTOOTH_H_ +#define INCLUDE_TOWER_SAWTOOTH_H_ -#include "tower.h" +#include class SawTooth : public Tower { -public: - SawTooth(Map* map); - ~SawTooth(); - void advance(int phase) override; - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - int angle; + public: + explicit SawTooth(GameMap* map); + ~SawTooth(); + void advance(int phase) override; + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + int angle; }; -#endif // SAWTOOTH_H +#endif // INCLUDE_TOWER_SAWTOOTH_H_ diff --git a/include/tower/shield.h b/include/tower/shield.h index 3bfa087..f277d44 100644 --- a/include/tower/shield.h +++ b/include/tower/shield.h @@ -1,14 +1,16 @@ -#ifndef SHIELD_H -#define SHIELD_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_TOWER_SHIELD_H_ +#define INCLUDE_TOWER_SHIELD_H_ -#include "tower.h" +#include class Shield : public Tower { -public: - Shield(Map* map); - ~Shield(); - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - void advance(int phase) override; + public: + explicit Shield(GameMap* map); + ~Shield(); + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + void advance(int phase) override; }; -#endif // SHIELD_H +#endif // INCLUDE_TOWER_SHIELD_H_ diff --git a/include/tower/tower.h b/include/tower/tower.h index 593ca50..6a2adad 100644 --- a/include/tower/tower.h +++ b/include/tower/tower.h @@ -1,5 +1,6 @@ -#ifndef TOWER_H -#define TOWER_H +// Copyright 2022 Flying-Tom +#ifndef INCLUDE_TOWER_TOWER_H_ +#define INCLUDE_TOWER_TOWER_H_ #include #include @@ -7,40 +8,39 @@ #include class Tower : public GameItem { - Q_INTERFACES(QGraphicsItem) -public: - enum towerAtkType { - MELEE = 0, - RANGE = 1, - }; - Tower(Map* map, - QString name = QString(), - GameValue HP = GameValue(), - int atkType = RANGE, - qreal atkRadius = 0, - GameValue blockNumber = GameValue(0, 3), + Q_INTERFACES(QGraphicsItem) + + public: + enum towerAtkType { + MELEE = 0, + RANGE = 1, + }; + Tower(GameMap* map, QString name = QString(), + GameValue HP = GameValue(), int atkType = RANGE, + qreal atkRadius = 0, GameValue blockNumber = GameValue(0, 3), GameValue aquireCounter = GameValue(0, 5)); - ~Tower(); - void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; - void advance(int phase) override; - -public: - enum { Type = UserType + 3 }; - int type() const override; - - void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) override; - -protected: - Map* map; - int atkType; - QImage image; - -public: - InfoPanel infopanel; - void aquireTarget() override; - void attack() override; - int getAtkType() const; - QImage getImage() const; + ~Tower(); + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) override; + void advance(int phase) override; + + public: + enum { Type = UserType + 3 }; + int type() const override; + + void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) override; + + protected: + GameMap* map; + int atkType; + QImage image; + + public: + InfoPanel infopanel; + void aquireTarget() override; + void attack() override; + int getAtkType() const; + QImage getImage() const; }; -#endif // TOWER_H +#endif // INCLUDE_TOWER_TOWER_H_ diff --git a/src/bullet/bullet.cpp b/src/bullet/bullet.cpp index 725887b..740b58c 100644 --- a/src/bullet/bullet.cpp +++ b/src/bullet/bullet.cpp @@ -1,36 +1,26 @@ +// Copyright 2022 Flying-Tom + #include #include -Bullet::Bullet() - : QObject() - , QGraphicsItem() -{ -} +Bullet::Bullet() : QObject(), QGraphicsItem() {} -QRectF Bullet::boundingRect() const -{ - return QRectF(-8, -8, 16, 16); -} +QRectF Bullet::boundingRect() const { return QRectF(-8, -8, 16, 16); } -void Bullet::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) - Q_UNUSED(painter) +void Bullet::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(option) + Q_UNUSED(widget) + Q_UNUSED(painter) } -void Bullet::advance(int phase) -{ - if (!phase) { - ; - } else { - update(); - } +void Bullet::advance(int phase) { + if (!phase) { + } else { + update(); + } } -int Bullet::type() const -{ - return Type; -} +int Bullet::type() const { return Type; } -void Bullet::moveForward() { } +void Bullet::moveForward() {} diff --git a/src/bullet/gunbullet.cpp b/src/bullet/gunbullet.cpp index 3f42e7c..65ed91b 100644 --- a/src/bullet/gunbullet.cpp +++ b/src/bullet/gunbullet.cpp @@ -1,54 +1,50 @@ +// Copyright 2022 Flying-Tom + #include -GunBullet::GunBullet(qreal atk) -{ - Bullet::atk = atk; - image = QImage(":images/gunbullet.png"); - speed = 10; +GunBullet::GunBullet(qreal atk) { + Bullet::atk = atk; + image = QImage(":images/gunbullet.png"); + speed = 10; } -QRectF GunBullet::boundingRect() const -{ - return QRectF(-6, -6, 12, 12); -} +QRectF GunBullet::boundingRect() const { return QRectF(-6, -6, 12, 12); } -void GunBullet::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) +void GunBullet::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(option) + Q_UNUSED(widget) - painter->drawImage(boundingRect(), image); + painter->drawImage(boundingRect(), image); } -void GunBullet::advance(int phase) -{ - if (!phase) { - if (Map::outofScreen(this)) { - delete this; - return; - } - QList items = collidingItems(); - if (!items.empty()) { - foreach (QGraphicsItem* item, items) { - if (item->type() == Enemy::Type) { - Enemy* e = qgraphicsitem_cast(item); - e->HP.changeCurValue(-atk); - e->underAtk = true; - delete this; - return; - } - } +void GunBullet::advance(int phase) { + if (!phase) { + if (GameMap::outofScreen(this)) { + delete this; + return; + } + QList items = collidingItems(); + if (!items.empty()) { + for (QGraphicsItem* item : items) { + if (item->type() == Enemy::Type) { + Enemy* e = qgraphicsitem_cast(item); + e->HP.changeCurValue(-atk); + e->underAtk = true; + delete this; + return; } - moveForward(); + } } + moveForward(); + } } -void GunBullet::moveForward() -{ - qreal theta = rotation(); +void GunBullet::moveForward() { + qreal theta = rotation(); - qreal dy = speed * qSin(qDegreesToRadians(theta)); - qreal dx = speed * qCos(qDegreesToRadians(theta)); + qreal dy = speed * qSin(qDegreesToRadians(theta)); + qreal dx = speed * qCos(qDegreesToRadians(theta)); - setPos(x() + dx, y() + dy); + setPos(x() + dx, y() + dy); } diff --git a/src/bullet/laser.cpp b/src/bullet/laser.cpp index 94bd9a4..6bf42c5 100644 --- a/src/bullet/laser.cpp +++ b/src/bullet/laser.cpp @@ -1,54 +1,48 @@ +// Copyright 2022 Flying-Tom + #include #include -Laser::Laser(GameItem* parent, qreal atk) - : parent(parent) -{ - Bullet::atk = atk; - counter = 0; - image = QImage(":images/laser.png"); +Laser::Laser(GameItem* parent, qreal atk) : parent(parent) { + Bullet::atk = atk; + counter = 0; + image = QImage(":images/laser.png"); } -Laser::~Laser() -{ - LaserTower* t = qgraphicsitem_cast(parent); - t->setTrajectory(nullptr); +Laser::~Laser() { + LaserTower* t = qgraphicsitem_cast(parent); + t->setTrajectory(nullptr); } -QRectF Laser::boundingRect() const -{ - return QRectF(-32, -220, 64, 256); -} +QRectF Laser::boundingRect() const { return QRectF(-32, -220, 64, 256); } -void Laser::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) - painter->drawImage(boundingRect(), image); - //painter->drawRect(boundingRect()); +void Laser::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(option) + Q_UNUSED(widget) + painter->drawImage(boundingRect(), image); + // painter->drawRect(boundingRect()); } -void Laser::advance(int phase) -{ - Bullet::advance(phase); - if (!phase) { - QList items = collidingItems(); - if (!items.empty()) { - bool selfdestroy = true; - foreach (QGraphicsItem* item, items) { - if (item->type() == Enemy::Type) { - Enemy* e = qgraphicsitem_cast(item); - e->HP.changeCurValue(-atk); - e->underAtk = true; - if (e->HP.getCurValue() > 0) - selfdestroy = false; - } - } - - if (selfdestroy == true) { - delete this; - return; - } +void Laser::advance(int phase) { + Bullet::advance(phase); + if (!phase) { + QList items = collidingItems(); + if (!items.empty()) { + bool selfdestroy = true; + for (QGraphicsItem* item : items) { + if (item->type() == Enemy::Type) { + Enemy* e = qgraphicsitem_cast(item); + e->HP.changeCurValue(-atk); + e->underAtk = true; + if (e->HP.getCurValue() > 0) selfdestroy = false; } + } + + if (selfdestroy == true) { + delete this; + return; + } } + } } diff --git a/src/bullet/missile.cpp b/src/bullet/missile.cpp index 961a332..26a9936 100644 --- a/src/bullet/missile.cpp +++ b/src/bullet/missile.cpp @@ -1,105 +1,99 @@ +// Copyright 2022 Flying-Tom + #include Missile::Missile(QPointer atkTarget, qreal atk) - : atkTarget(atkTarget) - , bombArea(nullptr) - , isBombing(false) - , movie(":images/bomb.gif") -{ - Bullet::atk = atk; - image = QImage(":images/missile.png"); - speed = 6; - bombRadius = 128; + : atkTarget(atkTarget), + bombArea(nullptr), + isBombing(false), + movie(":images/bomb.gif") { + Bullet::atk = atk; + image = QImage(":images/missile.png"); + speed = 6; + bombRadius = 128; } -Missile::~Missile() -{ - if (bombArea) - delete bombArea; +Missile::~Missile() { + if (bombArea) delete bombArea; } -QRectF Missile::boundingRect() const -{ - return QRectF(-40, -20, 80, 40); -} +QRectF Missile::boundingRect() const { return QRectF(-40, -20, 80, 40); } -QPainterPath Missile::shape() const -{ - QPainterPath path; - path.addRect(QRectF(-5, -5, 10, 10)); - return path; +QPainterPath Missile::shape() const { + QPainterPath path; + path.addRect(QRectF(-5, -5, 10, 10)); + return path; } -void Missile::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) +void Missile::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(option) + Q_UNUSED(widget) - if (isBombing) { - painter->drawImage(bombArea->boundingRect(), movie.currentImage()); - } else { - painter->drawImage(boundingRect(), image); - //painter->drawPath(shape()); - } + if (isBombing) { + painter->drawImage(bombArea->boundingRect(), movie.currentImage()); + } else { + painter->drawImage(boundingRect(), image); + // painter->drawPath(shape()); + } } -void Missile::advance(int phase) -{ - Bullet::advance(phase); - if (!phase) { - if (Map::outofScreen(this)) { - delete this; - return; - } - if (isBombing) { - if (movie.state() == QMovie::NotRunning) { - movie.start(); - bombing(); - } else if (movie.currentFrameNumber() == movie.frameCount() - 1) { - delete this; - return; - } - } else { - moveForward(); - QList items = collidingItems(); - if (!items.empty()) { - foreach (QGraphicsItem* item, items) { - if (item->type() == Enemy::Type and ((Enemy*)item)->getIsDead() == false) { - isBombing = true; - bombArea = new QGraphicsEllipseItem(this); - bombArea->setPen(Qt::NoPen); - bombArea->setRect(-bombRadius, -bombRadius, bombRadius * 2, bombRadius * 2); - break; - } - } - } +void Missile::advance(int phase) { + Bullet::advance(phase); + if (!phase) { + if (GameMap::outofScreen(this)) { + delete this; + return; + } + if (isBombing) { + if (movie.state() == QMovie::NotRunning) { + movie.start(); + bombing(); + } else if (movie.currentFrameNumber() == movie.frameCount() - 1) { + delete this; + return; + } + } else { + moveForward(); + QList items = collidingItems(); + if (!items.empty()) { + for (QGraphicsItem* item : items) { + if (item->type() == Enemy::Type && + qgraphicsitem_cast(item)->getIsDead() == false) { + isBombing = true; + bombArea = new QGraphicsEllipseItem(this); + bombArea->setPen(Qt::NoPen); + bombArea->setRect(-bombRadius, -bombRadius, bombRadius * 2, + bombRadius * 2); + break; + } } + } } + } } -void Missile::moveForward() -{ - if (atkTarget.isNull() == false and atkTarget->getIsDead() == false) { - QLineF ln(pos(), atkTarget.data()->pos()); - atkAngle = -ln.angle(); - } +void Missile::moveForward() { + if (atkTarget.isNull() == false && atkTarget->getIsDead() == false) { + QLineF ln(pos(), atkTarget.data()->pos()); + atkAngle = -ln.angle(); + } - setRotation(atkAngle); + setRotation(atkAngle); - qreal dy = speed * qSin(qDegreesToRadians(atkAngle)); - qreal dx = speed * qCos(qDegreesToRadians(atkAngle)); + qreal dy = speed * qSin(qDegreesToRadians(atkAngle)); + qreal dx = speed * qCos(qDegreesToRadians(atkAngle)); - moveBy(dx, dy); + moveBy(dx, dy); } -void Missile::bombing() -{ - QList items = bombArea->collidingItems(); - foreach (QGraphicsItem* item, items) { - if (item->type() == Enemy::Type) { - Enemy* e = qgraphicsitem_cast(item); - e->HP.changeCurValue(-atk); - e->underAtk = true; - } +void Missile::bombing() { + QList items = bombArea->collidingItems(); + for (QGraphicsItem* item : items) { + if (item->type() == Enemy::Type) { + Enemy* e = qgraphicsitem_cast(item); + e->HP.changeCurValue(-atk); + e->underAtk = true; } + } } diff --git a/src/enemy/blackwarrior.cpp b/src/enemy/blackwarrior.cpp index cbafef3..9de8373 100644 --- a/src/enemy/blackwarrior.cpp +++ b/src/enemy/blackwarrior.cpp @@ -1,25 +1,20 @@ +// Copyright 2022 Flying-Tom + #include -BlackWarrior::BlackWarrior(Game* game, QList& path) - : Enemy(game, - path, - 30, - GameValue(100, 100), - GameValue(10, 10), - GameValue(2, 2), - Enemy::WALKING, - 60) -{ - movie.setFileName(":/images/blackwarrior.gif"); - movie.start(); +BlackWarrior::BlackWarrior(Game* game, QList* path) + : Enemy(game, path, 30, GameValue(100, 100), + GameValue(10, 10), GameValue(2, 2), Enemy::WALKING, + 60) { + movie.setFileName(":/images/blackwarrior.gif"); + movie.start(); - canAttackMelee = true; - canAttackRange = false; + canAttackMelee = true; + canAttackRange = false; } -QPainterPath BlackWarrior::shape() const -{ - QPainterPath path; - path.addRect(QRectF(-20, -25, 60, 60)); - return path; +QPainterPath BlackWarrior::shape() const { + QPainterPath path; + path.addRect(QRectF(-20, -25, 60, 60)); + return path; } diff --git a/src/enemy/cowardplane.cpp b/src/enemy/cowardplane.cpp index 76cf04b..105df1c 100644 --- a/src/enemy/cowardplane.cpp +++ b/src/enemy/cowardplane.cpp @@ -1,70 +1,64 @@ +// Copyright 2022 Flying-Tom + #include #include -CowardPlane::CowardPlane(Game* game, QList& path) - : Enemy(game, - path, - 100, - GameValue(500, 500), - GameValue(500, 500), - GameValue(1.5, 6), - Enemy::FLYING, - 200) -{ - movie.setFileName(":/images/cowardplane.gif"); - movie.start(); +CowardPlane::CowardPlane(Game* game, QList* path) + : Enemy(game, path, 100, GameValue(500, 500), + GameValue(500, 500), GameValue(1.5, 6), Enemy::FLYING, + 200) { + movie.setFileName(":/images/cowardplane.gif"); + movie.start(); - canAttackMelee = false; - canAttackRange = true; + canAttackMelee = false; + canAttackRange = true; } -QPainterPath CowardPlane::shape() const -{ - QPainterPath path; - path.addRect(QRectF(-20, -25, 60, 60)); - return path; +QPainterPath CowardPlane::shape() const { + QPainterPath path; + path.addRect(QRectF(-20, -25, 60, 60)); + return path; } -void CowardPlane::advance(int phase) -{ - if (!phase) { - if (HP.getCurValue() == 0) { - isDead = true; - speed.setCurValue(0); +void CowardPlane::advance(int phase) { + if (!phase) { + if (HP.getCurValue() == 0) { + isDead = true; + speed.setCurValue(0); - if (deathCounter.getCurValue() < deathCounter.getMaxValue()) { - deathCounter.changeCurValue(1); - } else { - game->statistic.money.changeCurValue(money); - delete this; - return; - } - return; - } + if (deathCounter.getCurValue() < deathCounter.getMaxValue()) { + deathCounter.changeCurValue(1); + } else { + game->statistic.money.changeCurValue(money); + delete this; + return; + } + return; + } - QList colliding_items = atkArea->collidingItems(); - foreach (QGraphicsItem* item, colliding_items) { - if (item->type() == Tower::Type) { - Tower* t = (Tower*)item; - if (t->getAtkType() == Tower::RANGE) { - speed.setCurValue(4.5); - movie.setSpeed(450); - moveForward(); - return; - } - } - if (item->type() == Bullet::Type) { - speed.setCurValue(4.5); - movie.setSpeed(450); - moveForward(); - return; - } + QList colliding_items = atkArea->collidingItems(); + for (QGraphicsItem* item : colliding_items) { + if (item->type() == Tower::Type) { + Tower* t = qgraphicsitem_cast(item); + if (t->getAtkType() == Tower::RANGE) { + speed.setCurValue(4.5); + movie.setSpeed(450); + moveForward(); + return; } - - speed.setCurValue(1.5); - movie.setSpeed(150); + } + if (item->type() == Bullet::Type) { + speed.setCurValue(4.5); + movie.setSpeed(450); moveForward(); - } else { - update(); + return; + } } + + speed.setCurValue(1.5); + movie.setSpeed(150); + moveForward(); + } else { + update(); + } } diff --git a/src/enemy/dragon.cpp b/src/enemy/dragon.cpp index 55fc221..f0fa0eb 100644 --- a/src/enemy/dragon.cpp +++ b/src/enemy/dragon.cpp @@ -1,39 +1,29 @@ +// Copyright 2022 Flying-Tom + #include -Dragon::Dragon(Game* game, QList& path) - : Enemy(game, - path, - 300, - GameValue(700, 700), - GameValue(15, 15), - GameValue(0.3, 0.3), - Enemy::FLYING, - 100) -{ - movie.setFileName(":/images/dragon.gif"); - movie.start(); +Dragon::Dragon(Game* game, QList* path) + : Enemy(game, path, 300, GameValue(700, 700), + GameValue(15, 15), GameValue(0.3, 0.3), Enemy::FLYING, + 100) { + movie.setFileName(":/images/dragon.gif"); + movie.start(); - atkMovie.setFileName(":/images/dragonfire.gif"); - atkMovie.start(); - atkMovieRect.setWidth(160); - atkMovieRect.setHeight(160); + atkMovie.setFileName(":/images/dragonfire.gif"); + atkMovie.start(); + atkMovieRect.setWidth(160); + atkMovieRect.setHeight(160); - canAttackMelee = true; - canAttackRange = true; + canAttackMelee = true; + canAttackRange = true; } -Dragon::~Dragon() -{ -} +Dragon::~Dragon() {} -QRectF Dragon::boundingRect() const -{ - return QRectF(-128, -200, 256, 256); -} +QRectF Dragon::boundingRect() const { return QRectF(-128, -200, 256, 256); } -QPainterPath Dragon::shape() const -{ - QPainterPath path; - path.addRect(QRectF(-64, -64, 128, 128)); - return path; +QPainterPath Dragon::shape() const { + QPainterPath path; + path.addRect(QRectF(-64, -64, 128, 128)); + return path; } diff --git a/src/enemy/enemy.cpp b/src/enemy/enemy.cpp index 7027b63..11c0b86 100644 --- a/src/enemy/enemy.cpp +++ b/src/enemy/enemy.cpp @@ -1,147 +1,145 @@ +// Copyright 2022 Flying-Tom + #include int Enemy::enemyCount = 0; -Enemy::Enemy(Game* game, QList& path, int money, GameValue HP, - GameValue atk, GameValue speed, int moveType, - qreal atkRadius) - : game(game) - , points(path) - , dest(points[1]) - , pointIndex(1) - , moveType(moveType) - , isStopped(false) - , canAttackMelee(false) - , canAttackRange(false) -{ - ++enemyCount; - GameItem::HP = HP; - GameItem::atkRadius = atkRadius; - GameItem::isDead = false; - GameItem::money = money; - GameItem::atk = atk; - GameItem::speed = speed; - deathCounter = GameValue(0, 30); - - atkArea->setRect(-atkRadius, -atkRadius, atkRadius * 2, atkRadius * 2); - - HPMeter->setBrush(QBrush(QColor(QColor(255, 38, 0)))); - - setPos(Map::BlockToCoordinate(Map::CoordinateToBlock(points[0]))); +Enemy::Enemy(Game* game, QList* path, int money, GameValue HP, + GameValue atk, GameValue speed, int moveType, + qreal atkRadius) + : game(game), + points(*path), + dest(points[1]), + pointIndex(1), + moveType(moveType), + isStopped(false), + canAttackMelee(false), + canAttackRange(false) { + ++enemyCount; + GameItem::HP = HP; + GameItem::atkRadius = atkRadius; + GameItem::isDead = false; + GameItem::money = money; + GameItem::atk = atk; + GameItem::speed = speed; + deathCounter = GameValue(0, 30); + + atkArea->setRect(-atkRadius, -atkRadius, atkRadius * 2, atkRadius * 2); + + HPMeter->setBrush(QBrush(QColor(QColor(255, 38, 0)))); + + setPos(GameMap::BlockToCoordinate(GameMap::CoordinateToBlock(points[0]))); } -Enemy::~Enemy() -{ - --enemyCount; - if (atkTarget.isNull() == false and atkTarget->type() == Tower::Type) { - atkTarget->blockNumber.changeCurValue(-1); - } +Enemy::~Enemy() { + --enemyCount; + if (atkTarget.isNull() == false && atkTarget->type() == Tower::Type) { + atkTarget->blockNumber.changeCurValue(-1); + } } void Enemy::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, - QWidget* widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) - - GameItem::paint(painter, option, widget); - if (isDead == true) { - QFont font; - HPMeter->hide(); - font.setPointSizeF(12); - painter->setFont(font); - painter->setPen(Qt::yellow); - painter->drawImage(QRectF(-25, -15, 20, 20), QImage(":images/coin.png")); - painter->drawText(0, 0, QString("+") + QString::number(money)); + QWidget* widget) { + Q_UNUSED(option) + Q_UNUSED(widget) + + GameItem::paint(painter, option, widget); + if (isDead == true) { + QFont font; + HPMeter->hide(); + font.setPointSizeF(12); + painter->setFont(font); + painter->setPen(Qt::yellow); + painter->drawImage(QRectF(-25, -15, 20, 20), QImage(":images/coin.png")); + painter->drawText(0, 0, QString("+") + QString::number(money)); + } else { + if (underAtk == true) { + painter->setOpacity(0.5); + if (blinkCounter.getCurValue() > 0) { + blinkCounter.changeCurValue(-1); + } else { + blinkCounter.setCurValue(blinkCounter.getMaxValue()); + underAtk = false; + } } else { - - if (underAtk == true) { - painter->setOpacity(0.5); - if (blinkCounter.getCurValue() > 0) { - blinkCounter.changeCurValue(-1); - } else { - blinkCounter.setCurValue(blinkCounter.getMaxValue()); - underAtk = false; - } - } else { - painter->setOpacity(1); - } - - if (x() <= dest.x()) { - painter->drawImage(boundingRect(), movie.currentImage()); - } else { - painter->drawImage(boundingRect(), - movie.currentImage().mirrored(true, false)); - } - // painter->drawPath(shape()); + painter->setOpacity(1); } - if (atkTarget != nullptr and atkMovie.isValid()) { - painter->drawImage(atkMovieRect, atkMovie.currentImage()); + if (x() <= dest.x()) { + painter->drawImage(boundingRect(), movie.currentImage()); + } else { + painter->drawImage(boundingRect(), + movie.currentImage().mirrored(true, false)); } + // painter->drawPath(shape()); + } + + if (atkTarget != nullptr && atkMovie.isValid()) { + painter->drawImage(atkMovieRect, atkMovie.currentImage()); + } } -void Enemy::moveForward() -{ - qreal theta = 0; - QLineF ln(pos(), dest); - theta = -ln.angle(); - - if (ln.length() < 2 * speed.getCurValue()) { - if (pointIndex < points.length() - 1) { - dest = points[++pointIndex]; - theta = -QLineF(pos(), dest).angle(); - } else { - game->statistic.life.changeCurValue(-1); - delete this; - return; - } +void Enemy::moveForward() { + qreal theta = 0; + QLineF ln(pos(), dest); + theta = -ln.angle(); + + if (ln.length() < 2 * speed.getCurValue()) { + if (pointIndex < points.length() - 1) { + dest = points[++pointIndex]; + theta = -QLineF(pos(), dest).angle(); + } else { + game->statistic.life.changeCurValue(-1); + delete this; + return; } + } - qreal dy = speed.getCurValue() * qSin(qDegreesToRadians(theta)); - qreal dx = speed.getCurValue() * qCos(qDegreesToRadians(theta)); + qreal dy = speed.getCurValue() * qSin(qDegreesToRadians(theta)); + qreal dx = speed.getCurValue() * qCos(qDegreesToRadians(theta)); - moveBy(dx, dy); + moveBy(dx, dy); } -void Enemy::aquireTarget() -{ - QList colliding_items = atkArea->collidingItems(); - foreach (QGraphicsItem* item, colliding_items) { - if (item->type() == Tower::Type) { - Tower* t = (Tower*)item; - if ((t->getAtkType() == Tower::RANGE and canAttackRange == false) or (t->getAtkType() == Tower::MELEE and canAttackMelee == false) or (t->blockNumber.getCurValue() >= t->blockNumber.getMaxValue())) { - continue; - } else { - atkTarget = t; - break; - } - } - } - - if (atkTarget.isNull()) { - isStopped = false; - } else { - atkTarget->blockNumber.changeCurValue(1); - atkMovieRect.setRect(atkTarget->x() - x() - atkMovieRect.width() / 2, - atkTarget->y() - y() - atkMovieRect.height() / 2, - atkMovieRect.width(), atkMovieRect.height()); - isStopped = true; +void Enemy::aquireTarget() { + QList colliding_items = atkArea->collidingItems(); + for (QGraphicsItem* item : colliding_items) { + if (item->type() == Tower::Type) { + Tower* t = qgraphicsitem_cast(item); + if ((t->getAtkType() == Tower::RANGE && canAttackRange == false) || + (t->getAtkType() == Tower::MELEE && canAttackMelee == false) || + (t->blockNumber.getCurValue() >= t->blockNumber.getMaxValue())) { + continue; + } else { + atkTarget = t; + break; + } } + } + + if (atkTarget.isNull()) { + isStopped = false; + } else { + atkTarget->blockNumber.changeCurValue(1); + atkMovieRect.setRect(atkTarget->x() - x() - atkMovieRect.width() / 2, + atkTarget->y() - y() - atkMovieRect.height() / 2, + atkMovieRect.width(), atkMovieRect.height()); + isStopped = true; + } } -void Enemy::attack() -{ - if (atkMovie.isValid() and atkMovie.currentFrameNumber() == atkMovie.frameCount() - 1) { - atkTarget->HP.changeCurValue(-atk.getCurValue()); - atkTarget->underAtk = true; - atkMovie.jumpToNextFrame(); - if (atkTarget->HP.getCurValue() <= 0) { - atkTarget->blockNumber.changeCurValue(-1); - atkTarget = nullptr; - isStopped = false; - } +void Enemy::attack() { + if (atkMovie.isValid() && + atkMovie.currentFrameNumber() == atkMovie.frameCount() - 1) { + atkTarget->HP.changeCurValue(-atk.getCurValue()); + atkTarget->underAtk = true; + atkMovie.jumpToNextFrame(); + if (atkTarget->HP.getCurValue() <= 0) { + atkTarget->blockNumber.changeCurValue(-1); + atkTarget = nullptr; + isStopped = false; } + } } bool Enemy::getIsStopped() const { return isStopped; } @@ -156,37 +154,34 @@ QPointF Enemy::getDest() const { return dest; } int Enemy::getpointIndex() { return pointIndex; } -void Enemy::advance(int phase) -{ - if (!phase) { - if (HP.getCurValue() == 0) { - isDead = true; - speed.setCurValue(0); - - if (deathCounter.getCurValue() < deathCounter.getMaxValue()) { - deathCounter.changeCurValue(1); - } else { - game->statistic.money.changeCurValue(money); - delete this; - return; - } - return; - } - - if (atkTarget.isNull()) - aquireTarget(); - if (atkTarget.isNull() == false) { - attack(); - } - - if (isStopped == true) { - ; - } else { - moveForward(); - } +void Enemy::advance(int phase) { + if (!phase) { + if (HP.getCurValue() == 0) { + isDead = true; + speed.setCurValue(0); + + if (deathCounter.getCurValue() < deathCounter.getMaxValue()) { + deathCounter.changeCurValue(1); + } else { + game->statistic.money.changeCurValue(money); + delete this; + return; + } + return; + } + + if (atkTarget.isNull()) aquireTarget(); + if (atkTarget.isNull() == false) { + attack(); + } + + if (isStopped == true) { } else { - update(); + moveForward(); } + } else { + update(); + } } int Enemy::type() const { return Type; } diff --git a/src/enemy/fallenwarrior.cpp b/src/enemy/fallenwarrior.cpp index 1d77515..22f457d 100644 --- a/src/enemy/fallenwarrior.cpp +++ b/src/enemy/fallenwarrior.cpp @@ -1,124 +1,115 @@ +// Copyright 2022 Flying-Tom + #include -FallenWarrior::FallenWarrior(Game* game, QList& path) - : Enemy(game, - path, - 30, - GameValue(200, 200), - GameValue(500, 500), - GameValue(2, 2), - Enemy::WALKING, - 60) -{ - movie.setFileName(":/images/fallenwarrior.gif"); - movie.setCacheMode(QMovie::CacheAll); - movie.start(); +FallenWarrior::FallenWarrior(Game* game, QList* path) + : Enemy(game, path, 30, GameValue(200, 200), + GameValue(500, 500), GameValue(2, 2), Enemy::WALKING, + 60) { + movie.setFileName(":/images/fallenwarrior.gif"); + movie.setCacheMode(QMovie::CacheAll); + movie.start(); - canAttackMelee = true; - canAttackRange = false; + canAttackMelee = true; + canAttackRange = false; } -QRectF FallenWarrior::boundingRect() const -{ - return QRectF(-75, -105, 300, 150); +QRectF FallenWarrior::boundingRect() const { + return QRectF(-75, -105, 300, 150); } -QPainterPath FallenWarrior::shape() const -{ - QPainterPath path; - path.addRect(QRectF(-40, -40, 80, 80)); - return path; +QPainterPath FallenWarrior::shape() const { + QPainterPath path; + path.addRect(QRectF(-40, -40, 80, 80)); + return path; } -void FallenWarrior::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) +void FallenWarrior::paint(QPainter* painter, + const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(option) + Q_UNUSED(widget) - GameItem::paint(painter, option, widget); - if (isDead == true) { - QFont font; - HPMeter->hide(); - font.setPointSizeF(12); - painter->setFont(font); - painter->setPen(Qt::yellow); - painter->drawImage(QRectF(-25, -15, 20, 20), QImage(":images/coin.png")); - painter->drawText(0, 0, QString("+") + QString::number(money)); + GameItem::paint(painter, option, widget); + if (isDead == true) { + QFont font; + HPMeter->hide(); + font.setPointSizeF(12); + painter->setFont(font); + painter->setPen(Qt::yellow); + painter->drawImage(QRectF(-25, -15, 20, 20), QImage(":images/coin.png")); + painter->drawText(0, 0, QString("+") + QString::number(money)); + } else { + if (underAtk == true) { + painter->setOpacity(0.5); + if (blinkCounter.getCurValue() > 0) { + blinkCounter.changeCurValue(-1); + } else { + blinkCounter.setCurValue(blinkCounter.getMaxValue()); + underAtk = false; + } } else { - if (underAtk == true) { - painter->setOpacity(0.5); - if (blinkCounter.getCurValue() > 0) { - blinkCounter.changeCurValue(-1); - } else { - blinkCounter.setCurValue(blinkCounter.getMaxValue()); - underAtk = false; - } - } else { - painter->setOpacity(1); - } - - if (x() < dest.x()) { - painter->drawImage(QRectF(-75, -105, 300, 150), movie.currentImage()); - } else { - painter->drawImage(QRectF(-240, -105, 300, 150), - movie.currentImage().mirrored(true, false)); - } - // painter->drawPath(shape()); + painter->setOpacity(1); } - if (atkTarget != nullptr and atkMovie.isValid()) { - painter->drawImage(atkMovieRect, atkMovie.currentImage()); + if (x() < dest.x()) { + painter->drawImage(QRectF(-75, -105, 300, 150), movie.currentImage()); + } else { + painter->drawImage(QRectF(-240, -105, 300, 150), + movie.currentImage().mirrored(true, false)); } + // painter->drawPath(shape()); + } + + if (atkTarget != nullptr && atkMovie.isValid()) { + painter->drawImage(atkMovieRect, atkMovie.currentImage()); + } } -void FallenWarrior::attack() -{ - if (movie.currentFrameNumber() == movie.frameCount() - 1) { - atkTarget->HP.changeCurValue(-atk.getCurValue()); - atkTarget->underAtk = true; +void FallenWarrior::attack() { + if (movie.currentFrameNumber() == movie.frameCount() - 1) { + atkTarget->HP.changeCurValue(-atk.getCurValue()); + atkTarget->underAtk = true; - movie.jumpToFrame(5); - if (atkTarget->HP.getCurValue() <= 0) { - atkTarget->blockNumber.changeCurValue(-1); - atkTarget = nullptr; - isStopped = false; - } + movie.jumpToFrame(5); + if (atkTarget->HP.getCurValue() <= 0) { + atkTarget->blockNumber.changeCurValue(-1); + atkTarget = nullptr; + isStopped = false; } + } } -void FallenWarrior::advance(int phase) -{ - if (!phase) { - if (HP.getCurValue() == 0) { - isDead = true; - speed.setCurValue(0); +void FallenWarrior::advance(int phase) { + if (!phase) { + if (HP.getCurValue() == 0) { + isDead = true; + speed.setCurValue(0); - if (deathCounter.getCurValue() < deathCounter.getMaxValue()) { - deathCounter.changeCurValue(1); - } else { - game->statistic.money.changeCurValue(money); - delete this; - return; - } - return; - } + if (deathCounter.getCurValue() < deathCounter.getMaxValue()) { + deathCounter.changeCurValue(1); + } else { + game->statistic.money.changeCurValue(money); + delete this; + return; + } + return; + } - if (atkTarget.isNull()) - aquireTarget(); - if (atkTarget.isNull() == false) { - attack(); - } else { - if (movie.currentFrameNumber() > 4) { - movie.jumpToFrame(1); - } - } + if (atkTarget.isNull()) aquireTarget(); + if (atkTarget.isNull() == false) { + attack(); + } else { + if (movie.currentFrameNumber() > 4) { + movie.jumpToFrame(1); + } + } - if (isStopped == true) { - ; - } else { - moveForward(); - } + if (isStopped == true) { } else { - update(); + moveForward(); } + } else { + update(); + } } diff --git a/src/enemy/ghost.cpp b/src/enemy/ghost.cpp index 2c5db38..2dbde4d 100644 --- a/src/enemy/ghost.cpp +++ b/src/enemy/ghost.cpp @@ -1,25 +1,20 @@ +// Copyright 2022 Flying-Tom + #include -Ghost::Ghost(Game* game, QList& path) - : Enemy(game, - path, - 50, - GameValue(500, 500), - GameValue(10, 10), - GameValue(1.5, 1.5), - Enemy::WALKING, - 60) -{ - movie.setFileName(":/images/ghost.gif"); - movie.start(); +Ghost::Ghost(Game* game, QList* path) + : Enemy(game, path, 50, GameValue(500, 500), + GameValue(10, 10), GameValue(1.5, 1.5), + Enemy::WALKING, 60) { + movie.setFileName(":/images/ghost.gif"); + movie.start(); - canAttackMelee = true; - canAttackRange = false; + canAttackMelee = true; + canAttackRange = false; } -QPainterPath Ghost::shape() const -{ - QPainterPath path; - path.addRect(QRectF(-30, -30, 60, 60)); - return path; +QPainterPath Ghost::shape() const { + QPainterPath path; + path.addRect(QRectF(-30, -30, 60, 60)); + return path; } diff --git a/src/enemy/robotsoldier.cpp b/src/enemy/robotsoldier.cpp index 71ad9b2..0ea08a5 100644 --- a/src/enemy/robotsoldier.cpp +++ b/src/enemy/robotsoldier.cpp @@ -1,100 +1,93 @@ +// Copyright 2022 Flying-Tom + #include -RobotSoldier::RobotSoldier(Game* game, QList& path) - : Enemy(game, - path, - 50, - GameValue(3000, 3000), - GameValue(10, 10), - GameValue(0.25, 0.25), - Enemy::WALKING, - 200) - , atkSpeed(50) -{ - movie.setFileName(":/images/robotsoldier.gif"); - movie.start(); +RobotSoldier::RobotSoldier(Game* game, QList* path) + : Enemy(game, path, 50, GameValue(3000, 3000), + GameValue(10, 10), GameValue(0.25, 0.25), + Enemy::WALKING, 200), + atkSpeed(50) { + movie.setFileName(":/images/robotsoldier.gif"); + movie.start(); - atkMovie.setFileName(":/images/robotsoldierattack.gif"); - atkMovie.setSpeed(atkSpeed); - atkMovie.start(); + atkMovie.setFileName(":/images/robotsoldierattack.gif"); + atkMovie.setSpeed(atkSpeed); + atkMovie.start(); - canAttackMelee = true; - canAttackRange = true; + canAttackMelee = true; + canAttackRange = true; } -void RobotSoldier::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) - if (isStopped == true and atkTarget.isNull() == false) { - qreal theta = 0; - QLineF ln(pos(), atkTarget->pos()); - theta = -ln.angle(); +void RobotSoldier::paint(QPainter* painter, + const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(option) + Q_UNUSED(widget) + if (isStopped == true && atkTarget.isNull() == false) { + qreal theta = 0; + QLineF ln(pos(), atkTarget->pos()); + theta = -ln.angle(); - painter->setOpacity(0.7); - painter->rotate(theta); - painter->drawImage(QRectF(-125, -125, 300, 200), atkMovie.currentImage()); - painter->setOpacity(1); - painter->rotate(-theta); - } + painter->setOpacity(0.7); + painter->rotate(theta); + painter->drawImage(QRectF(-125, -125, 300, 200), atkMovie.currentImage()); + painter->setOpacity(1); + painter->rotate(-theta); + } - // Enemy::paint(painter, option, widget); - if (isDead == true) { - QFont font; - HPMeter->hide(); - font.setPointSizeF(12); - painter->setFont(font); - painter->setPen(Qt::yellow); - painter->drawImage(QRectF(-25, -15, 20, 20), QImage(":images/coin.png")); - painter->drawText(0, 0, QString("+") + QString::number(money)); + // Enemy::paint(painter, option, widget); + if (isDead == true) { + QFont font; + HPMeter->hide(); + font.setPointSizeF(12); + painter->setFont(font); + painter->setPen(Qt::yellow); + painter->drawImage(QRectF(-25, -15, 20, 20), QImage(":images/coin.png")); + painter->drawText(0, 0, QString("+") + QString::number(money)); + } else { + if (underAtk == true) { + painter->setOpacity(0.5); + if (blinkCounter.getCurValue() > 0) { + blinkCounter.changeCurValue(-1); + } else { + blinkCounter.setCurValue(blinkCounter.getMaxValue()); + underAtk = false; + } } else { + painter->setOpacity(1); + } - if (underAtk == true) { - painter->setOpacity(0.5); - if (blinkCounter.getCurValue() > 0) { - blinkCounter.changeCurValue(-1); - } else { - blinkCounter.setCurValue(blinkCounter.getMaxValue()); - underAtk = false; - } - } else { - painter->setOpacity(1); - } - - if (x() < dest.x()) { - painter->drawImage(boundingRect(), movie.currentImage()); - } else { - painter->drawImage(boundingRect(), - movie.currentImage().mirrored(true, false)); - } - // painter->drawPath(shape()); - - HPMeter->setRect( - -60, 40, (qreal)HP.getCurValue() / HP.getMaxValue() * 150, 12); + if (x() < dest.x()) { + painter->drawImage(boundingRect(), movie.currentImage()); + } else { + painter->drawImage(boundingRect(), + movie.currentImage().mirrored(true, false)); } + // painter->drawPath(shape()); + + HPMeter->setRect(-60, 40, (qreal)HP.getCurValue() / HP.getMaxValue() * 150, + 12); + } } -QRectF RobotSoldier::boundingRect() const -{ - return QRectF(-80, -120, 160, 160); +QRectF RobotSoldier::boundingRect() const { + return QRectF(-80, -120, 160, 160); } -QPainterPath RobotSoldier::shape() const -{ - QPainterPath path; - path.addRect(QRectF(-50, -90, 128, 128)); - return path; +QPainterPath RobotSoldier::shape() const { + QPainterPath path; + path.addRect(QRectF(-50, -90, 128, 128)); + return path; } -void RobotSoldier::attack() -{ - atkTarget->HP.changeCurValue(-0.05 * (qreal)atkSpeed / 100.0); - atkTarget->underAtk = true; - if (((Tower*)atkTarget.data())->HP.getCurValue() <= 0) { - isStopped = false; - atkSpeed = 50; - } +void RobotSoldier::attack() { + atkTarget->HP.changeCurValue(-0.05 * (qreal)atkSpeed / 100.0); + atkTarget->underAtk = true; + if (qgraphicsitem_cast(atkTarget.data())->HP.getCurValue() <= 0) { + isStopped = false; + atkSpeed = 50; + } - atkSpeed = std::min(atkSpeed + 0.3, 400.0); - atkMovie.setSpeed(atkSpeed); + atkSpeed = std::min(atkSpeed + 0.3, 400.0); + atkMovie.setSpeed(atkSpeed); } diff --git a/src/enemy/shaman.cpp b/src/enemy/shaman.cpp index 95180a6..bd0466a 100644 --- a/src/enemy/shaman.cpp +++ b/src/enemy/shaman.cpp @@ -1,81 +1,74 @@ +// Copyright 2022 Flying-Tom + #include -Shaman::Shaman(Game* game, QList& path) - : Enemy(game, - path, - 50, - GameValue(200, 200), - GameValue(100, 100), - GameValue(1, 100), - Enemy::WALKING, - 150) -{ - movie.setFileName(":/images/shaman.gif"); - movie.start(); +Shaman::Shaman(Game* game, QList* path) + : Enemy(game, path, 50, GameValue(200, 200), + GameValue(100, 100), GameValue(1, 100), + Enemy::WALKING, 150) { + movie.setFileName(":/images/shaman.gif"); + movie.start(); - atkMovie.setFileName(":/images/shamanattack.gif"); - atkMovie.start(); - atkMovieRect.setWidth(160); - atkMovieRect.setHeight(160); + atkMovie.setFileName(":/images/shamanattack.gif"); + atkMovie.start(); + atkMovieRect.setWidth(160); + atkMovieRect.setHeight(160); - canAttackMelee = true; - canAttackRange = false; + canAttackMelee = true; + canAttackRange = false; } -void Shaman::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - if (atkTarget.isNull() == false) { - atkMovieRect.setRect(atkTarget->x() - x() - atkMovieRect.width() / 2, - atkTarget->y() - y() - atkMovieRect.height() / 2, - 160, - 160); - } - Enemy::paint(painter, option, widget); +void Shaman::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + if (atkTarget.isNull() == false) { + atkMovieRect.setRect(atkTarget->x() - x() - atkMovieRect.width() / 2, + atkTarget->y() - y() - atkMovieRect.height() / 2, 160, + 160); + } + Enemy::paint(painter, option, widget); } -QPainterPath Shaman::shape() const -{ - QPainterPath path; - path.addRect(boundingRect()); - return path; +QPainterPath Shaman::shape() const { + QPainterPath path; + path.addRect(boundingRect()); + return path; } -void Shaman::aquireTarget() -{ - QList colliding_items = atkArea->collidingItems(); - foreach (QGraphicsItem* item, colliding_items) { - if (item->type() == Enemy::Type) { - if (((Enemy*)item)->HP.getMaxValue() == ((Enemy*)item)->HP.getCurValue()) - continue; - else { - atkTarget = (Enemy*)item; - break; - } - } +void Shaman::aquireTarget() { + QList colliding_items = atkArea->collidingItems(); + for (QGraphicsItem* item : colliding_items) { + if (item->type() == Enemy::Type) { + if (qgraphicsitem_cast(item)->HP.getMaxValue() == + qgraphicsitem_cast(item)->HP.getCurValue()) { + continue; + } else { + atkTarget = qgraphicsitem_cast(item); + break; + } } + } - if (atkTarget.isNull()) { - isStopped = false; - } else { - // atkMoiveRect.setRect(atkTarget->x() - x() - atkMoiveRect.width() / 2, - // atkTarget->y() - y() - atkMoiveRect.height() / 2, - // atkMoiveRect.width(), - // atkMoiveRect.height()); - // isStopped = true; - // qDebug() << "hasTarget"; - } + if (atkTarget.isNull()) { + isStopped = false; + } else { + // atkMoiveRect.setRect(atkTarget->x() - x() - atkMoiveRect.width() / 2, + // atkTarget->y() - y() - atkMoiveRect.height() / 2, + // atkMoiveRect.width(), + // atkMoiveRect.height()); + // isStopped = true; + // qDebug() << "hasTarget"; + } } -void Shaman::attack() -{ - if (atkTarget->getIsDead() == false - and atkMovie.currentFrameNumber() == atkMovie.frameCount() - 1) { - if (atkTarget != this) { - speed.setCurValue(atkTarget->speed.getCurValue()); - isStopped = ((Enemy*)atkTarget.data())->getIsStopped(); - } - atkTarget->HP.changeCurValue(atk.getCurValue()); - atkMovie.jumpToNextFrame(); - atkTarget.clear(); +void Shaman::attack() { + if (atkTarget->getIsDead() == false && + atkMovie.currentFrameNumber() == atkMovie.frameCount() - 1) { + if (atkTarget != this) { + speed.setCurValue(atkTarget->speed.getCurValue()); + isStopped = qgraphicsitem_cast(atkTarget.data())->getIsStopped(); } + atkTarget->HP.changeCurValue(atk.getCurValue()); + atkMovie.jumpToNextFrame(); + atkTarget.clear(); + } } diff --git a/src/enemy/spirit.cpp b/src/enemy/spirit.cpp index d230c05..e2b872b 100644 --- a/src/enemy/spirit.cpp +++ b/src/enemy/spirit.cpp @@ -1,35 +1,27 @@ +// Copyright 2022 Flying-Tom + #include -Spirit::Spirit(Game* game, QList& path) - : Enemy(game, - path, - 50, - GameValue(500, 500), - GameValue(100, 100), - GameValue(1, 1), - Enemy::WALKING, - 100) -{ - movie.setFileName(":/images/spirit.gif"); - movie.start(); +Spirit::Spirit(Game* game, QList* path) + : Enemy(game, path, 50, GameValue(500, 500), + GameValue(100, 100), GameValue(1, 1), Enemy::WALKING, + 100) { + movie.setFileName(":/images/spirit.gif"); + movie.start(); - atkMovie.setFileName(":/images/spiritattack.gif"); - atkMovie.start(); - atkMovieRect.setWidth(160); - atkMovieRect.setHeight(160); + atkMovie.setFileName(":/images/spiritattack.gif"); + atkMovie.start(); + atkMovieRect.setWidth(160); + atkMovieRect.setHeight(160); - canAttackMelee = true; - canAttackRange = false; + canAttackMelee = true; + canAttackRange = false; } -QRectF Spirit::boundingRect() const -{ - return QRectF(-80, -80, 160, 160); -} +QRectF Spirit::boundingRect() const { return QRectF(-80, -80, 160, 160); } -QPainterPath Spirit::shape() const -{ - QPainterPath path; - path.addRect(QRectF(-40, -40, 80, 80)); - return path; +QPainterPath Spirit::shape() const { + QPainterPath path; + path.addRect(QRectF(-40, -40, 80, 80)); + return path; } diff --git a/src/game.cpp b/src/game.cpp index eb89098..64453f8 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,4 +1,4 @@ -#include +// Copyright 2022 Flying-Tom #include #include @@ -8,193 +8,197 @@ #include #include #include +#include -Game::Game(MainWindow* parent, QString mapConfig) - : QGraphicsView(parent) - , parent(parent) - , scene(this) - , map(this, mapConfig) - , statistic(this) - , mapConfig(mapConfig) - , BGMplayer(this) -{ - /* main UI of the game init */ - scene.setSceneRect(0, 0, Map::width + 320, Map::height); - setFixedSize(Map::width + 320, Map::height); - - setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - - setRenderHint(QPainter::Antialiasing); - setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); - setScene(&scene); - - scene.addItem(&map); - scene.setBackgroundBrush(QPixmap::fromImage(map.mapImage)); - - scene.addItem(&statistic); - - /* game timer init */ - gameSpeed = 3; - - FPSCounterTimer.start(); - - connect(&advanceTimer, SIGNAL(timeout()), &scene, SLOT(advance())); - connect(&advanceTimer, SIGNAL(timeout()), this, SLOT(spawnEnemy())); - - updateGameSpeed(); - - /* media init */ - // BGMplayer->setMedia(QUrl("D:/Qtprojects/build-TDGame-Desktop_Qt_5_15_2_MinGW_64_bit-Release/PartyTime.mp3")); - BGMplayer.setVolume(50); - BGMplayer.play(); +Game::Game(MainWindow *parent, QString *mapConfig) + : QGraphicsView(parent), + parent(parent), + scene(this), + map(this, mapConfig), + statistic(this), + mapConfig(mapConfig), + BGMplayer(this) { + /* main UI of the game init */ + scene.setSceneRect(0, 0, GameMap::width + 320, GameMap::height); + setFixedSize(GameMap::width + 320, GameMap::height); + + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + + setRenderHint(QPainter::Antialiasing); + setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); + setScene(&scene); + + scene.addItem(&map); + scene.setBackgroundBrush(QPixmap::fromImage(map.mapImage)); + + scene.addItem(&statistic); + + /* game timer init */ + gameSpeed = 3; + + FPSCounterTimer.start(); + + connect(&advanceTimer, SIGNAL(timeout()), &scene, SLOT(advance())); + connect(&advanceTimer, SIGNAL(timeout()), this, SLOT(spawnEnemy())); + + updateGameSpeed(); + + /* media init */ + // BGMplayer->setMedia(QUrl("D:/Qtprojects/build-TDGame-Desktop_Qt_5_15_2_MinGW_64_bit-Release/PartyTime.mp3")); + BGMplayer.setVolume(50); + BGMplayer.play(); + + /* enemy spawning system based on probability init */ + + probEnemy = {100, 50, 20, 100, 100, 100, 100, 50}; + probRec = {0}; + for (size_t i = 1; i <= probEnemy.size(); i++) { + probRec.emplace_back(probRec[i - 1] + probEnemy[i - 1]); + } + probSum = std::accumulate(probEnemy.begin(), probEnemy.end(), 0); + + enemyLambda.emplace_back( + [&](Enemy *&enemy) { enemy = new BlackWarrior(this, &map.walkingPath); }); + enemyLambda.emplace_back( + [&](Enemy *&enemy) { enemy = new Dragon(this, &map.flyingPath); }); + enemyLambda.emplace_back( + [&](Enemy *&enemy) { enemy = new RobotSoldier(this, &map.walkingPath); }); + enemyLambda.emplace_back( + [&](Enemy *&enemy) { enemy = new Shaman(this, &map.walkingPath); }); + enemyLambda.emplace_back( + [&](Enemy *&enemy) { enemy = new Spirit(this, &map.walkingPath); }); + enemyLambda.emplace_back([&](Enemy *&enemy) { + enemy = new FallenWarrior(this, &map.walkingPath); + }); + enemyLambda.emplace_back( + [&](Enemy *&enemy) { enemy = new Ghost(this, &map.walkingPath); }); + enemyLambda.emplace_back([&](Enemy *&enemy) { + if (QRandomGenerator::global()->bounded(0, 15) < 15) { + QList path; + path << map.flyingPath.first(); + int pathLen = QRandomGenerator::global()->bounded(5, 15); + while (pathLen--) { + // path << + // Map::BlockToCoordinate({QRandomGenerator::global()->bounded(0, + // 16), + // QRandomGenerator::global()->bounded(0, + // 12)}); + path << QPointF(QRandomGenerator::global()->bounded(0, 1280), + QRandomGenerator::global()->bounded(0, 960)); + } + path << map.flyingPath.last(); + enemy = new CowardPlane(this, &path); + } else { + enemy = new CowardPlane(this, &map.flyingPath); + } + }); - /* enemy spawning system based on probability init */ + /* actually spawn the enemies */ + // Enemy *enemy = new RobotSoldier(this, map.path); + // scene.addItem(enemy); - probEnemy = { 100, 50, 20, 100, 100, 100, 100, 50 }; - probRec = { 0 }; - for (size_t i = 1; i <= probEnemy.size(); i++) { - probRec.emplace_back(probRec[i - 1] + probEnemy[i - 1]); - } - probSum = std::accumulate(probEnemy.begin(), probEnemy.end(), 0); - - enemyLambda.emplace_back( - [&](Enemy*& enemy) { enemy = new BlackWarrior(this, map.walkingPath); }); - enemyLambda.emplace_back([&](Enemy*& enemy) { enemy = new Dragon(this, map.flyingPath); }); - enemyLambda.emplace_back( - [&](Enemy*& enemy) { enemy = new RobotSoldier(this, map.walkingPath); }); - enemyLambda.emplace_back([&](Enemy*& enemy) { enemy = new Shaman(this, map.walkingPath); }); - enemyLambda.emplace_back([&](Enemy*& enemy) { enemy = new Spirit(this, map.walkingPath); }); - enemyLambda.emplace_back( - [&](Enemy*& enemy) { enemy = new FallenWarrior(this, map.walkingPath); }); - enemyLambda.emplace_back([&](Enemy*& enemy) { enemy = new Ghost(this, map.walkingPath); }); - enemyLambda.emplace_back([&](Enemy*& enemy) { - if (QRandomGenerator::global()->bounded(0, 15) < 15) { - QList path; - path << map.flyingPath.first(); - int pathLen = QRandomGenerator::global()->bounded(5, 15); - while (pathLen--) { - // path << Map::BlockToCoordinate({QRandomGenerator::global()->bounded(0, 16), - // QRandomGenerator::global()->bounded(0, 12)}); - path << QPointF(QRandomGenerator::global()->bounded(0, 1280), - QRandomGenerator::global()->bounded(0, 960)); - } - path << map.flyingPath.last(); - enemy = new CowardPlane(this, path); - } else { - enemy = new CowardPlane(this, map.flyingPath); - } - }); - - /* actually spawn the enemies */ - // Enemy *enemy = new RobotSoldier(this, map.path); - // scene.addItem(enemy); - - createEnemy(1000); + createEnemy(1000); } -Game::~Game() { } - -void Game::endThisGame(QString s) -{ - if (s == QString("Road Error!")) { - QMessageBox message(QMessageBox::NoIcon, "Initializing Fail", "Road Error!"); - message.setStyleSheet("QLabel{min-width: 200px;min-height: 100px;}" - "border-image: url(:/images/messagebox.png);" - "QWidget{border-top-left-radius:15px;border-top-right-radius:5px;}"); - - message.setWindowModality(Qt::NonModal); - message.setWindowFlags(Qt::FramelessWindowHint); - message.setWindowOpacity(0.8); - //message.setMask(QPixmap(":/images/messagebox.png").scaled(200, 100).mask()); - message.setButtonText(QMessageBox::Ok, "Return"); - message.exec(); - } else if (s == QString("GameOver")) { - advanceTimer.disconnect(); - spawnTimer.disconnect(); - //map.disconnect(); - - QMessageBox message(QMessageBox::NoIcon, "Mission Failed", "Game Over😈"); - message.setWindowModality(Qt::NonModal); - message.resize(200, 50); - message.setButtonText(QMessageBox::Ok, "Return"); - message.exec(); - } - parent->setGame(nullptr); - deleteLater(); +Game::~Game() {} + +void Game::endThisGame(QString s) { + if (s == QString("Road Error!")) { + QMessageBox message(QMessageBox::NoIcon, "Initializing Fail", + "Road Error!"); + message.setStyleSheet( + "QLabel{min-width: 200px;min-height: 100px;}" + "border-image: url(:/images/messagebox.png);" + "QWidget{border-top-left-radius:15px;border-top-right-radius:5px;}"); + + message.setWindowModality(Qt::NonModal); + message.setWindowFlags(Qt::FramelessWindowHint); + message.setWindowOpacity(0.8); + // message.setMask(QPixmap(":/images/messagebox.png").scaled(200, + // 100).mask()); + message.setButtonText(QMessageBox::Ok, "Return"); + message.exec(); + } else if (s == QString("GameOver")) { + advanceTimer.disconnect(); + spawnTimer.disconnect(); + // map.disconnect(); + + QMessageBox message(QMessageBox::NoIcon, "Mission Failed", "Game Over😈"); + message.setWindowModality(Qt::NonModal); + message.resize(200, 50); + message.setButtonText(QMessageBox::Ok, "Return"); + message.exec(); + } + parent->setGame(nullptr); + deleteLater(); } -void Game::updateGameSpeed() -{ - advanceTimer.start(33 / gameSpeed); +void Game::updateGameSpeed() { + advanceTimer.start(33 / gameSpeed); - // if (spawnTimer.isActive() == true) { - // spawnTimer.start(1000 / gameSpeed); - // } + // if (spawnTimer.isActive() == true) { + // spawnTimer.start(1000 / gameSpeed); + // } } -void Game::createEnemy(int num) -{ - enemyNum = 0; - enemyMaxnum = num; +void Game::createEnemy(int num) { + enemyNum = 0; + enemyMaxnum = num; - spawnTimer.start(2500 / gameSpeed); + spawnTimer.start(2500 / gameSpeed); } -void Game::spawnEnemy() -{ - static int count = 0; - if (count < 100) - count++; - else { - Enemy* enemy = nullptr; - enemyLambda[RandEnemyIndex()](enemy); - - scene.addItem(enemy); - count = 0; - } +void Game::spawnEnemy() { + static int count = 0; + if (count < 100) { + count++; + } else { + Enemy *enemy = nullptr; + enemyLambda[RandEnemyIndex()](enemy); + + scene.addItem(enemy); + count = 0; + } } -int Game::RandEnemyIndex() -{ - int index = QRandomGenerator::global()->bounded(0, probSum + 1); - for (size_t i = 0; i < probRec.size(); i++) { - if (probRec[i] <= index and index < probRec[i + 1]) - return i; - } - return 0; +int Game::RandEnemyIndex() { + int index = QRandomGenerator::global()->bounded(0, probSum + 1); + for (size_t i = 0; i < probRec.size(); i++) { + if (probRec[i] <= index && index < probRec[i + 1]) return i; + } + return 0; } -void Game::keyPressEvent(QKeyEvent* event) -{ - switch (event->key()) { +void Game::keyPressEvent(QKeyEvent *event) { + switch (event->key()) { case Qt::Key_N: - createEnemy(100); - break; + createEnemy(100); + break; case Qt::Key_T: - createEnemy(1000); - break; + createEnemy(1000); + break; case Qt::Key_Left: - gameSpeed = std::max(1, gameSpeed - 1); - updateGameSpeed(); - break; + gameSpeed = std::max(1, gameSpeed - 1); + updateGameSpeed(); + break; case Qt::Key_Right: - gameSpeed = std::min(5, gameSpeed + 1); - updateGameSpeed(); - break; + gameSpeed = std::min(5, gameSpeed + 1); + updateGameSpeed(); + break; case Qt::Key_F5: - if (advanceTimer.isActive()) { - advanceTimer.stop(); - statistic.setEnabled(false); - scene.advance(); - - } else { - statistic.setEnabled(true); - advanceTimer.start(); - } - break; + if (advanceTimer.isActive()) { + advanceTimer.stop(); + statistic.setEnabled(false); + scene.advance(); + + } else { + statistic.setEnabled(true); + advanceTimer.start(); + } + break; default: - event->ignore(); - break; - } + event->ignore(); + break; + } } diff --git a/src/gameitem.cpp b/src/gameitem.cpp index 745f4cd..50f4423 100644 --- a/src/gameitem.cpp +++ b/src/gameitem.cpp @@ -1,82 +1,62 @@ +// Copyright 2022 Flying-Tom + #include -GameItem::GameItem() -{ - atkTarget = nullptr; - atkArea = new QGraphicsEllipseItem(this); - QPen pen = atkArea->pen(); - pen.setWidth(3); - pen.setColor(QColor(255, 255, 255)); - pen.setStyle(Qt::DotLine); - atkArea->setPen(pen); - atkArea->hide(); - HPMeter = new QGraphicsRectItem(this); - HPMeter->setPen(Qt::NoPen); - underAtk = false; - blinkCounter = GameValue(10, 10); +GameItem::GameItem() { + atkTarget = nullptr; + atkArea = new QGraphicsEllipseItem(this); + QPen pen = atkArea->pen(); + pen.setWidth(3); + pen.setColor(QColor(255, 255, 255)); + pen.setStyle(Qt::DotLine); + atkArea->setPen(pen); + atkArea->hide(); + HPMeter = new QGraphicsRectItem(this); + HPMeter->setPen(Qt::NoPen); + underAtk = false; + blinkCounter = GameValue(10, 10); } -GameItem::~GameItem() -{ - delete atkArea; - delete HPMeter; +GameItem::~GameItem() { + delete atkArea; + delete HPMeter; } -QRectF GameItem::boundingRect() const -{ - return QRectF(-40, -40, 80, 80); -} +QRectF GameItem::boundingRect() const { return QRectF(-40, -40, 80, 80); } -void GameItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(painter) - Q_UNUSED(option) - Q_UNUSED(widget) - if (HP.getCurValue() == 0) { - HPMeter->hide(); - } else { - HPMeter->setRect(-30, 40, (qreal)HP.getCurValue() / HP.getMaxValue() * 60, 8); - } +void GameItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(painter) + Q_UNUSED(option) + Q_UNUSED(widget) + if (HP.getCurValue() == 0) { + HPMeter->hide(); + } else { + HPMeter->setRect(-30, 40, (qreal)HP.getCurValue() / HP.getMaxValue() * 60, + 8); + } } -bool GameItem::getIsDead() const -{ - return isDead; -} +bool GameItem::getIsDead() const { return isDead; } -void GameItem::setIsDead(bool newIsDead) -{ - isDead = newIsDead; -} +void GameItem::setIsDead(bool newIsDead) { isDead = newIsDead; } -QPointer GameItem::getAtkTarget() const -{ - return atkTarget; -} +QPointer GameItem::getAtkTarget() const { return atkTarget; } -void GameItem::setAtkTarget(QPointer newAtkTarget) -{ - atkTarget = newAtkTarget; +void GameItem::setAtkTarget(QPointer newAtkTarget) { + atkTarget = newAtkTarget; } -const QString& GameItem::getName() const -{ - return name; -} +const QString& GameItem::getName() const { return name; } -void GameItem::setName(const QString& newName) -{ - name = newName; -} +void GameItem::setName(const QString& newName) { name = newName; } -void GameItem::mousePressEvent(QGraphicsSceneMouseEvent* event) -{ - Q_UNUSED(event); - atkArea->show(); +void GameItem::mousePressEvent(QGraphicsSceneMouseEvent* event) { + Q_UNUSED(event); + atkArea->show(); } -void GameItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) -{ - Q_UNUSED(event); - atkArea->hide(); +void GameItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) { + Q_UNUSED(event); + atkArea->hide(); } diff --git a/src/gamemap.cpp b/src/gamemap.cpp new file mode 100644 index 0000000..c0ff876 --- /dev/null +++ b/src/gamemap.cpp @@ -0,0 +1,376 @@ +// Copyright 2022 Flying-Tom + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static int blockWidthNumber = 16; +static int blockHeightNumber = 12; + +template +Val bounded(Val minx, Val x, Val maxx) { + if (x < minx) + x = minx; + else if (x > maxx) + x = maxx; + return x; +} + +GameMap::GameMap(Game* game, QString* mapConfig) + : game(game), + mapConfig(mapConfig), + destinationMovie(":/images/startbutton.gif"), + towerShadow(nullptr) { + setAcceptDrops(true); + + loadConfig(); + + for (int i = 0; i < road.length() - 1; i++) { + if (road[i].x() == road[i + 1].x()) { + for (int y = std::min(road[i].y(), road[i + 1].y()), + y1 = std::max(road[i].y(), road[i + 1].y()), + x = bounded(0, road[i].x(), blockWidthNumber); + y <= y1; y++) { + isroad[x][bounded(0, y, blockHeightNumber)] = true; + } + } else if (road[i].y() == road[i + 1].y()) { + for (int x = std::min(road[i].x(), road[i + 1].x()), + x1 = std::max(road[i].x(), road[i + 1].x()), + y = bounded(0, road[i].y(), blockHeightNumber); + x <= x1; x++) { + isroad[bounded(0, x, blockWidthNumber)][y] = true; + } + } else { + game->endThisGame("Road Error!"); + } + } + + destinationMovie.setSpeed(150); + destinationMovie.start(); +} + +GameMap::~GameMap() { + for (size_t i = 0; i < 17; i++) { + qDeleteAll(itemoccupied[i]); + isroad[i].clear(); + isoccupied[i].clear(); + itemoccupied[i].clear(); + } + +#ifdef MAPGENERATING + for (auto i : pointCount) + std::cout << "(" << i.first << "," << i.second << ")|"; + std::cout << std::endl; +#endif +} + +QPoint GameMap::CoordinateToBlock(QPointF p) { + qreal x = p.x() / 80, y = p.y() / 80; + return QPoint((qint32)x, (qint32)y); +} + +QPointF GameMap::BlockToCoordinate(QPoint p) { + return QPointF(p.x() * 80 + 40, p.y() * 80 + 40); +} + +bool GameMap::IsRoad(QPoint p) { return isroad[p.x()][p.y()]; } + +bool GameMap::IsOccupied(QPoint p) { return isoccupied[p.x()][p.y()]; } + +QGraphicsItem* GameMap::ItemOccupied(QPoint p) { + return itemoccupied[p.x()][p.y()]; +} + +void GameMap::Occupy(QGraphicsItem* item, QPointF pos) { + QPoint p = CoordinateToBlock(pos); + itemoccupied[p.x()][p.y()] = item; + isoccupied[p.x()][p.y()] = true; + +#ifdef MAPGENERATING + pointCount.insert({p.x(), p.y()}); +#endif +} + +void GameMap::Destory(QPointF pos) { + QPoint p = CoordinateToBlock(pos); + + isoccupied[p.x()][p.y()] = false; + itemoccupied[p.x()][p.y()] = nullptr; +} + +QRectF GameMap::boundingRect() const { return QRectF(0, 0, width, height); } + +void GameMap::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(painter) + Q_UNUSED(option) + Q_UNUSED(widget) + QPointF destination = BlockToCoordinate(road.last()); + painter->drawImage( + QRectF(destination.x() - 100, destination.y() - 128, 160, 160), + destinationMovie.currentImage()); + + if (towerShadow) { + painter->setOpacity(0.6); + QPen pen; + pen.setWidth(3); + pen.setColor(QColor(255, 255, 255)); + pen.setStyle(Qt::DotLine); + painter->setPen(pen); + QRectF r = towerShadow->atkArea->boundingRect(); + + painter->drawImage(towerShadowRect, towerShadow->getImage()); + painter->drawEllipse(QRectF(r.x() + towerShadowRect.x() + 40, + r.y() + towerShadowRect.y() + 40, r.width(), + r.height())); + } + + if (game->advanceTimer.isActive() == false) { + painter->drawImage(QRectF(720 - 64, 480 - 64, 128, 128), + QImage(":/images/pause.png")); + } +} + +void GameMap::advance(int phase) { + if (!phase) { + } else { + update(); + } +} + +bool GameMap::outofScreen(QGraphicsItem* p) { + return !(((0 <= p->pos().x()) && (p->pos().x() <= width)) && + ((0 <= p->pos().y()) && (p->pos().y() <= height))); +} + +void GameMap::mousePressEvent(QGraphicsSceneMouseEvent* event) { +#ifdef MAPGENERATING + QPoint p = CoordinateToBlock(event->pos()); + pointCount.insert({p.x(), p.y()}); + // qDebug() << CoordinateToBlock(event->pos()) << "|"; +#endif + event->ignore(); +} + +void GameMap::dragEnterEvent(QGraphicsSceneDragDropEvent* event) { + if (event->mimeData()->hasText()) { + event->setAccepted(true); + update(); + } else { + event->setAccepted(false); + } +} + +void GameMap::dragMoveEvent(QGraphicsSceneDragDropEvent* event) { + if (event->mimeData()->hasText()) { + QString s = event->mimeData()->text(); + addTowerShadow(s, event->pos()); +#ifdef MAPGENERATING + if (s == "guntower") addTower(s, event->pos()); +#endif + } +} + +void GameMap::dropEvent(QGraphicsSceneDragDropEvent* event) { + if (event->mimeData()->hasText()) { + QString s = event->mimeData()->text(); + addTower(s, event->pos()); + if (towerShadow) { + delete towerShadow; + towerShadow = nullptr; + } + } +} + +void GameMap::loadConfig() { + isroad.resize(blockWidthNumber + 1); + isoccupied.resize(blockWidthNumber + 1); + itemoccupied.resize(blockWidthNumber + 1); + + for (int i = 0; i < blockWidthNumber + 1; i++) { + isroad[i].resize(blockHeightNumber + 1); + isoccupied[i].resize(blockHeightNumber + 1); + itemoccupied[i].resize(blockHeightNumber + 1); + } + + QSettings ConfigIni(*mapConfig, QSettings::IniFormat, 0); + + mapImage = QImage(ConfigIni.value("/map/image").toString()); + + char buf[65536]; + char* point = nullptr; + char* save_ptr = NULL; + memset(buf, '\0', sizeof(buf)); + strncpy(buf, + ConfigIni.value("/path/walkingPathPoints") + .toString() + .toStdString() + .c_str(), + sizeof(buf)); + point = strtok_r(buf, "|", &save_ptr); + while (point != NULL) { + int x, y; + sscanf(point, "(%d,%d)", &x, &y); + road << QPoint(x, y); + walkingPath << BlockToCoordinate({x, y}); + point = strtok_r(NULL, "|", &save_ptr); + } + + memset(buf, '\0', sizeof(buf)); + strncpy(buf, + ConfigIni.value("/path/flyingPathPoints") + .toString() + .toStdString() + .c_str(), + sizeof(buf)); + + point = strtok_r(buf, "|", &save_ptr); + while (point != NULL) { + int x, y; + sscanf(point, "(%d,%d)", &x, &y); + flyingPath << BlockToCoordinate({x, y}); + point = strtok_r(NULL, "|", &save_ptr); + } + + memset(buf, '\0', sizeof(buf)); + strncpy( + buf, + ConfigIni.value("/landform/obstacles").toString().toStdString().c_str(), + sizeof(buf)); + point = strtok_r(buf, "|", &save_ptr); + while (point != NULL) { + int x, y; + sscanf(point, "(%d,%d)", &x, &y); + Occupy(nullptr, BlockToCoordinate({x, y})); + point = strtok_r(NULL, "|", &save_ptr); + } +} + +void GameMap::addTower(QString s, QPointF pos) { + QPoint block = CoordinateToBlock(pos); + if (IsOccupied(block) == false) { + pos = BlockToCoordinate(block); + + Tower* tower = nullptr; + switch (Shop::map[s]) { + case 0: + tower = new GunTower(this); + break; + case 1: + tower = new Bomb(this); + break; + case 2: + tower = new Repeller(this); + break; + case 3: + tower = new LaserTower(this); + break; + case 4: + tower = new MissileTower(this); + break; + case 5: + tower = new SawTooth(this); + break; + case 6: + tower = new CampFire(this); + break; + case 7: + tower = new Shield(this); + break; + default: + assert(false); + break; + } + + if ((tower->getAtkType() == Tower::MELEE && IsRoad(block) == false) || + (tower->getAtkType() == Tower::RANGE && IsRoad(block) == true)) { + delete tower; + return; + } + + if (game->statistic.money.getCurValue() >= Shop::cost[Shop::map[s]]) { + game->statistic.money.changeCurValue(-Shop::cost[Shop::map[s]]); + } else { + delete tower; + return; + } + + tower->setParentItem(this); + tower->setPos(pos); + tower->infopanel.setParentItem(this); + tower->infopanel.hide(); + Occupy(tower, tower->pos()); + } else { + Tower* t = qgraphicsitem_cast(ItemOccupied(block)); + if (s == QString("repeller") && t->getName() == QString("repeller")) { + t->HP.setCurValue(t->HP.getMaxValue()); + } + } +} + +void GameMap::addTowerShadow(QString s, QPointF pos) { + QPoint block = CoordinateToBlock(pos); + if (IsOccupied(block) == false) { + pos = BlockToCoordinate(block); + + if (towerShadow == nullptr) { + switch (Shop::map[s]) { + case 0: + towerShadow = new GunTower(nullptr); + break; + case 1: + towerShadow = new Bomb(nullptr); + break; + case 2: + towerShadow = new Repeller(nullptr); + break; + case 3: + towerShadow = new LaserTower(nullptr); + break; + case 4: + towerShadow = new MissileTower(nullptr); + break; + case 5: + towerShadow = new SawTooth(this); + break; + case 6: + towerShadow = new CampFire(this); + break; + case 7: + towerShadow = new Shield(this); + break; + default: + qDebug() << Shop::map[s]; + assert(false); + break; + } + } + + if ((towerShadow->getAtkType() == Tower::MELEE && IsRoad(block) == true) || + (towerShadow->getAtkType() == Tower::RANGE && IsRoad(block) == false)) { + QRectF r = towerShadow->boundingRect(); + towerShadowRect = + QRectF(pos.x() + r.x(), pos.y() + r.y(), r.width(), r.height()); + } + } +} + +void GameMap::removeTower(QPointF pos) { + QPoint block = CoordinateToBlock(pos); + if (IsOccupied(block) == false) return; + Tower* t = qgraphicsitem_cast(ItemOccupied(block)); + game->statistic.money.changeCurValue(Shop::cost[Shop::map[t->getName()]] / 2); + + Destory(block); +} diff --git a/src/main.cpp b/src/main.cpp index eccdc52..43b1ca5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,15 +1,17 @@ -#include +// Copyright 2022 Flying-Tom + #include #include -int main(int argc, char* argv[]) -{ - QApplication a(argc, argv); +#include + +int main(int argc, char* argv[]) { + QApplication a(argc, argv); - MainWindow w; - w.setWindowIcon(QIcon(":/images/icon.png")); - w.setWindowTitle("Tom's TD Game"); - w.show(); + MainWindow w; + w.setWindowIcon(QIcon(":/images/icon.png")); + w.setWindowTitle("Tom's TD Game"); + w.show(); - return a.exec(); + return a.exec(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index c3d7764..3ba7cbb 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1,129 +1,120 @@ +// Copyright 2022 Flying-Tom + +#include + #include #include -#include -MainWindow::MainWindow(QWidget* parent) - : QMainWindow(parent) -{ - setFixedSize(1600, 960); - move((QApplication::desktop()->width() - width()) / 2, - (QApplication::desktop()->height() - height()) / 2 - 50); - starticonangle = 0; - game = nullptr; - mapConfig = QString(":maps/map1.txt"); - setCursor(QPixmap(":images/mouse.png")); - - QWidget* menu = new QWidget(this); - - setCentralWidget(menu); - - QString menuButtonStyle( - "QPushButton{border-image: url(:/images/startbutton.png);color:rgb(255,255,255);font:bold " - "20px;}" - "QPushButton:hover{border-image: " - "url(:/images/lightstartbutton.png);color:rgb(255,255,255);font:bold " - "26px;}"); - - QPushButton* startGameButton = new QPushButton("Start Game"); - - startGameButton->setParent(menu); - startGameButton->move(700, 460); - startGameButton->resize(200, 100); - startGameButton->setStyleSheet(menuButtonStyle); - - connect(startGameButton, &QPushButton::clicked, this, &MainWindow::startGame); - - QPushButton* chooseMapButton = new QPushButton("Choose Map"); - chooseMapButton->setParent(menu); - chooseMapButton->move(700, 580); - chooseMapButton->resize(200, 100); - chooseMapButton->setStyleSheet(menuButtonStyle); - - QPushButton* exportMapButton = new QPushButton("Export Map"); - exportMapButton->setParent(menu); - exportMapButton->move(700, 700); - exportMapButton->resize(200, 100); - exportMapButton->setStyleSheet(menuButtonStyle); - - QPushButton* ExitButton = new QPushButton("Exit Game"); - ExitButton->setParent(menu); - ExitButton->move(700, 820); - ExitButton->resize(200, 100); - ExitButton->setStyleSheet(menuButtonStyle); - - connect(chooseMapButton, &QPushButton::clicked, this, &MainWindow::chooseMap); - connect(exportMapButton, &QPushButton::clicked, this, &MainWindow::exportMap); - connect(ExitButton, &QPushButton::clicked, this, &MainWindow::close); - - menu->show(); +MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) { + setFixedSize(1600, 960); + move((QApplication::desktop()->width() - width()) / 2, + (QApplication::desktop()->height() - height()) / 2 - 50); + starticonangle = 0; + game = nullptr; + mapConfig = QString(":maps/map1.txt"); + setCursor(QPixmap(":images/mouse.png")); + + QWidget* menu = new QWidget(this); + + setCentralWidget(menu); + + QString menuButtonStyle( + "QPushButton{border-image: " + "url(:/images/startbutton.png);color:rgb(255,255,255);font:bold " + "20px;}" + "QPushButton:hover{border-image: " + "url(:/images/lightstartbutton.png);color:rgb(255,255,255);font:bold " + "26px;}"); + + QPushButton* startGameButton = new QPushButton("Start Game"); + + startGameButton->setParent(menu); + startGameButton->move(700, 460); + startGameButton->resize(200, 100); + startGameButton->setStyleSheet(menuButtonStyle); + + connect(startGameButton, &QPushButton::clicked, this, &MainWindow::startGame); + + QPushButton* chooseMapButton = new QPushButton("Choose Map"); + chooseMapButton->setParent(menu); + chooseMapButton->move(700, 580); + chooseMapButton->resize(200, 100); + chooseMapButton->setStyleSheet(menuButtonStyle); + + QPushButton* exportMapButton = new QPushButton("Export Map"); + exportMapButton->setParent(menu); + exportMapButton->move(700, 700); + exportMapButton->resize(200, 100); + exportMapButton->setStyleSheet(menuButtonStyle); + + QPushButton* ExitButton = new QPushButton("Exit Game"); + ExitButton->setParent(menu); + ExitButton->move(700, 820); + ExitButton->resize(200, 100); + ExitButton->setStyleSheet(menuButtonStyle); + + connect(chooseMapButton, &QPushButton::clicked, this, &MainWindow::chooseMap); + connect(exportMapButton, &QPushButton::clicked, this, &MainWindow::exportMap); + connect(ExitButton, &QPushButton::clicked, this, &MainWindow::close); + + menu->show(); } -void MainWindow::keyPressEvent(QKeyEvent* event) -{ - switch (event->key()) { +void MainWindow::keyPressEvent(QKeyEvent* event) { + switch (event->key()) { case Qt::Key_Escape: - if (game) { - delete game; - game = nullptr; - } - break; + if (game) { + delete game; + game = nullptr; + } + break; case Qt::Key_S: - startGame(); - break; + startGame(); + break; default: - event->ignore(); - break; - } + event->ignore(); + break; + } } -void MainWindow::paintEvent(QPaintEvent* e) -{ - Q_UNUSED(e) - QPainter p(this); - p.setOpacity(1); - p.drawImage(rect(), QImage(":/images/startupbackground1.png")); - p.setOpacity(0.6); - p.drawImage(rect(), QImage(":/images/startupbackground2.png")); - p.setOpacity(1); - p.drawImage(QRectF(604, 200, 391, 180), QImage(":/images/gametitle.png")); - p.drawImage(QRectF(768, 120, 64, 64), QImage(":/images/icon.png")); +void MainWindow::paintEvent(QPaintEvent* e) { + Q_UNUSED(e) + QPainter p(this); + p.setOpacity(1); + p.drawImage(rect(), QImage(":/images/startupbackground1.png")); + p.setOpacity(0.6); + p.drawImage(rect(), QImage(":/images/startupbackground2.png")); + p.setOpacity(1); + p.drawImage(QRectF(604, 200, 391, 180), QImage(":/images/gametitle.png")); + p.drawImage(QRectF(768, 120, 64, 64), QImage(":/images/icon.png")); } -Game* MainWindow::getGame() const -{ - return game; -} +Game* MainWindow::getGame() const { return game; } -void MainWindow::setGame(Game* newGame) -{ - game = newGame; -} +void MainWindow::setGame(Game* newGame) { game = newGame; } -void MainWindow::startGame() -{ - game = new Game(this, mapConfig); - game->show(); +void MainWindow::startGame() { + game = new Game(this, &mapConfig); + game->show(); } -void MainWindow::chooseMap() -{ - QString path = QFileDialog::getOpenFileName(this, - tr("Open File"), - "../TDGame/resource/maps", - tr("Text File (*.txt)")); - if (path.isEmpty()) { - return; - } - mapConfig = QString(path); +void MainWindow::chooseMap() { + QString path = QFileDialog::getOpenFileName(this, tr("Open File"), + "../TDGame/resource/maps", + tr("Text File (*.txt)")); + if (path.isEmpty()) { + return; + } + mapConfig = QString(path); } -void MainWindow::exportMap() -{ - QString path = QFileDialog::getSaveFileName(this, tr("Open File"), ".", tr("Text File (*.txt)")); - if (path.isEmpty()) { - return; - } - QFile::copy(mapConfig, path); - QFile exportConfig(path); - exportConfig.setPermissions(QFile::WriteUser | QFile::ReadUser); +void MainWindow::exportMap() { + QString path = QFileDialog::getSaveFileName(this, tr("Open File"), ".", + tr("Text File (*.txt)")); + if (path.isEmpty()) { + return; + } + QFile::copy(mapConfig, path); + QFile exportConfig(path); + exportConfig.setPermissions(QFile::WriteUser | QFile::ReadUser); } diff --git a/src/map.cpp b/src/map.cpp deleted file mode 100644 index 99a3aca..0000000 --- a/src/map.cpp +++ /dev/null @@ -1,396 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -static int blockWidthNumber = 16; -static int blockHeightNumber = 12; - -template -Val bounded(Val minx, Val x, Val maxx) -{ - if (x < minx) - x = minx; - else if (x > maxx) - x = maxx; - return x; -} - -Map::Map(Game* game, QString& mapConfig) - : game(game) - , mapConfig(mapConfig) - , destinationMovie(":/images/startbutton.gif") - , towerShadow(nullptr) - -{ - setAcceptDrops(true); - - loadConfig(); - - for (int i = 0; i < road.length() - 1; i++) { - if (road[i].x() == road[i + 1].x()) { - for (int y = std::min(road[i].y(), road[i + 1].y()), - y1 = std::max(road[i].y(), road[i + 1].y()), - x = bounded(0, road[i].x(), blockWidthNumber); - y <= y1; - y++) { - isroad[x][bounded(0, y, blockHeightNumber)] = true; - } - } else if (road[i].y() == road[i + 1].y()) { - for (int x = std::min(road[i].x(), road[i + 1].x()), - x1 = std::max(road[i].x(), road[i + 1].x()), - y = bounded(0, road[i].y(), blockHeightNumber); - x <= x1; - x++) { - isroad[bounded(0, x, blockWidthNumber)][y] = true; - } - } else { - game->endThisGame("Road Error!"); - } - } - - destinationMovie.setSpeed(150); - destinationMovie.start(); -} - -Map::~Map() -{ - for (size_t i = 0; i < 17; i++) { - qDeleteAll(itemoccupied[i]); - isroad[i].clear(); - isoccupied[i].clear(); - itemoccupied[i].clear(); - } - -#ifdef MAPGENERATING - for (auto i : pointCount) - std::cout << "(" << i.first << "," << i.second << ")|"; - std::cout << std::endl; -#endif -} - -QPoint Map::CoordinateToBlock(QPointF p) -{ - qreal x = p.x() / 80, y = p.y() / 80; - return QPoint((long)x, (long)y); -} - -QPointF Map::BlockToCoordinate(QPoint p) -{ - return QPointF(p.x() * 80 + 40, p.y() * 80 + 40); -} - -bool Map::IsRoad(QPoint p) -{ - return isroad[p.x()][p.y()]; -} - -bool Map::IsOccupied(QPoint p) -{ - return isoccupied[p.x()][p.y()]; -} - -QGraphicsItem* Map::ItemOccupied(QPoint p) -{ - return itemoccupied[p.x()][p.y()]; -} - -void Map::Occupy(QGraphicsItem* item, QPointF pos) -{ - QPoint p = CoordinateToBlock(pos); - itemoccupied[p.x()][p.y()] = item; - isoccupied[p.x()][p.y()] = true; - -#ifdef MAPGENERATING - pointCount.insert({ p.x(), p.y() }); -#endif -} - -void Map::Destory(QPointF pos) -{ - QPoint p = CoordinateToBlock(pos); - - isoccupied[p.x()][p.y()] = false; - itemoccupied[p.x()][p.y()] = nullptr; -} - -QRectF Map::boundingRect() const -{ - return QRectF(0, 0, width, height); -} - -void Map::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(painter) - Q_UNUSED(option) - Q_UNUSED(widget) - QPointF destination = BlockToCoordinate(road.last()); - painter->drawImage(QRectF(destination.x() - 100, destination.y() - 128, 160, 160), - destinationMovie.currentImage()); - - if (towerShadow) { - painter->setOpacity(0.6); - QPen pen; - pen.setWidth(3); - pen.setColor(QColor(255, 255, 255)); - pen.setStyle(Qt::DotLine); - painter->setPen(pen); - QRectF r = towerShadow->atkArea->boundingRect(); - - painter->drawImage(towerShadowRect, towerShadow->getImage()); - painter->drawEllipse(QRectF(r.x() + towerShadowRect.x() + 40, - r.y() + towerShadowRect.y() + 40, - r.width(), - r.height())); - } - - if (game->advanceTimer.isActive() == false) { - painter->drawImage(QRectF(720 - 64, 480 - 64, 128, 128), QImage(":/images/pause.png")); - } -} - -void Map::advance(int phase) -{ - if (!phase) { - } else { - update(); - } -} - -bool Map::outofScreen(QGraphicsItem* p) -{ - return !(((0 <= p->pos().x()) and (p->pos().x() <= width)) - and ((0 <= p->pos().y()) and (p->pos().y() <= height))); -} - -void Map::mousePressEvent(QGraphicsSceneMouseEvent* event) -{ -#ifdef MAPGENERATING - QPoint p = CoordinateToBlock(event->pos()); - pointCount.insert({ p.x(), p.y() }); - // qDebug() << CoordinateToBlock(event->pos()) << "|"; -#endif - event->ignore(); -} - -void Map::dragEnterEvent(QGraphicsSceneDragDropEvent* event) -{ - if (event->mimeData()->hasText()) { - event->setAccepted(true); - update(); - } else - event->setAccepted(false); -} - -void Map::dragMoveEvent(QGraphicsSceneDragDropEvent* event) -{ - if (event->mimeData()->hasText()) { - QString s = event->mimeData()->text(); - addTowerShadow(s, event->pos()); -#ifdef MAPGENERATING - if (s == "guntower") - addTower(s, event->pos()); -#endif - } -} - -void Map::dropEvent(QGraphicsSceneDragDropEvent* event) -{ - if (event->mimeData()->hasText()) { - QString s = event->mimeData()->text(); - addTower(s, event->pos()); - if (towerShadow) { - delete towerShadow; - towerShadow = nullptr; - } - } -} - -void Map::loadConfig() -{ - isroad.resize(blockWidthNumber + 1); - isoccupied.resize(blockWidthNumber + 1); - itemoccupied.resize(blockWidthNumber + 1); - - for (int i = 0; i < blockWidthNumber + 1; i++) { - isroad[i].resize(blockHeightNumber + 1); - isoccupied[i].resize(blockHeightNumber + 1); - itemoccupied[i].resize(blockHeightNumber + 1); - } - - QSettings ConfigIni(mapConfig, QSettings::IniFormat, 0); - - mapImage = QImage(ConfigIni.value("/map/image").toString()); - - char buf[65536]; - char* point = nullptr; - memset(buf, '\0', sizeof(buf)); - strncpy(buf, - ConfigIni.value("/path/walkingPathPoints").toString().toStdString().c_str(), - sizeof(buf)); - point = strtok(buf, "|"); - while (point != NULL) { - int x, y; - sscanf(point, "(%d,%d)", &x, &y); - road << QPoint(x, y); - walkingPath << BlockToCoordinate({ x, y }); - point = strtok(NULL, "|"); - } - - memset(buf, '\0', sizeof(buf)); - strncpy(buf, - ConfigIni.value("/path/flyingPathPoints").toString().toStdString().c_str(), - sizeof(buf)); - - point = strtok(buf, "|"); - while (point != NULL) { - int x, y; - sscanf(point, "(%d,%d)", &x, &y); - flyingPath << BlockToCoordinate({ x, y }); - point = strtok(NULL, "|"); - } - - memset(buf, '\0', sizeof(buf)); - strncpy(buf, - ConfigIni.value("/landform/obstacles").toString().toStdString().c_str(), - sizeof(buf)); - point = strtok(buf, "|"); - while (point != NULL) { - int x, y; - sscanf(point, "(%d,%d)", &x, &y); - Occupy(nullptr, BlockToCoordinate({ x, y })); - point = strtok(NULL, "|"); - } -} - -void Map::addTower(QString s, QPointF pos) -{ - QPoint block = CoordinateToBlock(pos); - if (IsOccupied(block) == false) { - pos = BlockToCoordinate(block); - - Tower* tower = nullptr; - switch (Shop::map[s]) { - case 0: - tower = new GunTower(this); - break; - case 1: - tower = new Bomb(this); - break; - case 2: - tower = new Repeller(this); - break; - case 3: - tower = new LaserTower(this); - break; - case 4: - tower = new MissileTower(this); - break; - case 5: - tower = new SawTooth(this); - break; - case 6: - tower = new CampFire(this); - break; - case 7: - tower = new Shield(this); - break; - default: - assert(false); - break; - } - - if ((tower->getAtkType() == Tower::MELEE and IsRoad(block) == false) - or (tower->getAtkType() == Tower::RANGE and IsRoad(block) == true)) { - delete tower; - return; - } - - if (game->statistic.money.getCurValue() >= Shop::cost[Shop::map[s]]) { - game->statistic.money.changeCurValue(-Shop::cost[Shop::map[s]]); - } else { - delete tower; - return; - } - - tower->setParentItem(this); - tower->setPos(pos); - tower->infopanel.setParentItem(this); - tower->infopanel.hide(); - Occupy(tower, tower->pos()); - } else { - Tower* t = (Tower*)ItemOccupied(block); - if (s == QString("repeller") and t->getName() == QString("repeller")) { - t->HP.setCurValue(t->HP.getMaxValue()); - } - } -} - -void Map::addTowerShadow(QString s, QPointF pos) -{ - QPoint block = CoordinateToBlock(pos); - if (IsOccupied(block) == false) { - pos = BlockToCoordinate(block); - - if (towerShadow == nullptr) { - switch (Shop::map[s]) { - case 0: - towerShadow = new GunTower(nullptr); - break; - case 1: - towerShadow = new Bomb(nullptr); - break; - case 2: - towerShadow = new Repeller(nullptr); - break; - case 3: - towerShadow = new LaserTower(nullptr); - break; - case 4: - towerShadow = new MissileTower(nullptr); - break; - case 5: - towerShadow = new SawTooth(this); - break; - case 6: - towerShadow = new CampFire(this); - break; - case 7: - towerShadow = new Shield(this); - break; - default: - qDebug() << Shop::map[s]; - assert(false); - break; - } - } - - if ((towerShadow->getAtkType() == Tower::MELEE and IsRoad(block) == true) - or (towerShadow->getAtkType() == Tower::RANGE and IsRoad(block) == false)) { - QRectF r = towerShadow->boundingRect(); - towerShadowRect = QRectF(pos.x() + r.x(), pos.y() + r.y(), r.width(), r.height()); - } - } -} - -void Map::removeTower(QPointF pos) -{ - QPoint block = CoordinateToBlock(pos); - if (IsOccupied(block) == false) - return; - Tower* t = (Tower*)ItemOccupied(block); - game->statistic.money.changeCurValue(Shop::cost[Shop::map[t->getName()]] / 2); - - Destory(block); -} diff --git a/src/shop.cpp b/src/shop.cpp index 3e9e63d..979a350 100644 --- a/src/shop.cpp +++ b/src/shop.cpp @@ -1,111 +1,94 @@ +// Copyright 2022 Flying-Tom + #include #include const QMap Shop::map = { - { "guntower", 0 }, - { "bomb", 1 }, - { "repeller", 2 }, - { "lasertower", 3 }, - { "missiletower", 4 }, - { "sawtooth", 5 }, - { "campfire", 6 }, - { "shield", 7 } -}; -const QVector Shop::name = { - "guntower", - "bomb", - "repeller", - "lasertower", - "missiletower", - "sawtooth", - "campfire", - "shield" -}; -const QVector Shop::cost = { 1000, 1000, 500, 4000, 5000, 5000, 2000, 1000 }; -const QVector Shop::cdtime = { 30, 40, 50, 3, 3, 5, 20, 20 }; - -Shop::Shop(QString s, qreal left, qreal top, qreal width, qreal height) -{ - towername = s; - counter = 0; + {"guntower", 0}, {"bomb", 1}, {"repeller", 2}, {"lasertower", 3}, + {"missiletower", 4}, {"sawtooth", 5}, {"campfire", 6}, {"shield", 7}}; +const QVector Shop::name = {"guntower", "bomb", "repeller", + "lasertower", "missiletower", "sawtooth", + "campfire", "shield"}; +const QVector Shop::cost = {1000, 1000, 500, 4000, 5000, 5000, 2000, 1000}; +const QVector Shop::cdtime = {30, 40, 50, 3, 3, 5, 20, 20}; - aleft = left; - atop = top; - awidth = width; - aheight = height; +Shop::Shop(QString s, qreal left, qreal top, qreal width, qreal height) { + towername = s; + counter = 0; + + aleft = left; + atop = top; + awidth = width; + aheight = height; } -QRectF Shop::boundingRect() const -{ - return QRectF(aleft, atop, awidth, aheight); +QRectF Shop::boundingRect() const { + return QRectF(aleft, atop, awidth, aheight); } -void Shop::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) - - painter->drawImage(boundingRect(), QImage(":/images/" + towername + ".png")); - QFont font; - font.setPointSizeF(15); - painter->setFont(font); - painter->setPen(Qt::white); - painter->drawText(QRectF(awidth + 5, aheight / 2 - 12, awidth, aheight), - QString().asprintf("%3d", cost[map[towername]])); - - QBrush brush(QColor(0, 0, 0, 200)); - painter->setBrush(brush); - - if (Shop::cost[Shop::map[towername]] - > qgraphicsitem_cast(parentItem())->money.getCurValue()) { - painter->drawRect(QRectF(0, 0, awidth, aheight)); - } else if (counter < cdtime[map[towername]]) { - painter->drawRect( - QRectF(0, 0, awidth, aheight * (1 - qreal(counter) / cdtime[map[towername]]))); - } +void Shop::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(option) + Q_UNUSED(widget) + + painter->drawImage(boundingRect(), QImage(":/images/" + towername + ".png")); + QFont font; + font.setPointSizeF(15); + painter->setFont(font); + painter->setPen(Qt::white); + painter->drawText(QRectF(awidth + 5, aheight / 2 - 12, awidth, aheight), + QString().asprintf("%3d", cost[map[towername]])); + + QBrush brush(QColor(0, 0, 0, 200)); + painter->setBrush(brush); + + if (Shop::cost[Shop::map[towername]] > + qgraphicsitem_cast(parentItem())->money.getCurValue()) { + painter->drawRect(QRectF(0, 0, awidth, aheight)); + } else if (counter < cdtime[map[towername]]) { + painter->drawRect(QRectF( + 0, 0, awidth, aheight * (1 - qreal(counter) / cdtime[map[towername]]))); + } } -void Shop::advance(int phase) -{ - if (!phase) { - if (counter < cdtime[map[towername]]) - ++counter; - } else { - update(); - } +void Shop::advance(int phase) { + if (!phase) { + if (counter < cdtime[map[towername]]) ++counter; + } else { + update(); + } } -void Shop::mousePressEvent(QGraphicsSceneMouseEvent* event) -{ - Q_UNUSED(event) +void Shop::mousePressEvent(QGraphicsSceneMouseEvent* event) { + Q_UNUSED(event) - if (cost[map[towername]] > qgraphicsitem_cast(parentItem())->money.getCurValue()) { - event->setAccepted(false); - return; - } + if (cost[map[towername]] > + qgraphicsitem_cast(parentItem())->money.getCurValue()) { + event->setAccepted(false); + return; + } - if (counter < cdtime[map[towername]]) - event->setAccepted(false); - else - counter = 0; + if (counter < cdtime[map[towername]]) + event->setAccepted(false); + else + counter = 0; } -void Shop::mouseMoveEvent(QGraphicsSceneMouseEvent* event) -{ - if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton)).length() - < QApplication::startDragDistance()) - return; - - QDrag* drag = new QDrag(event->widget()); - QMimeData* mime = new QMimeData(); - mime->setText(towername); - drag->setMimeData(mime); - drag->setPixmap(QPixmap(":/images/" + towername + ".png").scaled(awidth, aheight)); - drag->setHotSpot(QPoint(35, 35)); - drag->exec(); +void Shop::mouseMoveEvent(QGraphicsSceneMouseEvent* event) { + if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton)) + .length() < QApplication::startDragDistance()) + return; + + QDrag* drag = new QDrag(event->widget()); + QMimeData* mime = new QMimeData(); + mime->setText(towername); + drag->setMimeData(mime); + drag->setPixmap( + QPixmap(":/images/" + towername + ".png").scaled(awidth, aheight)); + drag->setHotSpot(QPoint(35, 35)); + drag->exec(); } -void Shop::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) -{ - Q_UNUSED(event) +void Shop::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) { + Q_UNUSED(event) } diff --git a/src/statistic.cpp b/src/statistic.cpp index f4101dc..ee6c5ee 100644 --- a/src/statistic.cpp +++ b/src/statistic.cpp @@ -1,3 +1,5 @@ +// Copyright 2022 Flying-Tom + #include #include #include @@ -6,122 +8,122 @@ int Statistic::frameCount = 0; Statistic::Statistic(Game* game) - : game(game) - , shopBackground(":images/shop.png") - , lifeIcon(":images/life.png") - , coinIcon(":images/coin.png") -{ - setAcceptDrops(false); - life = GameValue(20, 50); - money = GameValue(1000000, 1000000); - moneyAddCounter = GameValue(500, 500); - FPS = 0; - - Shop* icon = nullptr; - - icon = new Shop(Shop::name[0], 0, 0, 64, 128); - icon->setParentItem(this); - icon->setPos(1290, 55 + 96 * 1); - - icon = new Shop(Shop::name[3], 0, 0, 64, 128); - icon->setParentItem(this); - icon->setPos(1450, 55 + 96 * 1); - - icon = new Shop(Shop::name[1], 0, 0, 64, 64); - icon->setParentItem(this); - icon->setPos(1290, 85 + 96 * 2); - - icon = new Shop(Shop::name[2], 0, 0, 64, 64); - icon->setParentItem(this); - icon->setPos(1450, 85 + 96 * 2); - - icon = new Shop(Shop::name[4], 0, 0, 64, 64); - icon->setParentItem(this); - icon->setPos(1450, 85 + 96 * 3); - - icon = new Shop(Shop::name[5], 0, 0, 64, 64); - icon->setParentItem(this); - icon->setPos(1450, 85 + 96 * 4); - - icon = new Shop(Shop::name[6], 0, 0, 64, 64); - icon->setParentItem(this); - icon->setPos(1450, 85 + 96 * 5); - - icon = new Shop(Shop::name[7], 0, 0, 64, 64); - icon->setParentItem(this); - icon->setPos(1290, 85 + 96 * 3); + : game(game), + shopBackground(":images/shop.png"), + lifeIcon(":images/life.png"), + coinIcon(":images/coin.png") { + setAcceptDrops(false); + life = GameValue(20, 50); + money = GameValue(1000000, 1000000); + moneyAddCounter = GameValue(500, 500); + FPS = 0; + + Shop* icon = nullptr; + + icon = new Shop(Shop::name[0], 0, 0, 64, 128); + icon->setParentItem(this); + icon->setPos(1290, 55 + 96 * 1); + + icon = new Shop(Shop::name[3], 0, 0, 64, 128); + icon->setParentItem(this); + icon->setPos(1450, 55 + 96 * 1); + + icon = new Shop(Shop::name[1], 0, 0, 64, 64); + icon->setParentItem(this); + icon->setPos(1290, 85 + 96 * 2); + + icon = new Shop(Shop::name[2], 0, 0, 64, 64); + icon->setParentItem(this); + icon->setPos(1450, 85 + 96 * 2); + + icon = new Shop(Shop::name[4], 0, 0, 64, 64); + icon->setParentItem(this); + icon->setPos(1450, 85 + 96 * 3); + + icon = new Shop(Shop::name[5], 0, 0, 64, 64); + icon->setParentItem(this); + icon->setPos(1450, 85 + 96 * 4); + + icon = new Shop(Shop::name[6], 0, 0, 64, 64); + icon->setParentItem(this); + icon->setPos(1450, 85 + 96 * 5); + + icon = new Shop(Shop::name[7], 0, 0, 64, 64); + icon->setParentItem(this); + icon->setPos(1290, 85 + 96 * 3); } -QRectF Statistic::boundingRect() const -{ - return QRectF(1280, 0, 320, 1000); +QRectF Statistic::boundingRect() const { return QRectF(1280, 0, 320, 1000); } + +void Statistic::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(option) + Q_UNUSED(widget) + + painter->setBrush(QColor(78, 85, 97)); + painter->drawRect(boundingRect()); + + painter->setPen(Qt::NoPen); + painter->setBrush(QColor(62, 69, 80)); + painter->drawRect(QRectF(1290, 140, 300, 800)); + + QFont font; + font.setPointSizeF(15); + painter->setFont(font); + painter->setPen(Qt::white); + + painter->drawImage(QRectF(1380, 45, 25, 25), lifeIcon); + painter->drawText(QRectF(1420, 40, 100, 50), + QString::number(life.getCurValue())); + painter->drawImage(QRectF(1380, 73, 30, 30), coinIcon); + painter->drawText(QRectF(1420, 70, 100, 50), + QString::number(money.getCurValue())); + + font.setPointSizeF(9); + painter->setFont(font); + painter->setPen(Qt::gray); + painter->drawText(QRectF(1540, 5, 100, 50), + QString("%1").arg(FPS, -3) + QString(" FPS")); + + painter->drawText( + QRectF(1500, 880, 100, 50), + QString("%1").arg(Enemy::enemyCount, -3) + QString(" enemies")); + painter->drawText( + QRectF(1500, 900, 100, 50), + QString("%1").arg(scene()->items().size(), -3) + QString(" items")); + + if (game->FPSCounterTimer.hasExpired(1000)) { + FPS = frameCount; + frameCount = 0; + game->FPSCounterTimer.restart(); + } } -void Statistic::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) - - painter->setBrush(QColor(78, 85, 97)); - painter->drawRect(boundingRect()); - - painter->setPen(Qt::NoPen); - painter->setBrush(QColor(62, 69, 80)); - painter->drawRect(QRectF(1290, 140, 300, 800)); - - QFont font; - font.setPointSizeF(15); - painter->setFont(font); - painter->setPen(Qt::white); - - painter->drawImage(QRectF(1380, 45, 25, 25), lifeIcon); - painter->drawText(QRectF(1420, 40, 100, 50), QString::number(life.getCurValue())); - painter->drawImage(QRectF(1380, 73, 30, 30), coinIcon); - painter->drawText(QRectF(1420, 70, 100, 50), QString::number(money.getCurValue())); - - font.setPointSizeF(9); - painter->setFont(font); - painter->setPen(Qt::gray); - painter->drawText(QRectF(1540, 5, 100, 50), QString("%1").arg(FPS, -3) + QString(" FPS")); - - painter->drawText(QRectF(1500, 880, 100, 50), - QString("%1").arg(Enemy::enemyCount, -3) + QString(" enemies")); - painter->drawText(QRectF(1500, 900, 100, 50), - QString("%1").arg(scene()->items().size(), -3) + QString(" items")); - - if (game->FPSCounterTimer.hasExpired(1000)) { - FPS = frameCount; - frameCount = 0; - game->FPSCounterTimer.restart(); +void Statistic::advance(int phase) { + if (!phase) { + if (life.getCurValue() == 0) { + game->endThisGame("GameOver"); } -} -void Statistic::advance(int phase) -{ - if (!phase) { - if (life.getCurValue() == 0) { - game->endThisGame("GameOver"); - } - - if (FPS < 75) { - if (game->spawnTimer.isActive() == true) { - game->spawnTimer.stop(); - } - } else { - if (game->spawnTimer.isActive() == false) { - game->spawnTimer.start(2000 / game->gameSpeed); - } - } - - if (moneyAddCounter.getCurValue() < moneyAddCounter.getMaxValue()) { - moneyAddCounter.changeCurValue(1); - } else { - moneyAddCounter.setCurValue(0); - money.changeCurValue(200); - } + if (FPS < 75) { + if (game->spawnTimer.isActive() == true) { + game->spawnTimer.stop(); + } + } else { + if (game->spawnTimer.isActive() == false) { + game->spawnTimer.start(2000 / game->gameSpeed); + } + } + if (moneyAddCounter.getCurValue() < moneyAddCounter.getMaxValue()) { + moneyAddCounter.changeCurValue(1); } else { - ++frameCount; - update(); + moneyAddCounter.setCurValue(0); + money.changeCurValue(200); } + + } else { + ++frameCount; + update(); + } } diff --git a/src/tower/bomb.cpp b/src/tower/bomb.cpp index fc17867..503b605 100644 --- a/src/tower/bomb.cpp +++ b/src/tower/bomb.cpp @@ -1,60 +1,60 @@ +// Copyright 2022 Flying-Tom + #include -Bomb::Bomb(Map* map) - : Tower(map, "bomb", GameValue(8, 8), MELEE, 256, GameValue(0, 20)) - , movie(":images/bomb.gif") -{ - image = QImage(":images/bomb.png"); +Bomb::Bomb(GameMap* map) + : Tower(map, "bomb", GameValue(8, 8), MELEE, 256, + GameValue(0, 20)), + movie(":images/bomb.gif") { + image = QImage(":images/bomb.png"); } -Bomb::~Bomb() { } +Bomb::~Bomb() {} -void Bomb::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) +void Bomb::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(option) + Q_UNUSED(widget) - // Tower::paint(painter, option, widget); - GameItem::paint(painter, option, widget); - if (HP.getCurValue() == 0) { - painter->drawImage(QRectF(-256, -256, 512, 512), movie.currentImage()); - } else { - painter->drawImage(boundingRect(), image); - } + // Tower::paint(painter, option, widget); + GameItem::paint(painter, option, widget); + if (HP.getCurValue() == 0) { + painter->drawImage(QRectF(-256, -256, 512, 512), movie.currentImage()); + } else { + painter->drawImage(boundingRect(), image); + } } -void Bomb::advance(int phase) -{ - if (!phase) { - if (HP.getCurValue() == 0) { - if (movie.state() == QMovie::NotRunning) { - movie.start(); - bombing(); - } else if (movie.currentFrameNumber() >= movie.frameCount() - 2) { - delete this; - return; - } - } - } else { - update(); +void Bomb::advance(int phase) { + if (!phase) { + if (HP.getCurValue() == 0) { + if (movie.state() == QMovie::NotRunning) { + movie.start(); + bombing(); + } else if (movie.currentFrameNumber() >= movie.frameCount() - 2) { + delete this; + return; + } } + } else { + update(); + } } -void Bomb::bombing() -{ - QList colliding_items = atkArea->collidingItems(); - if (!colliding_items.empty()) { - foreach (QGraphicsItem* item, colliding_items) { - if (item->type() == Enemy::Type) { - Enemy* e = qgraphicsitem_cast(item); - if (e->getMoveType() == Enemy::WALKING) - e->HP.setCurValue(0); - } else if (item->type() == Tower::Type) { - GameItem* gi = (GameItem*)item; - if (gi->getName() == QString("bomb") or gi->getName() == QString("repeller")) { - gi->HP.setCurValue(0); - } - } +void Bomb::bombing() { + QList colliding_items = atkArea->collidingItems(); + if (!colliding_items.empty()) { + for (QGraphicsItem* item : colliding_items) { + if (item->type() == Enemy::Type) { + Enemy* e = qgraphicsitem_cast(item); + if (e->getMoveType() == Enemy::WALKING) e->HP.setCurValue(0); + } else if (item->type() == Tower::Type) { + GameItem* gi = qgraphicsitem_cast(item); + if (gi->getName() == QString("bomb") || + gi->getName() == QString("repeller")) { + gi->HP.setCurValue(0); } + } } + } } diff --git a/src/tower/campfire.cpp b/src/tower/campfire.cpp index 35d5fb0..69044f5 100644 --- a/src/tower/campfire.cpp +++ b/src/tower/campfire.cpp @@ -1,63 +1,58 @@ +// Copyright 2022 Flying-Tom + #include #include -CampFire::CampFire(Map* map) - : Tower(map, - "campfire", - GameValue(10, 10), - RANGE, - 250, - GameValue(0, 3), - GameValue(0, 500)) - , movie(":images/campfire.gif") -{ - image = QImage(":images/campfire.png"); - movie.start(); +CampFire::CampFire(GameMap* map) + : Tower(map, "campfire", GameValue(10, 10), RANGE, 250, + GameValue(0, 3), GameValue(0, 500)), + movie(":images/campfire.gif") { + image = QImage(":images/campfire.png"); + movie.start(); } -CampFire::~CampFire() { } +CampFire::~CampFire() {} -void CampFire::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) - GameItem::paint(painter, option, widget); - painter->drawImage(boundingRect(), movie.currentImage()); +void CampFire::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(option) + Q_UNUSED(widget) + GameItem::paint(painter, option, widget); + painter->drawImage(boundingRect(), movie.currentImage()); } -void CampFire::aquireTarget() -{ - static int ax[3] = { -80, 0, 80 }; - static int ay[3] = { -80, 0, 80 }; - for (int i = 0; i < 3; i++) - for (int j = 0; j < 3; j++) { - QPoint block(Map::CoordinateToBlock(QPointF(x() + ax[i], y() + ay[j]))); - if (map->IsOccupied(block) == false and map->IsRoad(block) == true) { - Tower* tower = new Bomb(map); - tower->setParentItem(map); - tower->setPos(QPointF(x() + ax[i], y() + ay[j])); - tower->infopanel.setParentItem(map); - tower->infopanel.hide(); - map->Occupy(tower, tower->pos()); - return; - } - } +void CampFire::aquireTarget() { + static int ax[3] = {-80, 0, 80}; + static int ay[3] = {-80, 0, 80}; + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) { + QPoint block( + GameMap::CoordinateToBlock(QPointF(x() + ax[i], y() + ay[j]))); + if (map->IsOccupied(block) == false && map->IsRoad(block) == true) { + Tower* tower = new Bomb(map); + tower->setParentItem(map); + tower->setPos(QPointF(x() + ax[i], y() + ay[j])); + tower->infopanel.setParentItem(map); + tower->infopanel.hide(); + map->Occupy(tower, tower->pos()); + return; + } + } } -void CampFire::advance(int phase) -{ - if (!phase) { - if (HP.getCurValue() == 0) { - delete this; - return; - } - if (aquireCounter.getCurValue() < aquireCounter.getMaxValue()) { - aquireCounter.changeCurValue(1); - } else { - aquireTarget(); - aquireCounter.setCurValue(0); - } +void CampFire::advance(int phase) { + if (!phase) { + if (HP.getCurValue() == 0) { + delete this; + return; + } + if (aquireCounter.getCurValue() < aquireCounter.getMaxValue()) { + aquireCounter.changeCurValue(1); } else { - update(); + aquireTarget(); + aquireCounter.setCurValue(0); } + } else { + update(); + } } diff --git a/src/tower/guntower.cpp b/src/tower/guntower.cpp index 36c1858..f24db69 100644 --- a/src/tower/guntower.cpp +++ b/src/tower/guntower.cpp @@ -1,43 +1,40 @@ +// Copyright 2022 Flying-Tom + #include #include -GunTower::GunTower(Map* map) - : Tower(map, "guntower", GameValue(20, 20), RANGE, 250) -{ - image = QImage(":images/guntower.png"); - atkTarget = nullptr; +GunTower::GunTower(GameMap* map) + : Tower(map, "guntower", GameValue(20, 20), RANGE, 250) { + image = QImage(":images/guntower.png"); + atkTarget = nullptr; } -GunTower::~GunTower() { } +GunTower::~GunTower() {} -QRectF GunTower::boundingRect() const -{ - return QRectF(-32, -64, 64, 128); -} +QRectF GunTower::boundingRect() const { return QRectF(-32, -64, 64, 128); } -void GunTower::attack() -{ - GunBullet* bullet = new GunBullet(5); +void GunTower::attack() { + GunBullet* bullet = new GunBullet(5); - QPointF atkTargetPos; - if (atkTarget.isNull()) { - atkTargetPos = pos(); - } else { - atkTargetPos = atkTarget->pos(); - } + QPointF atkTargetPos; + if (atkTarget.isNull()) { + atkTargetPos = pos(); + } else { + atkTargetPos = atkTarget->pos(); + } - QLineF ln(QPointF(x(), y()), atkTargetPos); - qreal angle = -1 * ln.angle(); + QLineF ln(QPointF(x(), y()), atkTargetPos); + qreal angle = -1 * ln.angle(); - qreal dy = 64 * qSin(qDegreesToRadians(angle)); - qreal dx = 64 * qCos(qDegreesToRadians(angle)); - bullet->setPos(x() + dx, y() + dy); + qreal dy = 64 * qSin(qDegreesToRadians(angle)); + qreal dx = 64 * qCos(qDegreesToRadians(angle)); + bullet->setPos(x() + dx, y() + dy); - setTransformOriginPoint(boundingRect().center()); - setRotation(angle + 90); + setTransformOriginPoint(boundingRect().center()); + setRotation(angle + 90); - HPMeter->setRotation(-angle - 90); - atkArea->setRotation(-angle); - bullet->setRotation(angle); - scene()->addItem(bullet); + HPMeter->setRotation(-angle - 90); + atkArea->setRotation(-angle); + bullet->setRotation(angle); + scene()->addItem(bullet); } diff --git a/src/tower/infopanel.cpp b/src/tower/infopanel.cpp index cfaa8bd..a851c40 100644 --- a/src/tower/infopanel.cpp +++ b/src/tower/infopanel.cpp @@ -1,55 +1,49 @@ +// Copyright 2022 Flying-Tom + #include -InfoPanel::InfoPanel(GameItem* parent) - : parent(parent) -{ - return; -} +InfoPanel::InfoPanel(GameItem* parent) : parent(parent) { return; } -InfoPanel::~InfoPanel() { } +InfoPanel::~InfoPanel() {} -QRectF InfoPanel::boundingRect() const -{ - return QRectF(0, 0, 150, 150); -} +QRectF InfoPanel::boundingRect() const { return QRectF(0, 0, 150, 150); } + +void InfoPanel::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(option) + Q_UNUSED(widget) -void InfoPanel::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) - - painter->setBrush(QBrush(QColor(96, 96, 96, 120))); - painter->drawRect(boundingRect()); - painter->drawImage(QRect(10, 100, 30, 30), QImage(":/images/cross.png")); - - QFont font; - font.setPointSizeF(10); - painter->setFont(font); - painter->setPen(Qt::white); - painter->drawText(QRect(40, 10, 130, 30), QString("Info Panel")); - painter->drawText(QRect(10, 35, 130, 30), QString("Name:") + parent->getName()); - painter->drawText(QRect(10, 55, 100, 30), - QString("HP:") + QString::number(parent->HP.getCurValue())); - painter->drawText(QRect(50, 105, 80, 30), QString("Destroy it")); + painter->setBrush(QBrush(QColor(96, 96, 96, 120))); + painter->drawRect(boundingRect()); + painter->drawImage(QRect(10, 100, 30, 30), QImage(":/images/cross.png")); + + QFont font; + font.setPointSizeF(10); + painter->setFont(font); + painter->setPen(Qt::white); + painter->drawText(QRect(40, 10, 130, 30), QString("Info Panel")); + painter->drawText(QRect(10, 35, 130, 30), + QString("Name:") + parent->getName()); + painter->drawText(QRect(10, 55, 100, 30), + QString("HP:") + QString::number(parent->HP.getCurValue())); + painter->drawText(QRect(50, 105, 80, 30), QString("Destroy it")); } -void InfoPanel::advance(int phase) -{ - if (!phase) { - ; - } +void InfoPanel::advance(int phase) { + if (!phase) { + return; + } } -void InfoPanel::mousePressEvent(QGraphicsSceneMouseEvent* event) -{ - if (event->button() == Qt::RightButton) { - hide(); - return; - } else if (event->button() == Qt::LeftButton) { - if (QRectF(50, 105, 80, 30).contains(event->pos())) { - delete parent; - return; - } +void InfoPanel::mousePressEvent(QGraphicsSceneMouseEvent* event) { + if (event->button() == Qt::RightButton) { + hide(); + return; + } else if (event->button() == Qt::LeftButton) { + if (QRectF(50, 105, 80, 30).contains(event->pos())) { + delete parent; + return; } - event->ignore(); + } + event->ignore(); } diff --git a/src/tower/lasertower.cpp b/src/tower/lasertower.cpp index 8801317..bcae5bf 100644 --- a/src/tower/lasertower.cpp +++ b/src/tower/lasertower.cpp @@ -1,74 +1,64 @@ +// Copyright 2022 Flying-Tom + #include -LaserTower::LaserTower(Map *map) - : Tower(map, "lasertower", GameValue(20, 20), RANGE, 256) -{ - trajectory = nullptr; +LaserTower::LaserTower(GameMap *map) + : Tower(map, "lasertower", GameValue(20, 20), RANGE, 256) { + trajectory = nullptr; - image = QImage(":images/lasertower.png"); + image = QImage(":images/lasertower.png"); } -LaserTower::~LaserTower() -{ - if (trajectory) - delete trajectory; +LaserTower::~LaserTower() { + if (trajectory) delete trajectory; } -QRectF LaserTower::boundingRect() const -{ - return QRectF(-32, -64, 64, 128); -} +QRectF LaserTower::boundingRect() const { return QRectF(-32, -64, 64, 128); } -void LaserTower::attack() -{ - if (trajectory == nullptr) { - trajectory = new Laser(this, 1); - scene()->addItem(trajectory); - } +void LaserTower::attack() { + if (trajectory == nullptr) { + trajectory = new Laser(this, 1); + scene()->addItem(trajectory); + } - QPointF atkTargetPos; - if (atkTarget.isNull()) { - atkTargetPos = pos(); - } else { - atkTargetPos = atkTarget->pos(); - } + QPointF atkTargetPos; + if (atkTarget.isNull()) { + atkTargetPos = pos(); + } else { + atkTargetPos = atkTarget->pos(); + } - QLineF ln(QPointF(x(), y()), atkTargetPos); - qreal angle = -1 * ln.angle(); + QLineF ln(QPointF(x(), y()), atkTargetPos); + qreal angle = -1 * ln.angle(); - qreal dy = 64 * qSin(qDegreesToRadians(angle)); - qreal dx = 64 * qCos(qDegreesToRadians(angle)); - trajectory->setPos(x() + dx, y() + dy); + qreal dy = 64 * qSin(qDegreesToRadians(angle)); + qreal dx = 64 * qCos(qDegreesToRadians(angle)); + trajectory->setPos(x() + dx, y() + dy); - setTransformOriginPoint(boundingRect().center()); - setRotation(angle + 90); + setTransformOriginPoint(boundingRect().center()); + setRotation(angle + 90); - HPMeter->setRotation(-angle - 90); - atkArea->setRotation(-angle); - trajectory->setRotation(angle + 90); + HPMeter->setRotation(-angle - 90); + atkArea->setRotation(-angle); + trajectory->setRotation(angle + 90); } -Laser *LaserTower::getTrajectory() const -{ - return trajectory; -} +Laser *LaserTower::getTrajectory() const { return trajectory; } -void LaserTower::setTrajectory(Laser *newTrajectory) -{ - trajectory = newTrajectory; +void LaserTower::setTrajectory(Laser *newTrajectory) { + trajectory = newTrajectory; } -void LaserTower::aquireTarget() -{ - QList colliding_items = atkArea->collidingItems(); +void LaserTower::aquireTarget() { + QList colliding_items = atkArea->collidingItems(); - foreach (QGraphicsItem *item, colliding_items) { - Enemy *e = (Enemy *) item; - if (item->type() == Enemy::Type and e->getMoveType() == Enemy::WALKING - and e->getIsDead() == false) { - atkTarget = e; - attack(); - return; - } + for (QGraphicsItem *item : colliding_items) { + Enemy *e = qgraphicsitem_cast(item); + if (item->type() == Enemy::Type && e->getMoveType() == Enemy::WALKING && + e->getIsDead() == false) { + atkTarget = e; + attack(); + return; } + } } diff --git a/src/tower/missiletower.cpp b/src/tower/missiletower.cpp index 984d4d0..dbbb60c 100644 --- a/src/tower/missiletower.cpp +++ b/src/tower/missiletower.cpp @@ -1,42 +1,37 @@ +// Copyright 2022 Flying-Tom + #include -MissileTower::MissileTower(Map* map) - : Tower(map, - "missiletower", - GameValue(5, 5), - RANGE, - 2048, - GameValue(0, 5), - GameValue(0, 175)) -{ - image = QImage(":images/missiletower.png"); +MissileTower::MissileTower(GameMap* map) + : Tower(map, "missiletower", GameValue(5, 5), RANGE, 2048, + GameValue(0, 5), GameValue(0, 175)) { + image = QImage(":images/missiletower.png"); } -MissileTower::~MissileTower() { } +MissileTower::~MissileTower() {} -void MissileTower::aquireTarget() -{ - QList colliding_items = atkArea->collidingItems(); - QList enemy_list = QList(); +void MissileTower::aquireTarget() { + QList colliding_items = atkArea->collidingItems(); + QList enemy_list = QList(); - foreach (QGraphicsItem* item, colliding_items) { - if (item->type() == Enemy::Type and ((Enemy*)item)->getIsDead() == false) { - enemy_list.append(item); - } + for (QGraphicsItem* item : colliding_items) { + if (item->type() == Enemy::Type && + qgraphicsitem_cast(item)->getIsDead() == false) { + enemy_list.append(item); } + } - if (enemy_list.empty()) { - return; - } else { - int index = QRandomGenerator::global()->bounded(0, enemy_list.size()); - atkTarget = (Enemy*)enemy_list[index]; - attack(); - } + if (enemy_list.empty()) { + return; + } else { + int index = QRandomGenerator::global()->bounded(0, enemy_list.size()); + atkTarget = qgraphicsitem_cast(enemy_list[index]); + attack(); + } } -void MissileTower::attack() -{ - Missile* bullet = new Missile(atkTarget, 50); - bullet->setPos(pos()); - scene()->addItem(bullet); +void MissileTower::attack() { + Missile* bullet = new Missile(atkTarget, 50); + bullet->setPos(pos()); + scene()->addItem(bullet); } diff --git a/src/tower/repeller.cpp b/src/tower/repeller.cpp index e26c79e..6c12fde 100644 --- a/src/tower/repeller.cpp +++ b/src/tower/repeller.cpp @@ -1,42 +1,43 @@ +// Copyright 2022 Flying-Tom + #include -Repeller::Repeller(Map* map) - : Tower(map, "repeller", GameValue(6000, 6000), MELEE, 50, GameValue(0, 3)) -{ - image = QImage(":images/repeller.png"); +Repeller::Repeller(GameMap *map) + : Tower(map, "repeller", GameValue(6000, 6000), MELEE, 50, + GameValue(0, 3)) { + image = QImage(":images/repeller.png"); } -Repeller::~Repeller() { } +Repeller::~Repeller() {} -void Repeller::advance(int phase) -{ - if (!phase) { - if (HP.getCurValue() == 0) { - delete this; - return; - } - // QList items = collidingItems(); - // if (!items.empty()) { - // foreach (QGraphicsItem *item, items) { - // if (item->type() == Enemy::Type) { - // Enemy *e = qgraphicsitem_cast(item); - // // if (e->getIsDead() == false) { - // // QLineF ln(e->pos(), e->getPoint(e->getpointIndex() - 1)); +void Repeller::advance(int phase) { + if (!phase) { + if (HP.getCurValue() == 0) { + delete this; + return; + } + // QList items = collidingItems(); + // if (!items.empty()) { + // foreach (QGraphicsItem *item, items) { + // if (item->type() == Enemy::Type) { + // Enemy *e = qgraphicsitem_cast(item); + // if (e->getIsDead() == false) { + // QLineF ln(e->pos(), e->getPoint(e->getpointIndex() - 1)); - // // qreal length = (qreal) QRandomGenerator::global()->bounded(5, 8) / 10 - // // * ln.length(); + // qreal length = (qreal)QRandomGenerator::global()->bounded(5, 8) / + // 10 * ln.length(); - // // qreal dy = length * qSin(qDegreesToRadians(-ln.angle())); - // // qreal dx = length * qCos(qDegreesToRadians(-ln.angle())); + // qreal dy = length * qSin(qDegreesToRadians(-ln.angle())); + // qreal dx = length * qCos(qDegreesToRadians(-ln.angle())); - // // e->setPos(e->pos().x() + dx, e->pos().y() + dy); - // // //e->changeHP(-0.5); - // // HP -= 1; - // // } - // } - // } - // } - } else { - update(); - } + // e->setPos(e->pos().x() + dx, e->pos().y() + dy); + // e->changeHP(-0.5); + // HP -= 1; + // } + // } + // } + // } + } else { + update(); + } } diff --git a/src/tower/sawtooth.cpp b/src/tower/sawtooth.cpp index a6f5b1e..e46d46d 100644 --- a/src/tower/sawtooth.cpp +++ b/src/tower/sawtooth.cpp @@ -1,42 +1,43 @@ +// Copyright 2022 Flying-Tom + #include -SawTooth::SawTooth(Map* map) - : Tower(map, "sawtooth", GameValue(500, 500), MELEE, 0, GameValue(0, 0)) -{ - image = QImage(":images/sawtooth.png"); - angle = 0; +SawTooth::SawTooth(GameMap* map) + : Tower(map, "sawtooth", GameValue(500, 500), MELEE, 0, + GameValue(0, 0)) { + image = QImage(":images/sawtooth.png"); + angle = 0; } -SawTooth::~SawTooth() { } +SawTooth::~SawTooth() {} -void SawTooth::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) - painter->drawImage(boundingRect(), image); +void SawTooth::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(option) + Q_UNUSED(widget) + painter->drawImage(boundingRect(), image); } -void SawTooth::advance(int phase) -{ - if (!phase) { - if (HP.getCurValue() == 0) { - delete this; - return; - } - setRotation(angle); - angle += 20; - QList items = collidingItems(); - if (!items.empty()) { - foreach (QGraphicsItem* item, items) { - if (item->type() == Enemy::Type) { - Enemy* e = qgraphicsitem_cast(item); - if (e->getMoveType() == Enemy::WALKING) { - e->HP.changeCurValue(-0.5); - e->underAtk = true; - } - } - } +void SawTooth::advance(int phase) { + if (!phase) { + if (HP.getCurValue() == 0) { + delete this; + return; + } + setRotation(angle); + angle += 20; + QList items = collidingItems(); + if (!items.empty()) { + for (QGraphicsItem* item : items) { + if (item->type() == Enemy::Type) { + Enemy* e = qgraphicsitem_cast(item); + if (e->getMoveType() == Enemy::WALKING) { + e->HP.changeCurValue(-0.5); + e->underAtk = true; + } } - } else { - update(); + } } + } else { + update(); + } } diff --git a/src/tower/shield.cpp b/src/tower/shield.cpp index 00fbec8..0553fb1 100644 --- a/src/tower/shield.cpp +++ b/src/tower/shield.cpp @@ -1,49 +1,45 @@ +// Copyright 2022 Flying-Tom + #include -Shield::Shield(Map* map) - : Tower(map, - "shield", - GameValue(100, 100), - RANGE, - 200, - GameValue(0, 5), - GameValue(0, 175)) -{ - image = QImage(":images/shield.png"); - HPMeter->setBrush(QBrush(Qt::yellow)); - setZValue(5); +Shield::Shield(GameMap* map) + : Tower(map, "shield", GameValue(100, 100), RANGE, 200, + GameValue(0, 5), GameValue(0, 175)) { + image = QImage(":images/shield.png"); + HPMeter->setBrush(QBrush(Qt::yellow)); + setZValue(5); } -Shield::~Shield() { } +Shield::~Shield() {} -void Shield::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Tower::paint(painter, option, widget); - painter->setBrush(QColor(0, 204, 255)); - painter->setOpacity(0.1); - painter->drawEllipse(atkArea->boundingRect()); - //atkArea->show(); +void Shield::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Tower::paint(painter, option, widget); + painter->setBrush(QColor(0, 204, 255)); + painter->setOpacity(0.1); + painter->drawEllipse(atkArea->boundingRect()); + // atkArea->show(); } -void Shield::advance(int phase) -{ - if (!phase) { - if (HP.getCurValue() == 0) { - delete this; - return; - } +void Shield::advance(int phase) { + if (!phase) { + if (HP.getCurValue() == 0) { + delete this; + return; + } - foreach (QGraphicsItem* item, atkArea->collidingItems()) { - if (item->type() == Tower::Type and ((Tower*)item)->getAtkType() == Tower::RANGE - and item != this) { - if (((Tower*)item)->getName() != "shield" - and ((Tower*)item)->HP.getCurValue() > 0) { - double delta = ((Tower*)item)->HP.getMaxValue() - - ((Tower*)item)->HP.getCurValue(); - ((Tower*)item)->HP.changeCurValue(delta); - HP.changeCurValue(-delta); - } - } + for (QGraphicsItem* item : atkArea->collidingItems()) { + if (item->type() == Tower::Type && + qgraphicsitem_cast(item)->getAtkType() == Tower::RANGE && + item != this) { + if (qgraphicsitem_cast(item)->getName() != "shield" && + qgraphicsitem_cast(item)->HP.getCurValue() > 0) { + double delta = qgraphicsitem_cast(item)->HP.getMaxValue() - + qgraphicsitem_cast(item)->HP.getCurValue(); + qgraphicsitem_cast(item)->HP.changeCurValue(delta); + HP.changeCurValue(-delta); } + } } + } } diff --git a/src/tower/tower.cpp b/src/tower/tower.cpp index 3e7dc4d..3f8abb0 100644 --- a/src/tower/tower.cpp +++ b/src/tower/tower.cpp @@ -1,107 +1,92 @@ +// Copyright 2022 Flying-Tom + #include -Tower::Tower(Map* map, - QString name, - GameValue HP, - int atkType, - qreal atkRadius, - GameValue blockNumber, - GameValue aquireCounter) - : map(map) - , atkType(atkType) - , infopanel(this) -{ - GameItem::HP = HP; - GameItem::atkRadius = atkRadius; - GameItem::blockNumber = blockNumber; - GameItem::aquireCounter = aquireCounter; - GameItem::name = name; +Tower::Tower(GameMap* map, QString name, GameValue HP, int atkType, + qreal atkRadius, GameValue blockNumber, + GameValue aquireCounter) + : map(map), atkType(atkType), infopanel(this) { + GameItem::HP = HP; + GameItem::atkRadius = atkRadius; + GameItem::blockNumber = blockNumber; + GameItem::aquireCounter = aquireCounter; + GameItem::name = name; - atkArea->setRect(-atkRadius, -atkRadius, atkRadius * 2, atkRadius * 2); - HPMeter->setBrush(QBrush(QColor(QColor(0, 249, 0)))); + atkArea->setRect(-atkRadius, -atkRadius, atkRadius * 2, atkRadius * 2); + HPMeter->setBrush(QBrush(QColor(QColor(0, 249, 0)))); } -Tower::~Tower() -{ - if (map) { - map->Destory(pos()); - } +Tower::~Tower() { + if (map) { + map->Destory(pos()); + } } -void Tower::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) - GameItem::paint(painter, option, widget); - if (underAtk == true) { - painter->setOpacity(0.5); - if (blinkCounter.getCurValue() > 0) { - blinkCounter.changeCurValue(-1); - } else { - blinkCounter.setCurValue(blinkCounter.getMaxValue()); - underAtk = false; - } +void Tower::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, + QWidget* widget) { + Q_UNUSED(option) + Q_UNUSED(widget) + GameItem::paint(painter, option, widget); + if (underAtk == true) { + painter->setOpacity(0.5); + if (blinkCounter.getCurValue() > 0) { + blinkCounter.changeCurValue(-1); } else { - painter->setOpacity(1); + blinkCounter.setCurValue(blinkCounter.getMaxValue()); + underAtk = false; } - painter->drawImage(boundingRect(), image); + } else { + painter->setOpacity(1); + } + painter->drawImage(boundingRect(), image); } -void Tower::advance(int phase) -{ - if (!phase) { - if (map) { - if (HP.getCurValue() == 0) { - isDead = true; - delete this; - return; - } - if (aquireCounter.getCurValue() == aquireCounter.getMaxValue()) { - aquireTarget(); - if (atkTarget.isNull() == false) { - attack(); - } - aquireCounter.setCurValue(0); - } else - aquireCounter.changeCurValue(1); +void Tower::advance(int phase) { + if (!phase) { + if (map) { + if (HP.getCurValue() == 0) { + isDead = true; + delete this; + return; + } + if (aquireCounter.getCurValue() == aquireCounter.getMaxValue()) { + aquireTarget(); + if (atkTarget.isNull() == false) { + attack(); } - } else { - update(); + aquireCounter.setCurValue(0); + } else { + aquireCounter.changeCurValue(1); + } } + } else { + update(); + } } -void Tower::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) -{ - Q_UNUSED(event); - infopanel.setPos(pos()); - infopanel.show(); +void Tower::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) { + Q_UNUSED(event); + infopanel.setPos(pos()); + infopanel.show(); } -int Tower::type() const -{ - if (map == nullptr) - return Type + 114; - return Type; +int Tower::type() const { + if (map == nullptr) return Type + 114; + return Type; } -QImage Tower::getImage() const -{ - return image; -} +QImage Tower::getImage() const { return image; } -int Tower::getAtkType() const -{ - return atkType; -} +int Tower::getAtkType() const { return atkType; } -void Tower::aquireTarget() -{ - QList colliding_items = atkArea->collidingItems(); - foreach (QGraphicsItem* item, colliding_items) { - if (item->type() == Enemy::Type and ((Enemy*)item)->getIsDead() == false) { - atkTarget = (Enemy*)item; - } +void Tower::aquireTarget() { + QList colliding_items = atkArea->collidingItems(); + for (QGraphicsItem* item : colliding_items) { + if (item->type() == Enemy::Type && + qgraphicsitem_cast(item)->getIsDead() == false) { + atkTarget = qgraphicsitem_cast(item); } + } } -void Tower::attack() { } +void Tower::attack() {}