File indexing completed on 2024-04-21 04:03:16

0001 /*
0002     SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef CONTROLLER_H
0008 #define CONTROLLER_H
0009 
0010 #include "sea.h"
0011 
0012 class Entity;
0013 class AIEntity;
0014 class NetworkEntity;
0015 class UIEntity;
0016 class PlayerEntity;
0017 class SeaView;
0018 class ChatWidget;
0019 class Shot;
0020 class AudioPlayer;
0021 class Protocol;
0022 class BattleShipsConfiguration;
0023 
0024 class Controller : public QObject
0025 {
0026 Q_OBJECT
0027     QList<Entity*> m_entities;
0028     UIEntity* m_ui;
0029     Sea* m_sea;
0030     Shot* m_shot;
0031     int m_ready;
0032     AudioPlayer* m_player;
0033     bool m_has_ai;
0034     BattleShipsConfiguration mBattleShipsConfiguration;
0035 
0036     void notify(Sea::Player player, const Coord& c, const HitInfo& info);
0037     void setupEntity(Entity*);
0038     void finalizeShot(Sea::Player player, const Coord& c, const HitInfo& info);
0039     void finalizeGame(Sea::Player winner);
0040     bool allPlayers() const;
0041 
0042 
0043     friend class Shot;
0044 public:
0045     explicit Controller(QObject* parent, AudioPlayer* audioPlayer = nullptr, const BattleShipsConfiguration& battleConfiguration = BattleShipsConfiguration::defaultSingleShipsConfiguration(true));
0046 
0047     PlayerEntity* createPlayer(Sea::Player player, SeaView* view,
0048                                ChatWidget* chat, const QString& nick);
0049     AIEntity* createAI(Sea::Player player, SeaView* view);
0050     NetworkEntity* createRemotePlayer(Sea::Player player, SeaView* view, Protocol* protocol, bool client);
0051 
0052     bool start(SeaView* view);
0053     Entity* findEntity(Sea::Player) const;
0054     Sea::Player turn() const;
0055     bool hasAI() const;
0056     inline Sea* getSea() const { return m_sea; }
0057     void setBattleShipsConfiguration(const BattleShipsConfiguration& battleConfiguration);
0058     inline BattleShipsConfiguration& getBattleShipsConfiguration() { return mBattleShipsConfiguration; }
0059 public Q_SLOTS:
0060     void shoot(int player, const Coord& c);
0061     void ready(int player);
0062     void shipsPlaced();
0063     void receivedChat(const QString& text);
0064     void nick(int player, const QString& nick);
0065     void notifyRestartPlacingShips(Sea::Player player);
0066     void placing();
0067     void restart();
0068 Q_SIGNALS:
0069     void gameOver(Sea::Player);
0070     void restartRequested();
0071     void startPlacingShips(int player);
0072     void restartPlacingShips(Sea::Player player); // in case it is impossible to finish with the current board
0073     void compatibility(int);
0074     void nickChanged(int, const QString&);
0075     void turnChanged(int);
0076     void playerReady(int); // -1 means all players are ready
0077 };
0078 
0079 #endif // CONTROLLER_H
0080