File indexing completed on 2024-04-14 04:00:49

0001 /*
0002     SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com>
0003     SPDX-FileCopyrightText: 2010 Brian Croom <brian.s.croom@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef MAINAREA_H
0009 #define MAINAREA_H
0010 
0011 #include <QElapsedTimer>
0012 #include <QTime>
0013 #include <QList>
0014 #include <QGraphicsScene>
0015 #include <QRandomGenerator>
0016 
0017 #include <KGameRenderer>
0018 #include <KGameSound>
0019 #include "animator.h"
0020 #include "message.h"
0021 
0022 #include <memory>
0023 
0024 class Renderer;
0025 class Ball;
0026 class Animation;
0027 class QGraphicsSceneMouseEvent;
0028 class QAction;
0029 
0030 struct Collision;
0031 
0032 class MainArea : public QGraphicsScene
0033 {
0034 Q_OBJECT
0035 private:
0036     QTimer m_timer;
0037     int m_lastTime;
0038     int m_lastGameTime;
0039     QElapsedTimer m_time;
0040 
0041     /// time interval between two balls being added
0042     int m_ball_timeout;
0043 
0044     int m_size;
0045     KGameRenderer m_renderer;
0046     Animator m_animator;
0047     QFont m_msgFont;
0048 
0049     QList<Ball*> m_balls;
0050     QList<Ball*> m_fading;
0051     Ball* m_man;
0052 
0053     // Player's ball diameter
0054     int m_manBallDiameter;
0055     // Other balls' diameter
0056     int m_ballDiameter;
0057 
0058     /// the blue ball is dead
0059     bool m_death;
0060 
0061     /// the falling animation is over, we're waiting for a new game to start
0062     bool m_game_over;
0063 
0064     bool m_paused;
0065     int m_pauseTime;
0066     int m_penalty;
0067 
0068     QList<MessagePtr> m_welcomeMsg;
0069     QList<MessagePtr> m_pauseMsg;
0070 
0071     // Flag if automatic incremental ball size is enabled.
0072     bool m_increaseBallSize;
0073 
0074     // Flag if sound is enabled.
0075     bool m_soundEnabled;
0076 
0077     KGameSound m_soundHitWall;
0078     KGameSound m_soundYouLose;
0079     KGameSound m_soundBallLeaving;
0080     KGameSound m_soundStart;
0081 
0082     QAction * m_pauseAction;
0083 
0084     std::unique_ptr<QRandomGenerator> m_random;
0085 
0086     double radius() const;
0087     void setBallDiameter(int val);
0088 
0089     QPointF randomPoint() const;
0090     QPointF randomDirection(double val) const;
0091 
0092     Ball* addBall(const QString& id);
0093     bool collide(const QPointF& a, const QPointF& b,
0094                 double diamA, double diamB, Collision& collision);
0095 
0096     Animation* writeMessage(const QString& text);
0097     Animation* writeText(const QString& text, bool fade = true);
0098     void displayMessages(const QList<QExplicitlySharedDataPointer<Message> >& msgs);
0099     void playSound(int sound);
0100     void onDeath();
0101     void setManPosition(const QPointF& p);
0102 protected:
0103     void mousePressEvent(QGraphicsSceneMouseEvent* e) override;
0104     void focusOutEvent(QFocusEvent*) override;
0105 public:
0106     MainArea();
0107     void start();
0108     void setPauseAction(QAction * action);
0109 public Q_SLOTS:
0110     void tick();
0111     void increaseBallSize(bool enable);
0112     void enableSounds(bool enable);
0113     void abort();
0114     void togglePause();
0115 Q_SIGNALS:
0116     void starting();
0117     void gameOver(int);
0118     void changeBallNumber(int);
0119     void changeGameTime(int);
0120     void changeState(bool);
0121     void pause(bool);
0122 };
0123 
0124 #endif // MAINAREA_H