1 | /* |
2 | * Copyright (C) 2007 Tomasz Boczkowski <tboczkowski@onet.pl> |
3 | * |
4 | * This file is part of the KDE project "KBounce" |
5 | * |
6 | * KBounce is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Library General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2 of the License, or (at your option) any later version. |
10 | * |
11 | * KBounce is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Library General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Library General Public |
17 | * License along with KBounce; if not, write to the Free |
18 | * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
19 | * Boston, MA 02110-1301, USA. |
20 | */ |
21 | |
22 | #ifndef WALL_H |
23 | #define WALL_H |
24 | |
25 | #include <KGameRenderedItem> |
26 | |
27 | #include <QObject> |
28 | #include <KgSound> |
29 | #include "gameobject.h" |
30 | |
31 | class KBounceRenderer; |
32 | class KBounceBoard; |
33 | |
34 | /** |
35 | * KGameRenderedItem representing a wall "under-construction" |
36 | * |
37 | * There are four walls in a board, each of which extends in |
38 | * other direction: up, right, down or left |
39 | */ |
40 | class KBounceWall : public QObject, public KGameRenderedItem |
41 | { |
42 | Q_OBJECT |
43 | |
44 | public: |
45 | enum Direction { Up = 0, Down, Left, Right }; |
46 | |
47 | /** |
48 | * Constructor |
49 | * @param dir is set in construction and does not change in gameplay |
50 | * that means there will be no two walls extending in the same direction |
51 | * in the same time |
52 | */ |
53 | KBounceWall( Direction dir, KBounceRenderer* renderer, KBounceBoard* board ); |
54 | ~KBounceWall(); |
55 | |
56 | /** |
57 | * Changes object's state when collisions have been detected |
58 | * Called once per frame before advance() and update() |
59 | */ |
60 | void collide( KBounceCollision collision ); |
61 | /** |
62 | * Performs various movement and state calculations non-related |
63 | * to collision responses. Also updates m_boundingRect and |
64 | * m_nextBoundingRect. This method is called once per frame |
65 | * after collide() and before update() |
66 | */ |
67 | void goForward(); |
68 | /** |
69 | * Updates object's pixmap and position on screen |
70 | * Called once per frame after update() |
71 | */ |
72 | void update(); |
73 | |
74 | /** |
75 | * Starts building wall beginning from tile specified by x and y |
76 | * The direction has been specified in constructor |
77 | */ |
78 | void build( int x, int y ); |
79 | /** |
80 | * Returns the bounding rect that is expected for wall to |
81 | * have in next frame. Collision in KBounceBoard are based on |
82 | * the result of this method |
83 | */ |
84 | QRectF nextBoundingRect() const; |
85 | |
86 | /** |
87 | * Changes on-screen dimensions of the wall. |
88 | * Calculations are based on tile size, that is the on-screen |
89 | * size of board's tile |
90 | */ |
91 | void resize( const QSize& tileSize ); |
92 | |
93 | /** |
94 | * Set the wall velocity for wall filling speed. |
95 | */ |
96 | void setWallVelocity(qreal velocity); |
97 | |
98 | /** |
99 | * Load all sprites for top, down, left and right walls as well as for |
100 | * the vertical and horizontal semi transparent bars drawn before a wall is |
101 | * built. |
102 | */ |
103 | void static loadSprites(); |
104 | |
105 | signals: |
106 | void finished( int left, int top, int right, int bottom ); |
107 | void died(); |
108 | |
109 | private: |
110 | /** |
111 | * Returns true if rect2 intersects the edge at the end of wall |
112 | * e.g for wall extending in direction Up this will be the upper |
113 | * edge. |
114 | */ |
115 | bool safeEdgeHit( const QRectF& rect2 ) const; |
116 | /** |
117 | * Helper function replacing emiting long finished signal |
118 | * It also hides the wall and plays corresponding sound |
119 | * If ¶m shorten is true the wall will be one unit in |
120 | * direction ¶m dir shorter than normal |
121 | */ |
122 | void finish( bool shorten = false, Direction dir = Up); |
123 | |
124 | KBounceBoard *m_board; |
125 | Direction m_dir; |
126 | |
127 | KgSound m_soundWallstart; |
128 | KgSound m_soundReflect; |
129 | |
130 | QRectF m_boundingRect; |
131 | QRectF m_nextBoundingRect; |
132 | qreal m_wallVelocity; |
133 | |
134 | typedef struct { |
135 | QPixmap wallEndLeft; |
136 | QPixmap wallEndUp; |
137 | QPixmap wallEndRight; |
138 | QPixmap wallEndDown; |
139 | QPixmap wallH; |
140 | QPixmap wallV; |
141 | |
142 | } Sprites; |
143 | static Sprites *s_sprites; |
144 | |
145 | static QSize s_tileSize; |
146 | static KBounceRenderer *m_renderer; |
147 | }; |
148 | |
149 | #endif |
150 | |
151 | |
152 | |