1 /*
2     SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #ifndef CONTROLLER_H
8 #define CONTROLLER_H
9 
10 #include "sea.h"
11 
12 class Entity;
13 class AIEntity;
14 class NetworkEntity;
15 class UIEntity;
16 class PlayerEntity;
17 class SeaView;
18 class ChatWidget;
19 class Shot;
20 class AudioPlayer;
21 class Protocol;
22 class BattleShipsConfiguration;
23 
24 class Controller : public QObject
25 {
26 Q_OBJECT
27     QList<Entity*> m_entities;
28     UIEntity* m_ui;
29     Sea* m_sea;
30     Shot* m_shot;
31     int m_ready;
32     AudioPlayer* m_player;
33     bool m_has_ai;
34     BattleShipsConfiguration mBattleShipsConfiguration;
35 
36     void notify(Sea::Player player, const Coord& c, const HitInfo& info);
37     void setupEntity(Entity*);
38     void finalizeShot(Sea::Player player, const Coord& c, const HitInfo& info);
39     void finalizeGame(Sea::Player winner);
40     bool allPlayers() const;
41 
42 
43     friend class Shot;
44 public:
45     explicit Controller(QObject* parent, AudioPlayer* audioPlayer = nullptr, const BattleShipsConfiguration& battleConfiguration = BattleShipsConfiguration::defaultSingleShipsConfiguration(true));
46 
47     PlayerEntity* createPlayer(Sea::Player player, SeaView* view,
48                                ChatWidget* chat, const QString& nick);
49     AIEntity* createAI(Sea::Player player, SeaView* view);
50     NetworkEntity* createRemotePlayer(Sea::Player player, SeaView* view, Protocol* protocol, bool client);
51 
52     bool start(SeaView* view);
53     Entity* findEntity(Sea::Player) const;
54     Sea::Player turn() const;
55     bool hasAI() const;
getSea()56     inline Sea* getSea() const { return m_sea; }
57     void setBattleShipsConfiguration(const BattleShipsConfiguration& battleConfiguration);
getBattleShipsConfiguration()58     inline BattleShipsConfiguration& getBattleShipsConfiguration() { return mBattleShipsConfiguration; }
59 public Q_SLOTS:
60     void shoot(int player, const Coord& c);
61     void ready(int player);
62     void shipsPlaced();
63     void receivedChat(const QString& text);
64     void nick(int player, const QString& nick);
65     void notifyRestartPlacingShips(Sea::Player player);
66     void placing();
67     void restart();
68 Q_SIGNALS:
69     void gameOver(Sea::Player);
70     void restartRequested();
71     void startPlacingShips(int player);
72     void restartPlacingShips(Sea::Player player); // in case it is impossible to finish with the current board
73     void compatibility(int);
74     void nickChanged(int, const QString&);
75     void turnChanged(int);
76     void playerReady(int); // -1 means all players are ready
77 };
78 
79 #endif // CONTROLLER_H
80 
81