File indexing completed on 2024-02-18 04:04:52

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 BATTLEFIELDVIEW_H
0008 #define BATTLEFIELDVIEW_H
0009 
0010 #include <QMultiHash>
0011 #include <QTime>
0012 #include <QWidget>
0013 #include <QGraphicsView>
0014 #include <QGraphicsLineItem>
0015 
0016 #include <KGameRenderedItem>
0017 
0018 #include "coord.h"
0019 #include "spritefactory.h"
0020 #include "grid.h"
0021 #include "sea.h"
0022 
0023 class KBSRenderer;
0024 class Sprite;
0025 class Ship;
0026 class WelcomeScreen;
0027 class Delegate;
0028 
0029 class BattleFieldView : public QGraphicsView 
0030 {
0031     static const qreal PREVIEW_OPACITY;
0032 
0033     KGameRenderedItem* m_background;
0034     KGameRenderedItem* m_background_lower;
0035 
0036     QGraphicsLineItem *hlines[11];
0037     QGraphicsLineItem *vlines[11];
0038 
0039     enum Planes {
0040         BACKGROUND = 1,
0041         FOREGROUND = 2
0042     };
0043 
0044     WelcomeScreen* m_screen;
0045     KBSRenderer* m_renderer;
0046     SpriteFactory m_factory;
0047     QString m_bgID;
0048     int m_gridSize;
0049     Sprite* m_impact;
0050     Sprite* m_last_hit;
0051     bool m_drawGrid;
0052 
0053     Delegate *m_delegate;
0054     Sea::Player m_player;
0055     Sea::Status m_status;
0056 
0057     struct Preview {
0058         Coord pos;
0059         Ship* ship;
0060         Sprite* sprite;
0061         
0062         Preview()
0063         : pos(-1, -1)
0064         , ship(nullptr)
0065         , sprite(nullptr)
0066         {
0067         }
0068     } m_preview;
0069     
0070     typedef QMultiHash<Coord, Sprite*> Sprites;
0071     Sprites m_sprites;
0072 
0073 private:
0074     void addSprite(const Coord& c, Sprite* ship);
0075     void loadPreviewSprite(Ship * ship);
0076 
0077 public:
0078     BattleFieldView(QWidget* parent, KBSRenderer* renderer, const QString& bgID, int gridSize);
0079 
0080     void toggleGrid(bool show);
0081     void refresh();
0082     
0083     void setPreview(const QPoint &pos);
0084     void cancelPreview();
0085     void add(Ship* ship);
0086     void hit(const Coord& c);
0087     void miss(const Coord& c);
0088     void removeImpact();
0089     void sink(Ship* ship);
0090     void clear();
0091 
0092     void setDelegate(Delegate *c);
0093     void setPlayer(Sea::Player player);
0094 
0095     WelcomeScreen* screen() const;
0096     inline void setStatus(Sea::Status status) { m_status = status; };
0097 protected:
0098     void drawGrid();
0099 
0100     void mousePressEvent(QMouseEvent *) override;
0101     void mouseReleaseEvent(QMouseEvent *) override;
0102     void mouseMoveEvent(QMouseEvent *) override;
0103     void leaveEvent(QEvent *) override;
0104 };
0105 
0106 #endif // BATTLEFIELD_H
0107