File indexing completed on 2024-12-01 06:51:10

0001 /*
0002     This file is part of Killbots.
0003 
0004     SPDX-FileCopyrightText: 2006-2009 Parker Coates <coates@kde.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef KILLBOTS_ENGINE_H
0010 #define KILLBOTS_ENGINE_H
0011 
0012 #include "actions.h"
0013 #include "ruleset.h"
0014 
0015 #include <QObject>
0016 class QPoint;
0017 
0018 namespace Killbots
0019 {
0020 class Coordinator;
0021 class Sprite;
0022 
0023 class Engine : public QObject
0024 {
0025     Q_OBJECT
0026 
0027 public: // functions
0028     explicit Engine(Coordinator *scene, QObject *parent = nullptr);
0029     ~Engine() override;
0030 
0031     void setRuleset(const Ruleset *ruleset);
0032     const Ruleset *ruleset() const;
0033 
0034     bool gameHasStarted() const;
0035     bool isRoundComplete() const;
0036     bool isHeroDead() const;
0037     bool isBoardFull() const;
0038     bool canSafeTeleport() const;
0039     bool canUseVaporizer() const;
0040 
0041     void startNewGame();
0042     void startNewRound(bool incrementRound = true, const QString &layout = QString());
0043 
0044     bool moveHero(Killbots::HeroAction direction);
0045     bool teleportHero();
0046     bool teleportHeroSafely();
0047     bool useVaporizer();
0048     bool waitOutRound();
0049 
0050     void moveRobots(bool justFastbots = false);
0051     void assessDamage();
0052     void resetBotCounts();
0053     void endGame();
0054 
0055 Q_SIGNALS:
0056     void roundChanged(int round);
0057     void scoreChanged(int score);
0058     void enemyCountChanged(int enemyCount);
0059     void energyChanged(int energy);
0060     void gameOver(int score, int round);
0061 
0062     void showNewGameMessage();
0063     void showRoundCompleteMessage();
0064     void showBoardFullMessage();
0065     void showGameOverMessage();
0066 
0067     void teleportAllowed(bool allowed);
0068     void teleportSafelyAllowed(bool allowed);
0069     void vaporizerAllowed(bool allowed);
0070     void waitOutRoundAllowed(bool allowed);
0071 
0072 private: // functions
0073     void refreshSpriteMap();
0074     int spriteTypeAt(const QPoint &cell) const;
0075     QPoint offsetFromDirection(int direction) const;
0076     QPoint randomEmptyCell() const;
0077 
0078     bool cellIsValid(const QPoint &cell) const;
0079     bool moveIsValid(const QPoint &cell, HeroAction direction) const;
0080     bool moveIsSafe(const QPoint   &cell, HeroAction direction) const;
0081     bool canPushJunkheap(const Sprite *junkheap, HeroAction direction) const;
0082 
0083     void pushJunkheap(Sprite *junkheap, HeroAction direction);
0084     void cleanUpRound();
0085     void destroySprite(Sprite *sprite, bool calculatePoints = true);
0086     bool destroyAllCollidingBots(const Sprite *sprite, bool calculatePoints = true);
0087     void updateScore(int changeInScore);
0088     void updateEnergy(int changeInEnergy);
0089 
0090     QString gridToString() const;
0091 
0092 private: // data members
0093     Coordinator *m_coordinator;
0094 
0095     Sprite *m_hero;
0096     QList<Sprite *> m_bots;
0097     QList<Sprite *> m_junkheaps;
0098 
0099     const Ruleset *m_rules;
0100     int m_round;
0101     int m_score;
0102     int m_energy;
0103     qreal m_maxEnergy;
0104     qreal m_robotCount;
0105     qreal m_fastbotCount;
0106     qreal m_junkheapCount;
0107 
0108     bool m_heroIsDead;
0109     bool m_waitingOutRound;
0110 
0111     QMultiHash<QPoint, Sprite *> m_spriteMap;
0112 
0113     friend class EngineTest;
0114 };
0115 }
0116 
0117 #endif