1 | /*************************************************************************** |
2 | * KBlocks, a falling blocks game for KDE * |
3 | * Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * (at your option) any later version. * |
9 | ***************************************************************************/ |
10 | #include "KBlocksKeyboardPlayer.h" |
11 | |
12 | KBlocksKeyboardPlayer::KBlocksKeyboardPlayer(KXmlGuiWindow * parent, string name, bool netMode) |
13 | { |
14 | mpGame = 0; |
15 | mPauseFlag = false; |
16 | mpKeyWindow = 0; |
17 | |
18 | mPlayerName = name; |
19 | mNetMode = netMode; |
20 | |
21 | if (parent) |
22 | { |
23 | mpKeyShortcuts = parent->actionCollection(); |
24 | } |
25 | else |
26 | { |
27 | mpKeyWindow = new KXmlGuiWindow(); |
28 | mpKeyWindow->setupGUI(); |
29 | mpKeyWindow->setFixedWidth(192); |
30 | mpKeyWindow->setFixedHeight(32); |
31 | mpKeyWindow->show(); |
32 | |
33 | mpKeyShortcuts = mpKeyWindow->actionCollection(); |
34 | } |
35 | |
36 | bindKeys(); |
37 | |
38 | mActionList.clear(); |
39 | } |
40 | |
41 | KBlocksKeyboardPlayer::~KBlocksKeyboardPlayer() |
42 | { |
43 | if (mpKeyWindow) |
44 | { |
45 | delete mpKeyWindow; |
46 | } |
47 | } |
48 | |
49 | void KBlocksKeyboardPlayer::startGame(SingleGameInterface * p) |
50 | { |
51 | mpGame = p; |
52 | mPauseFlag = false; |
53 | |
54 | mActionList.clear(); |
55 | } |
56 | |
57 | void KBlocksKeyboardPlayer::stopGame() |
58 | { |
59 | mpGame = 0; |
60 | |
61 | mActionList.clear(); |
62 | } |
63 | |
64 | void KBlocksKeyboardPlayer::pauseGame(bool flag) |
65 | { |
66 | mPauseFlag = flag; |
67 | } |
68 | |
69 | void KBlocksKeyboardPlayer::think(GamePlayer_ActionList * actionList) |
70 | { |
71 | if (mNetMode) |
72 | { |
73 | *actionList = mActionList; |
74 | mActionList.clear(); |
75 | } |
76 | } |
77 | |
78 | string KBlocksKeyboardPlayer::getName() |
79 | { |
80 | return mPlayerName; |
81 | } |
82 | |
83 | void KBlocksKeyboardPlayer::bindKeys() |
84 | { |
85 | rotatecw = mpKeyShortcuts->addAction( QLatin1String( "rotate_cw" )); |
86 | rotatecw->setText(i18n("Rotate Piece Clockwise" )); |
87 | rotatecw->setIcon(KIcon( QLatin1String( "object-rotate-right" ))); |
88 | rotatecw->setShortcuts( KShortcut( "z" ) ); |
89 | connect(rotatecw, SIGNAL(triggered(bool)), this, SLOT(rotateCW())); |
90 | |
91 | rotateccw = mpKeyShortcuts->addAction( QLatin1String( "rotate_ccw" )); |
92 | rotateccw->setText(i18n("Rotate Piece Counter Clockwise" )); |
93 | rotateccw->setIcon(KIcon( QLatin1String( "object-rotate-left" ))); |
94 | rotateccw->setShortcuts( KShortcut( Qt::Key_Up ) ); |
95 | connect(rotateccw, SIGNAL(triggered(bool)), this, SLOT(rotateCCW())); |
96 | |
97 | moveleft = mpKeyShortcuts->addAction( QLatin1String( "move_left" )); |
98 | moveleft->setText(i18n("Move Piece Left" )); |
99 | moveleft->setIcon(KIcon( QLatin1String( "arrow-left" ))); |
100 | moveleft->setShortcuts( KShortcut( Qt::Key_Left ) ); |
101 | connect(moveleft, SIGNAL(triggered(bool)), this, SLOT(moveLeft())); |
102 | |
103 | moveright = mpKeyShortcuts->addAction( QLatin1String( "move_right" )); |
104 | moveright->setText(i18n("Move Piece Right" )); |
105 | moveright->setIcon(KIcon( QLatin1String( "arrow-right" ))); |
106 | moveright->setShortcuts( KShortcut( Qt::Key_Right) ); |
107 | connect(moveright, SIGNAL(triggered(bool)), this, SLOT(moveRight())); |
108 | |
109 | movedown = mpKeyShortcuts->addAction( QLatin1String( "move_down" )); |
110 | movedown->setText(i18n("Move Piece Down" )); |
111 | movedown->setIcon(KIcon( QLatin1String( "arrow-down" ))); |
112 | movedown->setShortcuts( KShortcut( Qt::Key_Down ) ); |
113 | connect(movedown, SIGNAL(triggered(bool)), this, SLOT(moveDown())); |
114 | |
115 | pushdown = mpKeyShortcuts->addAction( QLatin1String( "push_down" )); |
116 | pushdown->setText(i18n("Drop the Piece" )); |
117 | pushdown->setIcon(KIcon( QLatin1String( "arrow-down" ))); |
118 | pushdown->setShortcuts( KShortcut( Qt::Key_Space ) ); |
119 | connect(pushdown, SIGNAL(triggered(bool)), this, SLOT(pushDown())); |
120 | } |
121 | |
122 | void KBlocksKeyboardPlayer::moveLeft() |
123 | { |
124 | if ((!mpGame) || (mPauseFlag)) |
125 | { |
126 | return; |
127 | } |
128 | if (mNetMode) |
129 | { |
130 | mActionList.push_back(PlayerAction_Move_Left); |
131 | } |
132 | else |
133 | { |
134 | mpGame->setCurrentPiece(-1, 0, 0); |
135 | } |
136 | } |
137 | |
138 | void KBlocksKeyboardPlayer::moveRight() |
139 | { |
140 | if ((!mpGame) || (mPauseFlag)) |
141 | { |
142 | return; |
143 | } |
144 | if (mNetMode) |
145 | { |
146 | mActionList.push_back(PlayerAction_Move_Right); |
147 | } |
148 | else |
149 | { |
150 | mpGame->setCurrentPiece(1, 0, 0); |
151 | } |
152 | } |
153 | |
154 | void KBlocksKeyboardPlayer::moveDown() |
155 | { |
156 | if ((!mpGame) || (mPauseFlag)) |
157 | { |
158 | return; |
159 | } |
160 | if (mNetMode) |
161 | { |
162 | mActionList.push_back(PlayerAction_Move_Down); |
163 | } |
164 | else |
165 | { |
166 | mpGame->setCurrentPiece(0, 1, 0); |
167 | } |
168 | } |
169 | |
170 | void KBlocksKeyboardPlayer::pushDown() |
171 | { |
172 | if ((!mpGame) || (mPauseFlag)) |
173 | { |
174 | return; |
175 | } |
176 | if (mNetMode) |
177 | { |
178 | mActionList.push_back(PlayerAction_Push_Down); |
179 | } |
180 | else |
181 | { |
182 | while(mpGame->setCurrentPiece(0, 1, 0)) ; |
183 | mpGame->forceUpdateGame(); |
184 | } |
185 | } |
186 | |
187 | void KBlocksKeyboardPlayer::rotateCW() |
188 | { |
189 | if ((!mpGame) || (mPauseFlag)) |
190 | { |
191 | return; |
192 | } |
193 | if (mNetMode) |
194 | { |
195 | mActionList.push_back(PlayerAction_Rotate_CW); |
196 | } |
197 | else |
198 | { |
199 | mpGame->setCurrentPiece(0, 0, -1); |
200 | } |
201 | } |
202 | |
203 | void KBlocksKeyboardPlayer::rotateCCW() |
204 | { |
205 | if ((!mpGame) || (mPauseFlag)) |
206 | { |
207 | return; |
208 | } |
209 | if (mNetMode) |
210 | { |
211 | mActionList.push_back(PlayerAction_Rotate_CCW); |
212 | } |
213 | else |
214 | { |
215 | mpGame->setCurrentPiece(0, 0, 1); |
216 | } |
217 | } |
218 | |