File indexing completed on 2022-10-04 14:29:26

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